Skip to content

Commit

Permalink
proper init of ibc middleware testsuite
Browse files Browse the repository at this point in the history
  • Loading branch information
pr0n00gler committed Sep 4, 2024
1 parent 0e5e0b9 commit 022dbff
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
32 changes: 30 additions & 2 deletions testutil/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
abcit "github.com/cometbft/cometbft/abci/types"
tmrand "github.com/cometbft/cometbft/libs/rand"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
Expand Down Expand Up @@ -547,7 +548,7 @@ func (suite *IBCConnectionTestSuite) SendMsgsNoCheck(chain *ibctesting.TestChain
return nil, err
}

//chain.commitBlock(resp)
commitBlock(chain, resp)

suite.Coordinator.IncrementTime()

Expand All @@ -563,6 +564,33 @@ func (suite *IBCConnectionTestSuite) SendMsgsNoCheck(chain *ibctesting.TestChain
return txResult, nil
}

func commitBlock(chain *ibctesting.TestChain, res *cometbfttypes.ResponseFinalizeBlock) {
_, err := chain.App.Commit()
require.NoError(chain.TB, err)

// set the last header to the current header
// use nil trusted fields
chain.LastHeader = chain.CurrentTMClientHeader()

// val set changes returned from previous block get applied to the next validators
// of this block. See tendermint spec for details.
chain.Vals = chain.NextVals
chain.NextVals = ibctesting.ApplyValSetChanges(chain, chain.Vals, res.ValidatorUpdates)

// increment the current header
chain.CurrentHeader = cmtproto.Header{
ChainID: chain.ChainID,
Height: chain.App.LastBlockHeight() + 1,
AppHash: chain.App.LastCommitID().Hash,
// NOTE: the time is increased by the coordinator to maintain time synchrony amongst
// chains.
Time: chain.CurrentHeader.Time,
ValidatorsHash: chain.Vals.Hash(),
NextValidatorsHash: chain.NextVals.Hash(),
ProposerAddress: chain.CurrentHeader.ProposerAddress,
}
}

