Skip to content

Commit

Permalink
add validator checks
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba committed Oct 14, 2020
1 parent 73c846b commit e7dca72
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 540 deletions.
1 change: 0 additions & 1 deletion types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 9 additions & 9 deletions x/distribution/keeper/allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
38 changes: 19 additions & 19 deletions x/distribution/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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])

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions x/distribution/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions x/distribution/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions x/slashing/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
25 changes: 13 additions & 12 deletions x/slashing/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)),
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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))
Expand Down Expand Up @@ -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(
Expand Down
Loading

0 comments on commit e7dca72

Please sign in to comment.