-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(upgrade): create v0.26.0 upgrade
Signed-off-by: Artur Troian <troian.ap@gmail.com>
- Loading branch information
Showing
8 changed files
with
289 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
return up.MM.RunMigrations(ctx, up.Configurator, fromVM) | ||
} | ||
} | ||
|
||
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 | ||
} | ||
|
||
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 | ||
} | ||
|
||
validator.Commission = newCommission | ||
validator.Commission.MaxChangeRate = maxRateCh | ||
} | ||
|
||
up.Keepers.Cosmos.Staking.BeforeValidatorModified(ctx, validator.GetOperator()) | ||
up.Keepers.Cosmos.Staking.SetValidator(ctx, validator) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// 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 | ||
} | ||
|
||
commission.Rate = newRate | ||
commission.UpdateTime = blockTime | ||
|
||
return commission, nil | ||
} | ||
|
||
// 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 | ||
|
||
case newRate.GT(commission.MaxRate): | ||
// new rate cannot be greater than the max rate | ||
return stakingtypes.ErrCommissionGTMaxRate | ||
|
||
case newRate.Sub(commission.Rate).GT(commission.MaxChangeRate): | ||
// new rate % points change cannot be greater than the max change rate | ||
return stakingtypes.ErrCommissionGTMaxChangeRate | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters