diff --git a/types/address.go b/types/address.go index 24c4591bf9b2..8683a50fe38a 100644 --- a/types/address.go +++ b/types/address.go @@ -634,7 +634,6 @@ func Bech32ifyPubKey(pkt Bech32PubKeyType, pubkey crypto.PubKey) (string, error) // TM's ed25519 bech32 encoding, we explicitly say to bech32-encode our own // ed25519 the same way as TM's ed25519. // TODO: Remove Bech32ifyPubKey and all usages (cosmos/cosmos-sdk/issues/#7357) - // TODO RZ: check if this is handled by the protobuf any serialization pkToMarshal := pubkey if ed25519Pk, ok := pubkey.(*ed25519.PubKey); ok { pkToMarshal = ed25519Pk.AsTmPubKey() diff --git a/x/distribution/keeper/allocation_test.go b/x/distribution/keeper/allocation_test.go index e243f0ab8119..bbc32a604854 100644 --- a/x/distribution/keeper/allocation_test.go +++ b/x/distribution/keeper/allocation_test.go @@ -20,11 +20,11 @@ func TestAllocateTokensToValidatorWithCommission(t *testing.T) { addrs := simapp.AddTestAddrs(app, ctx, 3, sdk.NewInt(1234)) valAddrs := simapp.ConvertAddrsToValAddrs(addrs) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // create validator with 50% commission tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - tstaking.CreateValidator(t, sdk.ValAddress(addrs[0]), valConsPk1, 100, true) + tstaking.CreateValidator(sdk.ValAddress(addrs[0]), valConsPk1, 100, true) val := app.StakingKeeper.Validator(ctx, valAddrs[0]) // allocate tokens @@ -49,15 +49,15 @@ func TestAllocateTokensToManyValidators(t *testing.T) { addrs := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1234)) valAddrs := simapp.ConvertAddrsToValAddrs(addrs) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // create validator with 50% commission tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - tstaking.CreateValidator(t, valAddrs[0], valConsPk1, 100, true) + tstaking.CreateValidator(valAddrs[0], valConsPk1, 100, true) // create second validator with 0% commission tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDec(0), sdk.NewDec(0), sdk.NewDec(0)) - tstaking.CreateValidator(t, valAddrs[1], valConsPk2, 100, true) + tstaking.CreateValidator(valAddrs[1], valConsPk2, 100, true) abciValA := abci.Validator{ Address: valConsPk1.Address(), @@ -119,19 +119,19 @@ func TestAllocateTokensTruncation(t *testing.T) { addrs := simapp.AddTestAddrs(app, ctx, 3, sdk.NewInt(1234)) valAddrs := simapp.ConvertAddrsToValAddrs(addrs) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // create validator with 10% commission tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0)) - tstaking.CreateValidator(t, valAddrs[0], valConsPk1, 110, true) + tstaking.CreateValidator(valAddrs[0], valConsPk1, 110, true) // create second validator with 10% commission tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0)) - tstaking.CreateValidator(t, valAddrs[1], valConsPk2, 100, true) + tstaking.CreateValidator(valAddrs[1], valConsPk2, 100, true) // create third validator with 10% commission tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0)) - tstaking.CreateValidator(t, valAddrs[2], valConsPk3, 100, true) + tstaking.CreateValidator(valAddrs[2], valConsPk3, 100, true) abciValA := abci.Validator{ Address: valConsPk1.Address(), diff --git a/x/distribution/keeper/delegation_test.go b/x/distribution/keeper/delegation_test.go index c4ad101f7161..92156067e6e4 100644 --- a/x/distribution/keeper/delegation_test.go +++ b/x/distribution/keeper/delegation_test.go @@ -16,14 +16,14 @@ import ( func TestCalculateRewardsBasic(t *testing.T) { app := simapp.Setup(false) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1000)) valAddrs := simapp.ConvertAddrsToValAddrs(addr) // create validator with 50% commission tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - tstaking.CreateValidator(t, valAddrs[0], valConsPk1, 100, true) + tstaking.CreateValidator(valAddrs[0], valConsPk1, 100, true) // end block to bond validator staking.EndBlocker(ctx, app.StakingKeeper) @@ -74,12 +74,12 @@ func TestCalculateRewardsAfterSlash(t *testing.T) { addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(100000000)) valAddrs := simapp.ConvertAddrsToValAddrs(addr) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // create validator with 50% commission tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) valPower := int64(100) - tstaking.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, true) + tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk1, valPower, true) // end block to bond validator staking.EndBlocker(ctx, app.StakingKeeper) @@ -135,14 +135,14 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) { app := simapp.Setup(false) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(100000000)) valAddrs := simapp.ConvertAddrsToValAddrs(addr) // create validator with 50% commission valPower := int64(100) tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - tstaking.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, true) + tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk1, valPower, true) // end block to bond validator staking.EndBlocker(ctx, app.StakingKeeper) @@ -210,13 +210,13 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) { app := simapp.Setup(false) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(100000000)) valAddrs := simapp.ConvertAddrsToValAddrs(addr) // create validator with 50% commission tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - tstaking.CreateValidator(t, valAddrs[0], valConsPk1, 100, true) + tstaking.CreateValidator(valAddrs[0], valConsPk1, 100, true) // end block to bond validator staking.EndBlocker(ctx, app.StakingKeeper) @@ -234,7 +234,7 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) { app.DistrKeeper.AllocateTokensToValidator(ctx, val, tokens) // second delegation - tstaking.Delegate(t, sdk.AccAddress(valAddrs[1]), valAddrs[0], 100) + tstaking.Delegate(sdk.AccAddress(valAddrs[1]), valAddrs[0], 100) del2 := app.StakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[1]), valAddrs[0]) // fetch updated validator @@ -276,7 +276,7 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) { addr := simapp.AddTestAddrs(app, ctx, 1, sdk.NewInt(1000000000)) valAddrs := simapp.ConvertAddrsToValAddrs(addr) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // set module account coins distrAcc := app.DistrKeeper.GetDistributionAccount(ctx) @@ -286,7 +286,7 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) { // create validator with 50% commission power := int64(100) tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - valTokens := tstaking.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, power, true) + valTokens := tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk1, power, true) // assert correct initial balance expTokens := balanceTokens.Sub(valTokens) @@ -345,12 +345,12 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) { addr := simapp.AddTestAddrs(app, ctx, 1, sdk.NewInt(1000000000)) valAddrs := simapp.ConvertAddrsToValAddrs(addr) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // create validator with 50% commission valPower := int64(100) tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - tstaking.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, true) + tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk1, valPower, true) // end block to bond validator staking.EndBlocker(ctx, app.StakingKeeper) @@ -411,14 +411,14 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { app := simapp.Setup(false) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1000000000)) valAddrs := simapp.ConvertAddrsToValAddrs(addr) // create validator with 50% commission tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) valPower := int64(100) - tstaking.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, true) + tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk1, valPower, true) // end block to bond validator staking.EndBlocker(ctx, app.StakingKeeper) @@ -441,7 +441,7 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3) // second delegation - tstaking.DelegateWithPower(t, sdk.AccAddress(valAddrs[1]), valAddrs[0], 100) + tstaking.DelegateWithPower(sdk.AccAddress(valAddrs[1]), valAddrs[0], 100) del2 := app.StakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[1]), valAddrs[0]) @@ -485,7 +485,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { app := simapp.Setup(false) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1000000000)) valAddrs := simapp.ConvertAddrsToValAddrs(addr) initial := int64(20) @@ -500,7 +500,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { // create validator with 50% commission tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - tstaking.CreateValidator(t, valAddrs[0], valConsPk1, 100, true) + tstaking.CreateValidator(valAddrs[0], valConsPk1, 100, true) // end block to bond validator staking.EndBlocker(ctx, app.StakingKeeper) @@ -519,7 +519,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { require.Equal(t, uint64(2), app.DistrKeeper.GetValidatorHistoricalReferenceCount(ctx)) // second delegation - tstaking.Delegate(t, sdk.AccAddress(valAddrs[1]), valAddrs[0], 100) + tstaking.Delegate(sdk.AccAddress(valAddrs[1]), valAddrs[0], 100) // historical count should be 3 (second delegation init) require.Equal(t, uint64(3), app.DistrKeeper.GetValidatorHistoricalReferenceCount(ctx)) diff --git a/x/distribution/keeper/grpc_query_test.go b/x/distribution/keeper/grpc_query_test.go index 76f700ea7e04..313339fa9d68 100644 --- a/x/distribution/keeper/grpc_query_test.go +++ b/x/distribution/keeper/grpc_query_test.go @@ -341,9 +341,9 @@ func (suite *KeeperTestSuite) TestGRPCValidatorSlashes() { func (suite *KeeperTestSuite) TestGRPCDelegationRewards() { app, ctx, addrs, valAddrs := suite.app, suite.ctx, suite.addrs, suite.valAddrs - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(suite.T(), ctx, app.StakingKeeper) tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - tstaking.CreateValidator(suite.T(), valAddrs[0], valConsPk1, 100, true) + tstaking.CreateValidator(valAddrs[0], valConsPk1, 100, true) staking.EndBlocker(ctx, app.StakingKeeper) ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) diff --git a/x/distribution/keeper/querier_test.go b/x/distribution/keeper/querier_test.go index 88e521ad047e..bfa2c1d4cdfc 100644 --- a/x/distribution/keeper/querier_test.go +++ b/x/distribution/keeper/querier_test.go @@ -170,9 +170,9 @@ func TestQueries(t *testing.T) { require.Equal(t, []types.ValidatorSlashEvent{slashOne, slashTwo}, slashes) // test delegation rewards query - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - tstaking.CreateValidator(t, valOpAddr1, valConsPk1, 100, true) + tstaking.CreateValidator(valOpAddr1, valConsPk1, 100, true) staking.EndBlocker(ctx, app.StakingKeeper) diff --git a/x/slashing/abci_test.go b/x/slashing/abci_test.go index c36159407f3f..d9f702780cbe 100644 --- a/x/slashing/abci_test.go +++ b/x/slashing/abci_test.go @@ -24,10 +24,10 @@ func TestBeginBlocker(t *testing.T) { pks := simapp.CreateTestPubKeys(1) simapp.AddTestAddrsFromPubKeys(app, ctx, pks, sdk.TokensFromConsensusPower(200)) addr, pk := sdk.ValAddress(pks[0].Address()), pks[0] - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // bond the validator - amt := tstaking.CreateValidatorWithValPower(t, addr, pk, 100, true) + amt := tstaking.CreateValidatorWithValPower(addr, pk, 100, true) staking.EndBlocker(ctx, app.StakingKeeper) require.Equal( t, app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(addr)), diff --git a/x/slashing/handler_test.go b/x/slashing/handler_test.go index 9d361dde4b08..2c46c327e998 100644 --- a/x/slashing/handler_test.go +++ b/x/slashing/handler_test.go @@ -19,6 +19,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/slashing/types" "github.com/cosmos/cosmos-sdk/x/staking" "github.com/cosmos/cosmos-sdk/x/staking/teststaking" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) func TestCannotUnjailUnlessJailed(t *testing.T) { @@ -28,11 +29,11 @@ func TestCannotUnjailUnlessJailed(t *testing.T) { pks := simapp.CreateTestPubKeys(1) simapp.AddTestAddrsFromPubKeys(app, ctx, pks, sdk.TokensFromConsensusPower(200)) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) slh := slashing.NewHandler(app.SlashingKeeper) addr, val := sdk.ValAddress(pks[0].Address()), pks[0] - amt := tstaking.CreateValidatorWithValPower(t, addr, val, 100, true) + amt := tstaking.CreateValidatorWithValPower(addr, val, 100, true) staking.EndBlocker(ctx, app.StakingKeeper) require.Equal( t, app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(addr)), @@ -54,10 +55,10 @@ func TestCannotUnjailUnlessMeetMinSelfDelegation(t *testing.T) { pks := simapp.CreateTestPubKeys(1) simapp.AddTestAddrsFromPubKeys(app, ctx, pks, sdk.TokensFromConsensusPower(200)) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) slh := slashing.NewHandler(app.SlashingKeeper) addr, val := sdk.ValAddress(pks[0].Address()), pks[0] - amt := tstaking.CreateValidatorWithValPower(t, addr, val, 100, true) + amt := tstaking.CreateValidatorWithValPower(addr, val, 100, true) staking.EndBlocker(ctx, app.StakingKeeper) require.Equal( @@ -66,7 +67,7 @@ func TestCannotUnjailUnlessMeetMinSelfDelegation(t *testing.T) { ) tstaking.Denom = app.StakingKeeper.GetParams(ctx).BondDenom - tstaking.Undelegate(t, sdk.AccAddress(addr), addr, sdk.OneInt(), true) + tstaking.Undelegate(sdk.AccAddress(addr), addr, sdk.OneInt(), true) require.True(t, app.StakingKeeper.Validator(ctx, addr).IsJailed()) // assert non-jailed validator can't be unjailed @@ -85,12 +86,12 @@ func TestJailedValidatorDelegations(t *testing.T) { simapp.AddTestAddrsFromPubKeys(app, ctx, pks, sdk.TokensFromConsensusPower(20)) app.SlashingKeeper.SetParams(ctx, keeper.TestParams()) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) stakingParams := app.StakingKeeper.GetParams(ctx) app.StakingKeeper.SetParams(ctx, stakingParams) valAddr, consAddr := sdk.ValAddress(pks[1].Address()), sdk.ConsAddress(pks[0].Address()) - amt := tstaking.CreateValidatorWithValPower(t, valAddr, pks[1], 10, true) + amt := tstaking.CreateValidatorWithValPower(valAddr, pks[1], 10, true) staking.EndBlocker(ctx, app.StakingKeeper) // set dummy signing info @@ -99,11 +100,11 @@ func TestJailedValidatorDelegations(t *testing.T) { // delegate tokens to the validator delAddr := sdk.AccAddress(pks[2].Address()) - tstaking.Delegate(t, delAddr, valAddr, amt.Int64()) + tstaking.Delegate(delAddr, valAddr, amt.Int64()) // unbond validator total self-delegations (which should jail the validator) valAcc := sdk.AccAddress(valAddr) - tstaking.Undelegate(t, valAcc, valAddr, amt, true) + tstaking.Undelegate(valAcc, valAddr, amt, true) _, err := app.StakingKeeper.CompleteUnbonding(ctx, sdk.AccAddress(valAddr), valAddr) require.Nil(t, err, "expected complete unbonding validator to be ok, got: %v", err) @@ -118,7 +119,7 @@ func TestJailedValidatorDelegations(t *testing.T) { require.Nil(t, res) // self-delegate to validator - tstaking.Delegate(t, valAcc, valAddr, amt.Int64()) + tstaking.Delegate(valAcc, valAddr, amt.Int64()) // verify the validator can now unjail itself res, err = slashing.NewHandler(app.SlashingKeeper)(ctx, types.NewMsgUnjail(valAddr)) @@ -149,9 +150,9 @@ func TestHandleAbsentValidator(t *testing.T) { power := int64(100) addr, val := sdk.ValAddress(pks[0].Address()), pks[0] slh := slashing.NewHandler(app.SlashingKeeper) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) - amt := tstaking.CreateValidatorWithValPower(t, addr, val, power, true) + amt := tstaking.CreateValidatorWithValPower(addr, val, power, true) staking.EndBlocker(ctx, app.StakingKeeper) require.Equal( diff --git a/x/slashing/keeper/keeper_test.go b/x/slashing/keeper/keeper_test.go index 9ed606483ba2..77e5cd7591cb 100644 --- a/x/slashing/keeper/keeper_test.go +++ b/x/slashing/keeper/keeper_test.go @@ -12,6 +12,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/slashing/keeper" "github.com/cosmos/cosmos-sdk/x/staking" "github.com/cosmos/cosmos-sdk/x/staking/teststaking" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) func TestUnJailNotBonded(t *testing.T) { @@ -25,12 +26,12 @@ func TestUnJailNotBonded(t *testing.T) { addrDels := simapp.AddTestAddrsIncremental(app, ctx, 6, sdk.TokensFromConsensusPower(200)) valAddrs := simapp.ConvertAddrsToValAddrs(addrDels) pks := simapp.CreateTestPubKeys(6) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // create max (5) validators all with the same power for i := uint32(0); i < p.MaxValidators; i++ { addr, val := valAddrs[i], pks[i] - tstaking.CreateValidatorWithValPower(t, addr, val, 100, true) + tstaking.CreateValidatorWithValPower(addr, val, 100, true) } staking.EndBlocker(ctx, app.StakingKeeper) @@ -39,29 +40,24 @@ func TestUnJailNotBonded(t *testing.T) { // create a 6th validator with less power than the cliff validator (won't be bonded) addr, val := valAddrs[5], pks[5] amt := sdk.TokensFromConsensusPower(50) - msg := tstaking.CreateValidatorMsg(t, addr, val, amt.Int64()) + msg := tstaking.CreateValidatorMsg(addr, val, amt.Int64()) msg.MinSelfDelegation = amt - tstaking.Handle(t, msg, true) + tstaking.Handle(msg, true) staking.EndBlocker(ctx, app.StakingKeeper) ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) - validator, ok := app.StakingKeeper.GetValidator(ctx, addr) - require.True(t, ok) - require.False(t, validator.Jailed) - require.Equal(t, stakingtypes.BondStatusUnbonded, validator.GetStatus().String()) + tstaking.CheckValidator(addr, stakingtypes.BondStatusUnbonded, false) // unbond below minimum self-delegation require.Equal(t, p.BondDenom, tstaking.Denom) - tstaking.Undelegate(t, sdk.AccAddress(addr), addr, sdk.TokensFromConsensusPower(1), true) + tstaking.Undelegate(sdk.AccAddress(addr), addr, sdk.TokensFromConsensusPower(1), true) staking.EndBlocker(ctx, app.StakingKeeper) ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) // verify that validator is jailed - validator, ok = app.StakingKeeper.GetValidator(ctx, addr) - require.True(t, ok) - require.True(t, validator.Jailed) + tstaking.CheckValidator(addr, "", true) // verify we cannot unjail (yet) require.Error(t, app.SlashingKeeper.Unjail(ctx, addr)) @@ -69,7 +65,7 @@ func TestUnJailNotBonded(t *testing.T) { staking.EndBlocker(ctx, app.StakingKeeper) ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) // bond to meet minimum self-delegation - tstaking.DelegateWithPower(t, sdk.AccAddress(addr), addr, 1) + tstaking.DelegateWithPower(sdk.AccAddress(addr), addr, 1) staking.EndBlocker(ctx, app.StakingKeeper) ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) @@ -77,9 +73,7 @@ func TestUnJailNotBonded(t *testing.T) { // verify we can immediately unjail require.NoError(t, app.SlashingKeeper.Unjail(ctx, addr)) - validator, ok = app.StakingKeeper.GetValidator(ctx, addr) - require.True(t, ok) - require.False(t, validator.Jailed) + tstaking.CheckValidator(addr, "", false) } // Test a new validator entering the validator set @@ -93,11 +87,11 @@ func TestHandleNewValidator(t *testing.T) { valAddrs := simapp.ConvertAddrsToValAddrs(addrDels) pks := simapp.CreateTestPubKeys(1) addr, val := valAddrs[0], pks[0] - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) ctx = ctx.WithBlockHeight(app.SlashingKeeper.SignedBlocksWindow(ctx) + 1) // Validator created - amt := tstaking.CreateValidatorWithValPower(t, addr, val, 100, true) + amt := tstaking.CreateValidatorWithValPower(addr, val, 100, true) staking.EndBlocker(ctx, app.StakingKeeper) require.Equal( @@ -138,9 +132,9 @@ func TestHandleAlreadyJailed(t *testing.T) { pks := simapp.CreateTestPubKeys(1) addr, val := valAddrs[0], pks[0] power := int64(100) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) - amt := tstaking.CreateValidatorWithValPower(t, addr, val, power, true) + amt := tstaking.CreateValidatorWithValPower(addr, val, power, true) staking.EndBlocker(ctx, app.StakingKeeper) @@ -198,9 +192,10 @@ func TestValidatorDippingInAndOut(t *testing.T) { addr, val := pks[0].Address(), pks[0] consAddr := sdk.ConsAddress(addr) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) + valAddr := sdk.ValAddress(addr) - tstaking.CreateValidatorWithValPower(t, sdk.ValAddress(addr), val, power, true) + tstaking.CreateValidatorWithValPower(valAddr, val, power, true) staking.EndBlocker(ctx, app.StakingKeeper) // 100 first blocks OK @@ -211,24 +206,21 @@ func TestValidatorDippingInAndOut(t *testing.T) { } // kick first validator out of validator set - tstaking.CreateValidatorWithValPower(t, sdk.ValAddress(pks[1].Address()), pks[1], 101, true) - + tstaking.CreateValidatorWithValPower(sdk.ValAddress(pks[1].Address()), pks[1], 101, true) validatorUpdates := staking.EndBlocker(ctx, app.StakingKeeper) require.Equal(t, 2, len(validatorUpdates)) - validator, _ := app.StakingKeeper.GetValidator(ctx, sdk.ValAddress(addr)) - require.Equal(t, stakingtypes.Unbonding, validator.Status) + tstaking.CheckValidator(valAddr, stakingtypes.Unbonding, false) // 600 more blocks happened height = int64(700) ctx = ctx.WithBlockHeight(height) // validator added back in - tstaking.DelegateWithPower(t, sdk.AccAddress(pks[2].Address()), sdk.ValAddress(pks[0].Address()), 50) + tstaking.DelegateWithPower(sdk.AccAddress(pks[2].Address()), sdk.ValAddress(pks[0].Address()), 50) validatorUpdates = staking.EndBlocker(ctx, app.StakingKeeper) require.Equal(t, 2, len(validatorUpdates)) - validator, _ = app.StakingKeeper.GetValidator(ctx, sdk.ValAddress(addr)) - require.Equal(t, stakingtypes.Bonded, validator.Status) + tstaking.CheckValidator(valAddr, stakingtypes.Bonded, false) newPower := int64(150) // validator misses a block @@ -236,8 +228,7 @@ func TestValidatorDippingInAndOut(t *testing.T) { height++ // shouldn't be jailed/kicked yet - validator, _ = app.StakingKeeper.GetValidator(ctx, sdk.ValAddress(addr)) - require.Equal(t, stakingtypes.Bonded, validator.Status) + tstaking.CheckValidator(valAddr, stakingtypes.Bonded, false) // validator misses 500 more blocks, 501 total latest := height @@ -248,8 +239,7 @@ func TestValidatorDippingInAndOut(t *testing.T) { // should now be jailed & kicked staking.EndBlocker(ctx, app.StakingKeeper) - validator, _ = app.StakingKeeper.GetValidator(ctx, sdk.ValAddress(addr)) - require.Equal(t, stakingtypes.Unbonding, validator.Status) + tstaking.CheckValidator(valAddr, stakingtypes.Unbonding, true) // check all the signing information signInfo, found := app.SlashingKeeper.GetValidatorSigningInfo(ctx, consAddr) @@ -273,8 +263,7 @@ func TestValidatorDippingInAndOut(t *testing.T) { // validator should not be kicked since we reset counter/array when it was jailed staking.EndBlocker(ctx, app.StakingKeeper) - validator, _ = app.StakingKeeper.GetValidator(ctx, sdk.ValAddress(addr)) - require.Equal(t, stakingtypes.Bonded, validator.Status) + tstaking.CheckValidator(valAddr, stakingtypes.Bonded, false) // validator misses 501 blocks latest = height @@ -285,6 +274,5 @@ func TestValidatorDippingInAndOut(t *testing.T) { // validator should now be jailed & kicked staking.EndBlocker(ctx, app.StakingKeeper) - validator, _ = app.StakingKeeper.GetValidator(ctx, sdk.ValAddress(addr)) - require.Equal(t, stakingtypes.Unbonding, validator.Status) + tstaking.CheckValidator(valAddr, stakingtypes.Bonded, true) } diff --git a/x/staking/common_test.go b/x/staking/common_test.go index 2879e5fcd6d3..30072c7b58ef 100644 --- a/x/staking/common_test.go +++ b/x/staking/common_test.go @@ -27,18 +27,6 @@ var ( PKs = simapp.CreateTestPubKeys(500) ) -// TODO:RZ -// func NewTestMsgCreateValidator(address sdk.ValAddress, pubKey crypto.PubKey, amt sdk.Int) *types.MsgCreateValidator { -// return types.NewMsgCreateValidator( -// address, pubKey, sdk.NewCoin(sdk.DefaultBondDenom, amt), types.Description{}, commissionRates, sdk.OneInt(), -// ) -// } - -// func NewTestMsgDelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amt sdk.Int) *types.MsgDelegate { -// amount := sdk.NewCoin(sdk.DefaultBondDenom, amt) -// return types.NewMsgDelegate(delAddr, valAddr, amount) -// } - // getBaseSimappWithCustomKeeper Returns a simapp with custom StakingKeeper // to avoid messing with the hooks. func getBaseSimappWithCustomKeeper() (*codec.LegacyAmino, *simapp.SimApp, sdk.Context) { diff --git a/x/staking/handler_test.go b/x/staking/handler_test.go index 74b2d19e010d..bec4e2da7c2f 100644 --- a/x/staking/handler_test.go +++ b/x/staking/handler_test.go @@ -46,10 +46,10 @@ func TestValidatorByPowerIndex(t *testing.T) { initPower := int64(1000000) app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 10, 10000000000000) validatorAddr, validatorAddr3 := valAddrs[0], valAddrs[1] - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // create validator - initBond := tstaking.CreateValidatorWithValPower(t, validatorAddr, PKs[0], initPower, true) + initBond := tstaking.CreateValidatorWithValPower(validatorAddr, PKs[0], initPower, true) // must end-block updates := app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) @@ -68,7 +68,7 @@ func TestValidatorByPowerIndex(t *testing.T) { require.True(t, keeper.ValidatorByPowerIndexExists(ctx, app.StakingKeeper, power)) // create a second validator keep it bonded - tstaking.CreateValidatorWithValPower(t, validatorAddr3, PKs[2], initPower, true) + tstaking.CreateValidatorWithValPower(validatorAddr3, PKs[2], initPower, true) // must end-block updates = app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) @@ -101,7 +101,7 @@ func TestValidatorByPowerIndex(t *testing.T) { // unbond self-delegation totalBond := validator.TokensFromShares(bond.GetShares()).TruncateInt() - res := tstaking.Undelegate(t, sdk.AccAddress(validatorAddr), validatorAddr, totalBond.Int64()) + res := tstaking.Undelegate(sdk.AccAddress(validatorAddr), validatorAddr, totalBond, true) ts := &gogotypes.Timestamp{} types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(res.Data, ts) @@ -109,6 +109,7 @@ func TestValidatorByPowerIndex(t *testing.T) { require.NoError(t, err) ctx = ctx.WithBlockTime(finishTime) + tstaking.Ctx = ctx staking.EndBlocker(ctx, app.StakingKeeper) staking.EndBlocker(ctx, app.StakingKeeper) @@ -124,14 +125,12 @@ func TestDuplicatesMsgCreateValidator(t *testing.T) { addr1, addr2 := valAddrs[0], valAddrs[1] pk1, pk2 := PKs[0], PKs[1] - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) - valTokens := tstaking.CreateValidatorWithValPower(t, addr1, pk1, 10, true) + valTokens := tstaking.CreateValidatorWithValPower(addr1, pk1, 10, true) app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) - validator, found := app.StakingKeeper.GetValidator(ctx, addr1) - require.True(t, found) - assert.Equal(t, types.Bonded, validator.Status) + validator := tstaking.CheckValidator(addr1, types.Bonded, false) assert.Equal(t, addr1.String(), validator.OperatorAddress) assert.Equal(t, pk1.(cryptotypes.IntoTmPubKey).AsTmPubKey(), validator.GetConsPubKey()) assert.Equal(t, valTokens, validator.BondedTokens()) @@ -139,22 +138,19 @@ func TestDuplicatesMsgCreateValidator(t *testing.T) { assert.Equal(t, types.Description{}, validator.Description) // two validators can't have the same operator address - tstaking.CreateValidator(t, addr1, pk2, valTokens.Int64(), false) + tstaking.CreateValidator(addr1, pk2, valTokens.Int64(), false) // two validators can't have the same pubkey - tstaking.CreateValidator(t, addr2, pk1, valTokens.Int64(), false) + tstaking.CreateValidator(addr2, pk1, valTokens.Int64(), false) // must have different pubkey and operator - tstaking.CreateValidator(t, addr2, pk2, valTokens.Int64(), true) + tstaking.CreateValidator(addr2, pk2, valTokens.Int64(), true) // must end-block updates := app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) require.Equal(t, 1, len(updates)) - validator, found = app.StakingKeeper.GetValidator(ctx, addr2) - - require.True(t, found) - assert.Equal(t, types.Bonded, validator.Status) + validator = tstaking.CheckValidator(addr2, types.Bonded, false) assert.Equal(t, addr2.String(), validator.OperatorAddress) assert.Equal(t, pk2.(cryptotypes.IntoTmPubKey).AsTmPubKey(), validator.GetConsPubKey()) assert.True(sdk.IntEq(t, valTokens, validator.Tokens)) @@ -170,45 +166,42 @@ func TestInvalidPubKeyTypeMsgCreateValidator(t *testing.T) { addr := valAddrs[0] invalidPk := secp256k1.GenPrivKey().PubKey() - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // invalid pukKey type should not be allowed - tstaking.CreateValidator(t, addr, invalidPk, 10, false) + tstaking.CreateValidator(addr, invalidPk, 10, false) } func TestLegacyValidatorDelegations(t *testing.T) { app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 2, 100000000) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) valAddr := valAddrs[0] valConsPubKey, valConsAddr := PKs[0], sdk.ConsAddress(PKs[0].Address()) delAddr := delAddrs[1] // create validator - bondAmount := tstaking.CreateValidatorWithValPower(t, valAddr, valConsPubKey, 10, true) + bondAmount := tstaking.CreateValidatorWithValPower(valAddr, valConsPubKey, 10, true) // must end-block updates := app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) require.Equal(t, 1, len(updates)) // verify the validator exists and has the correct attributes - validator, found := app.StakingKeeper.GetValidator(ctx, valAddr) - require.True(t, found) - require.Equal(t, types.Bonded, validator.Status) + validator := tstaking.CheckValidator(valAddr, types.Bonded, false) require.Equal(t, bondAmount, validator.DelegatorShares.RoundInt()) require.Equal(t, bondAmount, validator.BondedTokens()) // delegate tokens to the validator - tstaking.Delegate(t, delAddr, valAddr, bondAmount.Int64()) + tstaking.Delegate(delAddr, valAddr, bondAmount.Int64()) // verify validator bonded shares - validator, found = app.StakingKeeper.GetValidator(ctx, valAddr) - require.True(t, found) + validator = tstaking.CheckValidator(valAddr, types.Bonded, false) require.Equal(t, bondAmount.MulRaw(2), validator.DelegatorShares.RoundInt()) require.Equal(t, bondAmount.MulRaw(2), validator.BondedTokens()) // unbond validator total self-delegations (which should jail the validator) - res := tstaking.Undelegate(t, sdk.AccAddress(valAddr), valAddr, bondAmount.Int64()) + res := tstaking.Undelegate(sdk.AccAddress(valAddr), valAddr, bondAmount, true) ts := &gogotypes.Timestamp{} types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(res.Data, ts) @@ -217,12 +210,11 @@ func TestLegacyValidatorDelegations(t *testing.T) { require.NoError(t, err) ctx = ctx.WithBlockTime(finishTime) + tstaking.Ctx = ctx staking.EndBlocker(ctx, app.StakingKeeper) // verify the validator record still exists, is jailed, and has correct tokens - validator, found = app.StakingKeeper.GetValidator(ctx, valAddr) - require.True(t, found) - require.True(t, validator.Jailed) + validator = tstaking.CheckValidator(valAddr, -1, true) require.Equal(t, bondAmount, validator.Tokens) // verify delegation still exists @@ -232,7 +224,7 @@ func TestLegacyValidatorDelegations(t *testing.T) { require.Equal(t, bondAmount, validator.DelegatorShares.RoundInt()) // verify the validator can still self-delegate - tstaking.Delegate(t, sdk.AccAddress(valAddr), valAddr, bondAmount.Int64()) + tstaking.Delegate(sdk.AccAddress(valAddr), valAddr, bondAmount.Int64()) // verify validator bonded shares validator, found = app.StakingKeeper.GetValidator(ctx, valAddr) @@ -244,7 +236,7 @@ func TestLegacyValidatorDelegations(t *testing.T) { app.StakingKeeper.Unjail(ctx, valConsAddr) // verify the validator can now accept delegations - tstaking.Delegate(t, delAddr, valAddr, bondAmount.Int64()) + tstaking.Delegate(delAddr, valAddr, bondAmount.Int64()) // verify validator bonded shares validator, found = app.StakingKeeper.GetValidator(ctx, valAddr) @@ -266,22 +258,19 @@ func TestIncrementsMsgDelegate(t *testing.T) { params := app.StakingKeeper.GetParams(ctx) validatorAddr, delegatorAddr := valAddrs[0], delAddrs[1] - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // first create validator - bondAmount := tstaking.CreateValidatorWithValPower(t, validatorAddr, PKs[0], 10, true) + bondAmount := tstaking.CreateValidatorWithValPower(validatorAddr, PKs[0], 10, true) // apply TM updates app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) - validator, found := app.StakingKeeper.GetValidator(ctx, validatorAddr) - require.True(t, found) - require.Equal(t, types.Bonded, validator.Status) + validator := tstaking.CheckValidator(validatorAddr, types.Bonded, false) require.Equal(t, bondAmount, validator.DelegatorShares.RoundInt()) require.Equal(t, bondAmount, validator.BondedTokens(), "validator: %v", validator) - _, found = app.StakingKeeper.GetDelegation(ctx, delegatorAddr, validatorAddr) - require.False(t, found) + tstaking.CheckDelegator(delegatorAddr, validatorAddr) bond, found := app.StakingKeeper.GetDelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr) require.True(t, found) @@ -291,12 +280,12 @@ func TestIncrementsMsgDelegate(t *testing.T) { require.Equal(t, bondAmount.Int64(), bondedTokens.Int64()) // just send the same msgbond multiple times - tstaking.Delegate(t, delegatorAddr, validatorAddr, bondAmount.Int64()) + tstaking.Delegate(delegatorAddr, validatorAddr, bondAmount.Int64()) for i := int64(0); i < 5; i++ { ctx = ctx.WithBlockHeight(i) tstaking.Ctx = ctx - tstaking.Delegate(t, delegatorAddr, validatorAddr, bondAmount.Int64()) + tstaking.Delegate(delegatorAddr, validatorAddr, bondAmount.Int64()) //Check that the accounts and the bond account have the appropriate values validator, found := app.StakingKeeper.GetValidator(ctx, validatorAddr) @@ -330,12 +319,12 @@ func TestEditValidatorDecreaseMinSelfDelegation(t *testing.T) { app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 1, 1000000000) validatorAddr := valAddrs[0] - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // create validator - msgCreateValidator := tstaking.CreateValidatorMsg(t, validatorAddr, PKs[0], initBond.Int64()) + msgCreateValidator := tstaking.CreateValidatorMsg(validatorAddr, PKs[0], initBond.Int64()) msgCreateValidator.MinSelfDelegation = sdk.NewInt(2) - tstaking.Handle(t, msgCreateValidator, true) + tstaking.Handle(msgCreateValidator, true) // must end-block updates := app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) @@ -351,7 +340,7 @@ func TestEditValidatorDecreaseMinSelfDelegation(t *testing.T) { newMinSelfDelegation := sdk.OneInt() msgEditValidator := types.NewMsgEditValidator(validatorAddr, types.Description{}, nil, &newMinSelfDelegation) - tstaking.Handle(t, msgEditValidator, false) + tstaking.Handle(msgEditValidator, false) } func TestEditValidatorIncreaseMinSelfDelegationBeyondCurrentBond(t *testing.T) { @@ -360,12 +349,12 @@ func TestEditValidatorIncreaseMinSelfDelegationBeyondCurrentBond(t *testing.T) { app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 2, 1000000000) validatorAddr := valAddrs[0] - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // create validator - msgCreateValidator := tstaking.CreateValidatorMsg(t, validatorAddr, PKs[0], initBond.Int64()) + msgCreateValidator := tstaking.CreateValidatorMsg(validatorAddr, PKs[0], initBond.Int64()) msgCreateValidator.MinSelfDelegation = sdk.NewInt(2) - tstaking.Handle(t, msgCreateValidator, true) + tstaking.Handle(msgCreateValidator, true) // must end-block updates := app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) @@ -381,25 +370,25 @@ func TestEditValidatorIncreaseMinSelfDelegationBeyondCurrentBond(t *testing.T) { newMinSelfDelegation := initBond.Add(sdk.OneInt()) msgEditValidator := types.NewMsgEditValidator(validatorAddr, types.Description{}, nil, &newMinSelfDelegation) - tstaking.Handle(t, msgEditValidator, false) + tstaking.Handle(msgEditValidator, false) } func TestIncrementsMsgUnbond(t *testing.T) { initPower := int64(1000) app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 2, 1000000000) - tstaking := teststaking.NewService(ctx, app.StakingKeeper) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) params := app.StakingKeeper.GetParams(ctx) denom := params.BondDenom // create validator, delegate validatorAddr, delegatorAddr := valAddrs[0], delAddrs[1] - initBond := tstaking.CreateValidatorWithValPower(t, validatorAddr, PKs[0], initPower, true) + initBond := tstaking.CreateValidatorWithValPower(validatorAddr, PKs[0], initPower, true) // initial balance amt1 := app.BankKeeper.GetBalance(ctx, delegatorAddr, denom).Amount - tstaking.Delegate(t, delegatorAddr, validatorAddr, initBond.Int64()) + tstaking.Delegate(delegatorAddr, validatorAddr, initBond.Int64()) // balance should have been subtracted after delegation amt2 := app.BankKeeper.GetBalance(ctx, delegatorAddr, denom).Amount @@ -420,7 +409,7 @@ func TestIncrementsMsgUnbond(t *testing.T) { numUnbonds := int64(5) for i := int64(0); i < numUnbonds; i++ { - tstaking.Handle(t, msgUndelegate, true) + res := tstaking.Handle(msgUndelegate, true) ts := &gogotypes.Timestamp{} types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(res.Data, ts) @@ -428,6 +417,7 @@ func TestIncrementsMsgUnbond(t *testing.T) { require.NoError(t, err) ctx = ctx.WithBlockTime(finishTime) + tstaking.Ctx = ctx staking.EndBlocker(ctx, app.StakingKeeper) // check that the accounts and the bond account have the appropriate values @@ -465,17 +455,12 @@ func TestIncrementsMsgUnbond(t *testing.T) { } for _, c := range errorCases { - tstaking.Undelegate(t, delegatorAddr, validatorAddr, c, false) + tstaking.Undelegate(delegatorAddr, validatorAddr, c, false) } - leftBonded := initBond.Sub(unbondAmt.Amount.Mul(sdk.NewInt(numUnbonds))) - // should be able to unbond remaining - unbondAmt = sdk.NewCoin(sdk.DefaultBondDenom, leftBonded) - msgUndelegate = types.NewMsgUndelegate(delegatorAddr, validatorAddr, unbondAmt) - res, err = handler(ctx, msgUndelegate) - require.NoError(t, err, "msgUnbond: %v\nshares: %s\nleftBonded: %s\n", msgUndelegate, unbondAmt, leftBonded) - require.NotNil(t, res, "msgUnbond: %v\nshares: %s\nleftBonded: %s\n", msgUndelegate, unbondAmt, leftBonded) + leftBonded := initBond.Sub(unbondAmt.Amount.Mul(sdk.NewInt(numUnbonds))) + tstaking.Undelegate(delegatorAddr, validatorAddr, leftBonded, true) } func TestMultipleMsgCreateValidator(t *testing.T) { @@ -483,11 +468,10 @@ func TestMultipleMsgCreateValidator(t *testing.T) { initTokens := sdk.TokensFromConsensusPower(initPower) app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 3, 1000000000) - handler := staking.NewHandler(app.StakingKeeper) - params := app.StakingKeeper.GetParams(ctx) blockTime := time.Now().UTC() ctx = ctx.WithBlockTime(blockTime) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) validatorAddrs := []sdk.ValAddress{ valAddrs[0], @@ -501,24 +485,19 @@ func TestMultipleMsgCreateValidator(t *testing.T) { } // bond them all + amt := sdk.TokensFromConsensusPower(10) for i, validatorAddr := range validatorAddrs { - valTokens := sdk.TokensFromConsensusPower(10) - msgCreateValidatorOnBehalfOf := NewTestMsgCreateValidator(validatorAddr, PKs[i], valTokens) - - res, err := handler(ctx, msgCreateValidatorOnBehalfOf) - require.NoError(t, err) - require.NotNil(t, res) - + tstaking.CreateValidator(validatorAddr, PKs[i], amt.Int64(), true) // verify that the account is bonded validators := app.StakingKeeper.GetValidators(ctx, 100) require.Equal(t, (i + 1), len(validators)) val := validators[i] - balanceExpd := initTokens.Sub(valTokens) + balanceExpd := initTokens.Sub(amt) balanceGot := app.BankKeeper.GetBalance(ctx, delegatorAddrs[i], params.BondDenom).Amount require.Equal(t, i+1, len(validators), "expected %d validators got %d, validators: %v", i+1, len(validators), validators) - require.Equal(t, valTokens, val.DelegatorShares.RoundInt(), "expected %d shares, got %d", 10, val.DelegatorShares) + require.Equal(t, amt, val.DelegatorShares.RoundInt(), "expected %d shares, got %d", 10, val.DelegatorShares) require.Equal(t, balanceExpd, balanceGot, "expected account to have %d, got %d", balanceExpd, balanceGot) } @@ -529,16 +508,12 @@ func TestMultipleMsgCreateValidator(t *testing.T) { _, found := app.StakingKeeper.GetValidator(ctx, validatorAddr) require.True(t, found) - unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(10)) - msgUndelegate := types.NewMsgUndelegate(delegatorAddrs[i], validatorAddr, unbondAmt) // remove delegation - res, err := handler(ctx, msgUndelegate) - require.NoError(t, err) - require.NotNil(t, res) + res := tstaking.Undelegate(delegatorAddrs[i], validatorAddr, amt, true) ts := &gogotypes.Timestamp{} types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(res.Data, ts) - _, err = gogotypes.TimestampFromProto(ts) + _, err := gogotypes.TimestampFromProto(ts) require.NoError(t, err) // adds validator into unbonding queue @@ -562,45 +537,31 @@ func TestMultipleMsgCreateValidator(t *testing.T) { func TestMultipleMsgDelegate(t *testing.T) { app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 50, 1000000000) - handler := staking.NewHandler(app.StakingKeeper) validatorAddr, delegatorAddrs := valAddrs[0], delAddrs[1:] + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) + var amount int64 = 10 // first make a validator - msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, PKs[0], sdk.NewInt(10)) - res, err := handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.CreateValidator(validatorAddr, PKs[0], amount, true) // delegate multiple parties for _, delegatorAddr := range delegatorAddrs { - msgDelegate := NewTestMsgDelegate(delegatorAddr, validatorAddr, sdk.NewInt(10)) - res, err := handler(ctx, msgDelegate) - require.NoError(t, err) - require.NotNil(t, res) - - // check that the account is bonded - bond, found := app.StakingKeeper.GetDelegation(ctx, delegatorAddr, validatorAddr) - require.True(t, found) - require.NotNil(t, bond, "expected delegatee bond %d to exist", bond) + tstaking.Delegate(delegatorAddr, validatorAddr, 10) + tstaking.CheckDelegator(delegatorAddr, validatorAddr) } // unbond them all for _, delegatorAddr := range delegatorAddrs { - unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10)) - msgUndelegate := types.NewMsgUndelegate(delegatorAddr, validatorAddr, unbondAmt) - - res, err := handler(ctx, msgUndelegate) - require.NoError(t, err) - require.NotNil(t, res) + res := tstaking.Undelegate(delegatorAddr, validatorAddr, sdk.NewInt(amount), true) ts := &gogotypes.Timestamp{} types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(res.Data, ts) - finishTime, err := gogotypes.TimestampFromProto(ts) require.NoError(t, err) ctx = ctx.WithBlockTime(finishTime) staking.EndBlocker(ctx, app.StakingKeeper) + tstaking.Ctx = ctx // check that the account is unbonded _, found := app.StakingKeeper.GetDelegation(ctx, delegatorAddr, validatorAddr) @@ -610,28 +571,17 @@ func TestMultipleMsgDelegate(t *testing.T) { func TestJailValidator(t *testing.T) { app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 2, 1000000000) - handler := staking.NewHandler(app.StakingKeeper) validatorAddr, delegatorAddr := valAddrs[0], delAddrs[1] + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) + var amt int64 = 10 - // create the validator - msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, PKs[0], sdk.NewInt(10)) - res, err := handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) - - // bond a delegator - msgDelegate := NewTestMsgDelegate(delegatorAddr, validatorAddr, sdk.NewInt(10)) - res, err = handler(ctx, msgDelegate) - require.NoError(t, err) - require.NotNil(t, res) + // create the validator and delegate + tstaking.CreateValidator(validatorAddr, PKs[0], amt, true) + tstaking.Delegate(delegatorAddr, validatorAddr, amt) // unbond the validators bond portion - unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10)) - msgUndelegateValidator := types.NewMsgUndelegate(sdk.AccAddress(validatorAddr), validatorAddr, unbondAmt) - res, err = handler(ctx, msgUndelegateValidator) - require.NoError(t, err) - require.NotNil(t, res) - + unamt := sdk.NewInt(amt) + res := tstaking.Undelegate(sdk.AccAddress(validatorAddr), validatorAddr, unamt, true) ts := &gogotypes.Timestamp{} types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(res.Data, ts) @@ -640,18 +590,12 @@ func TestJailValidator(t *testing.T) { ctx = ctx.WithBlockTime(finishTime) staking.EndBlocker(ctx, app.StakingKeeper) + tstaking.Ctx = ctx - validator, found := app.StakingKeeper.GetValidator(ctx, validatorAddr) - require.True(t, found) - require.True(t, validator.Jailed, "%v", validator) + tstaking.CheckValidator(validatorAddr, "", true) // test that the delegator can still withdraw their bonds - msgUndelegateDelegator := types.NewMsgUndelegate(delegatorAddr, validatorAddr, unbondAmt) - - res, err = handler(ctx, msgUndelegateDelegator) - require.NoError(t, err) - require.NotNil(t, res) - + tstaking.Undelegate(delegatorAddr, validatorAddr, unamt, true) ts = &gogotypes.Timestamp{} types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(res.Data, ts) @@ -660,55 +604,36 @@ func TestJailValidator(t *testing.T) { ctx = ctx.WithBlockTime(finishTime) staking.EndBlocker(ctx, app.StakingKeeper) + tstaking.Ctx = ctx // verify that the pubkey can now be reused - res, err = handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.CreateValidator(validatorAddr, PKs[0], amt, true) } func TestValidatorQueue(t *testing.T) { app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 2, 1000000000) - handler := staking.NewHandler(app.StakingKeeper) validatorAddr, delegatorAddr := valAddrs[0], delAddrs[1] + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // set the unbonding time params := app.StakingKeeper.GetParams(ctx) params.UnbondingTime = 7 * time.Second app.StakingKeeper.SetParams(ctx, params) - // create the validator - valTokens := sdk.TokensFromConsensusPower(10) - msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, PKs[0], valTokens) - res, err := handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) - - // bond a delegator - delTokens := sdk.TokensFromConsensusPower(10) - msgDelegate := NewTestMsgDelegate(delegatorAddr, validatorAddr, delTokens) - res, err = handler(ctx, msgDelegate) - require.NoError(t, err) - require.NotNil(t, res) - + // create the validator and make a bond + amt := tstaking.CreateValidatorWithValPower(validatorAddr, PKs[0], 10, true) + tstaking.Delegate(delegatorAddr, validatorAddr, amt.Int64()) staking.EndBlocker(ctx, app.StakingKeeper) // unbond the all self-delegation to put validator in unbonding state - unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, delTokens) - msgUndelegateValidator := types.NewMsgUndelegate(sdk.AccAddress(validatorAddr), validatorAddr, unbondAmt) - res, err = handler(ctx, msgUndelegateValidator) - require.NoError(t, err) - require.NotNil(t, res) - + res := tstaking.Undelegate(sdk.AccAddress(validatorAddr), validatorAddr, amt, true) ts := &gogotypes.Timestamp{} types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(res.Data, ts) finishTime, err := gogotypes.TimestampFromProto(ts) require.NoError(t, err) - ctx = ctx.WithBlockTime(finishTime) - staking.EndBlocker(ctx, app.StakingKeeper) - + ctx = tstaking.TurnBlock(finishTime) origHeader := ctx.BlockHeader() validator, found := app.StakingKeeper.GetValidator(ctx, validatorAddr) @@ -716,16 +641,14 @@ func TestValidatorQueue(t *testing.T) { require.True(t, validator.IsUnbonding(), "%v", validator) // should still be unbonding at time 6 seconds later - ctx = ctx.WithBlockTime(origHeader.Time.Add(time.Second * 6)) - staking.EndBlocker(ctx, app.StakingKeeper) + ctx = tstaking.TurnBlock(origHeader.Time.Add(time.Second * 6)) validator, found = app.StakingKeeper.GetValidator(ctx, validatorAddr) require.True(t, found) require.True(t, validator.IsUnbonding(), "%v", validator) // should be in unbonded state at time 7 seconds later - ctx = ctx.WithBlockTime(origHeader.Time.Add(time.Second * 7)) - staking.EndBlocker(ctx, app.StakingKeeper) + ctx = tstaking.TurnBlock(origHeader.Time.Add(time.Second * 7)) validator, found = app.StakingKeeper.GetValidator(ctx, validatorAddr) require.True(t, found) @@ -734,8 +657,8 @@ func TestValidatorQueue(t *testing.T) { func TestUnbondingPeriod(t *testing.T) { app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 1, 1000000000) - handler := staking.NewHandler(app.StakingKeeper) validatorAddr := valAddrs[0] + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // set the unbonding time params := app.StakingKeeper.GetParams(ctx) @@ -743,20 +666,11 @@ func TestUnbondingPeriod(t *testing.T) { app.StakingKeeper.SetParams(ctx, params) // create the validator - valTokens := sdk.TokensFromConsensusPower(10) - msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, PKs[0], valTokens) - res, err := handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) - + amt := tstaking.CreateValidatorWithValPower(validatorAddr, PKs[0], 10, true) staking.EndBlocker(ctx, app.StakingKeeper) // begin unbonding - unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(10)) - msgUndelegate := types.NewMsgUndelegate(sdk.AccAddress(validatorAddr), validatorAddr, unbondAmt) - res, err = handler(ctx, msgUndelegate) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.Undelegate(sdk.AccAddress(validatorAddr), validatorAddr, amt, true) origHeader := ctx.BlockHeader() @@ -769,61 +683,41 @@ func TestUnbondingPeriod(t *testing.T) { require.True(t, found, "should not have unbonded") // cannot complete unbonding at time 6 seconds later - ctx = ctx.WithBlockTime(origHeader.Time.Add(time.Second * 6)) - staking.EndBlocker(ctx, app.StakingKeeper) + ctx = tstaking.TurnBlock(origHeader.Time.Add(time.Second * 6)) _, found = app.StakingKeeper.GetUnbondingDelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr) require.True(t, found, "should not have unbonded") // can complete unbonding at time 7 seconds later - ctx = ctx.WithBlockTime(origHeader.Time.Add(time.Second * 7)) - staking.EndBlocker(ctx, app.StakingKeeper) + ctx = tstaking.TurnBlock(origHeader.Time.Add(time.Second * 7)) _, found = app.StakingKeeper.GetUnbondingDelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr) require.False(t, found, "should have unbonded") } func TestUnbondingFromUnbondingValidator(t *testing.T) { app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 2, 1000000000) - handler := staking.NewHandler(app.StakingKeeper) validatorAddr, delegatorAddr := valAddrs[0], delAddrs[1] + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) - // create the validator - msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, PKs[0], sdk.NewInt(10)) - res, err := handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) - - // bond a delegator - msgDelegate := NewTestMsgDelegate(delegatorAddr, validatorAddr, sdk.NewInt(10)) - res, err = handler(ctx, msgDelegate) - require.NoError(t, err) - require.NotNil(t, res) + // create the validator and delegate + tstaking.CreateValidator(validatorAddr, PKs[0], 10, true) + tstaking.Delegate(delegatorAddr, validatorAddr, 10) // unbond the validators bond portion - unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10)) - msgUndelegateValidator := types.NewMsgUndelegate(sdk.AccAddress(validatorAddr), validatorAddr, unbondAmt) - res, err = handler(ctx, msgUndelegateValidator) - require.NoError(t, err) - require.NotNil(t, res) + unbondAmt := sdk.NewInt(10) + res := tstaking.Undelegate(sdk.AccAddress(validatorAddr), validatorAddr, unbondAmt, true) // change the ctx to Block Time one second before the validator would have unbonded ts := &gogotypes.Timestamp{} types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(res.Data, ts) - finishTime, err := gogotypes.TimestampFromProto(ts) require.NoError(t, err) - ctx = ctx.WithBlockTime(finishTime.Add(time.Second * -1)) + tstaking.Ctx = ctx // unbond the delegator from the validator - msgUndelegateDelegator := types.NewMsgUndelegate(delegatorAddr, validatorAddr, unbondAmt) - res, err = handler(ctx, msgUndelegateDelegator) - require.NoError(t, err) - require.NotNil(t, res) + res = tstaking.Undelegate(delegatorAddr, validatorAddr, unbondAmt, true) - ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(app.StakingKeeper.UnbondingTime(ctx))) - - // Run the EndBlocker - staking.EndBlocker(ctx, app.StakingKeeper) + ctx = tstaking.TurnBlockTimeDiff(app.StakingKeeper.UnbondingTime(ctx)) // Check to make sure that the unbonding delegation is no longer in state // (meaning it was deleted in the above EndBlocker) @@ -833,9 +727,9 @@ func TestUnbondingFromUnbondingValidator(t *testing.T) { func TestRedelegationPeriod(t *testing.T) { app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 2, 1000000000) - handler := staking.NewHandler(app.StakingKeeper) validatorAddr, validatorAddr2 := valAddrs[0], valAddrs[1] denom := app.StakingKeeper.GetParams(ctx).BondDenom + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // set the unbonding time params := app.StakingKeeper.GetParams(ctx) @@ -843,32 +737,22 @@ func TestRedelegationPeriod(t *testing.T) { app.StakingKeeper.SetParams(ctx, params) // create the validators - msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, PKs[0], sdk.NewInt(10)) + tstaking.CreateValidator(validatorAddr, PKs[0], 10, true) // initial balance amt1 := app.BankKeeper.GetBalance(ctx, sdk.AccAddress(validatorAddr), denom).Amount - res, err := handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) - // balance should have been subtracted after creation amt2 := app.BankKeeper.GetBalance(ctx, sdk.AccAddress(validatorAddr), denom).Amount require.Equal(t, amt1.Sub(sdk.NewInt(10)).Int64(), amt2.Int64(), "expected coins to be subtracted") - msgCreateValidator = NewTestMsgCreateValidator(validatorAddr2, PKs[1], sdk.NewInt(10)) - res, err = handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) - + tstaking.CreateValidator(validatorAddr2, PKs[1], 10, true) bal1 := app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(validatorAddr)) // begin redelegate redAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10)) msgBeginRedelegate := types.NewMsgBeginRedelegate(sdk.AccAddress(validatorAddr), validatorAddr, validatorAddr2, redAmt) - res, err = handler(ctx, msgBeginRedelegate) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.Handle(msgBeginRedelegate, true) // origin account should not lose tokens as with a regular delegation bal2 := app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(validatorAddr)) @@ -882,76 +766,54 @@ func TestRedelegationPeriod(t *testing.T) { require.True(t, found, "should not have unbonded") // cannot complete redelegation at time 6 seconds later - ctx = ctx.WithBlockTime(origHeader.Time.Add(time.Second * 6)) - staking.EndBlocker(ctx, app.StakingKeeper) + ctx = tstaking.TurnBlock(origHeader.Time.Add(time.Second * 6)) _, found = app.StakingKeeper.GetRedelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr, validatorAddr2) require.True(t, found, "should not have unbonded") // can complete redelegation at time 7 seconds later - ctx = ctx.WithBlockTime(origHeader.Time.Add(time.Second * 7)) - staking.EndBlocker(ctx, app.StakingKeeper) + ctx = tstaking.TurnBlock(origHeader.Time.Add(time.Second * 7)) _, found = app.StakingKeeper.GetRedelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr, validatorAddr2) require.False(t, found, "should have unbonded") } func TestTransitiveRedelegation(t *testing.T) { app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 3, 1000000000) - handler := staking.NewHandler(app.StakingKeeper) - - validatorAddr := valAddrs[0] - validatorAddr2 := valAddrs[1] - validatorAddr3 := valAddrs[2] + val1, val2, val3 := valAddrs[0], valAddrs[1], valAddrs[2] blockTime := time.Now().UTC() ctx = ctx.WithBlockTime(blockTime) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // create the validators - msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, PKs[0], sdk.NewInt(10)) - res, err := handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) - - msgCreateValidator = NewTestMsgCreateValidator(validatorAddr2, PKs[1], sdk.NewInt(10)) - res, err = handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) - - msgCreateValidator = NewTestMsgCreateValidator(validatorAddr3, PKs[2], sdk.NewInt(10)) - res, err = handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.CreateValidator(val1, PKs[0], 10, true) + tstaking.CreateValidator(val2, PKs[1], 10, true) + tstaking.CreateValidator(val3, PKs[2], 10, true) // begin redelegate redAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10)) - msgBeginRedelegate := types.NewMsgBeginRedelegate(sdk.AccAddress(validatorAddr), validatorAddr, validatorAddr2, redAmt) - res, err = handler(ctx, msgBeginRedelegate) - require.NoError(t, err) - require.NotNil(t, res) + msgBeginRedelegate := types.NewMsgBeginRedelegate(sdk.AccAddress(val1), val1, val2, redAmt) + tstaking.Handle(msgBeginRedelegate, true) // cannot redelegation to next validator while first delegation exists - msgBeginRedelegate = types.NewMsgBeginRedelegate(sdk.AccAddress(validatorAddr), validatorAddr2, validatorAddr3, redAmt) - res, err = handler(ctx, msgBeginRedelegate) - require.Error(t, err) - require.Nil(t, res) + msgBeginRedelegate = types.NewMsgBeginRedelegate(sdk.AccAddress(val1), val2, val3, redAmt) + tstaking.Handle(msgBeginRedelegate, false) params := app.StakingKeeper.GetParams(ctx) ctx = ctx.WithBlockTime(blockTime.Add(params.UnbondingTime)) + tstaking.Ctx = ctx // complete first redelegation staking.EndBlocker(ctx, app.StakingKeeper) // now should be able to redelegate from the second validator to the third - res, err = handler(ctx, msgBeginRedelegate) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.Handle(msgBeginRedelegate, true) } func TestMultipleRedelegationAtSameTime(t *testing.T) { app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 2, 1000000000) - handler := staking.NewHandler(app.StakingKeeper) - valAddr := valAddrs[0] valAddr2 := valAddrs[1] + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // set the unbonding time params := app.StakingKeeper.GetParams(ctx) @@ -959,16 +821,8 @@ func TestMultipleRedelegationAtSameTime(t *testing.T) { app.StakingKeeper.SetParams(ctx, params) // create the validators - valTokens := sdk.TokensFromConsensusPower(10) - msgCreateValidator := NewTestMsgCreateValidator(valAddr, PKs[0], valTokens) - res, err := handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) - - msgCreateValidator = NewTestMsgCreateValidator(valAddr2, PKs[1], valTokens) - res, err = handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) + valTokens := tstaking.CreateValidatorWithValPower(valAddr, PKs[0], 10, true) + tstaking.CreateValidator(valAddr2, PKs[1], valTokens.Int64(), true) // end block to bond them staking.EndBlocker(ctx, app.StakingKeeper) @@ -977,9 +831,7 @@ func TestMultipleRedelegationAtSameTime(t *testing.T) { selfDelAddr := sdk.AccAddress(valAddr) // (the validator is it's own delegator) redAmt := sdk.NewCoin(sdk.DefaultBondDenom, valTokens.QuoRaw(2)) msgBeginRedelegate := types.NewMsgBeginRedelegate(selfDelAddr, valAddr, valAddr2, redAmt) - res, err = handler(ctx, msgBeginRedelegate) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.Handle(msgBeginRedelegate, true) // there should only be one entry in the redelegation object rd, found := app.StakingKeeper.GetRedelegation(ctx, selfDelAddr, valAddr, valAddr2) @@ -987,9 +839,7 @@ func TestMultipleRedelegationAtSameTime(t *testing.T) { require.Len(t, rd.Entries, 1) // start a second redelegation at this same time as the first - res, err = handler(ctx, msgBeginRedelegate) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.Handle(msgBeginRedelegate, true) // now there should be two entries rd, found = app.StakingKeeper.GetRedelegation(ctx, selfDelAddr, valAddr, valAddr2) @@ -997,19 +847,16 @@ func TestMultipleRedelegationAtSameTime(t *testing.T) { require.Len(t, rd.Entries, 2) // move forward in time, should complete both redelegations - ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(1 * time.Second)) - staking.EndBlocker(ctx, app.StakingKeeper) - + ctx = tstaking.TurnBlockTimeDiff(1 * time.Second) rd, found = app.StakingKeeper.GetRedelegation(ctx, selfDelAddr, valAddr, valAddr2) require.False(t, found) } func TestMultipleRedelegationAtUniqueTimes(t *testing.T) { app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 2, 1000000000) - handler := staking.NewHandler(app.StakingKeeper) - valAddr := valAddrs[0] valAddr2 := valAddrs[1] + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // set the unbonding time params := app.StakingKeeper.GetParams(ctx) @@ -1017,16 +864,8 @@ func TestMultipleRedelegationAtUniqueTimes(t *testing.T) { app.StakingKeeper.SetParams(ctx, params) // create the validators - valTokens := sdk.TokensFromConsensusPower(10) - msgCreateValidator := NewTestMsgCreateValidator(valAddr, PKs[0], valTokens) - res, err := handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) - - msgCreateValidator = NewTestMsgCreateValidator(valAddr2, PKs[1], valTokens) - res, err = handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) + valTokens := tstaking.CreateValidatorWithValPower(valAddr, PKs[0], 10, true) + tstaking.CreateValidator(valAddr2, PKs[1], valTokens.Int64(), true) // end block to bond them staking.EndBlocker(ctx, app.StakingKeeper) @@ -1035,15 +874,12 @@ func TestMultipleRedelegationAtUniqueTimes(t *testing.T) { selfDelAddr := sdk.AccAddress(valAddr) // (the validator is it's own delegator) redAmt := sdk.NewCoin(sdk.DefaultBondDenom, valTokens.QuoRaw(2)) msgBeginRedelegate := types.NewMsgBeginRedelegate(selfDelAddr, valAddr, valAddr2, redAmt) - res, err = handler(ctx, msgBeginRedelegate) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.Handle(msgBeginRedelegate, true) // move forward in time and start a second redelegation ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(5 * time.Second)) - res, err = handler(ctx, msgBeginRedelegate) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.Ctx = ctx + tstaking.Handle(msgBeginRedelegate, true) // now there should be two entries rd, found := app.StakingKeeper.GetRedelegation(ctx, selfDelAddr, valAddr, valAddr2) @@ -1051,47 +887,36 @@ func TestMultipleRedelegationAtUniqueTimes(t *testing.T) { require.Len(t, rd.Entries, 2) // move forward in time, should complete the first redelegation, but not the second - ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(5 * time.Second)) - staking.EndBlocker(ctx, app.StakingKeeper) + ctx = tstaking.TurnBlockTimeDiff(5 * time.Second) rd, found = app.StakingKeeper.GetRedelegation(ctx, selfDelAddr, valAddr, valAddr2) require.True(t, found) require.Len(t, rd.Entries, 1) // move forward in time, should complete the second redelegation - ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(5 * time.Second)) - staking.EndBlocker(ctx, app.StakingKeeper) + ctx = tstaking.TurnBlockTimeDiff(5 * time.Second) rd, found = app.StakingKeeper.GetRedelegation(ctx, selfDelAddr, valAddr, valAddr2) require.False(t, found) } func TestMultipleUnbondingDelegationAtSameTime(t *testing.T) { app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 1, 1000000000) - handler := staking.NewHandler(app.StakingKeeper) - valAddr := valAddrs[0] + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // set the unbonding time params := app.StakingKeeper.GetParams(ctx) params.UnbondingTime = 1 * time.Second app.StakingKeeper.SetParams(ctx, params) - // create the validator - valTokens := sdk.TokensFromConsensusPower(10) - msgCreateValidator := NewTestMsgCreateValidator(valAddr, PKs[0], valTokens) - res, err := handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) + // create the validators + valTokens := tstaking.CreateValidatorWithValPower(valAddr, PKs[0], 10, true) // end block to bond staking.EndBlocker(ctx, app.StakingKeeper) // begin an unbonding delegation selfDelAddr := sdk.AccAddress(valAddr) // (the validator is it's own delegator) - unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, valTokens.QuoRaw(2)) - msgUndelegate := types.NewMsgUndelegate(selfDelAddr, valAddr, unbondAmt) - res, err = handler(ctx, msgUndelegate) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.Undelegate(selfDelAddr, valAddr, valTokens.QuoRaw(2), true) // there should only be one entry in the ubd object ubd, found := app.StakingKeeper.GetUnbondingDelegation(ctx, selfDelAddr, valAddr) @@ -1099,9 +924,7 @@ func TestMultipleUnbondingDelegationAtSameTime(t *testing.T) { require.Len(t, ubd.Entries, 1) // start a second ubd at this same time as the first - res, err = handler(ctx, msgUndelegate) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.Undelegate(selfDelAddr, valAddr, valTokens.QuoRaw(2), true) // now there should be two entries ubd, found = app.StakingKeeper.GetUnbondingDelegation(ctx, selfDelAddr, valAddr) @@ -1109,17 +932,15 @@ func TestMultipleUnbondingDelegationAtSameTime(t *testing.T) { require.Len(t, ubd.Entries, 2) // move forwaubd in time, should complete both ubds - ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(1 * time.Second)) - staking.EndBlocker(ctx, app.StakingKeeper) - + ctx = tstaking.TurnBlockTimeDiff(1 * time.Second) ubd, found = app.StakingKeeper.GetUnbondingDelegation(ctx, selfDelAddr, valAddr) require.False(t, found) } func TestMultipleUnbondingDelegationAtUniqueTimes(t *testing.T) { app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 1, 1000000000) - handler := staking.NewHandler(app.StakingKeeper) valAddr := valAddrs[0] + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // set the unbonding time params := app.StakingKeeper.GetParams(ctx) @@ -1127,22 +948,14 @@ func TestMultipleUnbondingDelegationAtUniqueTimes(t *testing.T) { app.StakingKeeper.SetParams(ctx, params) // create the validator - valTokens := sdk.TokensFromConsensusPower(10) - msgCreateValidator := NewTestMsgCreateValidator(valAddr, PKs[0], valTokens) - res, err := handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) + valTokens := tstaking.CreateValidatorWithValPower(valAddr, PKs[0], 10, true) // end block to bond staking.EndBlocker(ctx, app.StakingKeeper) // begin an unbonding delegation selfDelAddr := sdk.AccAddress(valAddr) // (the validator is it's own delegator) - unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, valTokens.QuoRaw(2)) - msgUndelegate := types.NewMsgUndelegate(selfDelAddr, valAddr, unbondAmt) - res, err = handler(ctx, msgUndelegate) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.Undelegate(selfDelAddr, valAddr, valTokens.QuoRaw(2), true) // there should only be one entry in the ubd object ubd, found := app.StakingKeeper.GetUnbondingDelegation(ctx, selfDelAddr, valAddr) @@ -1151,9 +964,8 @@ func TestMultipleUnbondingDelegationAtUniqueTimes(t *testing.T) { // move forwaubd in time and start a second redelegation ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(5 * time.Second)) - res, err = handler(ctx, msgUndelegate) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.Ctx = ctx + tstaking.Undelegate(selfDelAddr, valAddr, valTokens.QuoRaw(2), true) // now there should be two entries ubd, found = app.StakingKeeper.GetUnbondingDelegation(ctx, selfDelAddr, valAddr) @@ -1161,26 +973,23 @@ func TestMultipleUnbondingDelegationAtUniqueTimes(t *testing.T) { require.Len(t, ubd.Entries, 2) // move forwaubd in time, should complete the first redelegation, but not the second - ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(5 * time.Second)) - staking.EndBlocker(ctx, app.StakingKeeper) + ctx = tstaking.TurnBlockTimeDiff(5 * time.Second) ubd, found = app.StakingKeeper.GetUnbondingDelegation(ctx, selfDelAddr, valAddr) require.True(t, found) require.Len(t, ubd.Entries, 1) // move forwaubd in time, should complete the second redelegation - ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(5 * time.Second)) - staking.EndBlocker(ctx, app.StakingKeeper) + ctx = tstaking.TurnBlockTimeDiff(5 * time.Second) ubd, found = app.StakingKeeper.GetUnbondingDelegation(ctx, selfDelAddr, valAddr) require.False(t, found) } func TestUnbondingWhenExcessValidators(t *testing.T) { app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 3, 1000000000) - handler := staking.NewHandler(app.StakingKeeper) - - validatorAddr1 := valAddrs[0] - validatorAddr2 := valAddrs[1] - validatorAddr3 := valAddrs[2] + val1 := valAddrs[0] + val2 := valAddrs[1] + val3 := valAddrs[2] + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) // set the unbonding time params := app.StakingKeeper.GetParams(ctx) @@ -1188,43 +997,21 @@ func TestUnbondingWhenExcessValidators(t *testing.T) { app.StakingKeeper.SetParams(ctx, params) // add three validators - valTokens1 := sdk.TokensFromConsensusPower(50) - msgCreateValidator := NewTestMsgCreateValidator(validatorAddr1, PKs[0], valTokens1) - res, err := handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) - + tstaking.CreateValidatorWithValPower(val1, PKs[0], 50, true) // apply TM updates app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) require.Equal(t, 1, len(app.StakingKeeper.GetLastValidators(ctx))) - valTokens2 := sdk.TokensFromConsensusPower(30) - msgCreateValidator = NewTestMsgCreateValidator(validatorAddr2, PKs[1], valTokens2) - res, err = handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) - - // apply TM updates + valTokens2 := tstaking.CreateValidatorWithValPower(val2, PKs[1], 30, true) app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) require.Equal(t, 2, len(app.StakingKeeper.GetLastValidators(ctx))) - valTokens3 := sdk.TokensFromConsensusPower(10) - msgCreateValidator = NewTestMsgCreateValidator(validatorAddr3, PKs[2], valTokens3) - res, err = handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) - - // apply TM updates + tstaking.CreateValidatorWithValPower(val3, PKs[2], 10, true) app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) require.Equal(t, 2, len(app.StakingKeeper.GetLastValidators(ctx))) // unbond the validator-2 - unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, valTokens2) - msgUndelegate := types.NewMsgUndelegate(sdk.AccAddress(validatorAddr2), validatorAddr2, unbondAmt) - res, err = handler(ctx, msgUndelegate) - require.NoError(t, err) - require.NotNil(t, res) - + tstaking.Undelegate(sdk.AccAddress(val2), val2, valTokens2, true) // apply TM updates app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) @@ -1233,35 +1020,20 @@ func TestUnbondingWhenExcessValidators(t *testing.T) { // the total number of validators should stay the same vals := app.StakingKeeper.GetLastValidators(ctx) require.Equal(t, 2, len(vals), "vals %v", vals) - val1, found := app.StakingKeeper.GetValidator(ctx, validatorAddr1) - require.True(t, found) - require.Equal(t, types.Bonded, val1.Status, "%v", val1) + tstaking.CheckValidator(val1, types.Bonded.String(), false) } func TestBondUnbondRedelegateSlashTwice(t *testing.T) { app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 3, 1000000000) - - handler := staking.NewHandler(app.StakingKeeper) - valA, valB, del := valAddrs[0], valAddrs[1], delAddrs[2] consAddr0 := sdk.ConsAddress(PKs[0].Address()) + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) - valTokens := sdk.TokensFromConsensusPower(10) - msgCreateValidator := NewTestMsgCreateValidator(valA, PKs[0], valTokens) - res, err := handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) - - msgCreateValidator = NewTestMsgCreateValidator(valB, PKs[1], valTokens) - res, err = handler(ctx, msgCreateValidator) - require.NoError(t, err) - require.NotNil(t, res) + valTokens := tstaking.CreateValidatorWithValPower(valA, PKs[0], 10, true) + tstaking.CreateValidator(valB, PKs[1], valTokens.Int64(), true) // delegate 10 stake - msgDelegate := NewTestMsgDelegate(del, valA, valTokens) - res, err = handler(ctx, msgDelegate) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.Delegate(del, valA, valTokens.Int64()) // apply Tendermint updates updates := app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) @@ -1269,20 +1041,16 @@ func TestBondUnbondRedelegateSlashTwice(t *testing.T) { // a block passes ctx = ctx.WithBlockHeight(1) + tstaking.Ctx = ctx // begin unbonding 4 stake - unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(4)) - msgUndelegate := types.NewMsgUndelegate(del, valA, unbondAmt) - res, err = handler(ctx, msgUndelegate) - require.NoError(t, err) - require.NotNil(t, res) + unbondAmt := sdk.TokensFromConsensusPower(4) + tstaking.Undelegate(del, valA, unbondAmt, true) // begin redelegate 6 stake redAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(6)) msgBeginRedelegate := types.NewMsgBeginRedelegate(del, valA, valB, redAmt) - res, err = handler(ctx, msgBeginRedelegate) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.Handle(msgBeginRedelegate, true) // destination delegation should have 6 shares delegation, found := app.StakingKeeper.GetDelegation(ctx, del, valB) @@ -1300,7 +1068,7 @@ func TestBondUnbondRedelegateSlashTwice(t *testing.T) { ubd, found := app.StakingKeeper.GetUnbondingDelegation(ctx, del, valA) require.True(t, found) require.Len(t, ubd.Entries, 1) - require.Equal(t, unbondAmt.Amount.QuoRaw(2), ubd.Entries[0].Balance) + require.Equal(t, unbondAmt.QuoRaw(2), ubd.Entries[0].Balance) // redelegation should have been slashed by half redelegation, found := app.StakingKeeper.GetRedelegation(ctx, del, valA, valB) @@ -1320,12 +1088,13 @@ func TestBondUnbondRedelegateSlashTwice(t *testing.T) { // slash the validator for an infraction committed after the unbonding and redelegation begin ctx = ctx.WithBlockHeight(3) app.StakingKeeper.Slash(ctx, consAddr0, 2, 10, sdk.NewDecWithPrec(5, 1)) + tstaking.Ctx = ctx // unbonding delegation should be unchanged ubd, found = app.StakingKeeper.GetUnbondingDelegation(ctx, del, valA) require.True(t, found) require.Len(t, ubd.Entries, 1) - require.Equal(t, unbondAmt.Amount.QuoRaw(2), ubd.Entries[0].Balance) + require.Equal(t, unbondAmt.QuoRaw(2), ubd.Entries[0].Balance) // redelegation should be unchanged redelegation, found = app.StakingKeeper.GetRedelegation(ctx, del, valA, valB) @@ -1358,9 +1127,8 @@ func TestInvalidMsg(t *testing.T) { func TestInvalidCoinDenom(t *testing.T) { app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 3, 1000000000) - handler := staking.NewHandler(app.StakingKeeper) - valA, valB, delAddr := valAddrs[0], valAddrs[1], delAddrs[2] + tstaking := teststaking.NewService(t, ctx, app.StakingKeeper) valTokens := sdk.TokensFromConsensusPower(100) invalidCoin := sdk.NewCoin("churros", valTokens) @@ -1368,49 +1136,33 @@ func TestInvalidCoinDenom(t *testing.T) { oneCoin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt()) commission := types.NewCommissionRates(sdk.OneDec(), sdk.OneDec(), sdk.ZeroDec()) + msgCreate, err := types.NewMsgCreateValidator(valA, PKs[0], invalidCoin, types.Description{}, commission, sdk.OneInt()) + require.NoError(t, err) + tstaking.Handle(msgCreate, false) - msgCreate := types.NewMsgCreateValidator(valA, PKs[0], invalidCoin, types.Description{}, commission, sdk.OneInt()) - res, err := handler(ctx, msgCreate) - require.Error(t, err) - require.Nil(t, res) - - msgCreate = types.NewMsgCreateValidator(valA, PKs[0], validCoin, types.Description{}, commission, sdk.OneInt()) - res, err = handler(ctx, msgCreate) + msgCreate, err = types.NewMsgCreateValidator(valA, PKs[0], validCoin, types.Description{}, commission, sdk.OneInt()) require.NoError(t, err) - require.NotNil(t, res) + tstaking.Handle(msgCreate, true) - msgCreate = types.NewMsgCreateValidator(valB, PKs[1], validCoin, types.Description{}, commission, sdk.OneInt()) - res, err = handler(ctx, msgCreate) + msgCreate, err = types.NewMsgCreateValidator(valB, PKs[1], validCoin, types.Description{}, commission, sdk.OneInt()) require.NoError(t, err) - require.NotNil(t, res) + tstaking.Handle(msgCreate, true) msgDelegate := types.NewMsgDelegate(delAddr, valA, invalidCoin) - res, err = handler(ctx, msgDelegate) - require.Error(t, err) - require.Nil(t, res) + tstaking.Handle(msgDelegate, false) msgDelegate = types.NewMsgDelegate(delAddr, valA, validCoin) - res, err = handler(ctx, msgDelegate) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.Handle(msgDelegate, true) msgUndelegate := types.NewMsgUndelegate(delAddr, valA, invalidCoin) - res, err = handler(ctx, msgUndelegate) - require.Error(t, err) - require.Nil(t, res) + tstaking.Handle(msgUndelegate, false) msgUndelegate = types.NewMsgUndelegate(delAddr, valA, oneCoin) - res, err = handler(ctx, msgUndelegate) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.Handle(msgUndelegate, true) msgRedelegate := types.NewMsgBeginRedelegate(delAddr, valA, valB, invalidCoin) - res, err = handler(ctx, msgRedelegate) - require.Error(t, err) - require.Nil(t, res) + tstaking.Handle(msgRedelegate, false) msgRedelegate = types.NewMsgBeginRedelegate(delAddr, valA, valB, oneCoin) - res, err = handler(ctx, msgRedelegate) - require.NoError(t, err) - require.NotNil(t, res) + tstaking.Handle(msgRedelegate, true) } diff --git a/x/staking/teststaking/service.go b/x/staking/teststaking/service.go index 773dbb7efb6c..0e6c9da1b736 100644 --- a/x/staking/teststaking/service.go +++ b/x/staking/teststaking/service.go @@ -2,6 +2,7 @@ package teststaking import ( "testing" + "time" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto" @@ -15,7 +16,9 @@ import ( // Service is a structure which wraps the staking handler // and provides methods useful in tests type Service struct { + t *testing.T h sdk.Handler + k keeper.Keeper Ctx sdk.Context Commission stakingtypes.CommissionRates @@ -24,73 +27,107 @@ type Service struct { } // NewService creates staking Handler wrapper for tests -func NewService(ctx sdk.Context, k keeper.Keeper) *Service { - return &Service{staking.NewHandler(k), ctx, ZeroCommission(), sdk.DefaultBondDenom} +func NewService(t *testing.T, ctx sdk.Context, k keeper.Keeper) *Service { + return &Service{t, staking.NewHandler(k), k, ctx, ZeroCommission(), sdk.DefaultBondDenom} } // CreateValidator calls handler to create a new staking validator -func (sh *Service) CreateValidator(t *testing.T, addr sdk.ValAddress, pk crypto.PubKey, stakeAmount int64, ok bool) { +func (sh *Service) CreateValidator(addr sdk.ValAddress, pk crypto.PubKey, stakeAmount int64, ok bool) { coin := sdk.NewCoin(sh.Denom, sdk.NewInt(stakeAmount)) - sh.createValidator(t, addr, pk, coin, ok) + sh.createValidator(addr, pk, coin, ok) } // CreateValidatorWithValPower calls handler to create a new staking validator with zero // commission -func (sh *Service) CreateValidatorWithValPower(t *testing.T, addr sdk.ValAddress, pk crypto.PubKey, valPower int64, ok bool) sdk.Int { +func (sh *Service) CreateValidatorWithValPower(addr sdk.ValAddress, pk crypto.PubKey, valPower int64, ok bool) sdk.Int { amount := sdk.TokensFromConsensusPower(valPower) coin := sdk.NewCoin(sh.Denom, amount) - sh.createValidator(t, addr, pk, coin, ok) + sh.createValidator(addr, pk, coin, ok) return amount } // CreateValidatorMsg returns a message used to create validator in this service. -func (sh *Service) CreateValidatorMsg(t *testing.T, addr sdk.ValAddress, pk crypto.PubKey, stakeAmount int64) *stakingtypes.MsgCreateValidator { +func (sh *Service) CreateValidatorMsg(addr sdk.ValAddress, pk crypto.PubKey, stakeAmount int64) *stakingtypes.MsgCreateValidator { coin := sdk.NewCoin(sh.Denom, sdk.NewInt(stakeAmount)) msg, err := stakingtypes.NewMsgCreateValidator(addr, pk, coin, stakingtypes.Description{}, sh.Commission, sdk.OneInt()) - require.NoError(t, err) + require.NoError(sh.t, err) return msg } -func (sh *Service) createValidator(t *testing.T, addr sdk.ValAddress, pk crypto.PubKey, coin sdk.Coin, ok bool) { +func (sh *Service) createValidator(addr sdk.ValAddress, pk crypto.PubKey, coin sdk.Coin, ok bool) { msg, err := stakingtypes.NewMsgCreateValidator(addr, pk, coin, stakingtypes.Description{}, sh.Commission, sdk.OneInt()) - require.NoError(t, err) - sh.Handle(t, msg, ok) + require.NoError(sh.t, err) + sh.Handle(msg, ok) } // Delegate calls handler to delegate stake for a validator -func (sh *Service) Delegate(t *testing.T, delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount int64) { +func (sh *Service) Delegate(delegator sdk.AccAddress, val sdk.ValAddress, amount int64) { coin := sdk.NewCoin(sh.Denom, sdk.NewInt(amount)) - msg := stakingtypes.NewMsgDelegate(delAddr, valAddr, coin) - sh.Handle(t, msg, true) + msg := stakingtypes.NewMsgDelegate(delegator, val, coin) + sh.Handle(msg, true) } // DelegateWithPower calls handler to delegate stake for a validator -func (sh *Service) DelegateWithPower(t *testing.T, delAddr sdk.AccAddress, valAddr sdk.ValAddress, power int64) { +func (sh *Service) DelegateWithPower(delegator sdk.AccAddress, val sdk.ValAddress, power int64) { coin := sdk.NewCoin(sh.Denom, sdk.TokensFromConsensusPower(power)) - msg := stakingtypes.NewMsgDelegate(delAddr, valAddr, coin) - sh.Handle(t, msg, true) + msg := stakingtypes.NewMsgDelegate(delegator, val, coin) + sh.Handle(msg, true) } // Undelegate calls handler to unbound some stake from a validator. -func (sh *Service) Undelegate(t *testing.T, delegator sdk.AccAddress, val sdk.ValAddress, amount sdk.Int, ok bool) *sdk.Result { +func (sh *Service) Undelegate(delegator sdk.AccAddress, val sdk.ValAddress, amount sdk.Int, ok bool) *sdk.Result { unbondAmt := sdk.NewCoin(sh.Denom, amount) msg := stakingtypes.NewMsgUndelegate(delegator, val, unbondAmt) - return sh.Handle(t, msg, ok) + return sh.Handle(msg, ok) } // Handle calls staking handler on a given message -func (sh *Service) Handle(t *testing.T, msg sdk.Msg, ok bool) *sdk.Result { +func (sh *Service) Handle(msg sdk.Msg, ok bool) *sdk.Result { res, err := sh.h(sh.Ctx, msg) if ok { - require.NoError(t, err) - require.NotNil(t, res) + require.NoError(sh.t, err) + require.NotNil(sh.t, res) } else { - require.Error(t, err) - require.Nil(t, res) + require.Error(sh.t, err) + require.Nil(sh.t, res) } return res } +// CheckValidator asserts that a validor exists and has a given status (if status!="") +// and if has a right jailed flag. +func (sh *Service) CheckValidator(addr sdk.ValAddress, status stakingtypes.BondStatus, jailed bool) stakingtypes.Validator { + v, ok := sh.k.GetValidator(sh.Ctx, addr) + require.True(sh.t, ok) + require.Equal(sh.t, jailed, v.Jailed, "wrong Jalied status") + if status >= 0 { + require.Equal(sh.t, status, v.Status) + } + return v +} + +// CheckDelegator asserts that a delegator exists +func (sh *Service) CheckDelegator(delegator sdk.AccAddress, val sdk.ValAddress) { + d, ok := sh.k.GetDelegation(sh.Ctx, delegator, val) + require.True(sh.t, ok) + require.NotNil(sh.t, d) +} + +// TurnBlock calls EndBlocker and updates the block time +func (sh *Service) TurnBlock(newTime time.Time) sdk.Context { + sh.Ctx = sh.Ctx.WithBlockTime(newTime) + staking.EndBlocker(sh.Ctx, sh.k) + return sh.Ctx +} + +// TurnBlockTimeDiff calls EndBlocker and updates the block time by adding the +// duration to the current block time +func (sh *Service) TurnBlockTimeDiff(diff time.Duration) sdk.Context { + sh.Ctx = sh.Ctx.WithBlockTime(sh.Ctx.BlockHeader().Time.Add(diff)) + staking.EndBlocker(sh.Ctx, sh.k) + return sh.Ctx +} + // ZeroCommission constructs a commission rates with all zeros. func ZeroCommission() stakingtypes.CommissionRates { return stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())