Skip to content

Commit

Permalink
feat: Adds validator qgb checks tests (celestiaorg#138)
Browse files Browse the repository at this point in the history
* first pass on tests fixes

* fixes the rest of unit tests

* remove unnecessary comments

* uses default eth address when starting sim network

* cosmetics

* comments failing test

* comments failing test

* adds orchestrator/ethereum address checks for validators when creating and editing

* uses correct error codes for eth/orch address errors

* uncomments test and fixes it from commit 434b308

* updates tests to be able to set orch/eth address when creating validators

* adds zero eth address check when creating/updating validator

* attempts to fix duplicate eth address in sim network

* adds validator check tests

* revert squashed changes

* uses unwrapped context for msg_server orch/eth validation

* increase DefaultGenTxGas to accomodate new qgb validator changes

* scaffolds an ethereum address from orchestrator address in staking simulation to have a deterministic way of creating it

* fix eth address creation from orch address in operations

* increase DefaultGenTxGas

* increase DefaultGenTxGas

* remove unnecessary eth address
  • Loading branch information
rach-id committed Aug 4, 2022
1 parent 99fcf70 commit c0080f9
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 41 deletions.
24 changes: 18 additions & 6 deletions x/distribution/keeper/allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ func TestAllocateTokensToValidatorWithCommission(t *testing.T) {
ctx := app.BaseApp.NewContext(false, tmproto.Header{})

addrs := simapp.AddTestAddrs(app, ctx, 3, sdk.NewInt(1234))
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)
valAddrs := simapp.ConvertAddrsToValAddrs(addrs)
tstaking := teststaking.NewHelper(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(sdk.ValAddress(addrs[0]), valConsPk1, sdk.NewInt(100), true)
tstaking.CreateValidator(sdk.ValAddress(addrs[0]), valConsPk1, sdk.NewInt(100), sdk.AccAddress(valConsPk1.Address()), *randomEthAddress, true)
val := app.StakingKeeper.Validator(ctx, valAddrs[0])

// allocate tokens
Expand All @@ -53,16 +55,20 @@ func TestAllocateTokensToManyValidators(t *testing.T) {
app.DistrKeeper.SetFeePool(ctx, disttypes.InitialFeePool())

addrs := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1234))
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)
valAddrs := simapp.ConvertAddrsToValAddrs(addrs)
tstaking := teststaking.NewHelper(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(valAddrs[0], valConsPk1, sdk.NewInt(100), true)
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(100), sdk.AccAddress(valConsPk1.Address()), *randomEthAddress, true)

// create second validator with 0% commission
randomEthAddress2, err := teststaking.RandomEthAddress()
require.NoError(t, err)
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDec(0), sdk.NewDec(0), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[1], valConsPk2, sdk.NewInt(100), true)
tstaking.CreateValidator(valAddrs[1], valConsPk2, sdk.NewInt(100), sdk.AccAddress(valConsPk2.Address()), *randomEthAddress2, true)

abciValA := abci.Validator{
Address: valConsPk1.Address(),
Expand Down Expand Up @@ -131,16 +137,22 @@ func TestAllocateTokensTruncation(t *testing.T) {
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)

// create validator with 10% commission
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(110), true)
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(110), sdk.AccAddress(valConsPk1.Address()), *randomEthAddress, true)

// create second validator with 10% commission
randomEthAddress2, err := teststaking.RandomEthAddress()
require.NoError(t, err)
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[1], valConsPk2, sdk.NewInt(100), true)
tstaking.CreateValidator(valAddrs[1], valConsPk2, sdk.NewInt(100), sdk.AccAddress(valConsPk2.Address()), *randomEthAddress2, true)

// create third validator with 10% commission
randomEthAddress3, err := teststaking.RandomEthAddress()
require.NoError(t, err)
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[2], valConsPk3, sdk.NewInt(100), true)
tstaking.CreateValidator(valAddrs[2], valConsPk3, sdk.NewInt(100), sdk.AccAddress(valConsPk3.Address()), *randomEthAddress3, true)

