Skip to content

Commit

Permalink
Add updates to ibctesting framework for MS (#1472)
Browse files Browse the repository at this point in the history
* Add message fees

* Init slashing module for tests

* Capture endblock ibc events and other ibctesting updates
  • Loading branch information
alpe authored Jul 4, 2023
1 parent 255ba09 commit f171a67
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 42 deletions.
45 changes: 35 additions & 10 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -168,14 +169,28 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs
)
// commit genesis changes
app.Commit()
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{
ChainID: chainID,
Height: app.LastBlockHeight() + 1,
AppHash: app.LastCommitID().Hash,
Time: time.Now().UTC(),
ValidatorsHash: valSet.Hash(),
NextValidatorsHash: valSet.Hash(),
}})

votes := make([]abci.VoteInfo, len(valSet.Validators))
for i, v := range valSet.Validators {
votes[i] = abci.VoteInfo{
Validator: abci.Validator{Address: v.Address, Power: v.VotingPower},
SignedLastBlock: true,
}
}

app.BeginBlock(abci.RequestBeginBlock{
Header: tmproto.Header{
ChainID: chainID,
Height: app.LastBlockHeight() + 1,
AppHash: app.LastCommitID().Hash,
Time: time.Now().UTC(),
ValidatorsHash: valSet.Hash(),
NextValidatorsHash: valSet.Hash(),
},
LastCommitInfo: abci.CommitInfo{
Votes: votes,
},
})

