Skip to content

Instantly share code, notes, and snippets.

@sainoe
Last active March 3, 2022 09:31
Show Gist options
  • Save sainoe/f37674a0a6f7e6e05889f7612a262a38 to your computer and use it in GitHub Desktop.
Save sainoe/f37674a0a6f7e6e05889f7612a262a38 to your computer and use it in GitHub Desktop.
IBC-GO: update validator set in testing suite
package ibc_testing_test
import (
"fmt"
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
ibctesting "github.com/cosmos/ibc-go/v3/testing"
"github.com/stretchr/testify/suite"
)
// KeeperTestSuite is a testing suite to test keeper functions.
type KeeperTestSuite struct {
suite.Suite
coordinator *ibctesting.Coordinator
// testing chains used for convenience and readability
chainA *ibctesting.TestChain
chainB *ibctesting.TestChain
ctx sdk.Context
}
// TestKeeperTestSuite runs all the tests within this package.
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}
// SetupTest creates a coordinator with 2 test chains.
func (s *KeeperTestSuite) SetupTest() {
s.coordinator = ibctesting.NewCoordinator(s.T(), 2)
s.chainA = s.coordinator.GetChain(ibctesting.GetChainID(1))
s.chainB = s.coordinator.GetChain(ibctesting.GetChainID(2))
s.ctx = s.chainA.GetContext()
}
func (s *KeeperTestSuite) TestUpdateValset() {
path := ibctesting.NewPath(s.chainA, s.chainB)
s.coordinator.Setup(path)
ctx := s.chainA.GetContext()
s.Require().Equal("07-tendermint-0", path.EndpointA.ClientID)
s.Require().Equal("connection-0", path.EndpointA.ConnectionID)
s.Require().Equal("channel-0", path.EndpointA.ChannelID)
chainAStakingKeeper := s.chainA.App.GetStakingKeeper()
// Choose a validator, and get its address and data structure into the correct types
tmValidator := s.chainA.Vals.Validators[0]
valAddr, err := sdk.ValAddressFromHex(tmValidator.Address.String())
s.Require().NoError(err)
// jail validator to zero its power in the validator set
chainAStakingKeeper.Jail(ctx, sdk.ConsAddress(tmValidator.Address))
// check that validator was jailed
val, ok := chainAStakingKeeper.GetValidatorByConsAddr(ctx, sdk.ConsAddress(valAddr))
s.Require().True(ok)
s.Require().True(val.Jailed)
s.coordinator.CommitBlock(s.chainA)
// Check that the chain next validator hash is updated
s.Require().NotEqualValues(s.chainA.CurrentHeader.GetNextValidatorsHash(), s.chainA.CurrentHeader.GetValidatorsHash())
err = path.EndpointB.UpdateClient()
s.Require().NoError(err)
// Check the staking module states are updated
vals, ok := s.chainA.GetValsAtHeight(s.chainA.CurrentHeader.Height)
s.Require().True(ok)
s.Require().Len(vals.Validators, 3)
// Check the chain validator hash is updated
s.Require().EqualValues(s.chainA.CurrentHeader.GetNextValidatorsHash(), s.chainA.CurrentHeader.GetValidatorsHash())
// Check the validator set is updated in chainA
s.Require().Len(s.chainA.Vals.Validators, 3)
// Update the second client chain;
err = path.EndpointB.UpdateClient()
s.Require().NoError(err)
}
func (s *KeeperTestSuite) TestUpdateValset2() {
path := ibctesting.NewPath(s.chainA, s.chainB)
s.coordinator.Setup(path)
ctx := s.chainA.GetContext()
chainAStakingKeeper := s.chainA.App.GetStakingKeeper()
bondAmt := sdk.NewInt(1000000)
delAddr := s.chainA.SenderAccount.GetAddress()
tmValidator := s.chainA.Vals.Validators[0]
valAddr, err := sdk.ValAddressFromHex(tmValidator.Address.String())
s.Require().NoError(err)
validator, found := chainAStakingKeeper.GetValidator(ctx, valAddr)
s.Require().True(found)
_, err = chainAStakingKeeper.Delegate(ctx, delAddr, bondAmt, stakingtypes.Unbonded,
validator, true)
s.Require().NoError(err)
s.coordinator.CommitBlock(s.chainA)
err = path.EndpointB.UpdateClient()
s.Require().NoError(err)
s.Require().Equal(s.chainA.Vals.TotalVotingPower(), int64(5))
// Undelegate all shares
_, err = chainAStakingKeeper.Undelegate(ctx, delAddr, valAddr, sdk.NewDec(2))
s.Require().NoError(err)
s.coordinator.CommitBlock(s.chainA)
err = path.EndpointB.UpdateClient()
s.Require().NoError(err)
ubd, found := chainAStakingKeeper.GetUnbondingDelegation(ctx, delAddr, valAddr)
s.Require().True(found)
s.Require().Len(ubd.Entries, 1)
fmt.Println(ubd.Entries[0].String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment