Skip to content

Commit

Permalink
feat(upgrade): create v0.26.0 network upgrade
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian committed Sep 13, 2023
1 parent ac8bc43 commit f28273a
Show file tree
Hide file tree
Showing 8 changed files with 293 additions and 2 deletions.
5 changes: 5 additions & 0 deletions meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"skipped": false,
"from_binary": "v0.22.8",
"from_version": "v0.22.0"
},
"v0.26.0": {
"skipped": false,
"from_binary": "v0.24.2",
"from_version": "v0.24.0"
}
}
}
4 changes: 2 additions & 2 deletions tests/upgrade/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ func executeCommand(ctx context.Context, env []string, cmd string, args ...strin
}

func (l *validator) run() error {
lStdout, err := os.Create(fmt.Sprintf("%s/%s-stdout.log", l.params.home, l.params.name))
lStdout, err := os.Create(fmt.Sprintf("%s/logs/%s-stdout.log", l.params.home, l.params.name))
if err != nil {
return err
}
Expand All @@ -773,7 +773,7 @@ func (l *validator) run() error {
_ = lStdout.Close()
}()

lStderr, err := os.Create(fmt.Sprintf("%s/%s-stderr.log", l.params.home, l.params.name))
lStderr, err := os.Create(fmt.Sprintf("%s/logs/%s-stderr.log", l.params.home, l.params.name))
if err != nil {
return err
}
Expand Down
117 changes: 117 additions & 0 deletions tests/upgrade/v0.26.0/postupgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//go:build e2e.upgrade

// Package v0_26_0
// nolint revive
package v0_26_0

import (
"context"
"fmt"
"strconv"
"strings"
"testing"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/params/types/proposal"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

astaking "github.com/akash-network/akash-api/go/node/staking/v1beta3"

"github.com/akash-network/node/app"
uttypes "github.com/akash-network/node/tests/upgrade/types"
)

func init() {
uttypes.RegisterPostUpgradeWorker("v0.26.0", &postUpgrade{})
}

type postUpgrade struct{}

var _ uttypes.TestWorker = (*postUpgrade)(nil)

func (pu *postUpgrade) Run(ctx context.Context, t *testing.T, params uttypes.TestParams) {
encodingConfig := app.MakeEncodingConfig()

rpcClient, err := client.NewClientFromNode(params.Node)
require.NoError(t, err)

cctx := client.Context{}.
WithCodec(encodingConfig.Marshaler).
WithInterfaceRegistry(encodingConfig.InterfaceRegistry).
WithTxConfig(encodingConfig.TxConfig).
WithLegacyAmino(encodingConfig.Amino).
WithAccountRetriever(authtypes.AccountRetriever{}).
WithBroadcastMode(flags.BroadcastBlock).
WithHomeDir(params.Home).
WithChainID(params.ChainID).
WithNodeURI(params.Node).
WithClient(rpcClient)

kr, err := client.NewKeyringFromBackend(cctx, params.KeyringBackend)
require.NoError(t, err)

cctx = cctx.WithKeyring(kr)

pqc := proposal.NewQueryClient(cctx)
res, err := pqc.Params(ctx, &proposal.QueryParamsRequest{
Subspace: stakingtypes.ModuleName,
Key: string(stakingtypes.KeyMaxValidators),
})
require.NoError(t, err)

maxValidators, err := strconv.ParseInt(strings.Trim(res.Param.Value, "\""), 10, 32)
require.NoError(t, err)

res, err = pqc.Params(ctx, &proposal.QueryParamsRequest{
Subspace: astaking.ModuleName,
Key: string(astaking.KeyMinCommissionRate),
})
require.NoError(t, err)

minCommission, err := sdk.NewDecFromStr(strings.Trim(res.Param.Value, "\""))
require.NoError(t, err)

qc := stakingtypes.NewQueryClient(cctx)

var pkey []byte

validators := make(stakingtypes.Validators, 0, maxValidators)

for {
var pgn *query.PageRequest
if pkey != nil {
pgn = &query.PageRequest{
Key: pkey,
}
}

result, err := qc.Validators(ctx, &stakingtypes.QueryValidatorsRequest{
Pagination: pgn,
})
require.NoError(t, err)

validators = append(validators, result.Validators...)

if pg := result.Pagination; pg != nil && len(pg.NextKey) > 0 {
pkey = pg.NextKey
} else {
break
}
}

for _, validator := range validators {
assert.True(t, validator.Commission.Rate.GTE(minCommission),
fmt.Sprintf("invalid commission Rate for validator (%s). (%s%%) < (%s%%)MinCommission",
validator.OperatorAddress, validator.Commission.Rate.String(), minCommission.String()))

assert.True(t, validator.Commission.MaxRate.GTE(minCommission),
fmt.Sprintf("invalid commission MaxRate for validator (%s). (%s%%) < (%s%%)MinCommission",
validator.OperatorAddress, validator.Commission.MaxRate.String(), minCommission.String()))
}
}
7 changes: 7 additions & 0 deletions tests/upgrade/workers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build e2e.upgrade

package upgrade

import (
_ "github.com/akash-network/node/tests/upgrade/v0.26.0"
)
4 changes: 4 additions & 0 deletions upgrades/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ Goal of the upgrade here
Add new upgrades after this line based on the template above
-----

##### v0.26.0

1. Enforce **Minimum Validators commission** using onchain parameter. Default value is set to 5%. This is carry-over from v0.24.0 upgrade, as this change was dry-run

##### v0.24.0

1. Update following stores to the `v1beta3`:
Expand Down
11 changes: 11 additions & 0 deletions upgrades/software/v0.26.0/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Package v0_26_0
// nolint revive
package v0_26_0

import (
utypes "github.com/akash-network/node/upgrades/types"
)

func init() {
utypes.RegisterUpgrade(UpgradeName, initUpgrade)
}
145 changes: 145 additions & 0 deletions upgrades/software/v0.26.0/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// Package v0_26_0
// nolint revive
package v0_26_0

import (
"fmt"
"time"

"github.com/tendermint/tendermint/libs/log"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

apptypes "github.com/akash-network/node/app/types"
utypes "github.com/akash-network/node/upgrades/types"
)

const (
UpgradeName = "v0.26.0"
)

type upgrade struct {
*apptypes.App
log log.Logger
}

var _ utypes.IUpgrade = (*upgrade)(nil)

func initUpgrade(log log.Logger, app *apptypes.App) (utypes.IUpgrade, error) {
up := &upgrade{
App: app,
log: log.With(fmt.Sprintf("upgrade/%s", UpgradeName)),
}

return up, nil
}

func (up *upgrade) StoreLoader() *storetypes.StoreUpgrades {
return &storetypes.StoreUpgrades{}
}

func (up *upgrade) UpgradeHandler() upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
if err := up.enforceMinValidatorCommission(ctx); err != nil {
return nil, err
}

Check warning on line 49 in upgrades/software/v0.26.0/upgrade.go

View check run for this annotation

Codecov / codecov/patch

upgrades/software/v0.26.0/upgrade.go#L47-L49

Added lines #L47 - L49 were not covered by tests

return up.MM.RunMigrations(ctx, up.Configurator, fromVM)

Check warning on line 51 in upgrades/software/v0.26.0/upgrade.go

View check run for this annotation

Codecov / codecov/patch

upgrades/software/v0.26.0/upgrade.go#L51

Added line #L51 was not covered by tests
}
}