// SignAndDeliver signs and delivers a transaction without asserting the results. This overrides the function
// from ibctesting
func SignAndDeliver(
Expand All @@ -575,7 +603,7 @@ func SignAndDeliver(
blockTime time.Time,
nextValHash []byte,
priv ...cryptotypes.PrivKey,
) (res *abcit.ResponseFinalizeBlock, err error) {
) (res *cometbfttypes.ResponseFinalizeBlock, err error) {
tb.Helper()
tx, err := simtestutil.GenSignedMockTx(
rand.New(rand.NewSource(time.Now().UnixNano())),
Expand Down
34 changes: 18 additions & 16 deletions x/ibc-rate-limit/ibc_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (

sdkmath "cosmossdk.io/math"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
"github.com/neutron-org/neutron/v4/testutil"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"

"github.com/neutron-org/neutron/v4/testutil"

bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"

abci "github.com/cometbft/cometbft/abci/types"
Expand All @@ -24,6 +25,7 @@ import (
"github.com/stretchr/testify/suite"

sdktypes "github.com/cosmos/cosmos-sdk/types"

"github.com/neutron-org/neutron/v4/x/ibc-rate-limit/types"
)

Expand Down Expand Up @@ -70,8 +72,8 @@ func (suite *MiddlewareTestSuite) MessageFromAToB(denom string, amount sdkmath.I

func (suite *MiddlewareTestSuite) MessageFromBToA(denom string, amount sdkmath.Int) sdk.Msg {
coin := sdk.NewCoin(denom, amount)
port := suite.Path.EndpointB.ChannelConfig.PortID
channel := suite.Path.EndpointB.ChannelID
port := suite.TransferPath.EndpointB.ChannelConfig.PortID
channel := suite.TransferPath.EndpointB.ChannelID
accountFrom := suite.ChainB.SenderAccount.GetAddress().String()
accountTo := suite.ChainA.SenderAccount.GetAddress().String()
timeoutHeight := clienttypes.NewHeight(10, 100)
Expand All @@ -89,8 +91,8 @@ func (suite *MiddlewareTestSuite) MessageFromBToA(denom string, amount sdkmath.I

//func (suite *MiddlewareTestSuite) MessageFromAToC(denom string, amount sdkmath.Int) sdk.Msg {

Check failure on line 92 in x/ibc-rate-limit/ibc_middleware_test.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
// coin := sdk.NewCoin(denom, amount)
// port := suite.PathAC.EndpointA.ChannelConfig.PortID
// channel := suite.PathAC.EndpointA.ChannelID
// port := suite.TransferPathAC.EndpointA.ChannelConfig.PortID
// channel := suite.TransferPathAC.EndpointA.ChannelID
// accountFrom := suite.ChainA.SenderAccount.GetAddress().String()
// accountTo := suite.ChainC.SenderAccount.GetAddress().String()
// timeoutHeight := clienttypes.NewHeight(10, 100)
Expand Down Expand Up @@ -210,12 +212,12 @@ func (suite *MiddlewareTestSuite) FullSendAToB(msg sdk.Msg) (*abci.ExecTxResult,
// return nil, "", err
// }
//
// err = suite.PathAC.EndpointB.UpdateClient()
// err = suite.TransferPathAC.EndpointB.UpdateClient()
// if err != nil {
// return nil, "", err
// }
//
// res, err := suite.PathAC.EndpointB.RecvPacketWithResult(packet)
// res, err := suite.TransferPathAC.EndpointB.RecvPacketWithResult(packet)
// if err != nil {
// return nil, "", err
// }
Expand All @@ -225,11 +227,11 @@ func (suite *MiddlewareTestSuite) FullSendAToB(msg sdk.Msg) (*abci.ExecTxResult,
// return nil, "", err
// }
//
// err = suite.PathAC.EndpointA.UpdateClient()
// err = suite.TransferPathAC.EndpointA.UpdateClient()
// if err != nil {
// return nil, "", err
// }
// err = suite.PathAC.EndpointB.UpdateClient()
// err = suite.TransferPathAC.EndpointB.UpdateClient()
// if err != nil {
// return nil, "", err
// }
Expand Down Expand Up @@ -498,8 +500,8 @@ func (suite *MiddlewareTestSuite) TestFailedSendTransfer() {

// Use the whole quota
coins := sdk.NewCoin(sdk.DefaultBondDenom, quota)
port := suite.Path.EndpointA.ChannelConfig.PortID
channel := suite.Path.EndpointA.ChannelID
port := suite.TransferPath.EndpointA.ChannelConfig.PortID
channel := suite.TransferPath.EndpointA.ChannelID
accountFrom := suite.ChainA.SenderAccount.GetAddress().String()
timeoutHeight := clienttypes.NewHeight(10, 100)
msg := transfertypes.NewMsgTransfer(port, channel, coins, accountFrom, "INVALID", timeoutHeight, 0, "")
Expand All @@ -520,9 +522,9 @@ func (suite *MiddlewareTestSuite) TestFailedSendTransfer() {
suite.ChainA.Coordinator.IncrementTime()

// Update both clients
err = suite.Path.EndpointA.UpdateClient()
err = suite.TransferPath.EndpointA.UpdateClient()
suite.Require().NoError(err)
err = suite.Path.EndpointB.UpdateClient()
err = suite.TransferPath.EndpointB.UpdateClient()
suite.Require().NoError(err)

// Execute the acknowledgement from chain B in chain A
Expand All @@ -532,15 +534,15 @@ func (suite *MiddlewareTestSuite) TestFailedSendTransfer() {
suite.Require().NoError(err)

// recv in chain b
newRes, err := suite.Path.EndpointB.RecvPacketWithResult(packet)
newRes, err := suite.TransferPath.EndpointB.RecvPacketWithResult(packet)
suite.Require().NoError(err)

// get the ack from the chain b's response
ack, err := ibctesting.ParseAckFromEvents(newRes.GetEvents())
suite.Require().NoError(err)

// manually relay it to chain a
err = suite.Path.EndpointA.AcknowledgePacket(packet, ack)
err = suite.TransferPath.EndpointA.AcknowledgePacket(packet, ack)
suite.Require().NoError(err)

// We should be able to send again because the packet that exceeded the quota failed and has been reverted
Expand Down Expand Up @@ -598,7 +600,7 @@ func (suite *MiddlewareTestSuite) TestUnsetRateLimitingContract() {
//
// denom := sdk.DefaultBondDenom
// sendAmount := sdkmath.NewInt(1000)
// acceptedChannel := suite.Path.EndpointA.ChannelID
// acceptedChannel := suite.TransferPath.EndpointA.ChannelID
//
// // Successfully send a denom before any restrictions are added.
// _, err := suite.AssertSend(true, suite.MessageFromAToB(denom, sendAmount))
Expand Down

0 comments on commit 022dbff

Please sign in to comment.