From 6a5cab2a34e8f23f4eaf52307ac4624f6eb254fb Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Thu, 20 Jan 2022 15:43:22 +0100 Subject: [PATCH 01/15] experiment with staking config --- simapp/app.go | 8 +++++++- simapp/simd/cmd/testnet.go | 6 +++--- testutil/network/network.go | 6 +++--- types/staking_test.go | 6 ++++-- x/staking/app_test.go | 4 ++-- x/staking/common_test.go | 1 + x/staking/keeper/common_test.go | 1 + x/staking/keeper/grpc_query_test.go | 1 + x/staking/keeper/keeper.go | 4 +++- x/staking/keeper/params.go | 5 +---- x/staking/types/config.go | 17 +++++++++++++++++ x/staking/types/historical_info_test.go | 5 ++--- x/staking/types/keys_test.go | 8 ++++---- x/staking/types/validator_test.go | 12 ++++++------ 14 files changed, 55 insertions(+), 29 deletions(-) create mode 100644 x/staking/types/config.go diff --git a/simapp/app.go b/simapp/app.go index 6fa1e2f4482c..8bf043abf1a0 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -264,8 +264,14 @@ func NewSimApp( app.BankKeeper = bankkeeper.NewBaseKeeper( appCodec, keys[banktypes.StoreKey], app.AccountKeeper, app.GetSubspace(banktypes.ModuleName), app.ModuleAccountAddrs(), ) + + stakingConfig := stakingtypes.DefaultConfig() + /* + Example of setting staking params: + stakingConfig.UnbondingTime = "72h" + */ stakingKeeper := stakingkeeper.NewKeeper( - appCodec, keys[stakingtypes.StoreKey], app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName), + appCodec, keys[stakingtypes.StoreKey], app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName), stakingConfig, ) app.MintKeeper = mintkeeper.NewKeeper( appCodec, keys[minttypes.StoreKey], app.GetSubspace(minttypes.ModuleName), &stakingKeeper, diff --git a/simapp/simd/cmd/testnet.go b/simapp/simd/cmd/testnet.go index 20ded76a92e1..1a1d20d4a1de 100644 --- a/simapp/simd/cmd/testnet.go +++ b/simapp/simd/cmd/testnet.go @@ -282,8 +282,8 @@ func initTestnetFiles( return err } - accTokens := sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction) - accStakingTokens := sdk.TokensFromConsensusPower(500, sdk.DefaultPowerReduction) + accTokens := sdk.TokensFromConsensusPower(1000, stakingtypes.DefaultConfig().PowerReduction) + accStakingTokens := sdk.TokensFromConsensusPower(500, stakingtypes.DefaultConfig().PowerReduction) coins := sdk.Coins{ sdk.NewCoin("testtoken", accTokens), sdk.NewCoin(sdk.DefaultBondDenom, accStakingTokens), @@ -292,7 +292,7 @@ func initTestnetFiles( genBalances = append(genBalances, banktypes.Balance{Address: addr.String(), Coins: coins.Sort()}) genAccounts = append(genAccounts, authtypes.NewBaseAccount(addr, nil, 0, 0)) - valTokens := sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction) + valTokens := sdk.TokensFromConsensusPower(100, stakingtypes.DefaultConfig().PowerReduction) createValMsg, err := stakingtypes.NewMsgCreateValidator( sdk.ValAddress(addr), valPubKeys[i], diff --git a/testutil/network/network.go b/testutil/network/network.go index 4de1c7288144..6de86057815e 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -116,9 +116,9 @@ func DefaultConfig() Config { NumValidators: 4, BondDenom: sdk.DefaultBondDenom, MinGasPrices: fmt.Sprintf("0.000006%s", sdk.DefaultBondDenom), - AccountTokens: sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction), - StakingTokens: sdk.TokensFromConsensusPower(500, sdk.DefaultPowerReduction), - BondedTokens: sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction), + AccountTokens: sdk.TokensFromConsensusPower(1000, stakingtypes.DefaultConfig().PowerReduction), + StakingTokens: sdk.TokensFromConsensusPower(500, stakingtypes.DefaultConfig().PowerReduction), + BondedTokens: sdk.TokensFromConsensusPower(100, stakingtypes.DefaultConfig().PowerReduction), PruningStrategy: storetypes.PruningOptionNothing, CleanupDir: true, SigningAlgo: string(hd.Secp256k1Type), diff --git a/types/staking_test.go b/types/staking_test.go index fe6c36bd991b..f41603b6bdd5 100644 --- a/types/staking_test.go +++ b/types/staking_test.go @@ -8,6 +8,8 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +var powerReduction = sdk.NewIntFromUint64(1000000) + type stakingTestSuite struct { suite.Suite } @@ -21,6 +23,6 @@ func (s *stakingTestSuite) SetupSuite() { } func (s *stakingTestSuite) TestTokensToConsensusPower() { - s.Require().Equal(int64(0), sdk.TokensToConsensusPower(sdk.NewInt(999_999), sdk.DefaultPowerReduction)) - s.Require().Equal(int64(1), sdk.TokensToConsensusPower(sdk.NewInt(1_000_000), sdk.DefaultPowerReduction)) + s.Require().Equal(int64(0), sdk.TokensToConsensusPower(sdk.NewInt(999_999), powerReduction)) + s.Require().Equal(int64(1), sdk.TokensToConsensusPower(sdk.NewInt(1_000_000), powerReduction)) } diff --git a/x/staking/app_test.go b/x/staking/app_test.go index a97954254826..862797d6be61 100644 --- a/x/staking/app_test.go +++ b/x/staking/app_test.go @@ -40,8 +40,8 @@ func checkDelegation( } func TestStakingMsgs(t *testing.T) { - genTokens := sdk.TokensFromConsensusPower(42, sdk.DefaultPowerReduction) - bondTokens := sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction) + genTokens := sdk.TokensFromConsensusPower(42, types.DefaultConfig().PowerReduction) + bondTokens := sdk.TokensFromConsensusPower(10, types.DefaultConfig().PowerReduction) genCoin := sdk.NewCoin(sdk.DefaultBondDenom, genTokens) bondCoin := sdk.NewCoin(sdk.DefaultBondDenom, bondTokens) diff --git a/x/staking/common_test.go b/x/staking/common_test.go index 93193e459ca1..6b119f44bf26 100644 --- a/x/staking/common_test.go +++ b/x/staking/common_test.go @@ -48,6 +48,7 @@ func getBaseSimappWithCustomKeeper(t *testing.T) (*codec.LegacyAmino, *simapp.Si app.AccountKeeper, app.BankKeeper, app.GetSubspace(types.ModuleName), + types.DefaultConfig(), ) app.StakingKeeper.SetParams(ctx, types.DefaultParams()) diff --git a/x/staking/keeper/common_test.go b/x/staking/keeper/common_test.go index 6833ea4c0baf..79c3726a7e5e 100644 --- a/x/staking/keeper/common_test.go +++ b/x/staking/keeper/common_test.go @@ -33,6 +33,7 @@ func createTestInput(t *testing.T) (*codec.LegacyAmino, *simapp.SimApp, sdk.Cont app.AccountKeeper, app.BankKeeper, app.GetSubspace(types.ModuleName), + types.DefaultConfig(), ) return app.LegacyAmino(), app, ctx } diff --git a/x/staking/keeper/grpc_query_test.go b/x/staking/keeper/grpc_query_test.go index 8bb355571cd2..eb698e7aebc3 100644 --- a/x/staking/keeper/grpc_query_test.go +++ b/x/staking/keeper/grpc_query_test.go @@ -780,6 +780,7 @@ func createValidators(t *testing.T, ctx sdk.Context, app *simapp.SimApp, powers app.AccountKeeper, app.BankKeeper, app.GetSubspace(types.ModuleName), + types.DefaultConfig(), ) val1 := teststaking.NewValidator(t, valAddrs[0], pks[0]) diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index 5dde8f1bb92a..893dd4116081 100644 --- a/x/staking/keeper/keeper.go +++ b/x/staking/keeper/keeper.go @@ -26,12 +26,13 @@ type Keeper struct { bankKeeper types.BankKeeper hooks types.StakingHooks paramstore paramtypes.Subspace + Config types.Config } // NewKeeper creates a new staking Keeper instance func NewKeeper( cdc codec.BinaryCodec, key storetypes.StoreKey, ak types.AccountKeeper, bk types.BankKeeper, - ps paramtypes.Subspace, + ps paramtypes.Subspace, cfg types.Config, ) Keeper { // set KeyTable if it has not already been set if !ps.HasKeyTable() { @@ -54,6 +55,7 @@ func NewKeeper( bankKeeper: bk, paramstore: ps, hooks: nil, + Config: cfg, } } diff --git a/x/staking/keeper/params.go b/x/staking/keeper/params.go index 498576fea53f..fc7e63389069 100644 --- a/x/staking/keeper/params.go +++ b/x/staking/keeper/params.go @@ -40,11 +40,8 @@ func (k Keeper) BondDenom(ctx sdk.Context) (res string) { } // PowerReduction - is the amount of staking tokens required for 1 unit of consensus-engine power. -// Currently, this returns a global variable that the app developer can tweak. -// TODO: we might turn this into an on-chain param: -// https://github.com/cosmos/cosmos-sdk/issues/8365 func (k Keeper) PowerReduction(ctx sdk.Context) sdk.Int { - return sdk.DefaultPowerReduction + return k.Config.PowerReduction } // MinCommissionRate - Minimum validator commission rate diff --git a/x/staking/types/config.go b/x/staking/types/config.go new file mode 100644 index 000000000000..481f69401e6c --- /dev/null +++ b/x/staking/types/config.go @@ -0,0 +1,17 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// Config is a config struct used for intialising the staking module to avoid using globals. +type Config struct { + // PowerReduction is the amount of staking tokens required for 1 unit of consensus-engine power + PowerReduction sdk.Int +} + +func DefaultConfig() Config { + return Config{ + PowerReduction: sdk.NewIntFromUint64(1000000), + } +} diff --git a/x/staking/types/historical_info_test.go b/x/staking/types/historical_info_test.go index 45305de91c56..079dabb23219 100644 --- a/x/staking/types/historical_info_test.go +++ b/x/staking/types/historical_info_test.go @@ -8,7 +8,6 @@ import ( "github.com/stretchr/testify/require" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -27,7 +26,7 @@ func createValidators(t *testing.T) []types.Validator { func TestHistoricalInfo(t *testing.T) { validators := createValidators(t) - hi := types.NewHistoricalInfo(header, validators, sdk.DefaultPowerReduction) + hi := types.NewHistoricalInfo(header, validators, types.DefaultConfig().PowerReduction) require.True(t, sort.IsSorted(types.Validators(hi.Valset)), "Validators are not sorted") var value []byte @@ -68,7 +67,7 @@ func TestValidateBasic(t *testing.T) { err = types.ValidateBasic(hi) require.Error(t, err, "ValidateBasic passed on unsorted ValSet") - hi = types.NewHistoricalInfo(header, validators, sdk.DefaultPowerReduction) + hi = types.NewHistoricalInfo(header, validators, types.DefaultConfig().PowerReduction) err = types.ValidateBasic(hi) require.NoError(t, err, "ValidateBasic failed on valid HistoricalInfo") } diff --git a/x/staking/types/keys_test.go b/x/staking/types/keys_test.go index 35667f949da5..e53688257032 100644 --- a/x/staking/types/keys_test.go +++ b/x/staking/types/keys_test.go @@ -28,10 +28,10 @@ func TestGetValidatorPowerRank(t *testing.T) { val1 := newValidator(t, valAddr1, keysPK1) val1.Tokens = sdk.ZeroInt() val2, val3, val4 := val1, val1, val1 - val2.Tokens = sdk.TokensFromConsensusPower(1, sdk.DefaultPowerReduction) - val3.Tokens = sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction) + val2.Tokens = sdk.TokensFromConsensusPower(1, types.DefaultConfig().PowerReduction) + val3.Tokens = sdk.TokensFromConsensusPower(10, types.DefaultConfig().PowerReduction) x := new(big.Int).Exp(big.NewInt(2), big.NewInt(40), big.NewInt(0)) - val4.Tokens = sdk.TokensFromConsensusPower(x.Int64(), sdk.DefaultPowerReduction) + val4.Tokens = sdk.TokensFromConsensusPower(x.Int64(), types.DefaultConfig().PowerReduction) tests := []struct { validator types.Validator @@ -43,7 +43,7 @@ func TestGetValidatorPowerRank(t *testing.T) { {val4, "230000010000000000149c288ede7df62742fc3b7d0962045a8cef0f79f6"}, } for i, tt := range tests { - got := hex.EncodeToString(types.GetValidatorsByPowerIndexKey(tt.validator, sdk.DefaultPowerReduction)) + got := hex.EncodeToString(types.GetValidatorsByPowerIndexKey(tt.validator, types.DefaultConfig().PowerReduction)) require.Equal(t, tt.wantHex, got, "Keys did not match on test case %d", i) } diff --git a/x/staking/types/validator_test.go b/x/staking/types/validator_test.go index 8601fbeec723..26b45e1c8586 100644 --- a/x/staking/types/validator_test.go +++ b/x/staking/types/validator_test.go @@ -58,7 +58,7 @@ func TestUpdateDescription(t *testing.T) { func TestABCIValidatorUpdate(t *testing.T) { validator := newValidator(t, valAddr1, pk1) - abciVal := validator.ABCIValidatorUpdate(sdk.DefaultPowerReduction) + abciVal := validator.ABCIValidatorUpdate(types.DefaultConfig().PowerReduction) pk, err := validator.TmConsPublicKey() require.NoError(t, err) require.Equal(t, pk, abciVal.PubKey) @@ -290,15 +290,15 @@ func TestValidatorsSortTendermint(t *testing.T) { valz := types.Validators(vals) // create expected tendermint validators by converting to tendermint then sorting - expectedVals, err := teststaking.ToTmValidators(valz, sdk.DefaultPowerReduction) + expectedVals, err := teststaking.ToTmValidators(valz, types.DefaultConfig().PowerReduction) require.NoError(t, err) sort.Sort(tmtypes.ValidatorsByVotingPower(expectedVals)) // sort in SDK and then convert to tendermint sort.SliceStable(valz, func(i, j int) bool { - return types.ValidatorsByVotingPower(valz).Less(i, j, sdk.DefaultPowerReduction) + return types.ValidatorsByVotingPower(valz).Less(i, j, types.DefaultConfig().PowerReduction) }) - actualVals, err := teststaking.ToTmValidators(valz, sdk.DefaultPowerReduction) + actualVals, err := teststaking.ToTmValidators(valz, types.DefaultConfig().PowerReduction) require.NoError(t, err) require.Equal(t, expectedVals, actualVals, "sorting in SDK is not the same as sorting in Tendermint") @@ -316,9 +316,9 @@ func TestValidatorToTm(t *testing.T) { vals[i] = val tmPk, err := cryptocodec.ToTmPubKeyInterface(pk) require.NoError(t, err) - expected[i] = tmtypes.NewValidator(tmPk, val.ConsensusPower(sdk.DefaultPowerReduction)) + expected[i] = tmtypes.NewValidator(tmPk, val.ConsensusPower(types.DefaultConfig().PowerReduction)) } - vs, err := teststaking.ToTmValidators(vals, sdk.DefaultPowerReduction) + vs, err := teststaking.ToTmValidators(vals, types.DefaultConfig().PowerReduction) require.NoError(t, err) require.Equal(t, expected, vs) } From 99ae22ec87ec40e7ce5d6b84505880da790ba324 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Thu, 20 Jan 2022 17:37:07 +0100 Subject: [PATCH 02/15] remove unneeded inits --- doc/proto-docs.md | 99 ------------------------ simapp/test_helpers.go | 2 +- types/module/module.go | 2 +- x/auth/client/testutil/suite.go | 4 +- x/auth/migrations/v043/store_test.go | 1 + x/bank/keeper/keeper_test.go | 3 +- x/bank/types/balance_test.go | 4 +- x/evidence/keeper/keeper_test.go | 2 +- x/feegrant/simulation/operations_test.go | 3 +- x/gov/abci_test.go | 2 +- x/gov/common_test.go | 2 +- x/gov/keeper/common_test.go | 1 + x/slashing/abci_test.go | 2 + x/slashing/app_test.go | 4 +- x/slashing/init_test.go | 10 --- x/slashing/keeper/common_test.go | 8 -- x/slashing/keeper/keeper_test.go | 2 + x/staking/client/cli/tx.go | 2 +- x/staking/common_test.go | 5 -- x/staking/keeper/common_test.go | 5 -- x/staking/keeper/power_reduction_test.go | 6 +- x/staking/migrations/v040/keys.go | 2 +- x/staking/migrations/v043/store_test.go | 2 +- x/staking/simulation/common_test.go | 11 --- x/staking/simulation/operations_test.go | 3 +- 25 files changed, 29 insertions(+), 158 deletions(-) delete mode 100644 doc/proto-docs.md delete mode 100644 x/slashing/init_test.go delete mode 100644 x/slashing/keeper/common_test.go delete mode 100644 x/staking/simulation/common_test.go diff --git a/doc/proto-docs.md b/doc/proto-docs.md deleted file mode 100644 index 82684d30ff9c..000000000000 --- a/doc/proto-docs.md +++ /dev/null @@ -1,99 +0,0 @@ - -# Protobuf Documentation - - -## Table of Contents - -- [cosmos/base/query/v1beta1/pagination.proto](#cosmos/base/query/v1beta1/pagination.proto) - - [PageRequest](#cosmos.base.query.v1beta1.PageRequest) - - [PageResponse](#cosmos.base.query.v1beta1.PageResponse) - -- [Scalar Value Types](#scalar-value-types) - - - - -