abciValA := abci.Validator{
Address: valConsPk1.Address(),
Expand Down
36 changes: 26 additions & 10 deletions x/distribution/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ func TestCalculateRewardsBasic(t *testing.T) {
valAddrs := simapp.ConvertAddrsToValAddrs(addr)

// create validator with 50% commission
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(100), true)
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(100), sdk.AccAddress(valConsPk1.Address()), *randomEthAddress, true)

// end block to bond validator and start new block
staking.EndBlocker(ctx, app.StakingKeeper)
Expand Down Expand Up @@ -82,7 +84,9 @@ func TestCalculateRewardsAfterSlash(t *testing.T) {
// 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(valAddrs[0], valConsPk1, valPower, true)
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)
tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk1, valPower, sdk.AccAddress(valConsPk1.Address()), *randomEthAddress, true)

// end block to bond validator
staking.EndBlocker(ctx, app.StakingKeeper)
Expand Down Expand Up @@ -143,9 +147,11 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) {
valAddrs := simapp.ConvertAddrsToValAddrs(addr)

// create validator with 50% commission
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)
valPower := int64(100)
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk1, valPower, true)
tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk1, valPower, sdk.AccAddress(valConsPk1.Address()), *randomEthAddress, true)

// end block to bond validator
staking.EndBlocker(ctx, app.StakingKeeper)
Expand Down Expand Up @@ -218,8 +224,10 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) {
valAddrs := simapp.ConvertAddrsToValAddrs(addr)

// create validator with 50% commission
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(100), true)
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(100), sdk.AccAddress(valConsPk1.Address()), *randomEthAddress, true)

// end block to bond validator
staking.EndBlocker(ctx, app.StakingKeeper)
Expand Down Expand Up @@ -290,9 +298,11 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) {
app.AccountKeeper.SetModuleAccount(ctx, distrAcc)

// create validator with 50% commission
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)
power := int64(100)
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
valTokens := tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk1, power, true)
valTokens := tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk1, power, sdk.AccAddress(valConsPk1.Address()), *randomEthAddress, true)

// assert correct initial balance
expTokens := balanceTokens.Sub(valTokens)
Expand Down Expand Up @@ -320,7 +330,7 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) {
require.Equal(t, uint64(2), app.DistrKeeper.GetValidatorHistoricalReferenceCount(ctx))

// withdraw rewards
_, err := app.DistrKeeper.WithdrawDelegationRewards(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
_, err = app.DistrKeeper.WithdrawDelegationRewards(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
require.Nil(t, err)

// historical count should still be 2 (added one record, cleared one)
Expand Down Expand Up @@ -354,9 +364,11 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) {
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)

// create validator with 50% commission
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)
valPower := int64(100)
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk1, valPower, true)
tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk1, valPower, sdk.AccAddress(valConsPk1.Address()), *randomEthAddress, true)

// end block to bond validator
staking.EndBlocker(ctx, app.StakingKeeper)
Expand Down Expand Up @@ -422,9 +434,11 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) {
valAddrs := simapp.ConvertAddrsToValAddrs(addr)

// create validator with 50% commission
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
valPower := int64(100)
tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk1, valPower, true)
tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk1, valPower, sdk.AccAddress(valConsPk1.Address()), *randomEthAddress, true)

// end block to bond validator
staking.EndBlocker(ctx, app.StakingKeeper)
Expand Down Expand Up @@ -506,8 +520,10 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) {
tokens := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, sdk.NewDec(initial))}

// create validator with 50% commission
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(100), true)
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(100), sdk.AccAddress(valConsPk1.Address()), *randomEthAddress, true)

