From 8c89023e9f7ce67492142c92acc9ba0d9f876c0e Mon Sep 17 00:00:00 2001 From: frog power 4000 Date: Wed, 12 Jun 2019 17:57:47 +0200 Subject: [PATCH] Merge PR #4524: Staking ValidatorPowerRank to use "Potential Consensus Power" --- .pending/breaking/sdk/3985-ValidatorPowerR | 1 + docs/spec/staking/01_state.md | 47 +-- types/bytes.go | 11 + types/staking.go | 24 +- x/distribution/keeper/delegation_test.go | 24 +- x/distribution/keeper/keeper_test.go | 4 +- x/distribution/keeper/test_common.go | 2 +- x/genutil/gentx.go | 2 +- x/gov/endblocker_test.go | 6 +- x/gov/genesis.go | 2 +- x/gov/keeper_test.go | 6 +- x/gov/tally_test.go | 16 +- x/gov/test_common.go | 6 +- x/slashing/abci_app_test.go | 2 +- x/slashing/app_test.go | 6 +- x/slashing/handler_test.go | 6 +- x/slashing/keeper_test.go | 28 +- x/slashing/test_common.go | 2 +- x/slashing/types/keys.go | 10 +- x/staking/app_test.go | 6 +- x/staking/client/cli/tx.go | 2 +- x/staking/exported/exported.go | 2 +- x/staking/genesis.go | 2 +- x/staking/genesis_test.go | 10 +- x/staking/handler_test.go | 54 ++-- x/staking/keeper/delegation_test.go | 66 ++-- x/staking/keeper/invariants.go | 2 +- x/staking/keeper/querier_test.go | 14 +- x/staking/keeper/slash.go | 2 +- x/staking/keeper/slash_test.go | 52 ++-- x/staking/keeper/test_common.go | 2 +- x/staking/keeper/val_state_change.go | 4 +- x/staking/keeper/validator_test.go | 100 +++---- x/staking/types/key.go | 282 ------------------ x/staking/types/keys.go | 265 ++++++++++++++++ x/staking/types/{key_test.go => keys_test.go} | 6 +- x/staking/types/validator.go | 16 +- 37 files changed, 546 insertions(+), 546 deletions(-) create mode 100644 .pending/breaking/sdk/3985-ValidatorPowerR create mode 100644 types/bytes.go delete mode 100644 x/staking/types/key.go rename x/staking/types/{key_test.go => keys_test.go} (95%) diff --git a/.pending/breaking/sdk/3985-ValidatorPowerR b/.pending/breaking/sdk/3985-ValidatorPowerR new file mode 100644 index 000000000000..81de934af2d7 --- /dev/null +++ b/.pending/breaking/sdk/3985-ValidatorPowerR @@ -0,0 +1 @@ +#3985 `ValidatorPowerRank` uses potential consensus power instead of tendermint power diff --git a/docs/spec/staking/01_state.md b/docs/spec/staking/01_state.md index 72b9e0908487..04e42f2071e4 100644 --- a/docs/spec/staking/01_state.md +++ b/docs/spec/staking/01_state.md @@ -52,8 +52,8 @@ records within a block. - Validators: `0x21 | OperatorAddr -> amino(validator)` - ValidatorsByConsAddr: `0x22 | ConsAddr -> OperatorAddr` -- ValidatorsByPower: `0x23 | BigEndian(Tokens) | OperatorAddr -> OperatorAddr` -- LastValidatorsPower: `0x11 OperatorAddr -> amino(Tokens) +- ValidatorsByPower: `0x23 | BigEndian(ConsensusPower) | OperatorAddr -> OperatorAddr` +- LastValidatorsPower: `0x11 OperatorAddr -> amino(ConsensusPower) `Validators` is the primary index - it ensures that each operator can have only one associated validator, where the public key of that validator can change in the @@ -66,8 +66,9 @@ map is needed to find the operator. Note that the `ConsAddr` corresponds to the address which can be derived from the validator's `ConsPubKey`. `ValidatorsByPower` is an additional index that provides a sorted list o -potential validators to quickly determine the current active set. Note -that all validators where `Jailed` is true are not stored within this index. +potential validators to quickly determine the current active set. Here +ConsensusPower is validator.Tokens/10^6. Note that all validators where +`Jailed` is true are not stored within this index. `LastValidatorsPower` is a special index that provides a historical list of the last-block's bonded validators. This index remains constant during a block but @@ -78,28 +79,28 @@ Each validator's state is stored in a `Validator` struct: ```golang type Validator struct { - OperatorAddr sdk.ValAddress // address of the validator's operator; bech encoded in JSON - ConsPubKey crypto.PubKey // Tendermint consensus pubkey of validator - Jailed bool // has the validator been jailed? - - Status sdk.BondStatus // validator status (bonded/unbonding/unbonded) - Tokens sdk.Int // delegated tokens (incl. self-delegation) - DelegatorShares sdk.Dec // total shares issued to a validator's delegators - - Description Description // description terms for the validator - - // Needed for ordering vals in the by-power key - UnbondingHeight int64 // if unbonding, height at which this validator has begun unbonding - UnbondingMinTime time.Time // if unbonding, min time for the validator to complete unbonding - - Commission Commission // info about the validator's commission + OperatorAddress sdk.ValAddress // address of the validator's operator; bech encoded in JSON + ConsPubKey crypto.PubKey // the consensus public key of the validator; bech encoded in JSON + Jailed bool // has the validator been jailed from bonded status? + Status sdk.BondStatus // validator status (bonded/unbonding/unbonded) + Tokens sdk.Int // delegated tokens (incl. self-delegation) + DelegatorShares sdk.Dec // total shares issued to a validator's delegators + Description Description // description terms for the validator + UnbondingHeight int64 // if unbonding, height at which this validator has begun unbonding + UnbondingCompletionTime time.Time // if unbonding, min time for the validator to complete unbonding + Commission Commission // commission parameters + MinSelfDelegation sdk.Int // validator's self declared minimum self delegation } type Commission struct { - Rate sdk.Dec // the commission rate charged to delegators - MaxRate sdk.Dec // maximum commission rate which this validator can ever charge - MaxChangeRate sdk.Dec // maximum daily increase of the validator commission - UpdateTime time.Time // the last time the commission rate was changed + CommissionRates + UpdateTime time.Time // the last time the commission rate was changed +} + +CommissionRates struct { + Rate sdk.Dec // the commission rate charged to delegators, as a fraction + MaxRate sdk.Dec // maximum commission rate which validator can ever charge, as a fraction + MaxChangeRate sdk.Dec // maximum daily increase of the validator commission, as a fraction } type Description struct { diff --git a/types/bytes.go b/types/bytes.go new file mode 100644 index 000000000000..600af9f04c39 --- /dev/null +++ b/types/bytes.go @@ -0,0 +1,11 @@ +package types + +// copy bytes +func CopyBytes(bz []byte) (ret []byte) { + if bz == nil { + return nil + } + ret = make([]byte, len(bz)) + copy(ret, bz) + return ret +} diff --git a/types/staking.go b/types/staking.go index ca4111a1d0ce..9f623b3575e6 100644 --- a/types/staking.go +++ b/types/staking.go @@ -10,24 +10,28 @@ const ( // default bond denomination DefaultBondDenom = "stake" - // Delay, in blocks, between when validator updates are returned to Tendermint and when they are applied. - // For example, if this is 0, the validator set at the end of a block will sign the next block, or - // if this is 1, the validator set at the end of a block will sign the block after the next. - // Constant as this should not change without a hard fork. - // TODO: Link to some Tendermint docs, this is very unobvious. + // 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 ) -// PowerReduction is the amount of staking tokens required for 1 unit of Tendermint power +// PowerReduction is the amount of staking tokens required for 1 unit of consensus-engine power var PowerReduction = NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(6), nil)) -// TokensToTendermintPower - convert input tokens to potential tendermint power -func TokensToTendermintPower(tokens Int) int64 { +// TokensToConsensusPower - convert input tokens to potential consensus-engine power +func TokensToConsensusPower(tokens Int) int64 { return (tokens.Quo(PowerReduction)).Int64() } -// TokensFromTendermintPower - convert input power to tokens -func TokensFromTendermintPower(power int64) Int { +// TokensFromConsensusPower - convert input power to tokens +func TokensFromConsensusPower(power int64) Int { return NewInt(power).Mul(PowerReduction) } diff --git a/x/distribution/keeper/delegation_test.go b/x/distribution/keeper/delegation_test.go index 989c84d2962e..a0ace76bc3a2 100644 --- a/x/distribution/keeper/delegation_test.go +++ b/x/distribution/keeper/delegation_test.go @@ -69,7 +69,7 @@ func TestCalculateRewardsAfterSlash(t *testing.T) { // create validator with 50% commission commission := staking.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) valPower := int64(100) - valTokens := sdk.TokensFromTendermintPower(valPower) + valTokens := sdk.TokensFromConsensusPower(valPower) msg := staking.NewMsgCreateValidator(valOpAddr1, valConsPk1, sdk.NewCoin(sdk.DefaultBondDenom, valTokens), staking.Description{}, commission, sdk.OneInt()) got := sh(ctx, msg) @@ -107,7 +107,7 @@ func TestCalculateRewardsAfterSlash(t *testing.T) { ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3) // allocate some rewards - initial := sdk.TokensFromTendermintPower(10) + initial := sdk.TokensFromConsensusPower(10) tokens := sdk.DecCoins{{sdk.DefaultBondDenom, initial.ToDec()}} k.AllocateTokensToValidator(ctx, val, tokens) @@ -131,7 +131,7 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) { // create validator with 50% commission power := int64(100) - valTokens := sdk.TokensFromTendermintPower(power) + valTokens := sdk.TokensFromConsensusPower(power) commission := staking.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) msg := staking.NewMsgCreateValidator(valOpAddr1, valConsPk1, sdk.NewCoin(sdk.DefaultBondDenom, valTokens), staking.Description{}, commission, sdk.OneInt()) @@ -169,7 +169,7 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) { ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3) // allocate some rewards - initial := sdk.TokensFromTendermintPower(10) + initial := sdk.TokensFromConsensusPower(10) tokens := sdk.DecCoins{{sdk.DefaultBondDenom, initial.ToDec()}} k.AllocateTokensToValidator(ctx, val, tokens) @@ -262,13 +262,13 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) { func TestWithdrawDelegationRewardsBasic(t *testing.T) { balancePower := int64(1000) - balanceTokens := sdk.TokensFromTendermintPower(balancePower) + balanceTokens := sdk.TokensFromConsensusPower(balancePower) ctx, ak, k, sk, _ := CreateTestInputDefault(t, false, balancePower) sh := staking.NewHandler(sk) // create validator with 50% commission power := int64(100) - valTokens := sdk.TokensFromTendermintPower(power) + valTokens := sdk.TokensFromConsensusPower(power) commission := staking.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) msg := staking.NewMsgCreateValidator( valOpAddr1, valConsPk1, @@ -294,7 +294,7 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) { val := sk.Validator(ctx, valOpAddr1) // allocate some rewards - initial := sdk.TokensFromTendermintPower(10) + initial := sdk.TokensFromConsensusPower(10) tokens := sdk.DecCoins{sdk.NewDecCoin(sdk.DefaultBondDenom, initial)} k.AllocateTokensToValidator(ctx, val, tokens) @@ -334,7 +334,7 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) { // create validator with 50% commission power := int64(100) - valTokens := sdk.TokensFromTendermintPower(power) + valTokens := sdk.TokensFromConsensusPower(power) commission := staking.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) msg := staking.NewMsgCreateValidator(valOpAddr1, valConsPk1, sdk.NewCoin(sdk.DefaultBondDenom, valTokens), staking.Description{}, commission, sdk.OneInt()) @@ -363,7 +363,7 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) { ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3) // allocate some rewards - initial := sdk.TokensFromTendermintPower(10).ToDec() + initial := sdk.TokensFromConsensusPower(10).ToDec() tokens := sdk.DecCoins{{sdk.DefaultBondDenom, initial}} k.AllocateTokensToValidator(ctx, val, tokens) @@ -402,7 +402,7 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { // create validator with 50% commission commission := staking.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) power := int64(100) - valTokens := sdk.TokensFromTendermintPower(power) + valTokens := sdk.TokensFromConsensusPower(power) msg := staking.NewMsgCreateValidator(valOpAddr1, valConsPk1, sdk.NewCoin(sdk.DefaultBondDenom, valTokens), staking.Description{}, commission, sdk.OneInt()) require.True(t, sh(ctx, msg).IsOK()) @@ -418,7 +418,7 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { del1 := sk.Delegation(ctx, sdk.AccAddress(valOpAddr1), valOpAddr1) // allocate some rewards - initial := sdk.TokensFromTendermintPower(30).ToDec() + initial := sdk.TokensFromConsensusPower(30).ToDec() tokens := sdk.DecCoins{{sdk.DefaultBondDenom, initial}} k.AllocateTokensToValidator(ctx, val, tokens) @@ -428,7 +428,7 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3) // second delegation - delTokens := sdk.TokensFromTendermintPower(100) + delTokens := sdk.TokensFromConsensusPower(100) msg2 := staking.NewMsgDelegate(sdk.AccAddress(valOpAddr2), valOpAddr1, sdk.NewCoin(sdk.DefaultBondDenom, delTokens)) require.True(t, sh(ctx, msg2).IsOK()) diff --git a/x/distribution/keeper/keeper_test.go b/x/distribution/keeper/keeper_test.go index 59471a13287a..925dd4988702 100644 --- a/x/distribution/keeper/keeper_test.go +++ b/x/distribution/keeper/keeper_test.go @@ -32,9 +32,9 @@ func TestWithdrawValidatorCommission(t *testing.T) { // check initial balance balance := ak.GetAccount(ctx, sdk.AccAddress(valOpAddr3)).GetCoins() - expTokens := sdk.TokensFromTendermintPower(1000) + expTokens := sdk.TokensFromConsensusPower(1000) require.Equal(t, sdk.Coins{ - sdk.NewCoin("stake", sdk.TokensFromTendermintPower(1000)), + sdk.NewCoin("stake", sdk.TokensFromConsensusPower(1000)), }, balance) // set outstanding rewards diff --git a/x/distribution/keeper/test_common.go b/x/distribution/keeper/test_common.go index ae49ca7d592f..80653ac7804e 100644 --- a/x/distribution/keeper/test_common.go +++ b/x/distribution/keeper/test_common.go @@ -87,7 +87,7 @@ func CreateTestInputAdvanced(t *testing.T, isCheckTx bool, initPower int64, communityTax sdk.Dec) (sdk.Context, auth.AccountKeeper, bank.Keeper, Keeper, staking.Keeper, DummyFeeCollectionKeeper, params.Keeper) { - initCoins := sdk.TokensFromTendermintPower(initPower) + initCoins := sdk.TokensFromConsensusPower(initPower) keyDistr := sdk.NewKVStoreKey(types.StoreKey) keyStaking := sdk.NewKVStoreKey(staking.StoreKey) diff --git a/x/genutil/gentx.go b/x/genutil/gentx.go index 31a8b53d69b1..d6a8cece47a9 100644 --- a/x/genutil/gentx.go +++ b/x/genutil/gentx.go @@ -15,7 +15,7 @@ import ( ) var ( - defaultTokens = sdk.TokensFromTendermintPower(100) + defaultTokens = sdk.TokensFromConsensusPower(100) defaultAmount = defaultTokens.String() + sdk.DefaultBondDenom defaultCommissionRate = "0.1" defaultCommissionMaxRate = "0.2" diff --git a/x/gov/endblocker_test.go b/x/gov/endblocker_test.go index 00898ac11474..24bbb939cc55 100644 --- a/x/gov/endblocker_test.go +++ b/x/gov/endblocker_test.go @@ -199,7 +199,7 @@ func TestTickPassedVotingPeriod(t *testing.T) { require.False(t, activeQueue.Valid()) activeQueue.Close() - proposalCoins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromTendermintPower(5))} + proposalCoins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(5))} newProposalMsg := NewMsgSubmitProposal(testProposal(), proposalCoins, input.addrs[0]) res := govHandler(ctx, newProposalMsg) @@ -264,7 +264,7 @@ func TestProposalPassedEndblocker(t *testing.T) { proposal, err := input.keeper.SubmitProposal(ctx, testProposal()) require.NoError(t, err) - proposalCoins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromTendermintPower(10))} + proposalCoins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(10))} newDepositMsg := NewMsgDeposit(input.addrs[0], proposal.ProposalID, proposalCoins) res := handler(ctx, newDepositMsg) require.True(t, res.IsOK()) @@ -306,7 +306,7 @@ func TestEndBlockerProposalHandlerFailed(t *testing.T) { proposal, err := input.keeper.SubmitProposal(ctx, testProposal()) require.NoError(t, err) - proposalCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromTendermintPower(10))) + proposalCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(10))) newDepositMsg := NewMsgDeposit(input.addrs[0], proposal.ProposalID, proposalCoins) res := handler(ctx, newDepositMsg) require.True(t, res.IsOK()) diff --git a/x/gov/genesis.go b/x/gov/genesis.go index 7818b795ba7e..ef9d1ae45af3 100644 --- a/x/gov/genesis.go +++ b/x/gov/genesis.go @@ -37,7 +37,7 @@ func NewGenesisState(startingProposalID uint64, dp DepositParams, vp VotingParam // get raw genesis raw message for testing func DefaultGenesisState() GenesisState { - minDepositTokens := sdk.TokensFromTendermintPower(10) + minDepositTokens := sdk.TokensFromConsensusPower(10) return GenesisState{ StartingProposalID: 1, DepositParams: DepositParams{ diff --git a/x/gov/keeper_test.go b/x/gov/keeper_test.go index 29ddfde90317..ecee9eecf451 100644 --- a/x/gov/keeper_test.go +++ b/x/gov/keeper_test.go @@ -95,13 +95,13 @@ func TestDeposits(t *testing.T) { require.NoError(t, err) proposalID := proposal.ProposalID - fourStake := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromTendermintPower(4))) - fiveStake := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromTendermintPower(5))) + fourStake := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(4))) + fiveStake := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(5))) addr0Initial := input.keeper.ck.GetCoins(ctx, input.addrs[0]) addr1Initial := input.keeper.ck.GetCoins(ctx, input.addrs[1]) - expTokens := sdk.TokensFromTendermintPower(42) + expTokens := sdk.TokensFromConsensusPower(42) require.Equal(t, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, expTokens)), addr0Initial) require.True(t, proposal.TotalDeposit.IsEqual(sdk.NewCoins())) diff --git a/x/gov/tally_test.go b/x/gov/tally_test.go index 05248e4d6169..8cc70301ee4b 100644 --- a/x/gov/tally_test.go +++ b/x/gov/tally_test.go @@ -370,7 +370,7 @@ func TestTallyDelgatorOverride(t *testing.T) { createValidators(t, stakingHandler, ctx, valAddrs, []int64{5, 6, 7}) staking.EndBlocker(ctx, input.sk) - delTokens := sdk.TokensFromTendermintPower(30) + delTokens := sdk.TokensFromConsensusPower(30) delegator1Msg := staking.NewMsgDelegate(input.addrs[3], sdk.ValAddress(input.addrs[2]), sdk.NewCoin(sdk.DefaultBondDenom, delTokens)) stakingHandler(ctx, delegator1Msg) @@ -416,7 +416,7 @@ func TestTallyDelgatorInherit(t *testing.T) { createValidators(t, stakingHandler, ctx, valAddrs, []int64{5, 6, 7}) staking.EndBlocker(ctx, input.sk) - delTokens := sdk.TokensFromTendermintPower(30) + delTokens := sdk.TokensFromConsensusPower(30) delegator1Msg := staking.NewMsgDelegate(input.addrs[3], sdk.ValAddress(input.addrs[2]), sdk.NewCoin(sdk.DefaultBondDenom, delTokens)) stakingHandler(ctx, delegator1Msg) @@ -460,7 +460,7 @@ func TestTallyDelgatorMultipleOverride(t *testing.T) { createValidators(t, stakingHandler, ctx, valAddrs, []int64{5, 6, 7}) staking.EndBlocker(ctx, input.sk) - delTokens := sdk.TokensFromTendermintPower(10) + delTokens := sdk.TokensFromConsensusPower(10) delegator1Msg := staking.NewMsgDelegate(input.addrs[3], sdk.ValAddress(input.addrs[2]), sdk.NewCoin(sdk.DefaultBondDenom, delTokens)) stakingHandler(ctx, delegator1Msg) delegator1Msg2 := staking.NewMsgDelegate(input.addrs[3], sdk.ValAddress(input.addrs[1]), sdk.NewCoin(sdk.DefaultBondDenom, delTokens)) @@ -500,25 +500,25 @@ func TestTallyDelgatorMultipleInherit(t *testing.T) { ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) stakingHandler := staking.NewHandler(input.sk) - valTokens1 := sdk.TokensFromTendermintPower(25) + valTokens1 := sdk.TokensFromConsensusPower(25) val1CreateMsg := staking.NewMsgCreateValidator( sdk.ValAddress(input.addrs[0]), ed25519.GenPrivKey().PubKey(), sdk.NewCoin(sdk.DefaultBondDenom, valTokens1), testDescription, testCommissionRates, sdk.OneInt(), ) stakingHandler(ctx, val1CreateMsg) - valTokens2 := sdk.TokensFromTendermintPower(6) + valTokens2 := sdk.TokensFromConsensusPower(6) val2CreateMsg := staking.NewMsgCreateValidator( sdk.ValAddress(input.addrs[1]), ed25519.GenPrivKey().PubKey(), sdk.NewCoin(sdk.DefaultBondDenom, valTokens2), testDescription, testCommissionRates, sdk.OneInt(), ) stakingHandler(ctx, val2CreateMsg) - valTokens3 := sdk.TokensFromTendermintPower(7) + valTokens3 := sdk.TokensFromConsensusPower(7) val3CreateMsg := staking.NewMsgCreateValidator( sdk.ValAddress(input.addrs[2]), ed25519.GenPrivKey().PubKey(), sdk.NewCoin(sdk.DefaultBondDenom, valTokens3), testDescription, testCommissionRates, sdk.OneInt(), ) stakingHandler(ctx, val3CreateMsg) - delTokens := sdk.TokensFromTendermintPower(10) + delTokens := sdk.TokensFromConsensusPower(10) delegator1Msg := staking.NewMsgDelegate(input.addrs[3], sdk.ValAddress(input.addrs[2]), sdk.NewCoin(sdk.DefaultBondDenom, delTokens)) stakingHandler(ctx, delegator1Msg) @@ -567,7 +567,7 @@ func TestTallyJailedValidator(t *testing.T) { createValidators(t, stakingHandler, ctx, valAddrs, []int64{25, 6, 7}) staking.EndBlocker(ctx, input.sk) - delTokens := sdk.TokensFromTendermintPower(10) + delTokens := sdk.TokensFromConsensusPower(10) delegator1Msg := staking.NewMsgDelegate(input.addrs[3], sdk.ValAddress(input.addrs[2]), sdk.NewCoin(sdk.DefaultBondDenom, delTokens)) stakingHandler(ctx, delegator1Msg) diff --git a/x/gov/test_common.go b/x/gov/test_common.go index 2b972cc3fdf6..ff66bdb626bd 100644 --- a/x/gov/test_common.go +++ b/x/gov/test_common.go @@ -58,7 +58,7 @@ func getMockApp(t *testing.T, numGenAccs int, genState GenesisState, genAccs []a require.NoError(t, mApp.CompleteSetup(keyStaking, tKeyStaking, keyGov)) - valTokens := sdk.TokensFromTendermintPower(42) + valTokens := sdk.TokensFromConsensusPower(42) var ( addrs []sdk.AccAddress @@ -94,7 +94,7 @@ func getInitChainer(mapp *mock.App, keeper Keeper, stakingKeeper staking.Keeper, mapp.InitChainer(ctx, req) stakingGenesis := staking.DefaultGenesisState() - tokens := sdk.TokensFromTendermintPower(100000) + tokens := sdk.TokensFromConsensusPower(100000) stakingGenesis.Pool.NotBondedTokens = tokens validators := staking.InitGenesis(ctx, stakingKeeper, accountKeeper, stakingGenesis) @@ -200,7 +200,7 @@ func createValidators(t *testing.T, stakingHandler sdk.Handler, ctx sdk.Context, for i := 0; i < len(addrs); i++ { - valTokens := sdk.TokensFromTendermintPower(powerAmt[i]) + valTokens := sdk.TokensFromConsensusPower(powerAmt[i]) valCreateMsg := staking.NewMsgCreateValidator( addrs[i], pubkeys[i], sdk.NewCoin(sdk.DefaultBondDenom, valTokens), testDescription, testCommissionRates, sdk.OneInt(), diff --git a/x/slashing/abci_app_test.go b/x/slashing/abci_app_test.go index cc0e5f13b4cb..51bf6feb2ce8 100644 --- a/x/slashing/abci_app_test.go +++ b/x/slashing/abci_app_test.go @@ -15,7 +15,7 @@ import ( func TestBeginBlocker(t *testing.T) { ctx, ck, sk, _, keeper := createTestInput(t, DefaultParams()) power := int64(100) - amt := sdk.TokensFromTendermintPower(power) + amt := sdk.TokensFromConsensusPower(power) addr, pk := addrs[2], pks[2] // bond the validator diff --git a/x/slashing/app_test.go b/x/slashing/app_test.go index 5fe20ad0cabc..c3745175c29b 100644 --- a/x/slashing/app_test.go +++ b/x/slashing/app_test.go @@ -62,7 +62,7 @@ func getInitChainer(mapp *mock.App, keeper staking.Keeper, accountKeeper types.A return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain { mapp.InitChainer(ctx, req) stakingGenesis := staking.DefaultGenesisState() - tokens := sdk.TokensFromTendermintPower(100000) + tokens := sdk.TokensFromConsensusPower(100000) stakingGenesis.Pool.NotBondedTokens = tokens validators := staking.InitGenesis(ctx, keeper, accountKeeper, stakingGenesis) return abci.ResponseInitChain{ @@ -90,8 +90,8 @@ func checkValidatorSigningInfo(t *testing.T, mapp *mock.App, keeper Keeper, func TestSlashingMsgs(t *testing.T) { mapp, stakingKeeper, keeper := getMockApp(t) - genTokens := sdk.TokensFromTendermintPower(42) - bondTokens := sdk.TokensFromTendermintPower(10) + genTokens := sdk.TokensFromConsensusPower(42) + bondTokens := sdk.TokensFromConsensusPower(10) genCoin := sdk.NewCoin(sdk.DefaultBondDenom, genTokens) bondCoin := sdk.NewCoin(sdk.DefaultBondDenom, bondTokens) diff --git a/x/slashing/handler_test.go b/x/slashing/handler_test.go index 95a559e8f4d1..dce7dd213627 100644 --- a/x/slashing/handler_test.go +++ b/x/slashing/handler_test.go @@ -15,7 +15,7 @@ func TestCannotUnjailUnlessJailed(t *testing.T) { // initial setup ctx, ck, sk, _, keeper := createTestInput(t, DefaultParams()) slh := NewHandler(keeper) - amt := sdk.TokensFromTendermintPower(100) + amt := sdk.TokensFromConsensusPower(100) addr, val := addrs[0], pks[0] msg := NewTestMsgCreateValidator(addr, val, amt) got := staking.NewHandler(sk)(ctx, msg) @@ -40,7 +40,7 @@ func TestCannotUnjailUnlessMeetMinSelfDelegation(t *testing.T) { ctx, ck, sk, _, keeper := createTestInput(t, DefaultParams()) slh := NewHandler(keeper) amtInt := int64(100) - addr, val, amt := addrs[0], pks[0], sdk.TokensFromTendermintPower(amtInt) + addr, val, amt := addrs[0], pks[0], sdk.TokensFromConsensusPower(amtInt) msg := NewTestMsgCreateValidator(addr, val, amt) msg.MinSelfDelegation = amt got := staking.NewHandler(sk)(ctx, msg) @@ -73,7 +73,7 @@ func TestJailedValidatorDelegations(t *testing.T) { stakingKeeper.SetParams(ctx, stakingParams) // create a validator - bondAmount := sdk.TokensFromTendermintPower(10) + bondAmount := sdk.TokensFromConsensusPower(10) valPubKey := pks[0] valAddr, consAddr := addrs[1], sdk.ConsAddress(addrs[0]) diff --git a/x/slashing/keeper_test.go b/x/slashing/keeper_test.go index 4d8654f38eaf..01f44b8eb39e 100644 --- a/x/slashing/keeper_test.go +++ b/x/slashing/keeper_test.go @@ -32,7 +32,7 @@ func TestHandleDoubleSign(t *testing.T) { // validator added pre-genesis ctx = ctx.WithBlockHeight(-1) power := int64(100) - amt := sdk.TokensFromTendermintPower(power) + amt := sdk.TokensFromConsensusPower(power) operatorAddr, val := addrs[0], pks[0] got := staking.NewHandler(sk)(ctx, NewTestMsgCreateValidator(operatorAddr, val, amt)) require.True(t, got.IsOK()) @@ -93,7 +93,7 @@ func TestPastMaxEvidenceAge(t *testing.T) { // validator added pre-genesis ctx = ctx.WithBlockHeight(-1) power := int64(100) - amt := sdk.TokensFromTendermintPower(power) + amt := sdk.TokensFromConsensusPower(power) operatorAddr, val := addrs[0], pks[0] got := staking.NewHandler(sk)(ctx, NewTestMsgCreateValidator(operatorAddr, val, amt)) require.True(t, got.IsOK()) @@ -109,7 +109,7 @@ func TestPastMaxEvidenceAge(t *testing.T) { ctx = ctx.WithBlockHeader(abci.Header{Time: time.Unix(1, 0).Add(keeper.MaxEvidenceAge(ctx))}) - oldPower := sk.Validator(ctx, operatorAddr).GetTendermintPower() + oldPower := sk.Validator(ctx, operatorAddr).GetConsensusPower() // double sign past max age keeper.HandleDoubleSign(ctx, val.Address(), 0, time.Unix(0, 0), power) @@ -118,7 +118,7 @@ func TestPastMaxEvidenceAge(t *testing.T) { require.True(t, sk.Validator(ctx, operatorAddr).IsBonded()) // should still have same power - require.Equal(t, oldPower, sk.Validator(ctx, operatorAddr).GetTendermintPower()) + require.Equal(t, oldPower, sk.Validator(ctx, operatorAddr).GetConsensusPower()) } // Test a validator through uptime, downtime, revocation, @@ -128,7 +128,7 @@ func TestHandleAbsentValidator(t *testing.T) { // initial setup ctx, ck, sk, _, keeper := createTestInput(t, keeperTestParams()) power := int64(100) - amt := sdk.TokensFromTendermintPower(power) + amt := sdk.TokensFromConsensusPower(power) addr, val := addrs[0], pks[0] sh := staking.NewHandler(sk) slh := NewHandler(keeper) @@ -279,7 +279,7 @@ func TestHandleNewValidator(t *testing.T) { // initial setup ctx, ck, sk, _, keeper := createTestInput(t, keeperTestParams()) addr, val := addrs[0], pks[0] - amt := sdk.TokensFromTendermintPower(100) + amt := sdk.TokensFromConsensusPower(100) sh := staking.NewHandler(sk) // 1000 first blocks not a validator @@ -312,7 +312,7 @@ func TestHandleNewValidator(t *testing.T) { validator, _ := sk.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(val)) require.Equal(t, sdk.Bonded, validator.GetStatus()) pool := sk.GetPool(ctx) - expTokens := sdk.TokensFromTendermintPower(100) + expTokens := sdk.TokensFromConsensusPower(100) require.Equal(t, expTokens, pool.BondedTokens) } @@ -323,7 +323,7 @@ func TestHandleAlreadyJailed(t *testing.T) { // initial setup ctx, _, sk, _, keeper := createTestInput(t, DefaultParams()) power := int64(100) - amt := sdk.TokensFromTendermintPower(power) + amt := sdk.TokensFromConsensusPower(power) addr, val := addrs[0], pks[0] sh := staking.NewHandler(sk) got := sh(ctx, NewTestMsgCreateValidator(addr, val, amt)) @@ -351,7 +351,7 @@ func TestHandleAlreadyJailed(t *testing.T) { require.Equal(t, sdk.Unbonding, validator.GetStatus()) // validator should have been slashed - resultingTokens := amt.Sub(sdk.TokensFromTendermintPower(1)) + resultingTokens := amt.Sub(sdk.TokensFromConsensusPower(1)) require.Equal(t, resultingTokens, validator.GetTokens()) // another block missed @@ -376,7 +376,7 @@ func TestValidatorDippingInAndOut(t *testing.T) { params.MaxValidators = 1 sk.SetParams(ctx, params) power := int64(100) - amt := sdk.TokensFromTendermintPower(power) + amt := sdk.TokensFromConsensusPower(power) addr, val := addrs[0], pks[0] consAddr := sdk.ConsAddress(addr) sh := staking.NewHandler(sk) @@ -391,8 +391,8 @@ func TestValidatorDippingInAndOut(t *testing.T) { keeper.HandleValidatorSignature(ctx, val.Address(), power, true) } - // validator kicked out of validator set - newAmt := sdk.TokensFromTendermintPower(101) + // kick first validator out of validator set + newAmt := sdk.TokensFromConsensusPower(101) got = sh(ctx, NewTestMsgCreateValidator(addrs[1], pks[1], newAmt)) require.True(t, got.IsOK()) validatorUpdates, _ := staking.EndBlocker(ctx, sk) @@ -405,14 +405,14 @@ func TestValidatorDippingInAndOut(t *testing.T) { ctx = ctx.WithBlockHeight(height) // validator added back in - delTokens := sdk.TokensFromTendermintPower(3) + delTokens := sdk.TokensFromConsensusPower(50) got = sh(ctx, newTestMsgDelegate(sdk.AccAddress(addrs[2]), addrs[0], delTokens)) require.True(t, got.IsOK()) validatorUpdates, _ = staking.EndBlocker(ctx, sk) require.Equal(t, 2, len(validatorUpdates)) validator, _ = sk.GetValidator(ctx, addr) require.Equal(t, sdk.Bonded, validator.Status) - newPower := int64(103) + newPower := int64(150) // validator misses a block keeper.HandleValidatorSignature(ctx, val.Address(), newPower, false) diff --git a/x/slashing/test_common.go b/x/slashing/test_common.go index d6eda9a0bb1f..25e158df466c 100644 --- a/x/slashing/test_common.go +++ b/x/slashing/test_common.go @@ -35,7 +35,7 @@ var ( sdk.ValAddress(pks[1].Address()), sdk.ValAddress(pks[2].Address()), } - initCoins = sdk.TokensFromTendermintPower(200) + initCoins = sdk.TokensFromConsensusPower(200) ) func createTestCodec() *codec.Codec { diff --git a/x/slashing/types/keys.go b/x/slashing/types/keys.go index 65e52a12786a..522101a06e0e 100644 --- a/x/slashing/types/keys.go +++ b/x/slashing/types/keys.go @@ -35,7 +35,7 @@ var ( AddrPubkeyRelationKey = []byte{0x04} // Prefix for address-pubkey relation ) -// stored by *Tendermint* address (not operator address) +// stored by *Consensus* address (not operator address) func GetValidatorSigningInfoKey(v sdk.ConsAddress) []byte { return append(ValidatorSigningInfoKey, v.Bytes()...) } @@ -49,24 +49,24 @@ func GetValidatorSigningInfoAddress(key []byte) (v sdk.ConsAddress) { return sdk.ConsAddress(addr) } -// stored by *Tendermint* address (not operator address) +// stored by *Consensus* address (not operator address) func GetValidatorMissedBlockBitArrayPrefixKey(v sdk.ConsAddress) []byte { return append(ValidatorMissedBlockBitArrayKey, v.Bytes()...) } -// stored by *Tendermint* address (not operator address) +// stored by *Consensus* address (not operator address) func GetValidatorMissedBlockBitArrayKey(v sdk.ConsAddress, i int64) []byte { b := make([]byte, 8) binary.LittleEndian.PutUint64(b, uint64(i)) return append(GetValidatorMissedBlockBitArrayPrefixKey(v), b...) } -// stored by *Tendermint* address (not operator address) +// stored by *Consensus* address (not operator address) func GetValidatorSlashingPeriodPrefix(v sdk.ConsAddress) []byte { return append(ValidatorSlashingPeriodKey, v.Bytes()...) } -// stored by *Tendermint* address (not operator address) followed by start height +// stored by *Consensus* address (not operator address) followed by start height func GetValidatorSlashingPeriodKey(v sdk.ConsAddress, startHeight int64) []byte { b := make([]byte, 8) // this needs to be height + ValidatorUpdateDelay because the slashing period for genesis validators starts at height -ValidatorUpdateDelay diff --git a/x/staking/app_test.go b/x/staking/app_test.go index b9855eea880c..41fb081c16d8 100644 --- a/x/staking/app_test.go +++ b/x/staking/app_test.go @@ -52,7 +52,7 @@ func getInitChainer(mapp *mock.App, keeper Keeper, accountKeeper types.AccountKe mapp.InitChainer(ctx, req) stakingGenesis := DefaultGenesisState() - tokens := sdk.TokensFromTendermintPower(100000) + tokens := sdk.TokensFromConsensusPower(100000) stakingGenesis.Pool.NotBondedTokens = tokens validators := InitGenesis(ctx, keeper, accountKeeper, stakingGenesis) @@ -94,8 +94,8 @@ func checkDelegation( func TestStakingMsgs(t *testing.T) { mApp, keeper := getMockApp(t) - genTokens := sdk.TokensFromTendermintPower(42) - bondTokens := sdk.TokensFromTendermintPower(10) + genTokens := sdk.TokensFromConsensusPower(42) + bondTokens := sdk.TokensFromConsensusPower(10) 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 665b6428f0bd..3f39e481d969 100644 --- a/x/staking/client/cli/tx.go +++ b/x/staking/client/cli/tx.go @@ -258,7 +258,7 @@ $ %s tx staking unbond cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj 100s //__________________________________________________________ var ( - defaultTokens = sdk.TokensFromTendermintPower(100) + defaultTokens = sdk.TokensFromConsensusPower(100) defaultAmount = defaultTokens.String() + sdk.DefaultBondDenom defaultCommissionRate = "0.1" defaultCommissionMaxRate = "0.2" diff --git a/x/staking/exported/exported.go b/x/staking/exported/exported.go index 496fda5a5618..cf2417cbefca 100644 --- a/x/staking/exported/exported.go +++ b/x/staking/exported/exported.go @@ -25,7 +25,7 @@ type ValidatorI interface { GetConsAddr() sdk.ConsAddress // validation consensus address GetTokens() sdk.Int // validation tokens GetBondedTokens() sdk.Int // validator bonded tokens - GetTendermintPower() int64 // validation power in tendermint + GetConsensusPower() int64 // validation power in tendermint GetCommission() sdk.Dec // validator commission rate GetMinSelfDelegation() sdk.Int // validator minimum self delegation GetDelegatorShares() sdk.Dec // total outstanding delegator shares diff --git a/x/staking/genesis.go b/x/staking/genesis.go index 86985ff1341d..e96c1d655f00 100644 --- a/x/staking/genesis.go +++ b/x/staking/genesis.go @@ -147,7 +147,7 @@ func WriteValidators(ctx sdk.Context, keeper Keeper) (vals []tmtypes.GenesisVali keeper.IterateLastValidators(ctx, func(_ int64, validator exported.ValidatorI) (stop bool) { vals = append(vals, tmtypes.GenesisValidator{ PubKey: validator.GetConsPubKey(), - Power: validator.GetTendermintPower(), + Power: validator.GetConsensusPower(), Name: validator.GetMoniker(), }) diff --git a/x/staking/genesis_test.go b/x/staking/genesis_test.go index 9b61d069d558..52cadae2ba5a 100644 --- a/x/staking/genesis_test.go +++ b/x/staking/genesis_test.go @@ -20,8 +20,8 @@ func TestInitGenesis(t *testing.T) { ctx, accKeeper, keeper := keep.CreateTestInput(t, false, 1000) pool := keeper.GetPool(ctx) - pool.BondedTokens = sdk.TokensFromTendermintPower(2) - valTokens := sdk.TokensFromTendermintPower(1) + pool.BondedTokens = sdk.TokensFromConsensusPower(2) + valTokens := sdk.TokensFromConsensusPower(1) params := keeper.GetParams(ctx) validators := make([]Validator, 2) @@ -75,7 +75,7 @@ func TestInitGenesisLargeValidatorSet(t *testing.T) { // Assigning 2 to the first 100 vals, 1 to the rest pool := keeper.GetPool(ctx) - bondedTokens := sdk.TokensFromTendermintPower(int64(200 + (size - 100))) + bondedTokens := sdk.TokensFromConsensusPower(int64(200 + (size - 100))) pool.BondedTokens = bondedTokens params := keeper.GetParams(ctx) @@ -88,9 +88,9 @@ func TestInitGenesisLargeValidatorSet(t *testing.T) { validators[i].Status = sdk.Bonded - tokens := sdk.TokensFromTendermintPower(1) + tokens := sdk.TokensFromConsensusPower(1) if i < 100 { - tokens = sdk.TokensFromTendermintPower(2) + tokens = sdk.TokensFromConsensusPower(2) } validators[i].Tokens = tokens validators[i].DelegatorShares = tokens.ToDec() diff --git a/x/staking/handler_test.go b/x/staking/handler_test.go index 29f4961c411a..e5e3acfd4b96 100644 --- a/x/staking/handler_test.go +++ b/x/staking/handler_test.go @@ -33,7 +33,7 @@ func TestValidatorByPowerIndex(t *testing.T) { validatorAddr, validatorAddr3 := sdk.ValAddress(keep.Addrs[0]), sdk.ValAddress(keep.Addrs[1]) initPower := int64(1000000) - initBond := sdk.TokensFromTendermintPower(initPower) + initBond := sdk.TokensFromConsensusPower(initPower) ctx, _, keeper := keep.CreateTestInput(t, false, initPower) _ = setInstantUnbondPeriod(keeper, ctx) @@ -119,7 +119,7 @@ func TestDuplicatesMsgCreateValidator(t *testing.T) { addr1, addr2 := sdk.ValAddress(keep.Addrs[0]), sdk.ValAddress(keep.Addrs[1]) pk1, pk2 := keep.PKs[0], keep.PKs[1] - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) msgCreateValidator1 := NewTestMsgCreateValidator(addr1, pk1, valTokens) got := handleMsgCreateValidator(ctx, msgCreateValidator1, keeper) require.True(t, got.IsOK(), "%v", got) @@ -188,7 +188,7 @@ func TestLegacyValidatorDelegations(t *testing.T) { ctx, _, keeper := keep.CreateTestInput(t, false, int64(1000)) setInstantUnbondPeriod(keeper, ctx) - bondAmount := sdk.TokensFromTendermintPower(10) + bondAmount := sdk.TokensFromConsensusPower(10) valAddr := sdk.ValAddress(keep.Addrs[0]) valConsPubKey, valConsAddr := keep.PKs[0], sdk.ConsAddress(keep.PKs[0].Address()) delAddr := keep.Addrs[1] @@ -278,11 +278,11 @@ func TestLegacyValidatorDelegations(t *testing.T) { func TestIncrementsMsgDelegate(t *testing.T) { initPower := int64(1000) - initBond := sdk.TokensFromTendermintPower(initPower) + initBond := sdk.TokensFromConsensusPower(initPower) ctx, accMapper, keeper := keep.CreateTestInput(t, false, initPower) params := keeper.GetParams(ctx) - bondAmount := sdk.TokensFromTendermintPower(10) + bondAmount := sdk.TokensFromConsensusPower(10) validatorAddr, delegatorAddr := sdk.ValAddress(keep.Addrs[0]), keep.Addrs[1] // first create validator @@ -348,7 +348,7 @@ func TestEditValidatorDecreaseMinSelfDelegation(t *testing.T) { validatorAddr := sdk.ValAddress(keep.Addrs[0]) initPower := int64(100) - initBond := sdk.TokensFromTendermintPower(100) + initBond := sdk.TokensFromConsensusPower(100) ctx, _, keeper := keep.CreateTestInput(t, false, initPower) _ = setInstantUnbondPeriod(keeper, ctx) @@ -380,7 +380,7 @@ func TestEditValidatorIncreaseMinSelfDelegationBeyondCurrentBond(t *testing.T) { validatorAddr := sdk.ValAddress(keep.Addrs[0]) initPower := int64(100) - initBond := sdk.TokensFromTendermintPower(100) + initBond := sdk.TokensFromConsensusPower(100) ctx, _, keeper := keep.CreateTestInput(t, false, initPower) _ = setInstantUnbondPeriod(keeper, ctx) @@ -410,7 +410,7 @@ func TestEditValidatorIncreaseMinSelfDelegationBeyondCurrentBond(t *testing.T) { func TestIncrementsMsgUnbond(t *testing.T) { initPower := int64(1000) - initBond := sdk.TokensFromTendermintPower(initPower) + initBond := sdk.TokensFromConsensusPower(initPower) ctx, accMapper, keeper := keep.CreateTestInput(t, false, initPower) params := setInstantUnbondPeriod(keeper, ctx) denom := params.BondDenom @@ -484,8 +484,8 @@ func TestIncrementsMsgUnbond(t *testing.T) { errorCases := []sdk.Int{ //1<<64 - 1, // more than int64 power //1<<63 + 1, // more than int64 power - sdk.TokensFromTendermintPower(1<<63 - 1), - sdk.TokensFromTendermintPower(1 << 31), + sdk.TokensFromConsensusPower(1<<63 - 1), + sdk.TokensFromConsensusPower(1 << 31), initBond, } @@ -508,7 +508,7 @@ func TestIncrementsMsgUnbond(t *testing.T) { func TestMultipleMsgCreateValidator(t *testing.T) { initPower := int64(1000) - initTokens := sdk.TokensFromTendermintPower(initPower) + initTokens := sdk.TokensFromConsensusPower(initPower) ctx, accMapper, keeper := keep.CreateTestInput(t, false, initPower) params := setInstantUnbondPeriod(keeper, ctx) @@ -525,7 +525,7 @@ func TestMultipleMsgCreateValidator(t *testing.T) { // bond them all for i, validatorAddr := range validatorAddrs { - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) msgCreateValidatorOnBehalfOf := NewTestMsgCreateValidator(validatorAddr, keep.PKs[i], valTokens) got := handleMsgCreateValidator(ctx, msgCreateValidatorOnBehalfOf, keeper) @@ -549,7 +549,7 @@ func TestMultipleMsgCreateValidator(t *testing.T) { _, found := keeper.GetValidator(ctx, validatorAddr) require.True(t, found) - unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromTendermintPower(10)) + unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(10)) msgUndelegate := NewMsgUndelegate(delegatorAddrs[i], validatorAddr, unbondAmt) // remove delegation got := handleMsgUndelegate(ctx, msgUndelegate, keeper) @@ -673,13 +673,13 @@ func TestValidatorQueue(t *testing.T) { keeper.SetParams(ctx, params) // create the validator - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], valTokens) got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper) require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator") // bond a delegator - delTokens := sdk.TokensFromTendermintPower(10) + delTokens := sdk.TokensFromConsensusPower(10) msgDelegate := NewTestMsgDelegate(delegatorAddr, validatorAddr, delTokens) got = handleMsgDelegate(ctx, msgDelegate, keeper) require.True(t, got.IsOK(), "expected ok, got %v", got) @@ -731,7 +731,7 @@ func TestUnbondingPeriod(t *testing.T) { keeper.SetParams(ctx, params) // create the validator - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], valTokens) got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper) require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator") @@ -739,7 +739,7 @@ func TestUnbondingPeriod(t *testing.T) { EndBlocker(ctx, keeper) // begin unbonding - unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromTendermintPower(10)) + unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(10)) msgUndelegate := NewMsgUndelegate(sdk.AccAddress(validatorAddr), validatorAddr, unbondAmt) got = handleMsgUndelegate(ctx, msgUndelegate, keeper) require.True(t, got.IsOK(), "expected no error") @@ -921,7 +921,7 @@ func TestMultipleRedelegationAtSameTime(t *testing.T) { keeper.SetParams(ctx, params) // create the validators - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) msgCreateValidator := NewTestMsgCreateValidator(valAddr, keep.PKs[0], valTokens) got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper) require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator") @@ -973,7 +973,7 @@ func TestMultipleRedelegationAtUniqueTimes(t *testing.T) { keeper.SetParams(ctx, params) // create the validators - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) msgCreateValidator := NewTestMsgCreateValidator(valAddr, keep.PKs[0], valTokens) got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper) require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator") @@ -1026,7 +1026,7 @@ func TestMultipleUnbondingDelegationAtSameTime(t *testing.T) { keeper.SetParams(ctx, params) // create the validator - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) msgCreateValidator := NewTestMsgCreateValidator(valAddr, keep.PKs[0], valTokens) got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper) require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator") @@ -1073,7 +1073,7 @@ func TestMultipleUnbondingDelegationAtUniqueTimes(t *testing.T) { keeper.SetParams(ctx, params) // create the validator - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) msgCreateValidator := NewTestMsgCreateValidator(valAddr, keep.PKs[0], valTokens) got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper) require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator") @@ -1130,7 +1130,7 @@ func TestUnbondingWhenExcessValidators(t *testing.T) { keeper.SetParams(ctx, params) // add three validators - valTokens1 := sdk.TokensFromTendermintPower(50) + valTokens1 := sdk.TokensFromConsensusPower(50) msgCreateValidator := NewTestMsgCreateValidator(validatorAddr1, keep.PKs[0], valTokens1) got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper) require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator") @@ -1138,7 +1138,7 @@ func TestUnbondingWhenExcessValidators(t *testing.T) { keeper.ApplyAndReturnValidatorSetUpdates(ctx) require.Equal(t, 1, len(keeper.GetLastValidators(ctx))) - valTokens2 := sdk.TokensFromTendermintPower(30) + valTokens2 := sdk.TokensFromConsensusPower(30) msgCreateValidator = NewTestMsgCreateValidator(validatorAddr2, keep.PKs[1], valTokens2) got = handleMsgCreateValidator(ctx, msgCreateValidator, keeper) require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator") @@ -1146,7 +1146,7 @@ func TestUnbondingWhenExcessValidators(t *testing.T) { keeper.ApplyAndReturnValidatorSetUpdates(ctx) require.Equal(t, 2, len(keeper.GetLastValidators(ctx))) - valTokens3 := sdk.TokensFromTendermintPower(10) + valTokens3 := sdk.TokensFromConsensusPower(10) msgCreateValidator = NewTestMsgCreateValidator(validatorAddr3, keep.PKs[2], valTokens3) got = handleMsgCreateValidator(ctx, msgCreateValidator, keeper) require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator") @@ -1178,7 +1178,7 @@ func TestBondUnbondRedelegateSlashTwice(t *testing.T) { valA, valB, del := sdk.ValAddress(keep.Addrs[0]), sdk.ValAddress(keep.Addrs[1]), keep.Addrs[2] consAddr0 := sdk.ConsAddress(keep.PKs[0].Address()) - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) msgCreateValidator := NewTestMsgCreateValidator(valA, keep.PKs[0], valTokens) got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper) require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator") @@ -1200,13 +1200,13 @@ func TestBondUnbondRedelegateSlashTwice(t *testing.T) { ctx = ctx.WithBlockHeight(1) // begin unbonding 4 stake - unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromTendermintPower(4)) + unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(4)) msgUndelegate := NewMsgUndelegate(del, valA, unbondAmt) got = handleMsgUndelegate(ctx, msgUndelegate, keeper) require.True(t, got.IsOK(), "expected no error on runMsgUndelegate") // begin redelegate 6 stake - redAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromTendermintPower(6)) + redAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(6)) msgBeginRedelegate := NewMsgBeginRedelegate(del, valA, valB, redAmt) got = handleMsgBeginRedelegate(ctx, msgBeginRedelegate, keeper) require.True(t, got.IsOK(), "expected no error on runMsgBeginRedelegate") diff --git a/x/staking/keeper/delegation_test.go b/x/staking/keeper/delegation_test.go index 0ab38591b552..9397aa41cc25 100644 --- a/x/staking/keeper/delegation_test.go +++ b/x/staking/keeper/delegation_test.go @@ -171,7 +171,7 @@ func TestUnbondingDelegation(t *testing.T) { func TestUnbondDelegation(t *testing.T) { ctx, _, keeper := CreateTestInput(t, false, 0) pool := keeper.GetPool(ctx) - startTokens := sdk.TokensFromTendermintPower(10) + startTokens := sdk.TokensFromConsensusPower(10) pool.NotBondedTokens = startTokens //create a validator and a delegator to that validator @@ -188,7 +188,7 @@ func TestUnbondDelegation(t *testing.T) { delegation := types.NewDelegation(addrDels[0], addrVals[0], issuedShares) keeper.SetDelegation(ctx, delegation) - bondTokens := sdk.TokensFromTendermintPower(6) + bondTokens := sdk.TokensFromConsensusPower(6) amount, err := keeper.unbond(ctx, addrDels[0], addrVals[0], bondTokens.ToDec()) require.NoError(t, err) require.Equal(t, bondTokens, amount) // shares to be added to an unbonding delegation @@ -209,7 +209,7 @@ func TestUnbondDelegation(t *testing.T) { func TestUnbondingDelegationsMaxEntries(t *testing.T) { ctx, _, keeper := CreateTestInput(t, false, 1) pool := keeper.GetPool(ctx) - startTokens := sdk.TokensFromTendermintPower(10) + startTokens := sdk.TokensFromConsensusPower(10) pool.NotBondedTokens = startTokens // create a validator and a delegator to that validator @@ -256,13 +256,13 @@ func TestUndelegateSelfDelegationBelowMinSelfDelegation(t *testing.T) { ctx, _, keeper := CreateTestInput(t, false, 0) pool := keeper.GetPool(ctx) - startTokens := sdk.TokensFromTendermintPower(20) + startTokens := sdk.TokensFromConsensusPower(20) pool.NotBondedTokens = startTokens //create a validator with a self-delegation validator := types.NewValidator(addrVals[0], PKs[0], types.Description{}) - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) validator.MinSelfDelegation = valTokens validator, pool, issuedShares := validator.AddTokensFromDel(pool, valTokens) require.Equal(t, valTokens, issuedShares.RoundInt()) @@ -275,7 +275,7 @@ func TestUndelegateSelfDelegationBelowMinSelfDelegation(t *testing.T) { // create a second delegation to this validator keeper.DeleteValidatorByPowerIndex(ctx, validator) - delTokens := sdk.TokensFromTendermintPower(10) + delTokens := sdk.TokensFromConsensusPower(10) validator, pool, issuedShares = validator.AddTokensFromDel(pool, delTokens) require.Equal(t, delTokens, issuedShares.RoundInt()) keeper.SetPool(ctx, pool) @@ -285,7 +285,7 @@ func TestUndelegateSelfDelegationBelowMinSelfDelegation(t *testing.T) { keeper.SetDelegation(ctx, delegation) val0AccAddr := sdk.AccAddress(addrVals[0].Bytes()) - _, err := keeper.Undelegate(ctx, val0AccAddr, addrVals[0], sdk.TokensFromTendermintPower(6).ToDec()) + _, err := keeper.Undelegate(ctx, val0AccAddr, addrVals[0], sdk.TokensFromConsensusPower(6).ToDec()) require.NoError(t, err) // end block @@ -294,7 +294,7 @@ func TestUndelegateSelfDelegationBelowMinSelfDelegation(t *testing.T) { validator, found := keeper.GetValidator(ctx, addrVals[0]) require.True(t, found) - require.Equal(t, sdk.TokensFromTendermintPower(14), validator.Tokens) + require.Equal(t, sdk.TokensFromConsensusPower(14), validator.Tokens) require.Equal(t, sdk.Unbonding, validator.Status) require.True(t, validator.Jailed) } @@ -302,13 +302,13 @@ func TestUndelegateSelfDelegationBelowMinSelfDelegation(t *testing.T) { func TestUndelegateFromUnbondingValidator(t *testing.T) { ctx, _, keeper := CreateTestInput(t, false, 0) pool := keeper.GetPool(ctx) - startTokens := sdk.TokensFromTendermintPower(20) + startTokens := sdk.TokensFromConsensusPower(20) pool.NotBondedTokens = startTokens //create a validator with a self-delegation validator := types.NewValidator(addrVals[0], PKs[0], types.Description{}) - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) validator, pool, issuedShares := validator.AddTokensFromDel(pool, valTokens) require.Equal(t, valTokens, issuedShares.RoundInt()) keeper.SetPool(ctx, pool) @@ -319,7 +319,7 @@ func TestUndelegateFromUnbondingValidator(t *testing.T) { // create a second delegation to this validator keeper.DeleteValidatorByPowerIndex(ctx, validator) - delTokens := sdk.TokensFromTendermintPower(10) + delTokens := sdk.TokensFromConsensusPower(10) validator, pool, issuedShares = validator.AddTokensFromDel(pool, delTokens) require.Equal(t, delTokens, issuedShares.RoundInt()) keeper.SetPool(ctx, pool) @@ -371,13 +371,13 @@ func TestUndelegateFromUnbondingValidator(t *testing.T) { func TestUndelegateFromUnbondedValidator(t *testing.T) { ctx, _, keeper := CreateTestInput(t, false, 1) pool := keeper.GetPool(ctx) - startTokens := sdk.TokensFromTendermintPower(20) + startTokens := sdk.TokensFromConsensusPower(20) pool.NotBondedTokens = startTokens // create a validator with a self-delegation validator := types.NewValidator(addrVals[0], PKs[0], types.Description{}) - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) validator, pool, issuedShares := validator.AddTokensFromDel(pool, valTokens) require.Equal(t, valTokens, issuedShares.RoundInt()) keeper.SetPool(ctx, pool) @@ -389,7 +389,7 @@ func TestUndelegateFromUnbondedValidator(t *testing.T) { // create a second delegation to this validator keeper.DeleteValidatorByPowerIndex(ctx, validator) - delTokens := sdk.TokensFromTendermintPower(10) + delTokens := sdk.TokensFromConsensusPower(10) validator, pool, issuedShares = validator.AddTokensFromDel(pool, delTokens) require.Equal(t, delTokens, issuedShares.RoundInt()) keeper.SetPool(ctx, pool) @@ -425,7 +425,7 @@ func TestUndelegateFromUnbondedValidator(t *testing.T) { require.Equal(t, validator.Status, sdk.Unbonded) // unbond some of the other delegation's shares - unbondTokens := sdk.TokensFromTendermintPower(6) + unbondTokens := sdk.TokensFromConsensusPower(6) _, err = keeper.Undelegate(ctx, addrDels[0], addrVals[0], unbondTokens.ToDec()) require.NoError(t, err) @@ -442,13 +442,13 @@ func TestUndelegateFromUnbondedValidator(t *testing.T) { func TestUnbondingAllDelegationFromValidator(t *testing.T) { ctx, _, keeper := CreateTestInput(t, false, 0) pool := keeper.GetPool(ctx) - startTokens := sdk.TokensFromTendermintPower(20) + startTokens := sdk.TokensFromConsensusPower(20) pool.NotBondedTokens = startTokens //create a validator with a self-delegation validator := types.NewValidator(addrVals[0], PKs[0], types.Description{}) - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) validator, pool, issuedShares := validator.AddTokensFromDel(pool, valTokens) require.Equal(t, valTokens, issuedShares.RoundInt()) keeper.SetPool(ctx, pool) @@ -460,7 +460,7 @@ func TestUnbondingAllDelegationFromValidator(t *testing.T) { // create a second delegation to this validator keeper.DeleteValidatorByPowerIndex(ctx, validator) - delTokens := sdk.TokensFromTendermintPower(10) + delTokens := sdk.TokensFromConsensusPower(10) validator, pool, issuedShares = validator.AddTokensFromDel(pool, delTokens) require.Equal(t, delTokens, issuedShares.RoundInt()) keeper.SetPool(ctx, pool) @@ -586,12 +586,12 @@ func TestRedelegation(t *testing.T) { func TestRedelegateToSameValidator(t *testing.T) { ctx, _, keeper := CreateTestInput(t, false, 0) pool := keeper.GetPool(ctx) - startTokens := sdk.TokensFromTendermintPower(30) + startTokens := sdk.TokensFromConsensusPower(30) pool.NotBondedTokens = startTokens // create a validator with a self-delegation validator := types.NewValidator(addrVals[0], PKs[0], types.Description{}) - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) validator, pool, issuedShares := validator.AddTokensFromDel(pool, valTokens) require.Equal(t, valTokens, issuedShares.RoundInt()) keeper.SetPool(ctx, pool) @@ -609,12 +609,12 @@ func TestRedelegateToSameValidator(t *testing.T) { func TestRedelegationMaxEntries(t *testing.T) { ctx, _, keeper := CreateTestInput(t, false, 0) pool := keeper.GetPool(ctx) - startTokens := sdk.TokensFromTendermintPower(20) + startTokens := sdk.TokensFromConsensusPower(20) pool.NotBondedTokens = startTokens // create a validator with a self-delegation validator := types.NewValidator(addrVals[0], PKs[0], types.Description{}) - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) validator, pool, issuedShares := validator.AddTokensFromDel(pool, valTokens) require.Equal(t, valTokens, issuedShares.RoundInt()) keeper.SetPool(ctx, pool) @@ -660,12 +660,12 @@ func TestRedelegationMaxEntries(t *testing.T) { func TestRedelegateSelfDelegation(t *testing.T) { ctx, _, keeper := CreateTestInput(t, false, 0) pool := keeper.GetPool(ctx) - startTokens := sdk.TokensFromTendermintPower(30) + startTokens := sdk.TokensFromConsensusPower(30) pool.NotBondedTokens = startTokens //create a validator with a self-delegation validator := types.NewValidator(addrVals[0], PKs[0], types.Description{}) - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) validator, pool, issuedShares := validator.AddTokensFromDel(pool, valTokens) require.Equal(t, valTokens, issuedShares.RoundInt()) keeper.SetPool(ctx, pool) @@ -685,7 +685,7 @@ func TestRedelegateSelfDelegation(t *testing.T) { require.Equal(t, sdk.Bonded, validator2.Status) // create a second delegation to validator 1 - delTokens := sdk.TokensFromTendermintPower(10) + delTokens := sdk.TokensFromConsensusPower(10) validator, pool, issuedShares = validator.AddTokensFromDel(pool, delTokens) require.Equal(t, delTokens, issuedShares.RoundInt()) keeper.SetPool(ctx, pool) @@ -711,13 +711,13 @@ func TestRedelegateSelfDelegation(t *testing.T) { func TestRedelegateFromUnbondingValidator(t *testing.T) { ctx, _, keeper := CreateTestInput(t, false, 0) pool := keeper.GetPool(ctx) - startTokens := sdk.TokensFromTendermintPower(30) + startTokens := sdk.TokensFromConsensusPower(30) pool.NotBondedTokens = startTokens //create a validator with a self-delegation validator := types.NewValidator(addrVals[0], PKs[0], types.Description{}) - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) validator, pool, issuedShares := validator.AddTokensFromDel(pool, valTokens) require.Equal(t, valTokens, issuedShares.RoundInt()) keeper.SetPool(ctx, pool) @@ -729,7 +729,7 @@ func TestRedelegateFromUnbondingValidator(t *testing.T) { // create a second delegation to this validator keeper.DeleteValidatorByPowerIndex(ctx, validator) - delTokens := sdk.TokensFromTendermintPower(10) + delTokens := sdk.TokensFromConsensusPower(10) validator, pool, issuedShares = validator.AddTokensFromDel(pool, delTokens) require.Equal(t, delTokens, issuedShares.RoundInt()) keeper.SetPool(ctx, pool) @@ -775,7 +775,7 @@ func TestRedelegateFromUnbondingValidator(t *testing.T) { ctx = ctx.WithBlockHeader(header) // unbond some of the other delegation's shares - redelegateTokens := sdk.TokensFromTendermintPower(6) + redelegateTokens := sdk.TokensFromConsensusPower(6) _, err = keeper.BeginRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1], redelegateTokens.ToDec()) require.NoError(t, err) @@ -790,13 +790,13 @@ func TestRedelegateFromUnbondingValidator(t *testing.T) { func TestRedelegateFromUnbondedValidator(t *testing.T) { ctx, _, keeper := CreateTestInput(t, false, 0) pool := keeper.GetPool(ctx) - startTokens := sdk.TokensFromTendermintPower(30) + startTokens := sdk.TokensFromConsensusPower(30) pool.NotBondedTokens = startTokens //create a validator with a self-delegation validator := types.NewValidator(addrVals[0], PKs[0], types.Description{}) - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) validator, pool, issuedShares := validator.AddTokensFromDel(pool, valTokens) require.Equal(t, valTokens, issuedShares.RoundInt()) keeper.SetPool(ctx, pool) @@ -808,7 +808,7 @@ func TestRedelegateFromUnbondedValidator(t *testing.T) { // create a second delegation to this validator keeper.DeleteValidatorByPowerIndex(ctx, validator) - delTokens := sdk.TokensFromTendermintPower(10) + delTokens := sdk.TokensFromConsensusPower(10) validator, pool, issuedShares = validator.AddTokensFromDel(pool, delTokens) require.Equal(t, delTokens, issuedShares.RoundInt()) keeper.SetPool(ctx, pool) @@ -846,7 +846,7 @@ func TestRedelegateFromUnbondedValidator(t *testing.T) { keeper.unbondingToUnbonded(ctx, validator) // redelegate some of the delegation's shares - redelegationTokens := sdk.TokensFromTendermintPower(6) + redelegationTokens := sdk.TokensFromConsensusPower(6) _, err = keeper.BeginRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1], redelegationTokens.ToDec()) require.NoError(t, err) diff --git a/x/staking/keeper/invariants.go b/x/staking/keeper/invariants.go index df4c69216bdb..9dfe6b8b177b 100644 --- a/x/staking/keeper/invariants.go +++ b/x/staking/keeper/invariants.go @@ -126,7 +126,7 @@ func NonNegativePowerInvariant(k Keeper) sdk.Invariant { if !bytes.Equal(iterator.Key(), powerKey) { return fmt.Errorf("power store invariance:\n\tvalidator.Power: %v"+ "\n\tkey should be: %v\n\tkey in store: %v", - validator.GetTendermintPower(), powerKey, iterator.Key()) + validator.GetConsensusPower(), powerKey, iterator.Key()) } if validator.Tokens.IsNegative() { diff --git a/x/staking/keeper/querier_test.go b/x/staking/keeper/querier_test.go index 9e938a26387d..4e8cee4eaf8c 100644 --- a/x/staking/keeper/querier_test.go +++ b/x/staking/keeper/querier_test.go @@ -190,7 +190,7 @@ func TestQueryDelegation(t *testing.T) { r.SetValidator(ctx, val2) r.SetValidatorByPowerIndex(ctx, val2) - delTokens := sdk.TokensFromTendermintPower(20) + delTokens := sdk.TokensFromConsensusPower(20) r.Delegate(ctx, addrAcc2, delTokens, val1, true) // apply TM updates @@ -315,7 +315,7 @@ func TestQueryDelegation(t *testing.T) { require.Equal(t, delegation.Shares.TruncateInt(), delegationsRes[0].Balance) // Query unbonging delegation - unbondingTokens := sdk.TokensFromTendermintPower(10) + unbondingTokens := sdk.TokensFromConsensusPower(10) _, err = r.Undelegate(ctx, addrAcc2, val1.OperatorAddress, unbondingTokens.ToDec()) require.Nil(t, err) @@ -368,7 +368,7 @@ func TestQueryDelegation(t *testing.T) { require.NotNil(t, err) // Query redelegation - redelegationTokens := sdk.TokensFromTendermintPower(10) + redelegationTokens := sdk.TokensFromConsensusPower(10) _, err = r.BeginRedelegation(ctx, addrAcc2, val1.OperatorAddress, val2.OperatorAddress, redelegationTokens.ToDec()) require.Nil(t, err) @@ -406,11 +406,11 @@ func TestQueryRedelegations(t *testing.T) { r.SetValidator(ctx, val1) r.SetValidator(ctx, val2) - delAmount := sdk.TokensFromTendermintPower(100) + delAmount := sdk.TokensFromConsensusPower(100) r.Delegate(ctx, addrAcc2, delAmount, val1, true) _ = r.ApplyAndReturnValidatorSetUpdates(ctx) - rdAmount := sdk.TokensFromTendermintPower(20) + rdAmount := sdk.TokensFromConsensusPower(20) r.BeginRedelegation(ctx, addrAcc2, val1.GetOperator(), val2.GetOperator(), rdAmount.ToDec()) r.ApplyAndReturnValidatorSetUpdates(ctx) @@ -470,13 +470,13 @@ func TestQueryUnbondingDelegation(t *testing.T) { keeper.SetValidator(ctx, val1) // delegate - delAmount := sdk.TokensFromTendermintPower(100) + delAmount := sdk.TokensFromConsensusPower(100) _, err := keeper.Delegate(ctx, addrAcc1, delAmount, val1, true) require.NoError(t, err) _ = keeper.ApplyAndReturnValidatorSetUpdates(ctx) // undelegate - undelAmount := sdk.TokensFromTendermintPower(20) + undelAmount := sdk.TokensFromConsensusPower(20) _, err = keeper.Undelegate(ctx, addrAcc1, val1.GetOperator(), undelAmount.ToDec()) require.NoError(t, err) keeper.ApplyAndReturnValidatorSetUpdates(ctx) diff --git a/x/staking/keeper/slash.go b/x/staking/keeper/slash.go index 571f5b8c66b8..6d3cb55aa6b3 100644 --- a/x/staking/keeper/slash.go +++ b/x/staking/keeper/slash.go @@ -29,7 +29,7 @@ func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, infractionHeigh } // Amount of slashing = slash slashFactor * power at time of infraction - amount := sdk.TokensFromTendermintPower(power) + amount := sdk.TokensFromConsensusPower(power) slashAmountDec := amount.ToDec().Mul(slashFactor) slashAmount := slashAmountDec.TruncateInt() diff --git a/x/staking/keeper/slash_test.go b/x/staking/keeper/slash_test.go index 283fc9f672a8..432b8b69cf92 100644 --- a/x/staking/keeper/slash_test.go +++ b/x/staking/keeper/slash_test.go @@ -21,7 +21,7 @@ func setupHelper(t *testing.T, power int64) (sdk.Context, Keeper, types.Params) params := keeper.GetParams(ctx) pool := keeper.GetPool(ctx) numVals := int64(3) - amt := sdk.TokensFromTendermintPower(power) + amt := sdk.TokensFromConsensusPower(power) pool.NotBondedTokens = amt.MulRaw(numVals) // add numVals validators @@ -197,9 +197,9 @@ func TestSlashAtNegativeHeight(t *testing.T) { validator = keeper.mustGetValidator(ctx, validator.OperatorAddress) // power decreased - require.Equal(t, int64(5), validator.GetTendermintPower()) + require.Equal(t, int64(5), validator.GetConsensusPower()) // pool bonded shares decreased - require.Equal(t, sdk.TokensFromTendermintPower(5), oldPool.BondedTokens.Sub(newPool.BondedTokens)) + require.Equal(t, sdk.TokensFromConsensusPower(5), oldPool.BondedTokens.Sub(newPool.BondedTokens)) } // tests Slash at the current height @@ -224,9 +224,9 @@ func TestSlashValidatorAtCurrentHeight(t *testing.T) { validator = keeper.mustGetValidator(ctx, validator.OperatorAddress) // power decreased - require.Equal(t, int64(5), validator.GetTendermintPower()) + require.Equal(t, int64(5), validator.GetConsensusPower()) // pool bonded shares decreased - require.Equal(t, sdk.TokensFromTendermintPower(5), oldPool.BondedTokens.Sub(newPool.BondedTokens)) + require.Equal(t, sdk.TokensFromConsensusPower(5), oldPool.BondedTokens.Sub(newPool.BondedTokens)) } // tests Slash at a previous height with an unbonding delegation @@ -237,7 +237,7 @@ func TestSlashWithUnbondingDelegation(t *testing.T) { // set an unbonding delegation with expiration timestamp beyond which the // unbonding delegation shouldn't be slashed - ubdTokens := sdk.TokensFromTendermintPower(4) + ubdTokens := sdk.TokensFromConsensusPower(4) ubd := types.NewUnbondingDelegation(addrDels[0], addrVals[0], 11, time.Unix(0, 0), ubdTokens) keeper.SetUnbondingDelegation(ctx, ubd) @@ -258,11 +258,11 @@ func TestSlashWithUnbondingDelegation(t *testing.T) { require.True(t, found) require.Len(t, ubd.Entries, 1) // balance decreased - require.Equal(t, sdk.TokensFromTendermintPower(2), ubd.Entries[0].Balance) + require.Equal(t, sdk.TokensFromConsensusPower(2), ubd.Entries[0].Balance) // read updated pool newPool := keeper.GetPool(ctx) // bonded tokens burned - require.Equal(t, sdk.TokensFromTendermintPower(3), oldPool.BondedTokens.Sub(newPool.BondedTokens)) + require.Equal(t, sdk.TokensFromConsensusPower(3), oldPool.BondedTokens.Sub(newPool.BondedTokens)) // read updated validator validator, found = keeper.GetValidatorByConsAddr(ctx, consAddr) require.True(t, found) @@ -270,7 +270,7 @@ func TestSlashWithUnbondingDelegation(t *testing.T) { // was still bonded at the time of discovery and was slashed by half, 4 stake // bonded at the time of discovery hadn't been bonded at the time of infraction // and wasn't slashed - require.Equal(t, int64(7), validator.GetTendermintPower()) + require.Equal(t, int64(7), validator.GetConsensusPower()) // slash validator again ctx = ctx.WithBlockHeight(13) @@ -283,12 +283,12 @@ func TestSlashWithUnbondingDelegation(t *testing.T) { // read updated pool newPool = keeper.GetPool(ctx) // bonded tokens burned again - require.Equal(t, sdk.TokensFromTendermintPower(6), oldPool.BondedTokens.Sub(newPool.BondedTokens)) + require.Equal(t, sdk.TokensFromConsensusPower(6), oldPool.BondedTokens.Sub(newPool.BondedTokens)) // read updated validator validator, found = keeper.GetValidatorByConsAddr(ctx, consAddr) require.True(t, found) // power decreased by 3 again - require.Equal(t, int64(4), validator.GetTendermintPower()) + require.Equal(t, int64(4), validator.GetConsensusPower()) // slash validator again // all originally bonded stake has been slashed, so this will have no effect @@ -304,12 +304,12 @@ func TestSlashWithUnbondingDelegation(t *testing.T) { // read updated pool newPool = keeper.GetPool(ctx) // bonded tokens burned again - require.Equal(t, sdk.TokensFromTendermintPower(9), oldPool.BondedTokens.Sub(newPool.BondedTokens)) + require.Equal(t, sdk.TokensFromConsensusPower(9), oldPool.BondedTokens.Sub(newPool.BondedTokens)) // read updated validator validator, found = keeper.GetValidatorByConsAddr(ctx, consAddr) require.True(t, found) // power decreased by 3 again - require.Equal(t, int64(1), validator.GetTendermintPower()) + require.Equal(t, int64(1), validator.GetConsensusPower()) // slash validator again // all originally bonded stake has been slashed, so this will have no effect @@ -325,7 +325,7 @@ func TestSlashWithUnbondingDelegation(t *testing.T) { // read updated pool newPool = keeper.GetPool(ctx) // just 1 bonded token burned again since that's all the validator now has - require.Equal(t, sdk.TokensFromTendermintPower(10), oldPool.BondedTokens.Sub(newPool.BondedTokens)) + require.Equal(t, sdk.TokensFromConsensusPower(10), oldPool.BondedTokens.Sub(newPool.BondedTokens)) // apply TM updates keeper.ApplyAndReturnValidatorSetUpdates(ctx) // read updated validator @@ -342,7 +342,7 @@ func TestSlashWithRedelegation(t *testing.T) { fraction := sdk.NewDecWithPrec(5, 1) // set a redelegation - rdTokens := sdk.TokensFromTendermintPower(6) + rdTokens := sdk.TokensFromConsensusPower(6) rd := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 11, time.Unix(0, 0), rdTokens, rdTokens.ToDec()) keeper.SetRedelegation(ctx, rd) @@ -370,7 +370,7 @@ func TestSlashWithRedelegation(t *testing.T) { // read updated pool newPool := keeper.GetPool(ctx) // bonded tokens burned - require.Equal(t, sdk.TokensFromTendermintPower(5), oldPool.BondedTokens.Sub(newPool.BondedTokens)) + require.Equal(t, sdk.TokensFromConsensusPower(5), oldPool.BondedTokens.Sub(newPool.BondedTokens)) // read updated validator validator, found = keeper.GetValidatorByConsAddr(ctx, consAddr) require.True(t, found) @@ -378,7 +378,7 @@ func TestSlashWithRedelegation(t *testing.T) { // was still bonded at the time of discovery and was slashed by half, 4 stake // bonded at the time of discovery hadn't been bonded at the time of infraction // and wasn't slashed - require.Equal(t, int64(8), validator.GetTendermintPower()) + require.Equal(t, int64(8), validator.GetConsensusPower()) // slash the validator again ctx = ctx.WithBlockHeight(12) @@ -393,12 +393,12 @@ func TestSlashWithRedelegation(t *testing.T) { // read updated pool newPool = keeper.GetPool(ctx) // seven bonded tokens burned - require.Equal(t, sdk.TokensFromTendermintPower(12), oldPool.BondedTokens.Sub(newPool.BondedTokens)) + require.Equal(t, sdk.TokensFromConsensusPower(12), oldPool.BondedTokens.Sub(newPool.BondedTokens)) // read updated validator validator, found = keeper.GetValidatorByConsAddr(ctx, consAddr) require.True(t, found) // power decreased by 4 - require.Equal(t, int64(4), validator.GetTendermintPower()) + require.Equal(t, int64(4), validator.GetConsensusPower()) // slash the validator again, by 100% ctx = ctx.WithBlockHeight(12) @@ -413,7 +413,7 @@ func TestSlashWithRedelegation(t *testing.T) { // read updated pool newPool = keeper.GetPool(ctx) // four more bonded tokens burned - require.Equal(t, sdk.TokensFromTendermintPower(16), oldPool.BondedTokens.Sub(newPool.BondedTokens)) + require.Equal(t, sdk.TokensFromConsensusPower(16), oldPool.BondedTokens.Sub(newPool.BondedTokens)) // apply TM updates keeper.ApplyAndReturnValidatorSetUpdates(ctx) // read updated validator @@ -436,7 +436,7 @@ func TestSlashWithRedelegation(t *testing.T) { // read updated pool newPool = keeper.GetPool(ctx) // no more bonded tokens burned - require.Equal(t, sdk.TokensFromTendermintPower(16), oldPool.BondedTokens.Sub(newPool.BondedTokens)) + require.Equal(t, sdk.TokensFromConsensusPower(16), oldPool.BondedTokens.Sub(newPool.BondedTokens)) // read updated validator // power still zero, still in unbonding period validator, _ = keeper.GetValidatorByConsAddr(ctx, consAddr) @@ -450,7 +450,7 @@ func TestSlashBoth(t *testing.T) { // set a redelegation with expiration timestamp beyond which the // redelegation shouldn't be slashed - rdATokens := sdk.TokensFromTendermintPower(6) + rdATokens := sdk.TokensFromConsensusPower(6) rdA := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 11, time.Unix(0, 0), rdATokens, rdATokens.ToDec()) @@ -462,7 +462,7 @@ func TestSlashBoth(t *testing.T) { // set an unbonding delegation with expiration timestamp (beyond which the // unbonding delegation shouldn't be slashed) - ubdATokens := sdk.TokensFromTendermintPower(4) + ubdATokens := sdk.TokensFromConsensusPower(4) ubdA := types.NewUnbondingDelegation(addrDels[0], addrVals[0], 11, time.Unix(0, 0), ubdATokens) keeper.SetUnbondingDelegation(ctx, ubdA) @@ -482,12 +482,12 @@ func TestSlashBoth(t *testing.T) { // read updated pool newPool := keeper.GetPool(ctx) // not-bonded tokens burned - require.Equal(t, sdk.TokensFromTendermintPower(2), oldPool.NotBondedTokens.Sub(newPool.NotBondedTokens)) + require.Equal(t, sdk.TokensFromConsensusPower(2), oldPool.NotBondedTokens.Sub(newPool.NotBondedTokens)) // bonded tokens burned - require.Equal(t, sdk.TokensFromTendermintPower(3), oldPool.BondedTokens.Sub(newPool.BondedTokens)) + require.Equal(t, sdk.TokensFromConsensusPower(3), oldPool.BondedTokens.Sub(newPool.BondedTokens)) // read updated validator validator, found = keeper.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(PKs[0])) require.True(t, found) // power not decreased, all stake was bonded since - require.Equal(t, int64(10), validator.GetTendermintPower()) + require.Equal(t, int64(10), validator.GetConsensusPower()) } diff --git a/x/staking/keeper/test_common.go b/x/staking/keeper/test_common.go index 2753bf0a7901..6b5cc33aa6e7 100644 --- a/x/staking/keeper/test_common.go +++ b/x/staking/keeper/test_common.go @@ -79,7 +79,7 @@ func MakeTestCodec() *codec.Codec { // If `initPower` is 0, no addrs get created. func CreateTestInput(t *testing.T, isCheckTx bool, initPower int64) (sdk.Context, auth.AccountKeeper, Keeper) { - initCoins := sdk.TokensFromTendermintPower(initPower) + initCoins := sdk.TokensFromConsensusPower(initPower) keyStaking := sdk.NewKVStoreKey(types.StoreKey) tkeyStaking := sdk.NewTransientStoreKey(types.TStoreKey) diff --git a/x/staking/keeper/val_state_change.go b/x/staking/keeper/val_state_change.go index 354fc1d87466..6ed450147d9f 100644 --- a/x/staking/keeper/val_state_change.go +++ b/x/staking/keeper/val_state_change.go @@ -49,7 +49,7 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx sdk.Context) (updates []ab // if we get to a zero-power validator (which we don't bond), // there are no more possible bonded validators - if validator.PotentialTendermintPower() == 0 { + if validator.PotentialConsensusPower() == 0 { break } @@ -71,7 +71,7 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx sdk.Context) (updates []ab oldPowerBytes, found := last[valAddrBytes] // calculate the new power bytes - newPower := validator.TendermintPower() + newPower := validator.ConsensusPower() newPowerBytes := k.cdc.MustMarshalBinaryLengthPrefixed(newPower) // update the validator set if power has changed diff --git a/x/staking/keeper/validator_test.go b/x/staking/keeper/validator_test.go index 82729fcfd21b..2f1a513690d8 100644 --- a/x/staking/keeper/validator_test.go +++ b/x/staking/keeper/validator_test.go @@ -22,7 +22,7 @@ func TestSetValidator(t *testing.T) { valPubKey := PKs[0] valAddr := sdk.ValAddress(valPubKey.Address().Bytes()) - valTokens := sdk.TokensFromTendermintPower(10) + valTokens := sdk.TokensFromConsensusPower(10) // test how the validator is set from a purely unbonbed pool validator := types.NewValidator(valAddr, valPubKey, types.Description{}) @@ -76,20 +76,20 @@ func TestUpdateValidatorByPowerIndex(t *testing.T) { pool := keeper.GetPool(ctx) // create a random pool - pool.NotBondedTokens = sdk.NewInt(10000) - pool.BondedTokens = sdk.NewInt(1234) + pool.NotBondedTokens = sdk.TokensFromConsensusPower(10000) + pool.BondedTokens = sdk.TokensFromConsensusPower(1234) keeper.SetPool(ctx, pool) // add a validator validator := types.NewValidator(addrVals[0], PKs[0], types.Description{}) - validator, pool, delSharesCreated := validator.AddTokensFromDel(pool, sdk.NewInt(100)) + validator, pool, delSharesCreated := validator.AddTokensFromDel(pool, sdk.TokensFromConsensusPower(100)) require.Equal(t, sdk.Unbonded, validator.Status) - require.Equal(t, int64(100), validator.Tokens.Int64()) + require.Equal(t, sdk.TokensFromConsensusPower(100), validator.Tokens) keeper.SetPool(ctx, pool) TestingUpdateValidator(keeper, ctx, validator, true) validator, found := keeper.GetValidator(ctx, addrVals[0]) require.True(t, found) - require.Equal(t, int64(100), validator.Tokens.Int64(), "\nvalidator %v\npool %v", validator, pool) + require.Equal(t, sdk.TokensFromConsensusPower(100), validator.Tokens, "\nvalidator %v\npool %v", validator, pool) pool = keeper.GetPool(ctx) power := types.GetValidatorsByPowerIndexKey(validator) @@ -98,7 +98,7 @@ func TestUpdateValidatorByPowerIndex(t *testing.T) { // burn half the delegator shares keeper.DeleteValidatorByPowerIndex(ctx, validator) validator, pool, burned := validator.RemoveDelShares(pool, delSharesCreated.Quo(sdk.NewDec(2))) - require.Equal(t, int64(50), burned.Int64()) + require.Equal(t, sdk.TokensFromConsensusPower(50), burned) keeper.SetPool(ctx, pool) // update the pool TestingUpdateValidator(keeper, ctx, validator, true) // update the validator, possibly kicking it out require.False(t, validatorByPowerIndexExists(keeper, ctx, power)) @@ -124,15 +124,15 @@ func TestUpdateBondedValidatorsDecreaseCliff(t *testing.T) { keeper.SetParams(ctx, params) // create a random pool - pool.NotBondedTokens = sdk.TokensFromTendermintPower(10000) - pool.BondedTokens = sdk.TokensFromTendermintPower(1234) + pool.NotBondedTokens = sdk.TokensFromConsensusPower(10000) + pool.BondedTokens = sdk.TokensFromConsensusPower(1234) keeper.SetPool(ctx, pool) validators := make([]types.Validator, numVals) for i := 0; i < len(validators); i++ { moniker := fmt.Sprintf("val#%d", int64(i)) val := types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{Moniker: moniker}) - delTokens := sdk.TokensFromTendermintPower(int64((i + 1) * 10)) + delTokens := sdk.TokensFromConsensusPower(int64((i + 1) * 10)) val, pool, _ = val.AddTokensFromDel(pool, delTokens) keeper.SetPool(ctx, pool) @@ -145,7 +145,7 @@ func TestUpdateBondedValidatorsDecreaseCliff(t *testing.T) { // remove enough tokens to kick out the validator below the current cliff // validator and next in line cliff validator keeper.DeleteValidatorByPowerIndex(ctx, nextCliffVal) - shares := sdk.TokensFromTendermintPower(21) + shares := sdk.TokensFromConsensusPower(21) nextCliffVal, pool, _ = nextCliffVal.RemoveDelShares(pool, shares.ToDec()) keeper.SetPool(ctx, pool) nextCliffVal = TestingUpdateValidator(keeper, ctx, nextCliffVal, true) @@ -174,7 +174,7 @@ func TestSlashToZeroPowerRemoved(t *testing.T) { // add a validator validator := types.NewValidator(addrVals[0], PKs[0], types.Description{}) - valTokens := sdk.TokensFromTendermintPower(100) + valTokens := sdk.TokensFromConsensusPower(100) validator, pool, _ = validator.AddTokensFromDel(pool, valTokens) require.Equal(t, sdk.Unbonded, validator.Status) require.Equal(t, valTokens, validator.Tokens) @@ -205,13 +205,13 @@ func TestValidatorBasics(t *testing.T) { validators[i] = types.NewValidator(addrVals[i], PKs[i], types.Description{}) validators[i].Status = sdk.Unbonded validators[i].Tokens = sdk.ZeroInt() - tokens := sdk.TokensFromTendermintPower(power) + tokens := sdk.TokensFromConsensusPower(power) validators[i], pool, _ = validators[i].AddTokensFromDel(pool, tokens) keeper.SetPool(ctx, pool) } - assert.Equal(t, sdk.TokensFromTendermintPower(9), validators[0].Tokens) - assert.Equal(t, sdk.TokensFromTendermintPower(8), validators[1].Tokens) - assert.Equal(t, sdk.TokensFromTendermintPower(7), validators[2].Tokens) + assert.Equal(t, sdk.TokensFromConsensusPower(9), validators[0].Tokens) + assert.Equal(t, sdk.TokensFromConsensusPower(8), validators[1].Tokens) + assert.Equal(t, sdk.TokensFromConsensusPower(7), validators[2].Tokens) // check the empty keeper first _, found := keeper.GetValidator(ctx, addrVals[0]) @@ -244,14 +244,14 @@ func TestValidatorBasics(t *testing.T) { require.Equal(t, 1, len(resVals)) assert.True(ValEq(t, validators[0], resVals[0])) assert.Equal(t, sdk.Bonded, validators[0].Status) - assert.True(sdk.IntEq(t, sdk.TokensFromTendermintPower(9), validators[0].BondedTokens())) + assert.True(sdk.IntEq(t, sdk.TokensFromConsensusPower(9), validators[0].BondedTokens())) pool = keeper.GetPool(ctx) assert.True(sdk.IntEq(t, pool.BondedTokens, validators[0].BondedTokens())) // modify a records, save, and retrieve validators[0].Status = sdk.Bonded - validators[0].Tokens = sdk.TokensFromTendermintPower(10) + validators[0].Tokens = sdk.TokensFromConsensusPower(10) validators[0].DelegatorShares = validators[0].Tokens.ToDec() validators[0] = TestingUpdateValidator(keeper, ctx, validators[0], true) resVal, found = keeper.GetValidator(ctx, addrVals[0]) @@ -457,7 +457,7 @@ func TestGetValidatorsEdgeCases(t *testing.T) { pool := keeper.GetPool(ctx) moniker := fmt.Sprintf("val#%d", int64(i)) validators[i] = types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{Moniker: moniker}) - tokens := sdk.TokensFromTendermintPower(power) + tokens := sdk.TokensFromConsensusPower(power) validators[i], pool, _ = validators[i].AddTokensFromDel(pool, tokens) keeper.SetPool(ctx, pool) validators[i] = TestingUpdateValidator(keeper, ctx, validators[i], true) @@ -474,7 +474,7 @@ func TestGetValidatorsEdgeCases(t *testing.T) { pool := keeper.GetPool(ctx) keeper.DeleteValidatorByPowerIndex(ctx, validators[0]) - delTokens := sdk.TokensFromTendermintPower(500) + delTokens := sdk.TokensFromConsensusPower(500) validators[0], pool, _ = validators[0].AddTokensFromDel(pool, delTokens) keeper.SetPool(ctx, pool) validators[0] = TestingUpdateValidator(keeper, ctx, validators[0], true) @@ -493,7 +493,7 @@ func TestGetValidatorsEdgeCases(t *testing.T) { validators[3], found = keeper.GetValidator(ctx, validators[3].OperatorAddress) require.True(t, found) keeper.DeleteValidatorByPowerIndex(ctx, validators[3]) - validators[3], pool, _ = validators[3].AddTokensFromDel(pool, sdk.NewInt(1)) + validators[3], pool, _ = validators[3].AddTokensFromDel(pool, sdk.TokensFromConsensusPower(1)) keeper.SetPool(ctx, pool) validators[3] = TestingUpdateValidator(keeper, ctx, validators[3], true) resValidators = keeper.GetBondedValidatorsByPower(ctx) @@ -539,9 +539,9 @@ func TestValidatorBondHeight(t *testing.T) { validators[1] = types.NewValidator(sdk.ValAddress(Addrs[1]), PKs[1], types.Description{}) validators[2] = types.NewValidator(sdk.ValAddress(Addrs[2]), PKs[2], types.Description{}) - tokens0 := sdk.TokensFromTendermintPower(200) - tokens1 := sdk.TokensFromTendermintPower(100) - tokens2 := sdk.TokensFromTendermintPower(100) + tokens0 := sdk.TokensFromConsensusPower(200) + tokens1 := sdk.TokensFromConsensusPower(100) + tokens2 := sdk.TokensFromConsensusPower(100) validators[0], pool, _ = validators[0].AddTokensFromDel(pool, tokens0) validators[1], pool, _ = validators[1].AddTokensFromDel(pool, tokens1) validators[2], pool, _ = validators[2].AddTokensFromDel(pool, tokens2) @@ -564,7 +564,7 @@ func TestValidatorBondHeight(t *testing.T) { assert.True(ValEq(t, validators[1], resValidators[1])) keeper.DeleteValidatorByPowerIndex(ctx, validators[1]) keeper.DeleteValidatorByPowerIndex(ctx, validators[2]) - delTokens := sdk.TokensFromTendermintPower(50) + delTokens := sdk.TokensFromConsensusPower(50) validators[1], pool, _ = validators[1].AddTokensFromDel(pool, delTokens) validators[2], pool, _ = validators[2].AddTokensFromDel(pool, delTokens) keeper.SetPool(ctx, pool) @@ -589,7 +589,7 @@ func TestFullValidatorSetPowerChange(t *testing.T) { for i, power := range powers { pool := keeper.GetPool(ctx) validators[i] = types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{}) - tokens := sdk.TokensFromTendermintPower(power) + tokens := sdk.TokensFromConsensusPower(power) validators[i], pool, _ = validators[i].AddTokensFromDel(pool, tokens) keeper.SetPool(ctx, pool) TestingUpdateValidator(keeper, ctx, validators[i], true) @@ -611,7 +611,7 @@ func TestFullValidatorSetPowerChange(t *testing.T) { // test a swap in voting power pool := keeper.GetPool(ctx) - tokens := sdk.TokensFromTendermintPower(600) + tokens := sdk.TokensFromConsensusPower(600) validators[0], pool, _ = validators[0].AddTokensFromDel(pool, tokens) keeper.SetPool(ctx, pool) validators[0] = TestingUpdateValidator(keeper, ctx, validators[0], true) @@ -633,7 +633,7 @@ func TestApplyAndReturnValidatorSetUpdatesAllNone(t *testing.T) { valAddr := sdk.ValAddress(valPubKey.Address().Bytes()) validators[i] = types.NewValidator(valAddr, valPubKey, types.Description{}) - tokens := sdk.TokensFromTendermintPower(power) + tokens := sdk.TokensFromConsensusPower(power) validators[i], pool, _ = validators[i].AddTokensFromDel(pool, tokens) keeper.SetPool(ctx, pool) } @@ -663,7 +663,7 @@ func TestApplyAndReturnValidatorSetUpdatesIdentical(t *testing.T) { pool := keeper.GetPool(ctx) validators[i] = types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{}) - tokens := sdk.TokensFromTendermintPower(power) + tokens := sdk.TokensFromConsensusPower(power) validators[i], pool, _ = validators[i].AddTokensFromDel(pool, tokens) keeper.SetPool(ctx, pool) } @@ -687,7 +687,7 @@ func TestApplyAndReturnValidatorSetUpdatesSingleValueChange(t *testing.T) { pool := keeper.GetPool(ctx) validators[i] = types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{}) - tokens := sdk.TokensFromTendermintPower(power) + tokens := sdk.TokensFromConsensusPower(power) validators[i], pool, _ = validators[i].AddTokensFromDel(pool, tokens) keeper.SetPool(ctx, pool) } @@ -698,7 +698,7 @@ func TestApplyAndReturnValidatorSetUpdatesSingleValueChange(t *testing.T) { // test single value change // tendermintUpdate set: {} -> {c1'} validators[0].Status = sdk.Bonded - validators[0].Tokens = sdk.TokensFromTendermintPower(600) + validators[0].Tokens = sdk.TokensFromConsensusPower(600) validators[0] = TestingUpdateValidator(keeper, ctx, validators[0], false) updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx) @@ -716,7 +716,7 @@ func TestApplyAndReturnValidatorSetUpdatesMultipleValueChange(t *testing.T) { pool := keeper.GetPool(ctx) validators[i] = types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{}) - tokens := sdk.TokensFromTendermintPower(power) + tokens := sdk.TokensFromConsensusPower(power) validators[i], pool, _ = validators[i].AddTokensFromDel(pool, tokens) keeper.SetPool(ctx, pool) } @@ -727,8 +727,8 @@ func TestApplyAndReturnValidatorSetUpdatesMultipleValueChange(t *testing.T) { // test multiple value change // tendermintUpdate set: {c1, c3} -> {c1', c3'} pool := keeper.GetPool(ctx) - delTokens1 := sdk.TokensFromTendermintPower(190) - delTokens2 := sdk.TokensFromTendermintPower(80) + delTokens1 := sdk.TokensFromConsensusPower(190) + delTokens2 := sdk.TokensFromConsensusPower(80) validators[0], pool, _ = validators[0].AddTokensFromDel(pool, delTokens1) validators[1], pool, _ = validators[1].AddTokensFromDel(pool, delTokens2) keeper.SetPool(ctx, pool) @@ -750,7 +750,7 @@ func TestApplyAndReturnValidatorSetUpdatesInserted(t *testing.T) { pool := keeper.GetPool(ctx) validators[i] = types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{}) - tokens := sdk.TokensFromTendermintPower(power) + tokens := sdk.TokensFromConsensusPower(power) validators[i], pool, _ = validators[i].AddTokensFromDel(pool, tokens) keeper.SetPool(ctx, pool) } @@ -799,7 +799,7 @@ func TestApplyAndReturnValidatorSetUpdatesWithCliffValidator(t *testing.T) { pool := keeper.GetPool(ctx) validators[i] = types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{}) - tokens := sdk.TokensFromTendermintPower(power) + tokens := sdk.TokensFromConsensusPower(power) validators[i], pool, _ = validators[i].AddTokensFromDel(pool, tokens) keeper.SetPool(ctx, pool) } @@ -818,7 +818,7 @@ func TestApplyAndReturnValidatorSetUpdatesWithCliffValidator(t *testing.T) { require.Equal(t, 0, len(keeper.ApplyAndReturnValidatorSetUpdates(ctx))) pool := keeper.GetPool(ctx) - tokens := sdk.TokensFromTendermintPower(10) + tokens := sdk.TokensFromConsensusPower(10) validators[2], pool, _ = validators[2].AddTokensFromDel(pool, tokens) keeper.SetPool(ctx, pool) keeper.SetValidator(ctx, validators[2]) @@ -839,7 +839,7 @@ func TestApplyAndReturnValidatorSetUpdatesPowerDecrease(t *testing.T) { pool := keeper.GetPool(ctx) validators[i] = types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{}) - tokens := sdk.TokensFromTendermintPower(power) + tokens := sdk.TokensFromConsensusPower(power) validators[i], pool, _ = validators[i].AddTokensFromDel(pool, tokens) keeper.SetPool(ctx, pool) } @@ -848,14 +848,14 @@ func TestApplyAndReturnValidatorSetUpdatesPowerDecrease(t *testing.T) { require.Equal(t, 2, len(keeper.ApplyAndReturnValidatorSetUpdates(ctx))) // check initial power - require.Equal(t, int64(100), validators[0].GetTendermintPower()) - require.Equal(t, int64(100), validators[1].GetTendermintPower()) + require.Equal(t, int64(100), validators[0].GetConsensusPower()) + require.Equal(t, int64(100), validators[1].GetConsensusPower()) // test multiple value change // tendermintUpdate set: {c1, c3} -> {c1', c3'} pool := keeper.GetPool(ctx) - delTokens1 := sdk.TokensFromTendermintPower(20) - delTokens2 := sdk.TokensFromTendermintPower(30) + delTokens1 := sdk.TokensFromConsensusPower(20) + delTokens2 := sdk.TokensFromConsensusPower(30) validators[0], pool, _ = validators[0].RemoveDelShares(pool, delTokens1.ToDec()) validators[1], pool, _ = validators[1].RemoveDelShares(pool, delTokens2.ToDec()) keeper.SetPool(ctx, pool) @@ -863,8 +863,8 @@ func TestApplyAndReturnValidatorSetUpdatesPowerDecrease(t *testing.T) { validators[1] = TestingUpdateValidator(keeper, ctx, validators[1], false) // power has changed - require.Equal(t, int64(80), validators[0].GetTendermintPower()) - require.Equal(t, int64(70), validators[1].GetTendermintPower()) + require.Equal(t, int64(80), validators[0].GetConsensusPower()) + require.Equal(t, int64(70), validators[1].GetConsensusPower()) // Tendermint updates should reflect power change updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx) @@ -890,7 +890,7 @@ func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) { valAddr := sdk.ValAddress(valPubKey.Address().Bytes()) validators[i] = types.NewValidator(valAddr, valPubKey, types.Description{}) - tokens := sdk.TokensFromTendermintPower(power) + tokens := sdk.TokensFromConsensusPower(power) validators[i], pool, _ = validators[i].AddTokensFromDel(pool, tokens) keeper.SetPool(ctx, pool) @@ -912,7 +912,7 @@ func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) { for i, power := range powers { pool := keeper.GetPool(ctx) keeper.DeleteValidatorByPowerIndex(ctx, validators[i]) - tokens := sdk.TokensFromTendermintPower(power) + tokens := sdk.TokensFromConsensusPower(power) validators[i], pool, _ = validators[i].AddTokensFromDel(pool, tokens) keeper.SetPool(ctx, pool) @@ -942,7 +942,7 @@ func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) { valAddr = sdk.ValAddress(valPubKey.Address().Bytes()) validator = types.NewValidator(valAddr, valPubKey, types.Description{}) - tokens := sdk.TokensFromTendermintPower(500) + tokens := sdk.TokensFromConsensusPower(500) validator, pool, _ = validator.AddTokensFromDel(pool, tokens) keeper.SetValidator(ctx, validator) keeper.SetValidatorByPowerIndex(ctx, validator) @@ -977,7 +977,7 @@ func TestApplyAndReturnValidatorSetUpdatesBondTransition(t *testing.T) { valAddr := sdk.ValAddress(valPubKey.Address().Bytes()) validators[i] = types.NewValidator(valAddr, valPubKey, types.Description{Moniker: moniker}) - tokens := sdk.TokensFromTendermintPower(power) + tokens := sdk.TokensFromConsensusPower(power) validators[i], pool, _ = validators[i].AddTokensFromDel(pool, tokens) keeper.SetPool(ctx, pool) keeper.SetValidator(ctx, validators[i]) @@ -1003,7 +1003,7 @@ func TestApplyAndReturnValidatorSetUpdatesBondTransition(t *testing.T) { require.True(t, found) keeper.DeleteValidatorByPowerIndex(ctx, validators[0]) - tokens := sdk.TokensFromTendermintPower(1) + tokens := sdk.TokensFromConsensusPower(1) validators[0], pool, _ = validators[0].AddTokensFromDel(pool, tokens) keeper.SetPool(ctx, pool) keeper.SetValidator(ctx, validators[0]) @@ -1029,7 +1029,7 @@ func TestApplyAndReturnValidatorSetUpdatesBondTransition(t *testing.T) { require.Equal(t, 0, len(updates)) keeper.DeleteValidatorByPowerIndex(ctx, validators[1]) - tokens = sdk.TokensFromTendermintPower(250) + tokens = sdk.TokensFromConsensusPower(250) validators[1], pool, _ = validators[1].AddTokensFromDel(pool, tokens) keeper.SetPool(ctx, pool) keeper.SetValidator(ctx, validators[1]) diff --git a/x/staking/types/key.go b/x/staking/types/key.go deleted file mode 100644 index 3c9078ad7efd..000000000000 --- a/x/staking/types/key.go +++ /dev/null @@ -1,282 +0,0 @@ -package types - -import ( - "encoding/binary" - "time" - - sdk "github.com/cosmos/cosmos-sdk/types" -) - -// TODO remove some of these prefixes once have working multistore - -//nolint -var ( - // Keys for store prefixes - PoolKey = []byte{0x00} // key for the staking pools - - // Last* values are constant during a block. - LastValidatorPowerKey = []byte{0x11} // prefix for each key to a validator index, for bonded validators - LastTotalPowerKey = []byte{0x12} // prefix for the total power - - ValidatorsKey = []byte{0x21} // prefix for each key to a validator - ValidatorsByConsAddrKey = []byte{0x22} // prefix for each key to a validator index, by pubkey - ValidatorsByPowerIndexKey = []byte{0x23} // prefix for each key to a validator index, sorted by power - - DelegationKey = []byte{0x31} // key for a delegation - UnbondingDelegationKey = []byte{0x32} // key for an unbonding-delegation - UnbondingDelegationByValIndexKey = []byte{0x33} // prefix for each key for an unbonding-delegation, by validator operator - RedelegationKey = []byte{0x34} // key for a redelegation - RedelegationByValSrcIndexKey = []byte{0x35} // prefix for each key for an redelegation, by source validator operator - RedelegationByValDstIndexKey = []byte{0x36} // prefix for each key for an redelegation, by destination validator operator - - UnbondingQueueKey = []byte{0x41} // prefix for the timestamps in unbonding queue - RedelegationQueueKey = []byte{0x42} // prefix for the timestamps in redelegations queue - ValidatorQueueKey = []byte{0x43} // prefix for the timestamps in validator queue -) - -// gets the key for the validator with address -// VALUE: staking/Validator -func GetValidatorKey(operatorAddr sdk.ValAddress) []byte { - return append(ValidatorsKey, operatorAddr.Bytes()...) -} - -// gets the key for the validator with pubkey -// VALUE: validator operator address ([]byte) -func GetValidatorByConsAddrKey(addr sdk.ConsAddress) []byte { - return append(ValidatorsByConsAddrKey, addr.Bytes()...) -} - -// Get the validator operator address from LastValidatorPowerKey -func AddressFromLastValidatorPowerKey(key []byte) []byte { - return key[1:] // remove prefix bytes -} - -// get the validator by power index. -// Power index is the key used in the power-store, and represents the relative -// power ranking of the validator. -// VALUE: validator operator address ([]byte) -func GetValidatorsByPowerIndexKey(validator Validator) []byte { - // NOTE the address doesn't need to be stored because counter bytes must always be different - return getValidatorPowerRank(validator) -} - -// get the bonded validator index key for an operator address -func GetLastValidatorPowerKey(operator sdk.ValAddress) []byte { - return append(LastValidatorPowerKey, operator...) -} - -// get the power ranking of a validator -// NOTE the larger values are of higher value -// nolint: unparam -func getValidatorPowerRank(validator Validator) []byte { - - potentialPower := validator.Tokens - - // todo: deal with cases above 2**64, ref https://github.com/cosmos/cosmos-sdk/issues/2439#issuecomment-427167556 - tendermintPower := potentialPower.Int64() - tendermintPowerBytes := make([]byte, 8) - binary.BigEndian.PutUint64(tendermintPowerBytes[:], uint64(tendermintPower)) - - powerBytes := tendermintPowerBytes - powerBytesLen := len(powerBytes) // 8 - - // key is of format prefix || powerbytes || addrBytes - key := make([]byte, 1+powerBytesLen+sdk.AddrLen) - - key[0] = ValidatorsByPowerIndexKey[0] - copy(key[1:powerBytesLen+1], powerBytes) - operAddrInvr := cp(validator.OperatorAddress) - for i, b := range operAddrInvr { - operAddrInvr[i] = ^b - } - copy(key[powerBytesLen+1:], operAddrInvr) - - return key -} - -// parse the validators operator address from power rank key -func ParseValidatorPowerRankKey(key []byte) (operAddr []byte) { - powerBytesLen := 8 - if len(key) != 1+powerBytesLen+sdk.AddrLen { - panic("Invalid validator power rank key length") - } - operAddr = cp(key[powerBytesLen+1:]) - for i, b := range operAddr { - operAddr[i] = ^b - } - return operAddr -} - -// gets the prefix for all unbonding delegations from a delegator -func GetValidatorQueueTimeKey(timestamp time.Time) []byte { - bz := sdk.FormatTimeBytes(timestamp) - return append(ValidatorQueueKey, bz...) -} - -//______________________________________________________________________________ - -// gets the key for delegator bond with validator -// VALUE: staking/Delegation -func GetDelegationKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte { - return append(GetDelegationsKey(delAddr), valAddr.Bytes()...) -} - -// gets the prefix for a delegator for all validators -func GetDelegationsKey(delAddr sdk.AccAddress) []byte { - return append(DelegationKey, delAddr.Bytes()...) -} - -//______________________________________________________________________________ - -// gets the key for an unbonding delegation by delegator and validator addr -// VALUE: staking/UnbondingDelegation -func GetUBDKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte { - return append( - GetUBDsKey(delAddr.Bytes()), - valAddr.Bytes()...) -} - -// gets the index-key for an unbonding delegation, stored by validator-index -// VALUE: none (key rearrangement used) -func GetUBDByValIndexKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte { - return append(GetUBDsByValIndexKey(valAddr), delAddr.Bytes()...) -} - -// rearranges the ValIndexKey to get the UBDKey -func GetUBDKeyFromValIndexKey(IndexKey []byte) []byte { - addrs := IndexKey[1:] // remove prefix bytes - if len(addrs) != 2*sdk.AddrLen { - panic("unexpected key length") - } - valAddr := addrs[:sdk.AddrLen] - delAddr := addrs[sdk.AddrLen:] - return GetUBDKey(delAddr, valAddr) -} - -//______________ - -// gets the prefix for all unbonding delegations from a delegator -func GetUBDsKey(delAddr sdk.AccAddress) []byte { - return append(UnbondingDelegationKey, delAddr.Bytes()...) -} - -// gets the prefix keyspace for the indexes of unbonding delegations for a validator -func GetUBDsByValIndexKey(valAddr sdk.ValAddress) []byte { - return append(UnbondingDelegationByValIndexKey, valAddr.Bytes()...) -} - -// gets the prefix for all unbonding delegations from a delegator -func GetUnbondingDelegationTimeKey(timestamp time.Time) []byte { - bz := sdk.FormatTimeBytes(timestamp) - return append(UnbondingQueueKey, bz...) -} - -//________________________________________________________________________________ - -// gets the key for a redelegation -// VALUE: staking/RedelegationKey -func GetREDKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) []byte { - key := make([]byte, 1+sdk.AddrLen*3) - - copy(key[0:sdk.AddrLen+1], GetREDsKey(delAddr.Bytes())) - copy(key[sdk.AddrLen+1:2*sdk.AddrLen+1], valSrcAddr.Bytes()) - copy(key[2*sdk.AddrLen+1:3*sdk.AddrLen+1], valDstAddr.Bytes()) - - return key -} - -// gets the index-key for a redelegation, stored by source-validator-index -// VALUE: none (key rearrangement used) -func GetREDByValSrcIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) []byte { - REDSFromValsSrcKey := GetREDsFromValSrcIndexKey(valSrcAddr) - offset := len(REDSFromValsSrcKey) - - // key is of the form REDSFromValsSrcKey || delAddr || valDstAddr - key := make([]byte, len(REDSFromValsSrcKey)+2*sdk.AddrLen) - copy(key[0:offset], REDSFromValsSrcKey) - copy(key[offset:offset+sdk.AddrLen], delAddr.Bytes()) - copy(key[offset+sdk.AddrLen:offset+2*sdk.AddrLen], valDstAddr.Bytes()) - return key -} - -// gets the index-key for a redelegation, stored by destination-validator-index -// VALUE: none (key rearrangement used) -func GetREDByValDstIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) []byte { - REDSToValsDstKey := GetREDsToValDstIndexKey(valDstAddr) - offset := len(REDSToValsDstKey) - - // key is of the form REDSToValsDstKey || delAddr || valSrcAddr - key := make([]byte, len(REDSToValsDstKey)+2*sdk.AddrLen) - copy(key[0:offset], REDSToValsDstKey) - copy(key[offset:offset+sdk.AddrLen], delAddr.Bytes()) - copy(key[offset+sdk.AddrLen:offset+2*sdk.AddrLen], valSrcAddr.Bytes()) - - return key -} - -// GetREDKeyFromValSrcIndexKey rearranges the ValSrcIndexKey to get the REDKey -func GetREDKeyFromValSrcIndexKey(indexKey []byte) []byte { - // note that first byte is prefix byte - if len(indexKey) != 3*sdk.AddrLen+1 { - panic("unexpected key length") - } - valSrcAddr := indexKey[1 : sdk.AddrLen+1] - delAddr := indexKey[sdk.AddrLen+1 : 2*sdk.AddrLen+1] - valDstAddr := indexKey[2*sdk.AddrLen+1 : 3*sdk.AddrLen+1] - - return GetREDKey(delAddr, valSrcAddr, valDstAddr) -} - -// GetREDKeyFromValDstIndexKey rearranges the ValDstIndexKey to get the REDKey -func GetREDKeyFromValDstIndexKey(indexKey []byte) []byte { - // note that first byte is prefix byte - if len(indexKey) != 3*sdk.AddrLen+1 { - panic("unexpected key length") - } - valDstAddr := indexKey[1 : sdk.AddrLen+1] - delAddr := indexKey[sdk.AddrLen+1 : 2*sdk.AddrLen+1] - valSrcAddr := indexKey[2*sdk.AddrLen+1 : 3*sdk.AddrLen+1] - return GetREDKey(delAddr, valSrcAddr, valDstAddr) -} - -// gets the prefix for all unbonding delegations from a delegator -func GetRedelegationTimeKey(timestamp time.Time) []byte { - bz := sdk.FormatTimeBytes(timestamp) - return append(RedelegationQueueKey, bz...) -} - -//______________ - -// gets the prefix keyspace for redelegations from a delegator -func GetREDsKey(delAddr sdk.AccAddress) []byte { - return append(RedelegationKey, delAddr.Bytes()...) -} - -// gets the prefix keyspace for all redelegations redelegating away from a source validator -func GetREDsFromValSrcIndexKey(valSrcAddr sdk.ValAddress) []byte { - return append(RedelegationByValSrcIndexKey, valSrcAddr.Bytes()...) -} - -// gets the prefix keyspace for all redelegations redelegating towards a destination validator -func GetREDsToValDstIndexKey(valDstAddr sdk.ValAddress) []byte { - return append(RedelegationByValDstIndexKey, valDstAddr.Bytes()...) -} - -// gets the prefix keyspace for all redelegations redelegating towards a destination validator -// from a particular delegator -func GetREDsByDelToValDstIndexKey(delAddr sdk.AccAddress, valDstAddr sdk.ValAddress) []byte { - return append( - GetREDsToValDstIndexKey(valDstAddr), - delAddr.Bytes()...) -} - -//------------------------------------------------- - -func cp(bz []byte) (ret []byte) { - if bz == nil { - return nil - } - ret = make([]byte, len(bz)) - copy(ret, bz) - return ret -} diff --git a/x/staking/types/keys.go b/x/staking/types/keys.go index bba59695c05c..dce8958e7280 100644 --- a/x/staking/types/keys.go +++ b/x/staking/types/keys.go @@ -1,5 +1,13 @@ package types +import ( + "encoding/binary" + "time" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + + const ( // ModuleName is the name of the staking module ModuleName = "staking" @@ -16,3 +24,260 @@ const ( // RouterKey is the msg router key for the staking module RouterKey = ModuleName ) + +//nolint +var ( + // Keys for store prefixes + PoolKey = []byte{0x00} // key for the staking pools + + // Last* values are constant during a block. + LastValidatorPowerKey = []byte{0x11} // prefix for each key to a validator index, for bonded validators + LastTotalPowerKey = []byte{0x12} // prefix for the total power + + ValidatorsKey = []byte{0x21} // prefix for each key to a validator + ValidatorsByConsAddrKey = []byte{0x22} // prefix for each key to a validator index, by pubkey + ValidatorsByPowerIndexKey = []byte{0x23} // prefix for each key to a validator index, sorted by power + + DelegationKey = []byte{0x31} // key for a delegation + UnbondingDelegationKey = []byte{0x32} // key for an unbonding-delegation + UnbondingDelegationByValIndexKey = []byte{0x33} // prefix for each key for an unbonding-delegation, by validator operator + RedelegationKey = []byte{0x34} // key for a redelegation + RedelegationByValSrcIndexKey = []byte{0x35} // prefix for each key for an redelegation, by source validator operator + RedelegationByValDstIndexKey = []byte{0x36} // prefix for each key for an redelegation, by destination validator operator + + UnbondingQueueKey = []byte{0x41} // prefix for the timestamps in unbonding queue + RedelegationQueueKey = []byte{0x42} // prefix for the timestamps in redelegations queue + ValidatorQueueKey = []byte{0x43} // prefix for the timestamps in validator queue +) + +// gets the key for the validator with address +// VALUE: staking/Validator +func GetValidatorKey(operatorAddr sdk.ValAddress) []byte { + return append(ValidatorsKey, operatorAddr.Bytes()...) +} + +// gets the key for the validator with pubkey +// VALUE: validator operator address ([]byte) +func GetValidatorByConsAddrKey(addr sdk.ConsAddress) []byte { + return append(ValidatorsByConsAddrKey, addr.Bytes()...) +} + +// Get the validator operator address from LastValidatorPowerKey +func AddressFromLastValidatorPowerKey(key []byte) []byte { + return key[1:] // remove prefix bytes +} + +// get the validator by power index. +// Power index is the key used in the power-store, and represents the relative +// power ranking of the validator. +// VALUE: validator operator address ([]byte) +func GetValidatorsByPowerIndexKey(validator Validator) []byte { + // NOTE the address doesn't need to be stored because counter bytes must always be different + return getValidatorPowerRank(validator) +} + +// get the bonded validator index key for an operator address +func GetLastValidatorPowerKey(operator sdk.ValAddress) []byte { + return append(LastValidatorPowerKey, operator...) +} + +// get the power ranking of a validator +// NOTE the larger values are of higher value +func getValidatorPowerRank(validator Validator) []byte { + + consensusPower := sdk.TokensToConsensusPower(validator.Tokens) + consensusPowerBytes := make([]byte, 8) + binary.BigEndian.PutUint64(consensusPowerBytes[:], uint64(consensusPower)) + + powerBytes := consensusPowerBytes + powerBytesLen := len(powerBytes) // 8 + + // key is of format prefix || powerbytes || addrBytes + key := make([]byte, 1+powerBytesLen+sdk.AddrLen) + + key[0] = ValidatorsByPowerIndexKey[0] + copy(key[1:powerBytesLen+1], powerBytes) + operAddrInvr := sdk.CopyBytes(validator.OperatorAddress) + for i, b := range operAddrInvr { + operAddrInvr[i] = ^b + } + copy(key[powerBytesLen+1:], operAddrInvr) + + return key +} + +// parse the validators operator address from power rank key +func ParseValidatorPowerRankKey(key []byte) (operAddr []byte) { + powerBytesLen := 8 + if len(key) != 1+powerBytesLen+sdk.AddrLen { + panic("Invalid validator power rank key length") + } + operAddr = sdk.CopyBytes(key[powerBytesLen+1:]) + for i, b := range operAddr { + operAddr[i] = ^b + } + return operAddr +} + +// gets the prefix for all unbonding delegations from a delegator +func GetValidatorQueueTimeKey(timestamp time.Time) []byte { + bz := sdk.FormatTimeBytes(timestamp) + return append(ValidatorQueueKey, bz...) +} + +//______________________________________________________________________________ + +// gets the key for delegator bond with validator +// VALUE: staking/Delegation +func GetDelegationKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte { + return append(GetDelegationsKey(delAddr), valAddr.Bytes()...) +} + +// gets the prefix for a delegator for all validators +func GetDelegationsKey(delAddr sdk.AccAddress) []byte { + return append(DelegationKey, delAddr.Bytes()...) +} + +//______________________________________________________________________________ + +// gets the key for an unbonding delegation by delegator and validator addr +// VALUE: staking/UnbondingDelegation +func GetUBDKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte { + return append( + GetUBDsKey(delAddr.Bytes()), + valAddr.Bytes()...) +} + +// gets the index-key for an unbonding delegation, stored by validator-index +// VALUE: none (key rearrangement used) +func GetUBDByValIndexKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte { + return append(GetUBDsByValIndexKey(valAddr), delAddr.Bytes()...) +} + +// rearranges the ValIndexKey to get the UBDKey +func GetUBDKeyFromValIndexKey(IndexKey []byte) []byte { + addrs := IndexKey[1:] // remove prefix bytes + if len(addrs) != 2*sdk.AddrLen { + panic("unexpected key length") + } + valAddr := addrs[:sdk.AddrLen] + delAddr := addrs[sdk.AddrLen:] + return GetUBDKey(delAddr, valAddr) +} + +//______________ + +// gets the prefix for all unbonding delegations from a delegator +func GetUBDsKey(delAddr sdk.AccAddress) []byte { + return append(UnbondingDelegationKey, delAddr.Bytes()...) +} + +// gets the prefix keyspace for the indexes of unbonding delegations for a validator +func GetUBDsByValIndexKey(valAddr sdk.ValAddress) []byte { + return append(UnbondingDelegationByValIndexKey, valAddr.Bytes()...) +} + +// gets the prefix for all unbonding delegations from a delegator +func GetUnbondingDelegationTimeKey(timestamp time.Time) []byte { + bz := sdk.FormatTimeBytes(timestamp) + return append(UnbondingQueueKey, bz...) +} + +//________________________________________________________________________________ + +// gets the key for a redelegation +// VALUE: staking/RedelegationKey +func GetREDKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) []byte { + key := make([]byte, 1+sdk.AddrLen*3) + + copy(key[0:sdk.AddrLen+1], GetREDsKey(delAddr.Bytes())) + copy(key[sdk.AddrLen+1:2*sdk.AddrLen+1], valSrcAddr.Bytes()) + copy(key[2*sdk.AddrLen+1:3*sdk.AddrLen+1], valDstAddr.Bytes()) + + return key +} + +// gets the index-key for a redelegation, stored by source-validator-index +// VALUE: none (key rearrangement used) +func GetREDByValSrcIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) []byte { + REDSFromValsSrcKey := GetREDsFromValSrcIndexKey(valSrcAddr) + offset := len(REDSFromValsSrcKey) + + // key is of the form REDSFromValsSrcKey || delAddr || valDstAddr + key := make([]byte, len(REDSFromValsSrcKey)+2*sdk.AddrLen) + copy(key[0:offset], REDSFromValsSrcKey) + copy(key[offset:offset+sdk.AddrLen], delAddr.Bytes()) + copy(key[offset+sdk.AddrLen:offset+2*sdk.AddrLen], valDstAddr.Bytes()) + return key +} + +// gets the index-key for a redelegation, stored by destination-validator-index +// VALUE: none (key rearrangement used) +func GetREDByValDstIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) []byte { + REDSToValsDstKey := GetREDsToValDstIndexKey(valDstAddr) + offset := len(REDSToValsDstKey) + + // key is of the form REDSToValsDstKey || delAddr || valSrcAddr + key := make([]byte, len(REDSToValsDstKey)+2*sdk.AddrLen) + copy(key[0:offset], REDSToValsDstKey) + copy(key[offset:offset+sdk.AddrLen], delAddr.Bytes()) + copy(key[offset+sdk.AddrLen:offset+2*sdk.AddrLen], valSrcAddr.Bytes()) + + return key +} + +// GetREDKeyFromValSrcIndexKey rearranges the ValSrcIndexKey to get the REDKey +func GetREDKeyFromValSrcIndexKey(indexKey []byte) []byte { + // note that first byte is prefix byte + if len(indexKey) != 3*sdk.AddrLen+1 { + panic("unexpected key length") + } + valSrcAddr := indexKey[1 : sdk.AddrLen+1] + delAddr := indexKey[sdk.AddrLen+1 : 2*sdk.AddrLen+1] + valDstAddr := indexKey[2*sdk.AddrLen+1 : 3*sdk.AddrLen+1] + + return GetREDKey(delAddr, valSrcAddr, valDstAddr) +} + +// GetREDKeyFromValDstIndexKey rearranges the ValDstIndexKey to get the REDKey +func GetREDKeyFromValDstIndexKey(indexKey []byte) []byte { + // note that first byte is prefix byte + if len(indexKey) != 3*sdk.AddrLen+1 { + panic("unexpected key length") + } + valDstAddr := indexKey[1 : sdk.AddrLen+1] + delAddr := indexKey[sdk.AddrLen+1 : 2*sdk.AddrLen+1] + valSrcAddr := indexKey[2*sdk.AddrLen+1 : 3*sdk.AddrLen+1] + return GetREDKey(delAddr, valSrcAddr, valDstAddr) +} + +// gets the prefix for all unbonding delegations from a delegator +func GetRedelegationTimeKey(timestamp time.Time) []byte { + bz := sdk.FormatTimeBytes(timestamp) + return append(RedelegationQueueKey, bz...) +} + +//______________ + +// gets the prefix keyspace for redelegations from a delegator +func GetREDsKey(delAddr sdk.AccAddress) []byte { + return append(RedelegationKey, delAddr.Bytes()...) +} + +// gets the prefix keyspace for all redelegations redelegating away from a source validator +func GetREDsFromValSrcIndexKey(valSrcAddr sdk.ValAddress) []byte { + return append(RedelegationByValSrcIndexKey, valSrcAddr.Bytes()...) +} + +// gets the prefix keyspace for all redelegations redelegating towards a destination validator +func GetREDsToValDstIndexKey(valDstAddr sdk.ValAddress) []byte { + return append(RedelegationByValDstIndexKey, valDstAddr.Bytes()...) +} + +// gets the prefix keyspace for all redelegations redelegating towards a destination validator +// from a particular delegator +func GetREDsByDelToValDstIndexKey(delAddr sdk.AccAddress, valDstAddr sdk.ValAddress) []byte { + return append( + GetREDsToValDstIndexKey(valDstAddr), + delAddr.Bytes()...) +} diff --git a/x/staking/types/key_test.go b/x/staking/types/keys_test.go similarity index 95% rename from x/staking/types/key_test.go rename to x/staking/types/keys_test.go index ffe66baf773c..5875c6528120 100644 --- a/x/staking/types/key_test.go +++ b/x/staking/types/keys_test.go @@ -26,10 +26,10 @@ func TestGetValidatorPowerRank(t *testing.T) { val1 := NewValidator(valAddr1, keysPK1, emptyDesc) val1.Tokens = sdk.ZeroInt() val2, val3, val4 := val1, val1, val1 - val2.Tokens = sdk.NewInt(1) - val3.Tokens = sdk.NewInt(10) + val2.Tokens = sdk.TokensFromConsensusPower(1) + val3.Tokens = sdk.TokensFromConsensusPower(10) x := new(big.Int).Exp(big.NewInt(2), big.NewInt(40), big.NewInt(0)) - val4.Tokens = sdk.NewIntFromBigInt(x) + val4.Tokens = sdk.TokensFromConsensusPower(x.Int64()) tests := []struct { validator Validator diff --git a/x/staking/types/validator.go b/x/staking/types/validator.go index f2b1f1777cd4..f91e12e3c440 100644 --- a/x/staking/types/validator.go +++ b/x/staking/types/validator.go @@ -323,7 +323,7 @@ func (d Description) EnsureLength() (Description, sdk.Error) { func (v Validator) ABCIValidatorUpdate() abci.ValidatorUpdate { return abci.ValidatorUpdate{ PubKey: tmtypes.TM2PB.PubKey(v.ConsPubKey), - Power: v.TendermintPower(), + Power: v.ConsensusPower(), } } @@ -509,18 +509,18 @@ func (v Validator) BondedTokens() sdk.Int { return sdk.ZeroInt() } -// get the Tendermint Power +// get the consensus-engine power // a reduction of 10^6 from validator tokens is applied -func (v Validator) TendermintPower() int64 { +func (v Validator) ConsensusPower() int64 { if v.IsBonded() { - return v.PotentialTendermintPower() + return v.PotentialConsensusPower() } return 0 } -// potential Tendermint power -func (v Validator) PotentialTendermintPower() int64 { - return sdk.TokensToTendermintPower(v.Tokens) +// potential consensus-engine power +func (v Validator) PotentialConsensusPower() int64 { + return sdk.TokensToConsensusPower(v.Tokens) } // nolint - for ValidatorI @@ -532,7 +532,7 @@ func (v Validator) GetConsPubKey() crypto.PubKey { return v.ConsPubKey } func (v Validator) GetConsAddr() sdk.ConsAddress { return sdk.ConsAddress(v.ConsPubKey.Address()) } func (v Validator) GetTokens() sdk.Int { return v.Tokens } func (v Validator) GetBondedTokens() sdk.Int { return v.BondedTokens() } -func (v Validator) GetTendermintPower() int64 { return v.TendermintPower() } +func (v Validator) GetConsensusPower() int64 { return v.ConsensusPower() } func (v Validator) GetCommission() sdk.Dec { return v.Commission.Rate } func (v Validator) GetMinSelfDelegation() sdk.Int { return v.MinSelfDelegation } func (v Validator) GetDelegatorShares() sdk.Dec { return v.DelegatorShares }