func (up *upgrade) enforceMinValidatorCommission(ctx sdk.Context) error {
minRate := up.Keepers.Akash.Staking.MinCommissionRate(ctx)
validators := up.Keepers.Cosmos.Staking.GetAllValidators(ctx)

for _, validator := range validators {
if validator.Commission.MaxRate.LT(minRate) || validator.GetCommission().LT(minRate) {
// update MaxRate if it is less than minimum required rate
if validator.Commission.MaxRate.LT(minRate) {
up.log.Info(
fmt.Sprintf(
"validator's `%s` commission MaxRate is %s%% < %[3]s%%(min required). Force updating to %[3]s%%",
validator.OperatorAddress,
validator.Commission.MaxRate,
minRate,
),
)

validator.Commission.MaxRate = minRate
}

Check warning on line 73 in upgrades/software/v0.26.0/upgrade.go

View check run for this annotation

Codecov / codecov/patch

upgrades/software/v0.26.0/upgrade.go#L55-L73

Added lines #L55 - L73 were not covered by tests

if validator.GetCommission().LT(minRate) {
up.log.Info(
fmt.Sprintf(
"validator's `%s` commission Rate is %s%% < %[3]s%%(min required). Force updating to %[3]s%%",
validator.OperatorAddress,
validator.Commission.Rate,
minRate,
),
)

// set max change rate temporarily to 100%
maxRateCh := validator.Commission.MaxChangeRate
validator.Commission.MaxChangeRate = sdk.NewDecWithPrec(1, 0)

newCommission, err := updateValidatorCommission(ctx, validator, minRate)
if err != nil {
return err
}

Check warning on line 92 in upgrades/software/v0.26.0/upgrade.go

View check run for this annotation

Codecov / codecov/patch

upgrades/software/v0.26.0/upgrade.go#L75-L92

Added lines #L75 - L92 were not covered by tests

validator.Commission = newCommission
validator.Commission.MaxChangeRate = maxRateCh

Check warning on line 95 in upgrades/software/v0.26.0/upgrade.go

View check run for this annotation

Codecov / codecov/patch

upgrades/software/v0.26.0/upgrade.go#L94-L95

Added lines #L94 - L95 were not covered by tests
}

up.Keepers.Cosmos.Staking.BeforeValidatorModified(ctx, validator.GetOperator())
up.Keepers.Cosmos.Staking.SetValidator(ctx, validator)

Check warning on line 99 in upgrades/software/v0.26.0/upgrade.go

View check run for this annotation

Codecov / codecov/patch

upgrades/software/v0.26.0/upgrade.go#L98-L99

Added lines #L98 - L99 were not covered by tests
}
}

return nil

Check warning on line 103 in upgrades/software/v0.26.0/upgrade.go

View check run for this annotation

Codecov / codecov/patch

upgrades/software/v0.26.0/upgrade.go#L103

Added line #L103 was not covered by tests
}