Top

- -## cosmos/base/query/v1beta1/pagination.proto - - - - - -### PageRequest -PageRequest is to be embedded in gRPC request messages for efficient -pagination. Ex: - - message SomeRequest { - Foo some_parameter = 1; - PageRequest pagination = 2; - } - - -| Field | Type | Label | Description | -| ----- | ---- | ----- | ----------- | -| `key` | [bytes](#bytes) | | key is a value returned in PageResponse.next_key to begin querying the next page most efficiently. Only one of offset or key should be set. | -| `offset` | [uint64](#uint64) | | offset is a numeric offset that can be used when key is unavailable. It is less efficient than using key. Only one of offset or key should be set. | -| `limit` | [uint64](#uint64) | | limit is the total number of results to be returned in the result page. If left empty it will default to a value to be set by each app. | -| `count_total` | [bool](#bool) | | count_total is set to true to indicate that the result set should include a count of the total number of items available for pagination in UIs. count_total is only respected when offset is used. It is ignored when key is set. | -| `reverse` | [bool](#bool) | | reverse is set to true if results are to be returned in the descending order. - -Since: cosmos-sdk 0.43 | - - - - - - - - -### PageResponse -PageResponse is to be embedded in gRPC response messages where the -corresponding request message has used PageRequest. - - message SomeResponse { - repeated Bar results = 1; - PageResponse page = 2; - } - - -| Field | Type | Label | Description | -| ----- | ---- | ----- | ----------- | -| `next_key` | [bytes](#bytes) | | next_key is the key to be passed to PageRequest.key to query the next page most efficiently | -| `total` | [uint64](#uint64) | | total is total number of results available if PageRequest.count_total was set, its value is undefined otherwise | - - - - - - - - - - - - - - - -## Scalar Value Types - -| .proto Type | Notes | C++ | Java | Python | Go | C# | PHP | Ruby | -| ----------- | ----- | --- | ---- | ------ | -- | -- | --- | ---- | -| double | | double | double | float | float64 | double | float | Float | -| float | | float | float | float | float32 | float | float | Float | -| int32 | Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead. | int32 | int | int | int32 | int | integer | Bignum or Fixnum (as required) | -| int64 | Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead. | int64 | long | int/long | int64 | long | integer/string | Bignum | -| uint32 | Uses variable-length encoding. | uint32 | int | int/long | uint32 | uint | integer | Bignum or Fixnum (as required) | -| uint64 | Uses variable-length encoding. | uint64 | long | int/long | uint64 | ulong | integer/string | Bignum or Fixnum (as required) | -| sint32 | Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s. | int32 | int | int | int32 | int | integer | Bignum or Fixnum (as required) | -| sint64 | Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s. | int64 | long | int/long | int64 | long | integer/string | Bignum | -| fixed32 | Always four bytes. More efficient than uint32 if values are often greater than 2^28. | uint32 | int | int | uint32 | uint | integer | Bignum or Fixnum (as required) | -| fixed64 | Always eight bytes. More efficient than uint64 if values are often greater than 2^56. | uint64 | long | int/long | uint64 | ulong | integer/string | Bignum | -| sfixed32 | Always four bytes. | int32 | int | int | int32 | int | integer | Bignum or Fixnum (as required) | -| sfixed64 | Always eight bytes. | int64 | long | int/long | int64 | long | integer/string | Bignum | -| bool | | bool | boolean | boolean | bool | bool | boolean | TrueClass/FalseClass | -| string | A string must always contain UTF-8 encoded or 7-bit ASCII text. | string | String | str/unicode | string | string | string | String (UTF-8) | -| bytes | May contain any arbitrary sequence of bytes. | string | ByteString | str | []byte | ByteString | string | String (ASCII-8BIT) | - diff --git a/simapp/test_helpers.go b/simapp/test_helpers.go index 364891a5f0c9..1be73c4257c4 100644 --- a/simapp/test_helpers.go +++ b/simapp/test_helpers.go @@ -154,7 +154,7 @@ func genesisStateWithValSet(t *testing.T, validators := make([]stakingtypes.Validator, 0, len(valSet.Validators)) delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators)) - bondAmt := sdk.DefaultPowerReduction + bondAmt := stakingtypes.DefaultConfig().PowerReduction for _, val := range valSet.Validators { pk, err := cryptocodec.FromTmPubKeyInterface(val.PubKey) diff --git a/types/module/module.go b/types/module/module.go index 4128d781adfc..958a278ddb5a 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -335,7 +335,7 @@ func (m *Manager) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, genesisData // a chain must initialize with a non-empty validator set if len(validatorUpdates) == 0 { - panic(fmt.Sprintf("validator set is empty after InitGenesis, please ensure at least one validator is initialized with a delegation greater than or equal to the DefaultPowerReduction (%d)", sdk.DefaultPowerReduction)) + panic(fmt.Sprintf("validator set is empty after InitGenesis, please ensure at least one validator is initialized with a delegation greater than or equal to the DefaultPowerReduction, found in x/staking/types/config.go")) } return abci.ResponseInitChain{ diff --git a/x/auth/client/testutil/suite.go b/x/auth/client/testutil/suite.go index 9120e21a5539..3d512c2b9875 100644 --- a/x/auth/client/testutil/suite.go +++ b/x/auth/client/testutil/suite.go @@ -648,7 +648,7 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() { account, err := val1.ClientCtx.Keyring.Key("newAccount") s.Require().NoError(err) - sendTokens := sdk.NewCoin(s.cfg.BondDenom, sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction)) + sendTokens := sdk.NewCoin(s.cfg.BondDenom, sdk.NewIntFromUint64(10_000_000)) addr, err := account.GetAddress() s.Require().NoError(err) @@ -849,7 +849,7 @@ func (s *IntegrationTestSuite) TestCLIMultisignInsufficientCosigners() { func (s *IntegrationTestSuite) TestCLIEncode() { val1 := s.network.Validators[0] - sendTokens := sdk.NewCoin(s.cfg.BondDenom, sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction)) + sendTokens := sdk.NewCoin(s.cfg.BondDenom, sdk.NewIntFromUint64(10_000_000)) normalGeneratedTx, err := s.createBankMsg( val1, val1.Address, diff --git a/x/auth/migrations/v043/store_test.go b/x/auth/migrations/v043/store_test.go index cae87b96885c..af5f68cce1c5 100644 --- a/x/auth/migrations/v043/store_test.go +++ b/x/auth/migrations/v043/store_test.go @@ -669,6 +669,7 @@ func createValidator(t *testing.T, ctx sdk.Context, app *simapp.SimApp, powers i app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName), + stakingtypes.DefaultConfig(), ) val1, err := stakingtypes.NewValidator(valAddrs[0], pks[0], stakingtypes.Description{}) diff --git a/x/bank/keeper/keeper_test.go b/x/bank/keeper/keeper_test.go index 87338b4fba1f..f03186eaa913 100644 --- a/x/bank/keeper/keeper_test.go +++ b/x/bank/keeper/keeper_test.go @@ -20,6 +20,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/bank/testutil" "github.com/cosmos/cosmos-sdk/x/bank/types" minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) const ( @@ -39,7 +40,7 @@ var ( randomPermAcc = authtypes.NewEmptyModuleAccount(randomPerm, "random") // The default power validators are initialized to have within tests - initTokens = sdk.TokensFromConsensusPower(initialPower, sdk.DefaultPowerReduction) + initTokens = sdk.TokensFromConsensusPower(initialPower, stakingtypes.DefaultConfig().PowerReduction) initCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens)) ) diff --git a/x/bank/types/balance_test.go b/x/bank/types/balance_test.go index 10ee2a74bf6e..fa6c8bb37722 100644 --- a/x/bank/types/balance_test.go +++ b/x/bank/types/balance_test.go @@ -141,7 +141,7 @@ func TestBalance_GetAddress(t *testing.T) { func TestSanitizeBalances(t *testing.T) { // 1. Generate balances - tokens := sdk.TokensFromConsensusPower(81, sdk.DefaultPowerReduction) + tokens := sdk.NewIntFromUint64(81_000_000) coin := sdk.NewCoin("benchcoin", tokens) coins := sdk.Coins{coin} addrs, _ := makeRandomAddressesAndPublicKeys(20) @@ -193,7 +193,7 @@ func BenchmarkSanitizeBalances1000(b *testing.B) { func benchmarkSanitizeBalances(b *testing.B, nAddresses int) { b.ReportAllocs() - tokens := sdk.TokensFromConsensusPower(81, sdk.DefaultPowerReduction) + tokens := sdk.NewIntFromUint64(81_000_000) coin := sdk.NewCoin("benchcoin", tokens) coins := sdk.Coins{coin} addrs, _ := makeRandomAddressesAndPublicKeys(nAddresses) diff --git a/x/evidence/keeper/keeper_test.go b/x/evidence/keeper/keeper_test.go index b0b12176efc2..c6b127e004ee 100644 --- a/x/evidence/keeper/keeper_test.go +++ b/x/evidence/keeper/keeper_test.go @@ -35,7 +35,7 @@ var ( } // The default power validators are initialized to have within tests - initAmt = sdk.TokensFromConsensusPower(200, sdk.DefaultPowerReduction) + initAmt = sdk.NewIntFromUint64(200_000_000) initCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt)) ) diff --git a/x/feegrant/simulation/operations_test.go b/x/feegrant/simulation/operations_test.go index 59554125a0aa..16decdd275c3 100644 --- a/x/feegrant/simulation/operations_test.go +++ b/x/feegrant/simulation/operations_test.go @@ -16,6 +16,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/bank/testutil" "github.com/cosmos/cosmos-sdk/x/feegrant" "github.com/cosmos/cosmos-sdk/x/feegrant/simulation" + "github.com/cosmos/cosmos-sdk/x/staking/types" ) type SimTestSuite struct { @@ -38,7 +39,7 @@ func (suite *SimTestSuite) SetupTest() { func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account { accounts := simtypes.RandomAccounts(r, n) - initAmt := sdk.TokensFromConsensusPower(200, sdk.DefaultPowerReduction) + initAmt := sdk.TokensFromConsensusPower(200, types.DefaultConfig().PowerReduction) initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt)) // add coins to the accounts diff --git a/x/gov/abci_test.go b/x/gov/abci_test.go index 0bc855bce04f..6501824fa41a 100644 --- a/x/gov/abci_test.go +++ b/x/gov/abci_test.go @@ -369,7 +369,7 @@ func createValidators(t *testing.T, stakingMsgSvr stakingtypes.MsgServer, ctx sd require.True(t, len(addrs) <= len(pubkeys), "Not enough pubkeys specified at top of file.") for i := 0; i < len(addrs); i++ { - valTokens := sdk.TokensFromConsensusPower(powerAmt[i], sdk.DefaultPowerReduction) + valTokens := sdk.TokensFromConsensusPower(powerAmt[i], stakingtypes.DefaultConfig().PowerReduction) valCreateMsg, err := stakingtypes.NewMsgCreateValidator( addrs[i], pubkeys[i], sdk.NewCoin(sdk.DefaultBondDenom, valTokens), TestDescription, TestCommissionRates, sdk.OneInt(), diff --git a/x/gov/common_test.go b/x/gov/common_test.go index dc1d9ad63e77..71132bda27e5 100644 --- a/x/gov/common_test.go +++ b/x/gov/common_test.go @@ -13,7 +13,7 @@ import ( ) var ( - valTokens = sdk.TokensFromConsensusPower(42, sdk.DefaultPowerReduction) + valTokens = sdk.TokensFromConsensusPower(42, stakingtypes.DefaultConfig().PowerReduction) TestProposal = v1beta1.NewTextProposal("Test", "description") TestDescription = stakingtypes.NewDescription("T", "E", "S", "T", "Z") TestCommissionRates = stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()) diff --git a/x/gov/keeper/common_test.go b/x/gov/keeper/common_test.go index 195938860514..b75be8704233 100644 --- a/x/gov/keeper/common_test.go +++ b/x/gov/keeper/common_test.go @@ -29,6 +29,7 @@ func createValidators(t *testing.T, ctx sdk.Context, app *simapp.SimApp, powers app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName), + stakingtypes.DefaultConfig(), ) val1, err := stakingtypes.NewValidator(valAddrs[0], pks[0], stakingtypes.Description{}) diff --git a/x/slashing/abci_test.go b/x/slashing/abci_test.go index f2958d4d3b62..fe3e5043b922 100644 --- a/x/slashing/abci_test.go +++ b/x/slashing/abci_test.go @@ -25,12 +25,14 @@ func TestBeginBlocker(t *testing.T) { addr, pk := sdk.ValAddress(pks[0].Address()), pks[0] tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper) + InitTokens := sdk.TokensFromConsensusPower(200, stakingtypes.DefaultConfig().PowerReduction) // bond the validator power := int64(100) amt := tstaking.CreateValidatorWithValPower(addr, pk, power, true) staking.EndBlocker(ctx, app.StakingKeeper) require.Equal( t, app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(addr)), + sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.GetParams(ctx).BondDenom, InitTokens.Sub(amt))), ) require.Equal(t, amt, app.StakingKeeper.Validator(ctx, addr).GetBondedTokens()) diff --git a/x/slashing/app_test.go b/x/slashing/app_test.go index e9ece2c690a1..208dfce829a9 100644 --- a/x/slashing/app_test.go +++ b/x/slashing/app_test.go @@ -41,8 +41,8 @@ func checkValidatorSigningInfo(t *testing.T, app *simapp.SimApp, addr sdk.ConsAd } func TestSlashingMsgs(t *testing.T) { - genTokens := sdk.TokensFromConsensusPower(42, sdk.DefaultPowerReduction) - bondTokens := sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction) + genTokens := sdk.TokensFromConsensusPower(42, stakingtypes.DefaultConfig().PowerReduction) + bondTokens := sdk.TokensFromConsensusPower(10, stakingtypes.DefaultConfig().PowerReduction) genCoin := sdk.NewCoin(sdk.DefaultBondDenom, genTokens) bondCoin := sdk.NewCoin(sdk.DefaultBondDenom, bondTokens) diff --git a/x/slashing/init_test.go b/x/slashing/init_test.go deleted file mode 100644 index a2217cfda7b0..000000000000 --- a/x/slashing/init_test.go +++ /dev/null @@ -1,10 +0,0 @@ -package slashing_test - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" -) - -var ( - // The default power validators are initialized to have within tests - InitTokens = sdk.TokensFromConsensusPower(200, sdk.DefaultPowerReduction) -) diff --git a/x/slashing/keeper/common_test.go b/x/slashing/keeper/common_test.go deleted file mode 100644 index 91a972ed1110..000000000000 --- a/x/slashing/keeper/common_test.go +++ /dev/null @@ -1,8 +0,0 @@ -package keeper_test - -import sdk "github.com/cosmos/cosmos-sdk/types" - -var ( - // The default power validators are initialized to have within tests - InitTokens = sdk.TokensFromConsensusPower(200, sdk.DefaultPowerReduction) -) diff --git a/x/slashing/keeper/keeper_test.go b/x/slashing/keeper/keeper_test.go index 7385c1f4d7e2..175105fd56f0 100644 --- a/x/slashing/keeper/keeper_test.go +++ b/x/slashing/keeper/keeper_test.go @@ -94,10 +94,12 @@ func TestHandleNewValidator(t *testing.T) { // Validator created amt := tstaking.CreateValidatorWithValPower(addr, val, 100, true) + InitTokens := sdk.TokensFromConsensusPower(200, sdk.DefaultPowerReduction) staking.EndBlocker(ctx, app.StakingKeeper) require.Equal( t, app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(addr)), + sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.GetParams(ctx).BondDenom, InitTokens.Sub(amt))), ) require.Equal(t, amt, app.StakingKeeper.Validator(ctx, addr).GetBondedTokens()) diff --git a/x/staking/client/cli/tx.go b/x/staking/client/cli/tx.go index 138bfa24930d..1fa962723546 100644 --- a/x/staking/client/cli/tx.go +++ b/x/staking/client/cli/tx.go @@ -20,7 +20,7 @@ import ( // default values var ( - DefaultTokens = sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction) + DefaultTokens = sdk.TokensFromConsensusPower(100, types.DefaultConfig().PowerReduction) defaultAmount = DefaultTokens.String() + sdk.DefaultBondDenom defaultCommissionRate = "0.1" defaultCommissionMaxRate = "0.2" diff --git a/x/staking/common_test.go b/x/staking/common_test.go index 6b119f44bf26..4ed51e5d116a 100644 --- a/x/staking/common_test.go +++ b/x/staking/common_test.go @@ -1,7 +1,6 @@ package staking_test import ( - "math/big" "testing" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -15,10 +14,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/types" ) -func init() { - sdk.DefaultPowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)) -} - // nolint:deadcode,unused,varcheck var ( priv1 = secp256k1.GenPrivKey() diff --git a/x/staking/keeper/common_test.go b/x/staking/keeper/common_test.go index 79c3726a7e5e..26dd80f7801e 100644 --- a/x/staking/keeper/common_test.go +++ b/x/staking/keeper/common_test.go @@ -1,7 +1,6 @@ package keeper_test import ( - "math/big" "testing" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -17,10 +16,6 @@ var ( PKs = simapp.CreateTestPubKeys(500) ) -func init() { - sdk.DefaultPowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)) -} - // createTestInput Returns a simapp with custom StakingKeeper // to avoid messing with the hooks. func createTestInput(t *testing.T) (*codec.LegacyAmino, *simapp.SimApp, sdk.Context) { diff --git a/x/staking/keeper/power_reduction_test.go b/x/staking/keeper/power_reduction_test.go index e18230d854b2..cca2f326b690 100644 --- a/x/staking/keeper/power_reduction_test.go +++ b/x/staking/keeper/power_reduction_test.go @@ -5,11 +5,11 @@ import ( ) func (suite *KeeperTestSuite) TestTokensToConsensusPower() { - suite.Require().Equal(int64(0), suite.app.StakingKeeper.TokensToConsensusPower(suite.ctx, sdk.DefaultPowerReduction.Sub(sdk.NewInt(1)))) - suite.Require().Equal(int64(1), suite.app.StakingKeeper.TokensToConsensusPower(suite.ctx, sdk.DefaultPowerReduction)) + suite.Require().Equal(int64(0), suite.app.StakingKeeper.TokensToConsensusPower(suite.ctx, suite.app.StakingKeeper.Config.PowerReduction.Sub(sdk.NewInt(1)))) + suite.Require().Equal(int64(1), suite.app.StakingKeeper.TokensToConsensusPower(suite.ctx, suite.app.StakingKeeper.Config.PowerReduction)) } func (suite *KeeperTestSuite) TestTokensFromConsensusPower() { suite.Require().Equal(sdk.NewInt(0), suite.app.StakingKeeper.TokensFromConsensusPower(suite.ctx, 0)) - suite.Require().Equal(sdk.DefaultPowerReduction, suite.app.StakingKeeper.TokensFromConsensusPower(suite.ctx, 1)) + suite.Require().Equal(suite.app.StakingKeeper.Config.PowerReduction, suite.app.StakingKeeper.TokensFromConsensusPower(suite.ctx, 1)) } diff --git a/x/staking/migrations/v040/keys.go b/x/staking/migrations/v040/keys.go index 27e2f9eae02b..45127fd2699b 100644 --- a/x/staking/migrations/v040/keys.go +++ b/x/staking/migrations/v040/keys.go @@ -79,7 +79,7 @@ func GetValidatorsByPowerIndexKey(validator types.Validator) []byte { // NOTE the address doesn't need to be stored because counter bytes must always be different // NOTE the larger values are of higher value - consensusPower := sdk.TokensToConsensusPower(validator.Tokens, sdk.DefaultPowerReduction) + consensusPower := sdk.TokensToConsensusPower(validator.Tokens, types.DefaultConfig().PowerReduction) consensusPowerBytes := make([]byte, 8) binary.BigEndian.PutUint64(consensusPowerBytes, uint64(consensusPower)) diff --git a/x/staking/migrations/v043/store_test.go b/x/staking/migrations/v043/store_test.go index 756590a5dc19..15d451f3aaf0 100644 --- a/x/staking/migrations/v043/store_test.go +++ b/x/staking/migrations/v043/store_test.go @@ -62,7 +62,7 @@ func TestStoreMigration(t *testing.T) { { "ValidatorsByPowerIndexKey", v040staking.GetValidatorsByPowerIndexKey(val), - types.GetValidatorsByPowerIndexKey(val, sdk.DefaultPowerReduction), + types.GetValidatorsByPowerIndexKey(val, types.DefaultConfig().PowerReduction), }, { "DelegationKey", diff --git a/x/staking/simulation/common_test.go b/x/staking/simulation/common_test.go deleted file mode 100644 index e22ecd15aa2c..000000000000 --- a/x/staking/simulation/common_test.go +++ /dev/null @@ -1,11 +0,0 @@ -package simulation_test - -import ( - "math/big" - - sdk "github.com/cosmos/cosmos-sdk/types" -) - -func init() { - sdk.DefaultPowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)) -} diff --git a/x/staking/simulation/operations_test.go b/x/staking/simulation/operations_test.go index 8c1ee9ec152e..956c5905271f 100644 --- a/x/staking/simulation/operations_test.go +++ b/x/staking/simulation/operations_test.go @@ -255,7 +255,7 @@ func TestSimulateMsgBeginRedelegate(t *testing.T) { // returns context and an app with updated mint keeper func createTestApp(t *testing.T, isCheckTx bool, r *rand.Rand, n int) (*simapp.SimApp, sdk.Context, []simtypes.Account) { - sdk.DefaultPowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)) + // sdk.DefaultPowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)) accounts := simtypes.RandomAccounts(r, n) // create validator set with single validator @@ -280,6 +280,7 @@ func createTestApp(t *testing.T, isCheckTx bool, r *rand.Rand, n int) (*simapp.S app.MintKeeper.SetParams(ctx, minttypes.DefaultParams()) app.MintKeeper.SetMinter(ctx, minttypes.DefaultInitialMinter()) + app.StakingKeeper.Config.PowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)) initAmt := app.StakingKeeper.TokensFromConsensusPower(ctx, 200) initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt)) From 8f8e8687d9332f307db95a156060f1184d1935b3 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Thu, 20 Jan 2022 17:41:00 +0100 Subject: [PATCH 03/15] minor change --- types/staking.go | 3 ++- x/bank/keeper/grpc_query_test.go | 2 +- x/slashing/keeper/keeper_test.go | 2 +- x/staking/simulation/operations_test.go | 1 - 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/types/staking.go b/types/staking.go index 2f17bb1dd85f..eb00a6a7bc77 100644 --- a/types/staking.go +++ b/types/staking.go @@ -19,8 +19,9 @@ const ( ) // DefaultPowerReduction is the default amount of staking tokens required for 1 unit of consensus-engine power -var DefaultPowerReduction = NewIntFromUint64(1000000) +var DefaultPowerReduction = NewIntFromUint64(1000000) // todo, remove or move to testutil as testing global +// todo: move to testutil or to staking. ideally staking since this is a staking module specific function // TokensToConsensusPower - convert input tokens to potential consensus-engine power func TokensToConsensusPower(tokens Int, powerReduction Int) int64 { return (tokens.Quo(powerReduction)).Int64() diff --git a/x/bank/keeper/grpc_query_test.go b/x/bank/keeper/grpc_query_test.go index 1f8a1de5efc4..be7387144140 100644 --- a/x/bank/keeper/grpc_query_test.go +++ b/x/bank/keeper/grpc_query_test.go @@ -324,7 +324,7 @@ func (suite *IntegrationTestSuite) TestGRPCDenomOwners() { bal := sdk.NewCoins(sdk.NewCoin( sdk.DefaultBondDenom, - sdk.TokensFromConsensusPower(initialPower/10, sdk.DefaultPowerReduction), + sdk.NewInt(initialPower/10).Mul(sdk.NewIntFromUint64(1_000_000)), )) suite.Require().NoError(keeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, acc.GetAddress(), bal)) } diff --git a/x/slashing/keeper/keeper_test.go b/x/slashing/keeper/keeper_test.go index 175105fd56f0..ad877bddd132 100644 --- a/x/slashing/keeper/keeper_test.go +++ b/x/slashing/keeper/keeper_test.go @@ -94,7 +94,7 @@ func TestHandleNewValidator(t *testing.T) { // Validator created amt := tstaking.CreateValidatorWithValPower(addr, val, 100, true) - InitTokens := sdk.TokensFromConsensusPower(200, sdk.DefaultPowerReduction) + InitTokens := sdk.NewIntFromUint64(200_000_000) staking.EndBlocker(ctx, app.StakingKeeper) require.Equal( diff --git a/x/staking/simulation/operations_test.go b/x/staking/simulation/operations_test.go index 956c5905271f..156463377d2f 100644 --- a/x/staking/simulation/operations_test.go +++ b/x/staking/simulation/operations_test.go @@ -255,7 +255,6 @@ func TestSimulateMsgBeginRedelegate(t *testing.T) { // returns context and an app with updated mint keeper func createTestApp(t *testing.T, isCheckTx bool, r *rand.Rand, n int) (*simapp.SimApp, sdk.Context, []simtypes.Account) { - // sdk.DefaultPowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)) accounts := simtypes.RandomAccounts(r, n) // create validator set with single validator From 371ccbc71f31a9e93a56632c75f13edaf7e1caf2 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Thu, 20 Jan 2022 18:05:55 +0100 Subject: [PATCH 04/15] modify comment --- simapp/app.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simapp/app.go b/simapp/app.go index 8bf043abf1a0..69c72399e42e 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -268,7 +268,7 @@ func NewSimApp( stakingConfig := stakingtypes.DefaultConfig() /* Example of setting staking params: - stakingConfig.UnbondingTime = "72h" + stakingConfig.PowerReduction = sdk.NewIntFromUint64(10) */ stakingKeeper := stakingkeeper.NewKeeper( appCodec, keys[stakingtypes.StoreKey], app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName), stakingConfig, From cbc5aa4a69c637a69df994e6a60059d2b3ab4dc9 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Fri, 21 Jan 2022 12:44:45 +0100 Subject: [PATCH 05/15] remove types/staking.go --- simapp/simd/cmd/testnet.go | 6 ++-- testutil/network/network.go | 6 ++-- types/globals.go | 19 +++++++++++ types/staking.go | 33 ------------------- x/auth/migrations/v043/store_test.go | 2 +- x/bank/keeper/keeper_test.go | 3 +- x/feegrant/simulation/operations_test.go | 3 +- x/gov/abci_test.go | 2 +- x/gov/common_test.go | 2 +- x/slashing/abci_test.go | 2 +- x/slashing/app_test.go | 4 +-- x/staking/app_test.go | 4 +-- x/staking/client/cli/tx.go | 2 +- x/staking/keeper/power_reduction.go | 5 +-- x/staking/migrations/v040/keys.go | 2 +- x/staking/types/keys.go | 2 +- x/staking/types/keys_test.go | 6 ++-- x/staking/types/power_reduction.go | 12 +++++++ .../staking/types/power_reduction_test.go | 5 +-- x/staking/types/validator.go | 2 +- 20 files changed, 60 insertions(+), 62 deletions(-) create mode 100644 types/globals.go delete mode 100644 types/staking.go create mode 100644 x/staking/types/power_reduction.go rename types/staking_test.go => x/staking/types/power_reduction_test.go (63%) diff --git a/simapp/simd/cmd/testnet.go b/simapp/simd/cmd/testnet.go index 1a1d20d4a1de..3c285c055de3 100644 --- a/simapp/simd/cmd/testnet.go +++ b/simapp/simd/cmd/testnet.go @@ -282,8 +282,8 @@ func initTestnetFiles( return err } - accTokens := sdk.TokensFromConsensusPower(1000, stakingtypes.DefaultConfig().PowerReduction) - accStakingTokens := sdk.TokensFromConsensusPower(500, stakingtypes.DefaultConfig().PowerReduction) + accTokens := sdk.NewIntFromUint64(1000_000_000) + accStakingTokens := sdk.NewIntFromUint64(500_000_000) coins := sdk.Coins{ sdk.NewCoin("testtoken", accTokens), sdk.NewCoin(sdk.DefaultBondDenom, accStakingTokens), @@ -292,7 +292,7 @@ func initTestnetFiles( genBalances = append(genBalances, banktypes.Balance{Address: addr.String(), Coins: coins.Sort()}) genAccounts = append(genAccounts, authtypes.NewBaseAccount(addr, nil, 0, 0)) - valTokens := sdk.TokensFromConsensusPower(100, stakingtypes.DefaultConfig().PowerReduction) + valTokens := sdk.NewIntFromUint64(100_000_000) createValMsg, err := stakingtypes.NewMsgCreateValidator( sdk.ValAddress(addr), valPubKeys[i], diff --git a/testutil/network/network.go b/testutil/network/network.go index 6de86057815e..aad86683a8b7 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -116,9 +116,9 @@ func DefaultConfig() Config { NumValidators: 4, BondDenom: sdk.DefaultBondDenom, MinGasPrices: fmt.Sprintf("0.000006%s", sdk.DefaultBondDenom), - AccountTokens: sdk.TokensFromConsensusPower(1000, stakingtypes.DefaultConfig().PowerReduction), - StakingTokens: sdk.TokensFromConsensusPower(500, stakingtypes.DefaultConfig().PowerReduction), - BondedTokens: sdk.TokensFromConsensusPower(100, stakingtypes.DefaultConfig().PowerReduction), + AccountTokens: sdk.NewIntFromUint64(1000_000_000), + StakingTokens: sdk.NewIntFromUint64(500_000_000), + BondedTokens: sdk.NewIntFromUint64(100_000_000), PruningStrategy: storetypes.PruningOptionNothing, CleanupDir: true, SigningAlgo: string(hd.Secp256k1Type), diff --git a/types/globals.go b/types/globals.go new file mode 100644 index 000000000000..9c7d6126144d --- /dev/null +++ b/types/globals.go @@ -0,0 +1,19 @@ +package types + +// staking constants +const ( + + // default bond denomination + DefaultBondDenom = "stake" + + // Delay, in blocks, between when validator updates are returned to the + // consensus-engine and when they are applied. For example, if + // ValidatorUpdateDelay is set to X, and if a validator set update is + // returned with new validators at the end of block 10, then the new + // validators are expected to sign blocks beginning at block 11+X. + // + // This value is constant as this should not change without a hard fork. + // For Tendermint this should be set to 1 block, for more details see: + // https://tendermint.com/docs/spec/abci/apps.html#endblock + ValidatorUpdateDelay int64 = 1 +) diff --git a/types/staking.go b/types/staking.go deleted file mode 100644 index eb00a6a7bc77..000000000000 --- a/types/staking.go +++ /dev/null @@ -1,33 +0,0 @@ -package types - -// staking constants -const ( - - // default bond denomination - DefaultBondDenom = "stake" - - // Delay, in blocks, between when validator updates are returned to the - // consensus-engine and when they are applied. For example, if - // ValidatorUpdateDelay is set to X, and if a validator set update is - // returned with new validators at the end of block 10, then the new - // validators are expected to sign blocks beginning at block 11+X. - // - // This value is constant as this should not change without a hard fork. - // For Tendermint this should be set to 1 block, for more details see: - // https://tendermint.com/docs/spec/abci/apps.html#endblock - ValidatorUpdateDelay int64 = 1 -) - -// DefaultPowerReduction is the default amount of staking tokens required for 1 unit of consensus-engine power -var DefaultPowerReduction = NewIntFromUint64(1000000) // todo, remove or move to testutil as testing global - -// todo: move to testutil or to staking. ideally staking since this is a staking module specific function -// TokensToConsensusPower - convert input tokens to potential consensus-engine power -func TokensToConsensusPower(tokens Int, powerReduction Int) int64 { - return (tokens.Quo(powerReduction)).Int64() -} - -// TokensFromConsensusPower - convert input power to tokens -func TokensFromConsensusPower(power int64, powerReduction Int) Int { - return NewInt(power).Mul(powerReduction) -} diff --git a/x/auth/migrations/v043/store_test.go b/x/auth/migrations/v043/store_test.go index af5f68cce1c5..07a941bcc8e9 100644 --- a/x/auth/migrations/v043/store_test.go +++ b/x/auth/migrations/v043/store_test.go @@ -657,7 +657,7 @@ func dirtyTrackingFields(ctx sdk.Context, vesting exported.VestingAccount, app * } func createValidator(t *testing.T, ctx sdk.Context, app *simapp.SimApp, powers int64) (sdk.AccAddress, sdk.ValAddress) { - valTokens := sdk.TokensFromConsensusPower(powers, sdk.DefaultPowerReduction) + valTokens := sdk.NewInt(powers).Mul(sdk.NewIntFromUint64(1_000_000)) addrs := simapp.AddTestAddrsIncremental(app, ctx, 1, valTokens) valAddrs := simapp.ConvertAddrsToValAddrs(addrs) pks := simapp.CreateTestPubKeys(1) diff --git a/x/bank/keeper/keeper_test.go b/x/bank/keeper/keeper_test.go index f03186eaa913..20718caf654e 100644 --- a/x/bank/keeper/keeper_test.go +++ b/x/bank/keeper/keeper_test.go @@ -20,7 +20,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/bank/testutil" "github.com/cosmos/cosmos-sdk/x/bank/types" minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) const ( @@ -40,7 +39,7 @@ var ( randomPermAcc = authtypes.NewEmptyModuleAccount(randomPerm, "random") // The default power validators are initialized to have within tests - initTokens = sdk.TokensFromConsensusPower(initialPower, stakingtypes.DefaultConfig().PowerReduction) + initTokens = sdk.NewInt(initialPower).Mul(sdk.NewIntFromUint64(1_000_000)) initCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens)) ) diff --git a/x/feegrant/simulation/operations_test.go b/x/feegrant/simulation/operations_test.go index 16decdd275c3..7b89a0ce8bde 100644 --- a/x/feegrant/simulation/operations_test.go +++ b/x/feegrant/simulation/operations_test.go @@ -16,7 +16,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/bank/testutil" "github.com/cosmos/cosmos-sdk/x/feegrant" "github.com/cosmos/cosmos-sdk/x/feegrant/simulation" - "github.com/cosmos/cosmos-sdk/x/staking/types" ) type SimTestSuite struct { @@ -39,7 +38,7 @@ func (suite *SimTestSuite) SetupTest() { func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account { accounts := simtypes.RandomAccounts(r, n) - initAmt := sdk.TokensFromConsensusPower(200, types.DefaultConfig().PowerReduction) + initAmt := sdk.NewIntFromUint64(200_000_000) initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt)) // add coins to the accounts diff --git a/x/gov/abci_test.go b/x/gov/abci_test.go index 6501824fa41a..980702cc3c64 100644 --- a/x/gov/abci_test.go +++ b/x/gov/abci_test.go @@ -369,7 +369,7 @@ func createValidators(t *testing.T, stakingMsgSvr stakingtypes.MsgServer, ctx sd require.True(t, len(addrs) <= len(pubkeys), "Not enough pubkeys specified at top of file.") for i := 0; i < len(addrs); i++ { - valTokens := sdk.TokensFromConsensusPower(powerAmt[i], stakingtypes.DefaultConfig().PowerReduction) + valTokens := sdk.NewInt(powerAmt[i]).Mul(sdk.NewIntFromUint64(1_000_000)) valCreateMsg, err := stakingtypes.NewMsgCreateValidator( addrs[i], pubkeys[i], sdk.NewCoin(sdk.DefaultBondDenom, valTokens), TestDescription, TestCommissionRates, sdk.OneInt(), diff --git a/x/gov/common_test.go b/x/gov/common_test.go index 71132bda27e5..896c98487ac4 100644 --- a/x/gov/common_test.go +++ b/x/gov/common_test.go @@ -13,7 +13,7 @@ import ( ) var ( - valTokens = sdk.TokensFromConsensusPower(42, stakingtypes.DefaultConfig().PowerReduction) + valTokens = sdk.NewIntFromUint64(42_000_000) TestProposal = v1beta1.NewTextProposal("Test", "description") TestDescription = stakingtypes.NewDescription("T", "E", "S", "T", "Z") TestCommissionRates = stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()) diff --git a/x/slashing/abci_test.go b/x/slashing/abci_test.go index fe3e5043b922..ad80e7f4fb12 100644 --- a/x/slashing/abci_test.go +++ b/x/slashing/abci_test.go @@ -25,7 +25,7 @@ func TestBeginBlocker(t *testing.T) { addr, pk := sdk.ValAddress(pks[0].Address()), pks[0] tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper) - InitTokens := sdk.TokensFromConsensusPower(200, stakingtypes.DefaultConfig().PowerReduction) + InitTokens := sdk.NewIntFromUint64(200_000_000) // bond the validator power := int64(100) amt := tstaking.CreateValidatorWithValPower(addr, pk, power, true) diff --git a/x/slashing/app_test.go b/x/slashing/app_test.go index 208dfce829a9..f4a384ab9016 100644 --- a/x/slashing/app_test.go +++ b/x/slashing/app_test.go @@ -41,8 +41,8 @@ func checkValidatorSigningInfo(t *testing.T, app *simapp.SimApp, addr sdk.ConsAd } func TestSlashingMsgs(t *testing.T) { - genTokens := sdk.TokensFromConsensusPower(42, stakingtypes.DefaultConfig().PowerReduction) - bondTokens := sdk.TokensFromConsensusPower(10, stakingtypes.DefaultConfig().PowerReduction) + genTokens := sdk.NewIntFromUint64(42_000_000) + bondTokens := sdk.NewIntFromUint64(10_000_000) genCoin := sdk.NewCoin(sdk.DefaultBondDenom, genTokens) bondCoin := sdk.NewCoin(sdk.DefaultBondDenom, bondTokens) diff --git a/x/staking/app_test.go b/x/staking/app_test.go index 862797d6be61..5b57ecbc1722 100644 --- a/x/staking/app_test.go +++ b/x/staking/app_test.go @@ -40,8 +40,8 @@ func checkDelegation( } func TestStakingMsgs(t *testing.T) { - genTokens := sdk.TokensFromConsensusPower(42, types.DefaultConfig().PowerReduction) - bondTokens := sdk.TokensFromConsensusPower(10, types.DefaultConfig().PowerReduction) + genTokens := sdk.NewIntFromUint64(42_000_000) + bondTokens := sdk.NewIntFromUint64(10_000_000) genCoin := sdk.NewCoin(sdk.DefaultBondDenom, genTokens) bondCoin := sdk.NewCoin(sdk.DefaultBondDenom, bondTokens) diff --git a/x/staking/client/cli/tx.go b/x/staking/client/cli/tx.go index 1fa962723546..dc35967691ee 100644 --- a/x/staking/client/cli/tx.go +++ b/x/staking/client/cli/tx.go @@ -20,7 +20,7 @@ import ( // default values var ( - DefaultTokens = sdk.TokensFromConsensusPower(100, types.DefaultConfig().PowerReduction) + DefaultTokens = sdk.NewIntFromUint64(100_000_000) defaultAmount = DefaultTokens.String() + sdk.DefaultBondDenom defaultCommissionRate = "0.1" defaultCommissionMaxRate = "0.2" diff --git a/x/staking/keeper/power_reduction.go b/x/staking/keeper/power_reduction.go index d979228b36fa..5a64cbead97f 100644 --- a/x/staking/keeper/power_reduction.go +++ b/x/staking/keeper/power_reduction.go @@ -2,14 +2,15 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/staking/types" ) // TokensToConsensusPower - convert input tokens to potential consensus-engine power func (k Keeper) TokensToConsensusPower(ctx sdk.Context, tokens sdk.Int) int64 { - return sdk.TokensToConsensusPower(tokens, k.PowerReduction(ctx)) + return types.TokensToConsensusPower(tokens, k.PowerReduction(ctx)) } // TokensFromConsensusPower - convert input power to tokens func (k Keeper) TokensFromConsensusPower(ctx sdk.Context, power int64) sdk.Int { - return sdk.TokensFromConsensusPower(power, k.PowerReduction(ctx)) + return types.TokensFromConsensusPower(power, k.PowerReduction(ctx)) } diff --git a/x/staking/migrations/v040/keys.go b/x/staking/migrations/v040/keys.go index 45127fd2699b..0384915bfac7 100644 --- a/x/staking/migrations/v040/keys.go +++ b/x/staking/migrations/v040/keys.go @@ -79,7 +79,7 @@ func GetValidatorsByPowerIndexKey(validator types.Validator) []byte { // NOTE the address doesn't need to be stored because counter bytes must always be different // NOTE the larger values are of higher value - consensusPower := sdk.TokensToConsensusPower(validator.Tokens, types.DefaultConfig().PowerReduction) + consensusPower := types.TokensToConsensusPower(validator.Tokens, types.DefaultConfig().PowerReduction) consensusPowerBytes := make([]byte, 8) binary.BigEndian.PutUint64(consensusPowerBytes, uint64(consensusPower)) diff --git a/x/staking/types/keys.go b/x/staking/types/keys.go index 74d73bf19c7e..0877680564f7 100644 --- a/x/staking/types/keys.go +++ b/x/staking/types/keys.go @@ -82,7 +82,7 @@ func GetValidatorsByPowerIndexKey(validator Validator, powerReduction sdk.Int) [ // NOTE the address doesn't need to be stored because counter bytes must always be different // NOTE the larger values are of higher value - consensusPower := sdk.TokensToConsensusPower(validator.Tokens, powerReduction) + consensusPower := TokensToConsensusPower(validator.Tokens, powerReduction) consensusPowerBytes := make([]byte, 8) binary.BigEndian.PutUint64(consensusPowerBytes, uint64(consensusPower)) diff --git a/x/staking/types/keys_test.go b/x/staking/types/keys_test.go index e53688257032..e4f36af1ef94 100644 --- a/x/staking/types/keys_test.go +++ b/x/staking/types/keys_test.go @@ -28,10 +28,10 @@ func TestGetValidatorPowerRank(t *testing.T) { val1 := newValidator(t, valAddr1, keysPK1) val1.Tokens = sdk.ZeroInt() val2, val3, val4 := val1, val1, val1 - val2.Tokens = sdk.TokensFromConsensusPower(1, types.DefaultConfig().PowerReduction) - val3.Tokens = sdk.TokensFromConsensusPower(10, types.DefaultConfig().PowerReduction) + val2.Tokens = sdk.NewIntFromUint64(1_000_000) + val3.Tokens = sdk.NewIntFromUint64(10_000_000) x := new(big.Int).Exp(big.NewInt(2), big.NewInt(40), big.NewInt(0)) - val4.Tokens = sdk.TokensFromConsensusPower(x.Int64(), types.DefaultConfig().PowerReduction) + val4.Tokens = sdk.NewInt(x.Int64()).Mul(sdk.NewIntFromUint64(1_000_000)) tests := []struct { validator types.Validator diff --git a/x/staking/types/power_reduction.go b/x/staking/types/power_reduction.go new file mode 100644 index 000000000000..6cd58db5d5c7 --- /dev/null +++ b/x/staking/types/power_reduction.go @@ -0,0 +1,12 @@ +package types + +import sdk "github.com/cosmos/cosmos-sdk/types" + +func TokensToConsensusPower(tokens sdk.Int, powerReduction sdk.Int) int64 { + return (tokens.Quo(powerReduction)).Int64() +} + +// TokensFromConsensusPower - convert input power to tokens +func TokensFromConsensusPower(power int64, powerReduction sdk.Int) sdk.Int { + return sdk.NewInt(power).Mul(powerReduction) +} diff --git a/types/staking_test.go b/x/staking/types/power_reduction_test.go similarity index 63% rename from types/staking_test.go rename to x/staking/types/power_reduction_test.go index f41603b6bdd5..14a06ca2c712 100644 --- a/types/staking_test.go +++ b/x/staking/types/power_reduction_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/suite" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/staking/types" ) var powerReduction = sdk.NewIntFromUint64(1000000) @@ -23,6 +24,6 @@ func (s *stakingTestSuite) SetupSuite() { } func (s *stakingTestSuite) TestTokensToConsensusPower() { - s.Require().Equal(int64(0), sdk.TokensToConsensusPower(sdk.NewInt(999_999), powerReduction)) - s.Require().Equal(int64(1), sdk.TokensToConsensusPower(sdk.NewInt(1_000_000), powerReduction)) + s.Require().Equal(int64(0), types.TokensToConsensusPower(sdk.NewInt(999_999), powerReduction)) + s.Require().Equal(int64(1), types.TokensToConsensusPower(sdk.NewInt(1_000_000), powerReduction)) } diff --git a/x/staking/types/validator.go b/x/staking/types/validator.go index 79bd2a4f55b1..2d5b434eb4fc 100644 --- a/x/staking/types/validator.go +++ b/x/staking/types/validator.go @@ -366,7 +366,7 @@ func (v Validator) ConsensusPower(r sdk.Int) int64 { // PotentialConsensusPower returns the potential consensus-engine power. func (v Validator) PotentialConsensusPower(r sdk.Int) int64 { - return sdk.TokensToConsensusPower(v.Tokens, r) + return TokensToConsensusPower(v.Tokens, r) } // UpdateStatus updates the location of the shares within a validator From d2b4dd744d537a090f88f49567c6b14b3305b421 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Fri, 21 Jan 2022 12:48:37 +0100 Subject: [PATCH 06/15] reuse values --- x/staking/common_test.go | 6 ++++-- x/staking/keeper/common_test.go | 6 +++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/x/staking/common_test.go b/x/staking/common_test.go index 4ed51e5d116a..d4681f27b1f6 100644 --- a/x/staking/common_test.go +++ b/x/staking/common_test.go @@ -1,6 +1,7 @@ package staking_test import ( + "math/big" "testing" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -36,14 +37,15 @@ func getBaseSimappWithCustomKeeper(t *testing.T) (*codec.LegacyAmino, *simapp.Si ctx := app.BaseApp.NewContext(false, tmproto.Header{}) appCodec := app.AppCodec() - + stakingConfig := types.DefaultConfig() + stakingConfig.PowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)) app.StakingKeeper = keeper.NewKeeper( appCodec, app.GetKey(types.StoreKey), app.AccountKeeper, app.BankKeeper, app.GetSubspace(types.ModuleName), - types.DefaultConfig(), + stakingConfig, ) app.StakingKeeper.SetParams(ctx, types.DefaultParams()) diff --git a/x/staking/keeper/common_test.go b/x/staking/keeper/common_test.go index 26dd80f7801e..8251f95afa1f 100644 --- a/x/staking/keeper/common_test.go +++ b/x/staking/keeper/common_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "math/big" "testing" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -22,13 +23,16 @@ func createTestInput(t *testing.T) (*codec.LegacyAmino, *simapp.SimApp, sdk.Cont app := simapp.Setup(t, false) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) + stakingConfig := types.DefaultConfig() + stakingConfig.PowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)) + app.StakingKeeper = keeper.NewKeeper( app.AppCodec(), app.GetKey(types.StoreKey), app.AccountKeeper, app.BankKeeper, app.GetSubspace(types.ModuleName), - types.DefaultConfig(), + stakingConfig, ) return app.LegacyAmino(), app, ctx } From 2708cf23542a814e3c3c21bedad28688f198b0ba Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Fri, 21 Jan 2022 17:00:37 +0100 Subject: [PATCH 07/15] handle overflow --- x/staking/types/power_reduction.go | 12 ++++++++++-- x/staking/types/power_reduction_test.go | 4 ++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/x/staking/types/power_reduction.go b/x/staking/types/power_reduction.go index 6cd58db5d5c7..808de0ba2361 100644 --- a/x/staking/types/power_reduction.go +++ b/x/staking/types/power_reduction.go @@ -1,9 +1,17 @@ package types -import sdk "github.com/cosmos/cosmos-sdk/types" +import ( + "math" + + sdk "github.com/cosmos/cosmos-sdk/types" +) func TokensToConsensusPower(tokens sdk.Int, powerReduction sdk.Int) int64 { - return (tokens.Quo(powerReduction)).Int64() + power := tokens.Quo(powerReduction) + if power.GT(sdk.NewIntFromUint64(math.MaxInt64)) { + return 0 + } + return power.Int64() } // TokensFromConsensusPower - convert input power to tokens diff --git a/x/staking/types/power_reduction_test.go b/x/staking/types/power_reduction_test.go index 14a06ca2c712..26cffe2d7a1c 100644 --- a/x/staking/types/power_reduction_test.go +++ b/x/staking/types/power_reduction_test.go @@ -1,6 +1,8 @@ package types_test import ( + "math" + "math/big" "testing" "github.com/stretchr/testify/suite" @@ -24,6 +26,8 @@ func (s *stakingTestSuite) SetupSuite() { } func (s *stakingTestSuite) TestTokensToConsensusPower() { + s.Require().Equal(int64(0), types.TokensToConsensusPower(sdk.NewInt(999_999), powerReduction)) s.Require().Equal(int64(1), types.TokensToConsensusPower(sdk.NewInt(1_000_000), powerReduction)) + s.Require().Equal(int64(0), types.TokensToConsensusPower(sdk.NewIntFromBigInt(big.NewInt(math.MaxInt64).Add(big.NewInt(math.MaxInt64), big.NewInt(1))), sdk.NewIntFromUint64(1))) } From 57e4f14028e000a86a004d5d27a6e27e5a4fce17 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Fri, 21 Jan 2022 18:17:57 +0100 Subject: [PATCH 08/15] add changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1ca41f204a5..4d10d096ac08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -114,6 +114,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [\#10748](https://github.com/cosmos/cosmos-sdk/pull/10748) Move legacy `x/gov` api to `v1beta1` directory. * [\#10816](https://github.com/cosmos/cosmos-sdk/pull/10816) Reuse blocked addresses from the bank module. No need to pass them to distribution. * [\#10852](https://github.com/cosmos/cosmos-sdk/pull/10852) Move `x/gov/types` to `x/gov/types/v1beta2`. +* [\#10988](https://github.com/cosmos/cosmos-sdk/pull/10988#pullrequestreview-859809392) Removes sdk.Powerreduction as a global and pass it directly to the staking keeper on creation. + - Deletes `types/staking.go` and moves the functions to `x/staking/types/power_reduction.go` ### Client Breaking Changes From 2a5a324d11e9ed8ac541a3da415153ea1c5e165b Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Fri, 21 Jan 2022 18:34:32 +0100 Subject: [PATCH 09/15] address comments --- x/staking/types/power_reduction.go | 6 ++++++ x/staking/types/power_reduction_test.go | 1 + 2 files changed, 7 insertions(+) diff --git a/x/staking/types/power_reduction.go b/x/staking/types/power_reduction.go index 808de0ba2361..a8565636fc35 100644 --- a/x/staking/types/power_reduction.go +++ b/x/staking/types/power_reduction.go @@ -6,8 +6,14 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +// TokensToConsensusPower - convert input tokens to Power func TokensToConsensusPower(tokens sdk.Int, powerReduction sdk.Int) int64 { + if powerReduction.IsZero() { + return 0 + } + power := tokens.Quo(powerReduction) + if power.GT(sdk.NewIntFromUint64(math.MaxInt64)) { return 0 } diff --git a/x/staking/types/power_reduction_test.go b/x/staking/types/power_reduction_test.go index 26cffe2d7a1c..448063574ba1 100644 --- a/x/staking/types/power_reduction_test.go +++ b/x/staking/types/power_reduction_test.go @@ -30,4 +30,5 @@ func (s *stakingTestSuite) TestTokensToConsensusPower() { s.Require().Equal(int64(0), types.TokensToConsensusPower(sdk.NewInt(999_999), powerReduction)) s.Require().Equal(int64(1), types.TokensToConsensusPower(sdk.NewInt(1_000_000), powerReduction)) s.Require().Equal(int64(0), types.TokensToConsensusPower(sdk.NewIntFromBigInt(big.NewInt(math.MaxInt64).Add(big.NewInt(math.MaxInt64), big.NewInt(1))), sdk.NewIntFromUint64(1))) + s.Require().Equal(int64(0), types.TokensToConsensusPower(sdk.NewInt(1_000_000), sdk.NewIntFromUint64(0))) } From 8c0ed03774da18d1b765f3322d4f59c8e9852840 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Fri, 21 Jan 2022 18:37:54 +0100 Subject: [PATCH 10/15] edit changelog --- CHANGELOG.md | 2 +- x/staking/keeper/params.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b562abde675a..bfdf48b9ea2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -116,7 +116,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [\#10816](https://github.com/cosmos/cosmos-sdk/pull/10816) Reuse blocked addresses from the bank module. No need to pass them to distribution. * [\#10852](https://github.com/cosmos/cosmos-sdk/pull/10852) Move `x/gov/types` to `x/gov/types/v1beta2`. * [\#10868](https://github.com/cosmos/cosmos-sdk/pull/10868) The Gov keeper accepts now a mandatory last argument, the ServiceMsgRouter. -* [\#10988](https://github.com/cosmos/cosmos-sdk/pull/10988#pullrequestreview-859809392) Removes sdk.Powerreduction as a global and pass it directly to the staking keeper on creation. +* [\#10988](https://github.com/cosmos/cosmos-sdk/pull/10988) Removes sdk.PowerReduction as a global and pass it directly to the staking keeper on creation. - Deletes `types/staking.go` and moves the functions to `x/staking/types/power_reduction.go` ### Client Breaking Changes diff --git a/x/staking/keeper/params.go b/x/staking/keeper/params.go index fc7e63389069..daf36ee42f1b 100644 --- a/x/staking/keeper/params.go +++ b/x/staking/keeper/params.go @@ -40,7 +40,7 @@ func (k Keeper) BondDenom(ctx sdk.Context) (res string) { } // PowerReduction - is the amount of staking tokens required for 1 unit of consensus-engine power. -func (k Keeper) PowerReduction(ctx sdk.Context) sdk.Int { +func (k Keeper) PowerReduction(_ sdk.Context) sdk.Int { return k.Config.PowerReduction } From cb6977f70b54a876d0e231f3b5b75649e63180c6 Mon Sep 17 00:00:00 2001 From: Marko Date: Sun, 23 Jan 2022 17:25:28 +0100 Subject: [PATCH 11/15] Update x/staking/types/power_reduction.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> --- x/staking/types/power_reduction.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/staking/types/power_reduction.go b/x/staking/types/power_reduction.go index a8565636fc35..8f61dd518b07 100644 --- a/x/staking/types/power_reduction.go +++ b/x/staking/types/power_reduction.go @@ -8,7 +8,7 @@ import ( // TokensToConsensusPower - convert input tokens to Power func TokensToConsensusPower(tokens sdk.Int, powerReduction sdk.Int) int64 { - if powerReduction.IsZero() { + if tokens.IsNil() || powerReduction.IsNil() || powerReduction.IsZero() { return 0 } From d43fe2b740f17728aaff4607be609cd3f91b2e9c Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Wed, 26 Jan 2022 10:40:08 +0100 Subject: [PATCH 12/15] fix group sims --- x/group/simulation/operations_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/group/simulation/operations_test.go b/x/group/simulation/operations_test.go index 31e082434e14..8e913c13a39a 100644 --- a/x/group/simulation/operations_test.go +++ b/x/group/simulation/operations_test.go @@ -77,7 +77,7 @@ func (suite *SimTestSuite) TestWeightedOperations() { func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account { accounts := simtypes.RandomAccounts(r, n) - initAmt := sdk.TokensFromConsensusPower(200, sdk.DefaultPowerReduction) + initAmt := sdk.NewIntFromUint64(200_000_000) initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt)) // add coins to the accounts From 522e92c88b297f131ff5909195e7383571533ca0 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Wed, 2 Feb 2022 13:46:04 +0100 Subject: [PATCH 13/15] add testutil token file --- testutil/token.go | 28 ++++++++++++++++++++++++ x/auth/client/testutil/suite.go | 4 ++-- x/auth/migrations/v043/store_test.go | 3 ++- x/bank/keeper/grpc_query_test.go | 3 ++- x/bank/keeper/keeper_test.go | 4 ++-- x/bank/types/balance_test.go | 8 +++---- x/evidence/keeper/keeper_test.go | 6 ++--- x/feegrant/simulation/operations_test.go | 3 ++- x/gov/abci_test.go | 3 ++- x/gov/common_test.go | 3 ++- x/group/simulation/operations_test.go | 3 ++- x/slashing/app_test.go | 5 +++-- x/staking/app_test.go | 5 +++-- x/staking/client/cli/tx.go | 3 ++- x/staking/common_test.go | 6 ++--- 15 files changed, 60 insertions(+), 27 deletions(-) create mode 100644 testutil/token.go diff --git a/testutil/token.go b/testutil/token.go new file mode 100644 index 000000000000..88338a9bf0cb --- /dev/null +++ b/testutil/token.go @@ -0,0 +1,28 @@ +package testutil + +import ( + "math" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +var DefaultpowerReduction = sdk.NewInt(1_000_000) + +// TokensToConsensusPower - convert input tokens to Power +func TokensToConsensusPower(tokens sdk.Int, powerReduction sdk.Int) int64 { + if tokens.IsNil() || powerReduction.IsNil() || powerReduction.IsZero() { + return 0 + } + + power := tokens.Quo(powerReduction) + + if power.GT(sdk.NewIntFromUint64(math.MaxInt64)) { + return 0 + } + return power.Int64() +} + +// TokensFromConsensusPower - convert input power to tokens +func TokensFromConsensusPower(power int64, powerReduction sdk.Int) sdk.Int { + return sdk.NewInt(power).Mul(powerReduction) +} diff --git a/x/auth/client/testutil/suite.go b/x/auth/client/testutil/suite.go index c496b3dab655..b5c0209a3327 100644 --- a/x/auth/client/testutil/suite.go +++ b/x/auth/client/testutil/suite.go @@ -648,7 +648,7 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() { account, err := val1.ClientCtx.Keyring.Key("newAccount") s.Require().NoError(err) - sendTokens := sdk.NewCoin(s.cfg.BondDenom, sdk.NewIntFromUint64(10_000_000)) + sendTokens := sdk.NewCoin(s.cfg.BondDenom, testutil.TokensFromConsensusPower(10, testutil.DefaultpowerReduction)) addr, err := account.GetAddress() s.Require().NoError(err) @@ -849,7 +849,7 @@ func (s *IntegrationTestSuite) TestCLIMultisignInsufficientCosigners() { func (s *IntegrationTestSuite) TestCLIEncode() { val1 := s.network.Validators[0] - sendTokens := sdk.NewCoin(s.cfg.BondDenom, sdk.NewIntFromUint64(10_000_000)) + sendTokens := sdk.NewCoin(s.cfg.BondDenom, testutil.TokensFromConsensusPower(10, testutil.DefaultpowerReduction)) normalGeneratedTx, err := s.createBankMsg( val1, val1.Address, diff --git a/x/auth/migrations/v043/store_test.go b/x/auth/migrations/v043/store_test.go index 07a941bcc8e9..386926eedc46 100644 --- a/x/auth/migrations/v043/store_test.go +++ b/x/auth/migrations/v043/store_test.go @@ -9,6 +9,7 @@ import ( tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" @@ -657,7 +658,7 @@ func dirtyTrackingFields(ctx sdk.Context, vesting exported.VestingAccount, app * } func createValidator(t *testing.T, ctx sdk.Context, app *simapp.SimApp, powers int64) (sdk.AccAddress, sdk.ValAddress) { - valTokens := sdk.NewInt(powers).Mul(sdk.NewIntFromUint64(1_000_000)) + valTokens := testutil.TokensFromConsensusPower(powers, testutil.DefaultpowerReduction) addrs := simapp.AddTestAddrsIncremental(app, ctx, 1, valTokens) valAddrs := simapp.ConvertAddrsToValAddrs(addrs) pks := simapp.CreateTestPubKeys(1) diff --git a/x/bank/keeper/grpc_query_test.go b/x/bank/keeper/grpc_query_test.go index be7387144140..ed58dcb7f356 100644 --- a/x/bank/keeper/grpc_query_test.go +++ b/x/bank/keeper/grpc_query_test.go @@ -4,6 +4,7 @@ import ( gocontext "context" "fmt" + sdktestutil "github.com/cosmos/cosmos-sdk/testutil" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/query" @@ -324,7 +325,7 @@ func (suite *IntegrationTestSuite) TestGRPCDenomOwners() { bal := sdk.NewCoins(sdk.NewCoin( sdk.DefaultBondDenom, - sdk.NewInt(initialPower/10).Mul(sdk.NewIntFromUint64(1_000_000)), + sdktestutil.TokensFromConsensusPower(initialPower/10, sdktestutil.DefaultpowerReduction), )) suite.Require().NoError(keeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, acc.GetAddress(), bal)) } diff --git a/x/bank/keeper/keeper_test.go b/x/bank/keeper/keeper_test.go index 20718caf654e..9bf4962c2a0c 100644 --- a/x/bank/keeper/keeper_test.go +++ b/x/bank/keeper/keeper_test.go @@ -11,6 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/simapp" + sdktestutil "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/query" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" @@ -39,8 +40,7 @@ var ( randomPermAcc = authtypes.NewEmptyModuleAccount(randomPerm, "random") // The default power validators are initialized to have within tests - initTokens = sdk.NewInt(initialPower).Mul(sdk.NewIntFromUint64(1_000_000)) - initCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens)) + initCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdktestutil.TokensFromConsensusPower(initialPower, sdktestutil.DefaultpowerReduction))) ) func newFooCoin(amt int64) sdk.Coin { diff --git a/x/bank/types/balance_test.go b/x/bank/types/balance_test.go index fa6c8bb37722..e1ecee4a9baa 100644 --- a/x/bank/types/balance_test.go +++ b/x/bank/types/balance_test.go @@ -7,6 +7,7 @@ import ( "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" bank "github.com/cosmos/cosmos-sdk/x/bank/types" ) @@ -141,8 +142,7 @@ func TestBalance_GetAddress(t *testing.T) { func TestSanitizeBalances(t *testing.T) { // 1. Generate balances - tokens := sdk.NewIntFromUint64(81_000_000) - coin := sdk.NewCoin("benchcoin", tokens) + coin := sdk.NewCoin("benchcoin", testutil.TokensFromConsensusPower(81, testutil.DefaultpowerReduction)) coins := sdk.Coins{coin} addrs, _ := makeRandomAddressesAndPublicKeys(20) @@ -193,9 +193,7 @@ func BenchmarkSanitizeBalances1000(b *testing.B) { func benchmarkSanitizeBalances(b *testing.B, nAddresses int) { b.ReportAllocs() - tokens := sdk.NewIntFromUint64(81_000_000) - coin := sdk.NewCoin("benchcoin", tokens) - coins := sdk.Coins{coin} + coins := sdk.NewCoins(sdk.NewCoin("benchcoin", testutil.TokensFromConsensusPower(81, testutil.DefaultpowerReduction))) addrs, _ := makeRandomAddressesAndPublicKeys(nAddresses) b.ResetTimer() diff --git a/x/evidence/keeper/keeper_test.go b/x/evidence/keeper/keeper_test.go index c6b127e004ee..517f129bf0aa 100644 --- a/x/evidence/keeper/keeper_test.go +++ b/x/evidence/keeper/keeper_test.go @@ -5,8 +5,6 @@ import ( "fmt" "time" - minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" - "github.com/stretchr/testify/suite" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -14,11 +12,13 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/evidence/exported" "github.com/cosmos/cosmos-sdk/x/evidence/keeper" "github.com/cosmos/cosmos-sdk/x/evidence/types" + minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" ) var ( @@ -35,7 +35,7 @@ var ( } // The default power validators are initialized to have within tests - initAmt = sdk.NewIntFromUint64(200_000_000) + initAmt = testutil.TokensFromConsensusPower(200, testutil.DefaultpowerReduction) initCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt)) ) diff --git a/x/feegrant/simulation/operations_test.go b/x/feegrant/simulation/operations_test.go index 7b89a0ce8bde..3faa6a8b7a8d 100644 --- a/x/feegrant/simulation/operations_test.go +++ b/x/feegrant/simulation/operations_test.go @@ -11,6 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/simapp" simappparams "github.com/cosmos/cosmos-sdk/simapp/params" + sdktestutil "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/cosmos/cosmos-sdk/x/bank/testutil" @@ -38,7 +39,7 @@ func (suite *SimTestSuite) SetupTest() { func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account { accounts := simtypes.RandomAccounts(r, n) - initAmt := sdk.NewIntFromUint64(200_000_000) + initAmt := sdktestutil.TokensFromConsensusPower(200, sdktestutil.DefaultpowerReduction) initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt)) // add coins to the accounts diff --git a/x/gov/abci_test.go b/x/gov/abci_test.go index 0031c87ff767..c95ff8ead901 100644 --- a/x/gov/abci_test.go +++ b/x/gov/abci_test.go @@ -9,6 +9,7 @@ import ( tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/gov" "github.com/cosmos/cosmos-sdk/x/gov/keeper" @@ -373,7 +374,7 @@ func createValidators(t *testing.T, stakingMsgSvr stakingtypes.MsgServer, ctx sd require.True(t, len(addrs) <= len(pubkeys), "Not enough pubkeys specified at top of file.") for i := 0; i < len(addrs); i++ { - valTokens := sdk.NewInt(powerAmt[i]).Mul(sdk.NewIntFromUint64(1_000_000)) + valTokens := testutil.TokensFromConsensusPower(powerAmt[i], testutil.DefaultpowerReduction) valCreateMsg, err := stakingtypes.NewMsgCreateValidator( addrs[i], pubkeys[i], sdk.NewCoin(sdk.DefaultBondDenom, valTokens), TestDescription, TestCommissionRates, sdk.OneInt(), diff --git a/x/gov/common_test.go b/x/gov/common_test.go index a5b7412927cf..8674dc204a48 100644 --- a/x/gov/common_test.go +++ b/x/gov/common_test.go @@ -8,6 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/gov/types" @@ -18,7 +19,7 @@ import ( ) var ( - valTokens = sdk.NewIntFromUint64(42_000_000) + valTokens = testutil.TokensFromConsensusPower(42, testutil.DefaultpowerReduction) TestProposal = v1beta1.NewTextProposal("Test", "description") TestDescription = stakingtypes.NewDescription("T", "E", "S", "T", "Z") TestCommissionRates = stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()) diff --git a/x/group/simulation/operations_test.go b/x/group/simulation/operations_test.go index 8e913c13a39a..68c843800c5b 100644 --- a/x/group/simulation/operations_test.go +++ b/x/group/simulation/operations_test.go @@ -10,6 +10,7 @@ import ( tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/cosmos/cosmos-sdk/simapp" + sdktestutil "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/cosmos/cosmos-sdk/x/bank/testutil" @@ -77,7 +78,7 @@ func (suite *SimTestSuite) TestWeightedOperations() { func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account { accounts := simtypes.RandomAccounts(r, n) - initAmt := sdk.NewIntFromUint64(200_000_000) + initAmt := sdktestutil.TokensFromConsensusPower(200, sdktestutil.DefaultpowerReduction) initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt)) // add coins to the accounts diff --git a/x/slashing/app_test.go b/x/slashing/app_test.go index f4a384ab9016..f4802ad24a61 100644 --- a/x/slashing/app_test.go +++ b/x/slashing/app_test.go @@ -11,6 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" @@ -41,8 +42,8 @@ func checkValidatorSigningInfo(t *testing.T, app *simapp.SimApp, addr sdk.ConsAd } func TestSlashingMsgs(t *testing.T) { - genTokens := sdk.NewIntFromUint64(42_000_000) - bondTokens := sdk.NewIntFromUint64(10_000_000) + genTokens := testutil.TokensFromConsensusPower(42, testutil.DefaultpowerReduction) + bondTokens := testutil.TokensFromConsensusPower(10, testutil.DefaultpowerReduction) genCoin := sdk.NewCoin(sdk.DefaultBondDenom, genTokens) bondCoin := sdk.NewCoin(sdk.DefaultBondDenom, bondTokens) diff --git a/x/staking/app_test.go b/x/staking/app_test.go index 5b57ecbc1722..c8a68ead15ca 100644 --- a/x/staking/app_test.go +++ b/x/staking/app_test.go @@ -8,6 +8,7 @@ import ( tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" @@ -40,8 +41,8 @@ func checkDelegation( } func TestStakingMsgs(t *testing.T) { - genTokens := sdk.NewIntFromUint64(42_000_000) - bondTokens := sdk.NewIntFromUint64(10_000_000) + genTokens := testutil.TokensFromConsensusPower(42, testutil.DefaultpowerReduction) + bondTokens := testutil.TokensFromConsensusPower(10, testutil.DefaultpowerReduction) genCoin := sdk.NewCoin(sdk.DefaultBondDenom, genTokens) bondCoin := sdk.NewCoin(sdk.DefaultBondDenom, bondTokens) diff --git a/x/staking/client/cli/tx.go b/x/staking/client/cli/tx.go index dc35967691ee..4d1259b2c8d1 100644 --- a/x/staking/client/cli/tx.go +++ b/x/staking/client/cli/tx.go @@ -12,6 +12,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/tx" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/version" @@ -20,7 +21,7 @@ import ( // default values var ( - DefaultTokens = sdk.NewIntFromUint64(100_000_000) + DefaultTokens = testutil.TokensFromConsensusPower(100, testutil.DefaultpowerReduction) defaultAmount = DefaultTokens.String() + sdk.DefaultBondDenom defaultCommissionRate = "0.1" defaultCommissionMaxRate = "0.2" diff --git a/x/staking/common_test.go b/x/staking/common_test.go index d4681f27b1f6..4ed51e5d116a 100644 --- a/x/staking/common_test.go +++ b/x/staking/common_test.go @@ -1,7 +1,6 @@ package staking_test import ( - "math/big" "testing" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -37,15 +36,14 @@ func getBaseSimappWithCustomKeeper(t *testing.T) (*codec.LegacyAmino, *simapp.Si ctx := app.BaseApp.NewContext(false, tmproto.Header{}) appCodec := app.AppCodec() - stakingConfig := types.DefaultConfig() - stakingConfig.PowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)) + app.StakingKeeper = keeper.NewKeeper( appCodec, app.GetKey(types.StoreKey), app.AccountKeeper, app.BankKeeper, app.GetSubspace(types.ModuleName), - stakingConfig, + types.DefaultConfig(), ) app.StakingKeeper.SetParams(ctx, types.DefaultParams()) From 0c529e3975cf56742ae09409814e278204f03f07 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Wed, 2 Feb 2022 13:52:06 +0100 Subject: [PATCH 14/15] remove changelog --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 073a5be204fa..4ca2f4437838 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -119,7 +119,6 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [\#10816](https://github.com/cosmos/cosmos-sdk/pull/10816) Reuse blocked addresses from the bank module. No need to pass them to distribution. * [\#10852](https://github.com/cosmos/cosmos-sdk/pull/10852) Move `x/gov/types` to `x/gov/types/v1beta2`. * [\#10868](https://github.com/cosmos/cosmos-sdk/pull/10868), [\#10989](https://github.com/cosmos/cosmos-sdk/pull/10989) The Gov keeper accepts now 2 more mandatory arguments, the ServiceMsgRouter and a maximum proposal metadata length. -* [\#10868](https://github.com/cosmos/cosmos-sdk/pull/10868) The Gov keeper accepts now a mandatory last argument, the ServiceMsgRouter. * [\#10988](https://github.com/cosmos/cosmos-sdk/pull/10988) Removes sdk.PowerReduction as a global and pass it directly to the staking keeper on creation. - Deletes `types/staking.go` and moves the functions to `x/staking/types/power_reduction.go` From 2e604609cc6aaf9c8364061fe2361aad64e38c35 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Thu, 3 Feb 2022 12:03:02 +0100 Subject: [PATCH 15/15] fix import cycle --- x/staking/app_test.go | 5 ++--- x/staking/client/cli/tx.go | 3 +-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/x/staking/app_test.go b/x/staking/app_test.go index c8a68ead15ca..4324891f745d 100644 --- a/x/staking/app_test.go +++ b/x/staking/app_test.go @@ -8,7 +8,6 @@ import ( tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/cosmos/cosmos-sdk/simapp" - "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" @@ -41,8 +40,8 @@ func checkDelegation( } func TestStakingMsgs(t *testing.T) { - genTokens := testutil.TokensFromConsensusPower(42, testutil.DefaultpowerReduction) - bondTokens := testutil.TokensFromConsensusPower(10, testutil.DefaultpowerReduction) + genTokens := types.TokensFromConsensusPower(42, types.DefaultConfig().PowerReduction) + bondTokens := types.TokensFromConsensusPower(10, types.DefaultConfig().PowerReduction) genCoin := sdk.NewCoin(sdk.DefaultBondDenom, genTokens) bondCoin := sdk.NewCoin(sdk.DefaultBondDenom, bondTokens) diff --git a/x/staking/client/cli/tx.go b/x/staking/client/cli/tx.go index 4d1259b2c8d1..f99e3924d8ac 100644 --- a/x/staking/client/cli/tx.go +++ b/x/staking/client/cli/tx.go @@ -12,7 +12,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/tx" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/version" @@ -21,7 +20,7 @@ import ( // default values var ( - DefaultTokens = testutil.TokensFromConsensusPower(100, testutil.DefaultpowerReduction) + DefaultTokens = types.TokensFromConsensusPower(100, types.DefaultConfig().PowerReduction) defaultAmount = DefaultTokens.String() + sdk.DefaultBondDenom defaultCommissionRate = "0.1" defaultCommissionMaxRate = "0.2"