return app
}
Expand Down Expand Up @@ -289,14 +304,14 @@ func NewTestNetworkFixture() network.TestFixture {

// SignAndDeliverWithoutCommit signs and delivers a transaction. No commit
func SignAndDeliverWithoutCommit(
t *testing.T, txCfg client.TxConfig, app *bam.BaseApp, header tmproto.Header, msgs []sdk.Msg,
t *testing.T, txCfg client.TxConfig, app *bam.BaseApp, msgs []sdk.Msg, fees sdk.Coins,
chainID string, accNums, accSeqs []uint64, priv ...cryptotypes.PrivKey,
) (sdk.GasInfo, *sdk.Result, error) {
tx, err := simtestutil.GenSignedMockTx(
rand.New(rand.NewSource(time.Now().UnixNano())),
txCfg,
msgs,
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)},
fees,
simtestutil.DefaultGenTxGas,
chainID,
accNums,
Expand Down Expand Up @@ -364,6 +379,16 @@ func GenesisStateWithValSet(
stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations)
genesisState[stakingtypes.ModuleName] = codec.MustMarshalJSON(stakingGenesis)

signingInfos := make([]slashingtypes.SigningInfo, len(valSet.Validators))
for i, val := range valSet.Validators {
signingInfos[i] = slashingtypes.SigningInfo{
Address: sdk.ConsAddress(val.Address).String(),
ValidatorSigningInfo: slashingtypes.ValidatorSigningInfo{},
}
}
slashingGenesis := slashingtypes.NewGenesisState(slashingtypes.DefaultParams(), signingInfos, nil)
genesisState[slashingtypes.ModuleName] = codec.MustMarshalJSON(slashingGenesis)

// add bonded amount to bonded pool module account
balances = append(balances, banktypes.Balance{
Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(),
Expand Down
60 changes: 36 additions & 24 deletions x/wasm/ibctesting/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,45 @@ import (
"testing"
"time"

"github.com/cosmos/cosmos-sdk/baseapp"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"

"github.com/CosmWasm/wasmd/app"
"github.com/CosmWasm/wasmd/x/wasm"

// simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/crypto/tmhash"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmprotoversion "github.com/cometbft/cometbft/proto/tendermint/version"
tmtypes "github.com/cometbft/cometbft/types"
tmversion "github.com/cometbft/cometbft/version"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/testutil"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
commitmenttypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types"
host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"
"github.com/cosmos/ibc-go/v7/modules/core/types"
ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
ibctesting "github.com/cosmos/ibc-go/v7/testing"
"github.com/cosmos/ibc-go/v7/testing/mock"
"github.com/stretchr/testify/require"

"github.com/CosmWasm/wasmd/app"
"github.com/CosmWasm/wasmd/x/wasm"
)

var MaxAccounts = 10
Expand Down Expand Up @@ -106,6 +104,7 @@ type TestChain struct {
SenderAccounts []SenderAccount

PendingSendPackets []channeltypes.Packet
DefaultMsgFees sdk.Coins
}

type PacketAck struct {
Expand Down Expand Up @@ -224,6 +223,7 @@ func NewTestChainWithValSet(t *testing.T, coord *Coordinator, appFactory ChainAp
SenderPrivKey: senderAccs[0].SenderPrivKey,
SenderAccount: senderAccs[0].SenderAccount,
SenderAccounts: senderAccs,
DefaultMsgFees: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.ZeroInt())),
}

coord.CommitBlock(chain)
Expand Down Expand Up @@ -315,10 +315,10 @@ func (chain *TestChain) QueryConsensusStateProof(clientID string) ([]byte, clien
// of the next block being created. This follows the Tendermint protocol of applying valset changes
// returned on block `n` to the validators of block `n+2`.
// It calls BeginBlock with the new block created before returning.
func (chain *TestChain) NextBlock() {
func (chain *TestChain) NextBlock() abci.ResponseEndBlock {
res := chain.App.EndBlock(abci.RequestEndBlock{Height: chain.CurrentHeader.Height})

chain.App.Commit()
chain.CaptureIBCEvents(res.Events)

// set the last header to the current header
// use nil trusted fields
Expand All @@ -342,7 +342,20 @@ func (chain *TestChain) NextBlock() {
ProposerAddress: chain.CurrentHeader.ProposerAddress,
}

chain.App.BeginBlock(abci.RequestBeginBlock{Header: chain.CurrentHeader})
votes := make([]abci.VoteInfo, len(chain.Vals.Validators))
for i, v := range chain.Vals.Validators {
votes[i] = abci.VoteInfo{
Validator: abci.Validator{Address: v.Address, Power: v.VotingPower},
SignedLastBlock: true,
}
}
chain.App.BeginBlock(abci.RequestBeginBlock{
Header: chain.CurrentHeader,
LastCommitInfo: abci.CommitInfo{
Votes: votes,
},
})
return res
}

// sendMsgs delivers a transaction through the application without returning the result.
Expand All @@ -357,13 +370,12 @@ func (chain *TestChain) sendMsgs(msgs ...sdk.Msg) error {
func (chain *TestChain) SendMsgs(msgs ...sdk.Msg) (*sdk.Result, error) {
// ensure the chain has the latest time
chain.Coordinator.UpdateTimeForChain(chain)

_, r, gotErr := app.SignAndDeliverWithoutCommit(
chain.t,
chain.TxConfig,
chain.App.GetBaseApp(),
chain.GetContext().BlockHeader(),
msgs,
chain.DefaultMsgFees,
chain.ChainID,
[]uint64{chain.SenderAccount.GetAccountNumber()},
[]uint64{chain.SenderAccount.GetSequence()},
Expand All @@ -381,13 +393,13 @@ func (chain *TestChain) SendMsgs(msgs ...sdk.Msg) (*sdk.Result, error) {
return nil, gotErr
}

chain.CaptureIBCEvents(r)
chain.CaptureIBCEvents(r.Events)

return r, nil
}

func (chain *TestChain) CaptureIBCEvents(r *sdk.Result) {
toSend := GetSendPackets(r.Events)
func (chain *TestChain) CaptureIBCEvents(evts []abci.Event) {
toSend := GetSendPackets(evts)
if len(toSend) > 0 {
// Keep a queue on the chain that we can relay in tests
chain.PendingSendPackets = append(chain.PendingSendPackets, toSend...)
Expand Down
11 changes: 5 additions & 6 deletions x/wasm/ibctesting/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ func (coord *Coordinator) CommitBlock(chains ...*TestChain) {
// CommitNBlocks commits n blocks to state and updates the block height by 1 for each commit.
func (coord *Coordinator) CommitNBlocks(chain *TestChain, n uint64) {
for i := uint64(0); i < n; i++ {
chain.App.BeginBlock(abci.RequestBeginBlock{Header: chain.CurrentHeader})
chain.NextBlock()
coord.IncrementTime()
}
Expand Down Expand Up @@ -254,6 +253,7 @@ func (coord *Coordinator) ChanOpenInitOnBothChains(path *Path) error {
func (coord *Coordinator) RelayAndAckPendingPackets(path *Path) error {
// get all the packet to relay src->dest
src := path.EndpointA
require.NoError(coord.t, src.UpdateClient())
coord.t.Logf("Relay: %d Packets A->B, %d Packets B->A\n", len(src.Chain.PendingSendPackets), len(path.EndpointB.Chain.PendingSendPackets))
for i, v := range src.Chain.PendingSendPackets {
err := path.RelayPacket(v, nil)
Expand All @@ -264,6 +264,7 @@ func (coord *Coordinator) RelayAndAckPendingPackets(path *Path) error {
}

src = path.EndpointB
require.NoError(coord.t, src.UpdateClient())
for i, v := range src.Chain.PendingSendPackets {
err := path.RelayPacket(v, nil)
if err != nil {
Expand All @@ -275,17 +276,15 @@ func (coord *Coordinator) RelayAndAckPendingPackets(path *Path) error {
}

// TimeoutPendingPackets returns the package to source chain to let the IBC app revert any operation.
// from A to A
// from A to B
func (coord *Coordinator) TimeoutPendingPackets(path *Path) error {
src := path.EndpointA
dest := path.EndpointB

toSend := src.Chain.PendingSendPackets
coord.t.Logf("Timeout %d Packets A->A\n", len(toSend))
coord.t.Logf("Timeout %d Packets A->B\n", len(toSend))
require.NoError(coord.t, src.UpdateClient())

if err := src.UpdateClient(); err != nil {
return err
}
// Increment time and commit block so that 5 second delay period passes between send and receive
coord.IncrementTime()
coord.CommitBlock(src.Chain, dest.Chain)
Expand Down
4 changes: 2 additions & 2 deletions x/wasm/ibctesting/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func (chain *TestChain) SendNonDefaultSenderMsgs(senderPrivKey cryptotypes.PrivK
chain.t,
chain.TxConfig,
chain.App.GetBaseApp(),
chain.GetContext().BlockHeader(),
msgs,
chain.DefaultMsgFees,
chain.ChainID,
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
Expand All @@ -48,6 +48,6 @@ func (chain *TestChain) SendNonDefaultSenderMsgs(senderPrivKey cryptotypes.PrivK
if err != nil {
return r, err
}
chain.CaptureIBCEvents(r)
chain.CaptureIBCEvents(r.Events)
return r, nil
}

0 comments on commit f171a67

Please sign in to comment.