// updateValidatorCommission use custom implementation of update commission,
// this prevents panic during upgrade if any of validators have changed their
// commission within 24h of upgrade height
func updateValidatorCommission(
ctx sdk.Context,
validator stakingtypes.Validator,
newRate sdk.Dec,
) (stakingtypes.Commission, error) {
commission := validator.Commission
blockTime := ctx.BlockHeader().Time

if err := validateNewRate(commission, newRate, blockTime); err != nil {
return commission, err
}

Check warning on line 119 in upgrades/software/v0.26.0/upgrade.go

View check run for this annotation

Codecov / codecov/patch

upgrades/software/v0.26.0/upgrade.go#L113-L119

Added lines #L113 - L119 were not covered by tests

commission.Rate = newRate
commission.UpdateTime = blockTime

return commission, nil

Check warning on line 124 in upgrades/software/v0.26.0/upgrade.go

View check run for this annotation

Codecov / codecov/patch

upgrades/software/v0.26.0/upgrade.go#L121-L124

Added lines #L121 - L124 were not covered by tests
}

// validateNewRate performs basic sanity validation checks of a new commission
// rate. If validation fails, an SDK error is returned.
func validateNewRate(commission stakingtypes.Commission, newRate sdk.Dec, _ time.Time) error {
switch {
case newRate.IsNegative():
// new rate cannot be negative
return stakingtypes.ErrCommissionNegative

Check warning on line 133 in upgrades/software/v0.26.0/upgrade.go

View check run for this annotation

Codecov / codecov/patch

upgrades/software/v0.26.0/upgrade.go#L129-L133

Added lines #L129 - L133 were not covered by tests

case newRate.GT(commission.MaxRate):
// new rate cannot be greater than the max rate
return stakingtypes.ErrCommissionGTMaxRate

Check warning on line 137 in upgrades/software/v0.26.0/upgrade.go

View check run for this annotation

Codecov / codecov/patch

upgrades/software/v0.26.0/upgrade.go#L135-L137

Added lines #L135 - L137 were not covered by tests

case newRate.Sub(commission.Rate).GT(commission.MaxChangeRate):
// new rate % points change cannot be greater than the max change rate
return stakingtypes.ErrCommissionGTMaxChangeRate

Check warning on line 141 in upgrades/software/v0.26.0/upgrade.go

View check run for this annotation

Codecov / codecov/patch

upgrades/software/v0.26.0/upgrade.go#L139-L141

Added lines #L139 - L141 were not covered by tests
}

return nil

Check warning on line 144 in upgrades/software/v0.26.0/upgrade.go

View check run for this annotation

Codecov / codecov/patch

upgrades/software/v0.26.0/upgrade.go#L144

Added line #L144 was not covered by tests
}
2 changes: 2 additions & 0 deletions upgrades/upgrades.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package upgrades

import (
// nolint: revive
_ "github.com/akash-network/node/upgrades/software/v0.26.0"
// nolint: revive
_ "github.com/akash-network/node/upgrades/software/v0.24.0"
// nolint: revive
Expand Down

0 comments on commit f28273a

Please sign in to comment.