// end block to bond validator
staking.EndBlocker(ctx, app.StakingKeeper)
Expand Down Expand Up @@ -545,7 +561,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) {
app.DistrKeeper.AllocateTokensToValidator(ctx, val, tokens)

// first delegator withdraws
_, err := app.DistrKeeper.WithdrawDelegationRewards(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
_, err = app.DistrKeeper.WithdrawDelegationRewards(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
require.NoError(t, err)

// second delegator withdraws
Expand Down
4 changes: 3 additions & 1 deletion x/distribution/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ func (suite *KeeperTestSuite) TestGRPCDelegationRewards() {

tstaking := teststaking.NewHelper(suite.T(), ctx, app.StakingKeeper)
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(100), true)
randomEthAddress, err := teststaking.RandomEthAddress()
suite.NoError(err)
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(100), sdk.AccAddress(valConsPk1.Address()), *randomEthAddress, true)

staking.EndBlocker(ctx, app.StakingKeeper)
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
Expand Down
4 changes: 3 additions & 1 deletion x/distribution/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ func TestQueries(t *testing.T) {
// test delegation rewards query
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
tstaking.CreateValidator(valOpAddr1, valConsPk1, sdk.NewInt(100), true)
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)
tstaking.CreateValidator(valOpAddr1, valConsPk1, sdk.NewInt(100), sdk.AccAddress(valConsPk1.Address()), *randomEthAddress, true)

staking.EndBlocker(ctx, app.StakingKeeper)

Expand Down
8 changes: 6 additions & 2 deletions x/evidence/keeper/infraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ func (suite *KeeperTestSuite) TestHandleDoubleSign() {
power := int64(100)
stakingParams := suite.app.StakingKeeper.GetParams(ctx)
operatorAddr, val := valAddresses[0], pubkeys[0]
randomEthAddress, err := teststaking.RandomEthAddress()
suite.NoError(err)
tstaking := teststaking.NewHelper(suite.T(), ctx, suite.app.StakingKeeper)

selfDelegation := tstaking.CreateValidatorWithValPower(operatorAddr, val, power, true)
selfDelegation := tstaking.CreateValidatorWithValPower(operatorAddr, val, power, sdk.AccAddress(val.Address()), *randomEthAddress, true)

// execute end-blocker and verify validator attributes
staking.EndBlocker(ctx, suite.app.StakingKeeper)
Expand Down Expand Up @@ -82,9 +84,11 @@ func (suite *KeeperTestSuite) TestHandleDoubleSign_TooOld() {
power := int64(100)
stakingParams := suite.app.StakingKeeper.GetParams(ctx)
operatorAddr, val := valAddresses[0], pubkeys[0]
randomEthAddress, err := teststaking.RandomEthAddress()
suite.NoError(err)
tstaking := teststaking.NewHelper(suite.T(), ctx, suite.app.StakingKeeper)

amt := tstaking.CreateValidatorWithValPower(operatorAddr, val, power, true)
amt := tstaking.CreateValidatorWithValPower(operatorAddr, val, power, sdk.AccAddress(val.Address()), *randomEthAddress, true)

// execute end-blocker and verify validator attributes
staking.EndBlocker(ctx, suite.app.StakingKeeper)
Expand Down
4 changes: 3 additions & 1 deletion x/slashing/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ func TestBeginBlocker(t *testing.T) {

// bond the validator
power := int64(100)
amt := tstaking.CreateValidatorWithValPower(addr, pk, power, true)
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)
amt := tstaking.CreateValidatorWithValPower(addr, pk, power, sdk.AccAddress(pk.Address()), *randomEthAddress, true)
staking.EndBlocker(ctx, app.StakingKeeper)
require.Equal(
t, app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(addr)),
Expand Down
24 changes: 18 additions & 6 deletions x/slashing/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func TestUnJailNotBonded(t *testing.T) {
// 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(addr, val, 100, true)
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)
tstaking.CreateValidatorWithValPower(addr, val, 100, sdk.AccAddress(val.Address()), *randomEthAddress, true)
}

staking.EndBlocker(ctx, app.StakingKeeper)
Expand All @@ -40,7 +42,9 @@ 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 := app.StakingKeeper.TokensFromConsensusPower(ctx, 50)
msg := tstaking.CreateValidatorMsg(addr, val, amt)
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)
msg := tstaking.CreateValidatorMsg(addr, val, amt, sdk.AccAddress(val.Address()), *randomEthAddress)
msg.MinSelfDelegation = amt
res, err := tstaking.CreateValidatorWithMsg(sdk.WrapSDKContext(ctx), msg)
require.NoError(t, err)
Expand Down Expand Up @@ -91,9 +95,11 @@ func TestHandleNewValidator(t *testing.T) {
addr, val := valAddrs[0], pks[0]
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
ctx = ctx.WithBlockHeight(app.SlashingKeeper.SignedBlocksWindow(ctx) + 1)
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)

// Validator created
amt := tstaking.CreateValidatorWithValPower(addr, val, 100, true)
amt := tstaking.CreateValidatorWithValPower(addr, val, 100, sdk.AccAddress(val.Address()), *randomEthAddress, true)

staking.EndBlocker(ctx, app.StakingKeeper)
require.Equal(
Expand Down Expand Up @@ -136,9 +142,11 @@ func TestHandleAlreadyJailed(t *testing.T) {
pks := simapp.CreateTestPubKeys(1)
addr, val := valAddrs[0], pks[0]
power := int64(100)
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)

amt := tstaking.CreateValidatorWithValPower(addr, val, power, true)
amt := tstaking.CreateValidatorWithValPower(addr, val, power, sdk.AccAddress(val.Address()), *randomEthAddress, true)

staking.EndBlocker(ctx, app.StakingKeeper)

Expand Down Expand Up @@ -197,8 +205,10 @@ func TestValidatorDippingInAndOut(t *testing.T) {
consAddr := sdk.ConsAddress(addr)
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
valAddr := sdk.ValAddress(addr)
randomEthAddress, err := teststaking.RandomEthAddress()
require.NoError(t, err)

tstaking.CreateValidatorWithValPower(valAddr, val, power, true)
tstaking.CreateValidatorWithValPower(valAddr, val, power, sdk.AccAddress(val.Address()), *randomEthAddress, true)
validatorUpdates := staking.EndBlocker(ctx, app.StakingKeeper)
require.Equal(t, 2, len(validatorUpdates))
tstaking.CheckValidator(valAddr, stakingtypes.Bonded, false)
Expand All @@ -211,7 +221,9 @@ func TestValidatorDippingInAndOut(t *testing.T) {
}

// kick first validator out of validator set
tstaking.CreateValidatorWithValPower(sdk.ValAddress(pks[1].Address()), pks[1], power+1, true)
randomEthAddress2, err := teststaking.RandomEthAddress()
require.NoError(t, err)
tstaking.CreateValidatorWithValPower(sdk.ValAddress(pks[1].Address()), pks[1], power+1, sdk.AccAddress(pks[1].Address()), *randomEthAddress2, true)
validatorUpdates = staking.EndBlocker(ctx, app.StakingKeeper)
require.Equal(t, 2, len(validatorUpdates))
tstaking.CheckValidator(sdk.ValAddress(pks[1].Address()), stakingtypes.Bonded, false)
Expand Down
2 changes: 1 addition & 1 deletion x/staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func (k msgServer) validateEthereumAddress(ctx sdk.Context, ethAddr string) (typ
if err != nil {
return types.EthAddress{}, err
}
if evmAddr.GetAddress() == types.EthZeroAddress {
if evmAddr.GetAddress() == types.EthZeroAddress.GetAddress() {
return types.EthAddress{}, types.ErrValidatorEthereumZeroAddress
}
if _, found := k.GetValidatorByEthereumAddress(ctx, *evmAddr); found {
Expand Down
Loading

0 comments on commit c0080f9

Please sign in to comment.