From 9edc79be9d80d2de090eb18c140a31c0602d2e50 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Fri, 3 May 2024 22:43:20 +0700 Subject: [PATCH] fix test --- app/helpers/test_helpers.go | 48 ++------ app/ibctesting/chain.go | 9 +- app/test_support.go | 5 + x/ibc-hooks/relay_test.go | 238 ------------------------------------ 4 files changed, 23 insertions(+), 277 deletions(-) delete mode 100644 x/ibc-hooks/relay_test.go diff --git a/app/helpers/test_helpers.go b/app/helpers/test_helpers.go index c64e82ea8..3de0a3975 100644 --- a/app/helpers/test_helpers.go +++ b/app/helpers/test_helpers.go @@ -60,27 +60,6 @@ func NewContextForApp(app composable.ComposableApp) sdk.Context { return ctx } -func Setup(t *testing.T, isCheckTx bool, invCheckPeriod uint) *composable.ComposableApp { - t.Helper() - app, genesisState := setup(!isCheckTx, invCheckPeriod) - if !isCheckTx { - // InitChain must be called to stop deliverState from being nil - stateBytes, err := json.MarshalIndent(genesisState, "", " ") - require.NoError(t, err) - - // Initialize the chain - app.InitChain( - &abci.RequestInitChain{ - Validators: []abci.ValidatorUpdate{}, - ConsensusParams: DefaultConsensusParams, - AppStateBytes: stateBytes, - }, - ) - } - - return app -} - func setup(withGenesis bool, invCheckPeriod uint, opts ...wasm.Option) (*composable.ComposableApp, composable.GenesisState) { db := dbm.NewMemDB() encCdc := composable.MakeEncodingConfig() @@ -118,28 +97,27 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs baseapp.SetChainID(chainID)(app.GetBaseApp()) // init chain will set the validator set and initialize the genesis accounts - app.InitChain( + _, err = app.InitChain( &abci.RequestInitChain{ ChainId: chainID, Validators: []abci.ValidatorUpdate{}, ConsensusParams: DefaultConsensusParams, + InitialHeight: app.LastBlockHeight() + 1, AppStateBytes: stateBytes, }, ) + if err != nil { + panic(err) + } - // commit genesis changes - // app.BaseApp.Set - app.Commit() - //* (baseapp) [#15519](https://github.com/cosmos/cosmos-sdk/pull/15519/files) BeginBlock and EndBlock are now internal to baseapp. For testing, user must call `FinalizeBlock`. BeginBlock and EndBlock calls are internal to Baseapp. - app.FinalizeBlock( - &abci.RequestFinalizeBlock{ - // ChainID: chainID, - Height: app.LastBlockHeight() + 1, - // AppHash: app.LastCommitID().Hash, - // ValidatorsHash: valSet.Hash(), - NextValidatorsHash: valSet.Hash(), - }, - ) + _, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{ + Height: app.LastBlockHeight() + 1, + Hash: app.LastCommitID().Hash, + NextValidatorsHash: valSet.Hash(), + }) + if err != nil { + panic(err) + } return app } diff --git a/app/ibctesting/chain.go b/app/ibctesting/chain.go index 00dab1490..8e412f70d 100644 --- a/app/ibctesting/chain.go +++ b/app/ibctesting/chain.go @@ -81,6 +81,7 @@ type ChainApp interface { GetIBCKeeper() *ibckeeper.Keeper GetBankKeeper() bankkeeper.Keeper GetStakingKeeper() *stakingkeeper.Keeper + GetGovKeeper() *govkeeper.Keeper GetAccountKeeper() authkeeper.AccountKeeper GetWasmKeeper() wasmkeeper.Keeper GetPfmKeeper() packetforwardkeeper.Keeper @@ -769,20 +770,20 @@ func (chain *TestChain) QueryContract(suite *suite.Suite, contract sdk.AccAddres // //func (chain *TestChain) StoreContractCode(suite *suite.Suite, path string) { -// govModuleAddress := chain.GetTestSupport().AccountKeeper().GetModuleAddress(govtypes.ModuleName) +// govModuleAddress := chain.App.GetAccountKeeper().GetModuleAddress(govtypes.ModuleName) // wasmCode, err := os.ReadFile(path) // suite.Require().NoError(err) // -// src := wasmtypes.StoreCodeProposalFixture(func(p *wasmtypes.StoreCodeProposal) { //nolint: staticcheck +// src := wasmtypes.New(func(p *wasmtypes.StoreCodeProposal) { //nolint: staticcheck // p.RunAs = govModuleAddress.String() // p.WASMByteCode = wasmCode // checksum := sha256.Sum256(wasmCode) // p.CodeHash = checksum[:] // }) // -// govKeeper := chain.GetTestSupport().GovKeeper() +// govKeeper := chain.App.GetGovKeeper() // // when -// mustSubmitAndExecuteLegacyProposal(suite.T(), chain.GetContext(), src, chain.SenderAccount.GetAddress().String(), &govKeeper, govModuleAddress.String()) +// mustSubmitAndExecuteLegacyProposal(suite.T(), chain.GetContext(), src, chain.SenderAccount.GetAddress().String(), govKeeper, govModuleAddress.String()) // suite.Require().NoError(err) //} diff --git a/app/test_support.go b/app/test_support.go index 4e8cd1190..49e97e342 100644 --- a/app/test_support.go +++ b/app/test_support.go @@ -6,6 +6,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" packetforwardkeeper "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8/packetforward/keeper" capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper" @@ -60,6 +61,10 @@ func (app *ComposableApp) GetTransferMiddlewareKeeper() transfermiddlewarekeeper return app.TransferMiddlewareKeeper } +func (app *ComposableApp) GetGovKeeper() *govkeeper.Keeper { + return &app.GovKeeper +} + // GetTxConfig implements the TestingApp interface. func (app *ComposableApp) GetTxConfig() client.TxConfig { cfg := MakeEncodingConfig() diff --git a/x/ibc-hooks/relay_test.go b/x/ibc-hooks/relay_test.go deleted file mode 100644 index 1aef17d30..000000000 --- a/x/ibc-hooks/relay_test.go +++ /dev/null @@ -1,238 +0,0 @@ -package ibchooks_test - -import ( - "fmt" - customibctesting "github.com/cosmos/ibc-go/v8/testing" - "testing" - "time" - - sdkmath "cosmossdk.io/math" - sdk "github.com/cosmos/cosmos-sdk/types" - transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types" - clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" - "github.com/stretchr/testify/suite" - - ibchookskeeper "github.com/notional-labs/composable/v6/x/ibc-hooks/keeper" -) - -// TODO: use testsuite here. -type IBCHooksTestSuite struct { - suite.Suite - - coordinator *customibctesting.Coordinator - - // testing chains used for convenience and readability - chainA *customibctesting.TestChain - chainB *customibctesting.TestChain - chainC *customibctesting.TestChain -} - -func (suite *IBCHooksTestSuite) SetupTest() { - suite.coordinator = customibctesting.NewCoordinator(suite.T(), 4) - suite.chainA = suite.coordinator.GetChain(customibctesting.GetChainID(1)) - suite.chainB = suite.coordinator.GetChain(customibctesting.GetChainID(2)) - suite.chainC = suite.coordinator.GetChain(customibctesting.GetChainID(3)) -} - -func NewTransferPath(chainA, chainB *customibctesting.TestChain) *customibctesting.Path { - path := customibctesting.NewPath(chainA, chainB) - path.EndpointA.ChannelConfig.PortID = customibctesting.TransferPort - path.EndpointB.ChannelConfig.PortID = customibctesting.TransferPort - path.EndpointA.ChannelConfig.Version = transfertypes.Version - path.EndpointB.ChannelConfig.Version = transfertypes.Version - - return path -} - -func TestKeeperTestSuite(t *testing.T) { - suite.Run(t, new(IBCHooksTestSuite)) -} - -func (suite *IBCHooksTestSuite) TestRecvHooks() { - var ( - transferAmount = sdkmath.NewInt(1000000000) - timeoutHeight = clienttypes.NewHeight(1, 110) - // when transfer via sdk transfer from A (module) -> B (contract) - // nativeTokenSendOnChainA = sdk.NewCoin(sdk.DefaultBondDenom, transferAmount) - ) - - suite.SetupTest() // reset - - path := NewTransferPath(suite.chainA, suite.chainB) - suite.coordinator.Setup(path) - - // store code - suite.chainB.StoreContractCode(&suite.Suite, "../../tests/ibc-hooks/bytecode/counter.wasm") - // instancetiate contract - addr := suite.chainB.InstantiateContract(&suite.Suite, `{"count": 0}`, 1) - suite.Require().NotEmpty(addr) - - memo := fmt.Sprintf(`{"wasm": {"contract": "%s", "msg": {"increment": {} } } }`, addr) - - msg := transfertypes.NewMsgTransfer( - path.EndpointA.ChannelConfig.PortID, - path.EndpointA.ChannelID, - sdk.NewCoin(sdk.DefaultBondDenom, transferAmount), - suite.chainA.SenderAccount.GetAddress().String(), - addr.String(), - timeoutHeight, - 0, - memo, - ) - _, err := suite.chainA.SendMsgs(msg) - suite.Require().NoError(err) - suite.Require().NoError(err, path.EndpointB.UpdateClient()) - - // then - suite.Require().Equal(1, len(suite.chainA.PendingSendPackets)) - suite.Require().Equal(0, len(suite.chainB.PendingSendPackets)) - - // and when relay to chain B and handle Ack on chain A - err = suite.coordinator.RelayAndAckPendingPackets(path) - suite.Require().NoError(err) - - // then - suite.Require().Equal(0, len(suite.chainA.PendingSendPackets)) - suite.Require().Equal(0, len(suite.chainB.PendingSendPackets)) - - senderLocalAcc, err := ibchookskeeper.DeriveIntermediateSender("channel-0", suite.chainA.SenderAccount.GetAddress().String(), "cosmos") - suite.Require().NoError(err) - - state := suite.chainB.QueryContract(&suite.Suite, addr, []byte(fmt.Sprintf(`{"get_count": {"addr": "%s"}}`, senderLocalAcc))) - suite.Require().Equal(`{"count":0}`, state) - - state = suite.chainB.QueryContract(&suite.Suite, addr, []byte(fmt.Sprintf(`{"get_total_funds": {"addr": "%s"}}`, senderLocalAcc))) - suite.Require().Equal(`{"total_funds":[{"denom":"ibc/C053D637CCA2A2BA030E2C5EE1B28A16F71CCB0E45E8BE52766DC1B241B77878","amount":"1000000000"}]}`, state) -} - -func (suite *IBCHooksTestSuite) TestAckHooks() { - var ( - transferAmount = sdkmath.NewInt(1000000000) - timeoutHeight = clienttypes.NewHeight(0, 110) - // when transfer via sdk transfer from A (module) -> B (contract) - // nativeTokenSendOnChainA = sdk.NewCoin(sdk.DefaultBondDenom, transferAmount) - ) - - suite.SetupTest() // reset - - path := NewTransferPath(suite.chainA, suite.chainB) - suite.coordinator.Setup(path) - - // store code - suite.chainA.StoreContractCode(&suite.Suite, "../../tests/ibc-hooks/bytecode/counter.wasm") - // instancetiate contract - addr := suite.chainA.InstantiateContract(&suite.Suite, `{"count": 0}`, 1) - suite.Require().NotEmpty(addr) - - fmt.Println(addr.String()) - - // Generate swap instructions for the contract - callbackMemo := fmt.Sprintf(`{"ibc_callback":"%s"}`, addr) - - msg := transfertypes.NewMsgTransfer( - path.EndpointA.ChannelConfig.PortID, - path.EndpointA.ChannelID, - sdk.NewCoin(sdk.DefaultBondDenom, transferAmount), - suite.chainA.SenderAccount.GetAddress().String(), - addr.String(), - timeoutHeight, - 0, - callbackMemo, - ) - _, err := suite.chainA.SendMsgs(msg) - suite.Require().NoError(err) - suite.Require().NoError(err, path.EndpointB.UpdateClient()) - - // then - suite.Require().Equal(1, len(suite.chainA.PendingSendPackets)) - suite.Require().Equal(0, len(suite.chainB.PendingSendPackets)) - - // and when relay to chain B and handle Ack on chain A - err = suite.coordinator.RelayAndAckPendingPackets(path) - suite.Require().NoError(err) - - // then - suite.Require().Equal(0, len(suite.chainA.PendingSendPackets)) - suite.Require().Equal(0, len(suite.chainB.PendingSendPackets)) - - state := suite.chainA.QueryContract( - &suite.Suite, addr, - []byte(fmt.Sprintf(`{"get_count": {"addr": "%s"}}`, addr))) - suite.Require().Equal(`{"count":1}`, state) - - _, err = suite.chainA.SendMsgs(msg) - suite.Require().NoError(err) - suite.Require().NoError(err, path.EndpointB.UpdateClient()) - - // then - suite.Require().Equal(1, len(suite.chainA.PendingSendPackets)) - suite.Require().Equal(0, len(suite.chainB.PendingSendPackets)) - - // and when relay to chain B and handle Ack on chain A - err = suite.coordinator.RelayAndAckPendingPackets(path) - suite.Require().NoError(err) - - // then - suite.Require().Equal(0, len(suite.chainA.PendingSendPackets)) - suite.Require().Equal(0, len(suite.chainB.PendingSendPackets)) - - state = suite.chainA.QueryContract( - &suite.Suite, addr, - []byte(fmt.Sprintf(`{"get_count": {"addr": "%s"}}`, addr))) - suite.Require().Equal(`{"count":2}`, state) -} - -func (suite *IBCHooksTestSuite) TestTimeoutHooks() { - var ( - transferAmount = sdkmath.NewInt(1000000000) - timeoutHeight = clienttypes.NewHeight(0, 500) - // when transfer via sdk transfer from A (module) -> B (contract) - // nativeTokenSendOnChainA = sdk.NewCoin(sdk.DefaultBondDenom, transferAmount) - ) - - suite.SetupTest() // reset - - path := NewTransferPath(suite.chainA, suite.chainB) - suite.coordinator.Setup(path) - - // store code - suite.chainA.StoreContractCode(&suite.Suite, "../../tests/ibc-hooks/bytecode/counter.wasm") - // instancetiate contract - addr := suite.chainA.InstantiateContract(&suite.Suite, `{"count": 0}`, 1) - suite.Require().NotEmpty(addr) - - fmt.Println(addr.String()) - - // Generate swap instructions for the contract - callbackMemo := fmt.Sprintf(`{"ibc_callback":"%s"}`, addr) - - msg := transfertypes.NewMsgTransfer( - path.EndpointA.ChannelConfig.PortID, - path.EndpointA.ChannelID, - sdk.NewCoin(sdk.DefaultBondDenom, transferAmount), - suite.chainA.SenderAccount.GetAddress().String(), - addr.String(), - timeoutHeight, - uint64(suite.coordinator.CurrentTime.Add(time.Minute).UnixNano()), - callbackMemo, - ) - sdkResult, err := suite.chainA.SendMsgs(msg) - suite.Require().NoError(err) - - packet, err := customibctesting.ParsePacketFromEvents(sdkResult.GetEvents()) - suite.Require().NoError(err) - - // Move chainB forward one block - suite.chainB.NextBlock() - // One month later - suite.coordinator.IncrementTimeBy(time.Hour) - err = path.EndpointA.UpdateClient() - suite.Require().NoError(err) - - err = path.EndpointA.TimeoutPacket(packet) - suite.Require().NoError(err) - - // The test contract will increment the counter for itself by 10 when a packet times out - state := suite.chainA.QueryContract(&suite.Suite, addr, []byte(fmt.Sprintf(`{"get_count": {"addr": "%s"}}`, addr))) - suite.Require().Equal(`{"count":10}`, state) -}