Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event tests assertion for 04-channel #2830

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (core/24-host) [\#2820](https://github.com/cosmos/ibc-go/pull/2820) Add `MustParseClientStatePath` which parses the clientID from a client state key path.
* (apps/27-interchain-accounts) [\#2147](https://github.com/cosmos/ibc-go/pull/2147) Adding a `SubmitTx` gRPC endpoint for the ICS27 Controller module which allows owners of interchain accounts to submit transactions. This replaces the previously existing need for authentication modules to implement this standard functionality.
* (testing/simapp) [\#2190](https://github.com/cosmos/ibc-go/pull/2190) Adding the new `x/group` cosmos-sdk module to simapp.
* (testing) [\#2829](https://github.com/cosmos/ibc-go/pull/2829) Add `AssertEvents` which asserts events against expected event map.

### Bug Fixes

Expand Down
32 changes: 26 additions & 6 deletions modules/apps/transfer/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

"github.com/cosmos/ibc-go/v6/modules/apps/transfer/types"
ibctesting "github.com/cosmos/ibc-go/v6/testing"
)

func (suite *KeeperTestSuite) assertTransferEvents(
Expand Down Expand Up @@ -38,7 +39,10 @@ func (suite *KeeperTestSuite) assertTransferEvents(
}

func (suite *KeeperTestSuite) TestMsgTransfer() {
var msg *types.MsgTransfer
var (
msg *types.MsgTransfer
hasEvents bool
)

testCases := []struct {
name string
Expand All @@ -47,12 +51,15 @@ func (suite *KeeperTestSuite) TestMsgTransfer() {
}{
{
"success",
func() {},
func() {
hasEvents = true
},
true,
},
{
"bank send enabled for denom",
func() {
hasEvents = true
suite.chainA.GetSimApp().BankKeeper.SetParams(suite.chainA.GetContext(),
banktypes.Params{
SendEnabled: []*banktypes.SendEnabled{{Denom: sdk.DefaultBondDenom, Enabled: true}},
Expand Down Expand Up @@ -109,6 +116,7 @@ func (suite *KeeperTestSuite) TestMsgTransfer() {
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest()
hasEvents = false // must be explicitly set

path := NewTransferPath(suite.chainA, suite.chainB)
suite.coordinator.Setup(path)
Expand All @@ -131,14 +139,26 @@ func (suite *KeeperTestSuite) TestMsgTransfer() {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().NotEqual(res.Sequence, uint64(0))

events := ctx.EventManager().Events()
suite.assertTransferEvents(events, coin, "memo")
} else {
suite.Require().Error(err)
suite.Require().Nil(res)
}

events := ctx.EventManager().Events()
// Verify events
events := ctx.EventManager().Events()
expEvents := map[string]map[string]string{
"ibc_transfer": {
"sender": suite.chainA.SenderAccount.GetAddress().String(),
"receiver": suite.chainB.SenderAccount.GetAddress().String(),
"amount": coin.Amount.String(),
"denom": coin.Denom,
"memo": "memo",
},
}

if hasEvents {
ibctesting.AssertEvents(&suite.Suite, expEvents, events)
} else {
suite.Require().Len(events, 0)
}
})
Expand Down
86 changes: 78 additions & 8 deletions modules/core/04-channel/keeper/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ func (suite *KeeperTestSuite) TestChanOpenInit() {

counterparty := types.NewCounterparty(ibctesting.MockPort, ibctesting.FirstChannelID)

ctx := suite.chainA.GetContext()
channelID, cap, err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.ChanOpenInit(
suite.chainA.GetContext(), path.EndpointA.ChannelConfig.Order, []string{path.EndpointA.ConnectionID},
ctx, path.EndpointA.ChannelConfig.Order, []string{path.EndpointA.ConnectionID},
path.EndpointA.ChannelConfig.PortID, portCap, counterparty, path.EndpointA.ChannelConfig.Version,
)

Expand All @@ -122,7 +123,7 @@ func (suite *KeeperTestSuite) TestChanOpenInit() {
suite.Require().Equal(types.FormatChannelIdentifier(0), channelID)

chanCap, ok := suite.chainA.App.GetScopedIBCKeeper().GetCapability(
suite.chainA.GetContext(),
ctx,
host.ChannelCapabilityPath(path.EndpointA.ChannelConfig.PortID, channelID),
)
suite.Require().True(ok, "could not retrieve channel capability after successful ChanOpenInit")
Expand All @@ -132,6 +133,10 @@ func (suite *KeeperTestSuite) TestChanOpenInit() {
suite.Require().Nil(cap)
suite.Require().Equal("", channelID)
}

// Verify events
events := ctx.EventManager().Events()
suite.Require().Len(events, 0) // assert no events emitted
}
})
}
Expand Down Expand Up @@ -254,8 +259,9 @@ func (suite *KeeperTestSuite) TestChanOpenTry() {
channelKey := host.ChannelKey(counterparty.PortId, counterparty.ChannelId)
proof, proofHeight := suite.chainA.QueryProof(channelKey)

ctx := suite.chainB.GetContext()
channelID, cap, err := suite.chainB.App.GetIBCKeeper().ChannelKeeper.ChanOpenTry(
suite.chainB.GetContext(), types.ORDERED, []string{path.EndpointB.ConnectionID},
ctx, types.ORDERED, []string{path.EndpointB.ConnectionID},
path.EndpointB.ChannelConfig.PortID, portCap, counterparty, path.EndpointA.ChannelConfig.Version,
proof, malleateHeight(proofHeight, heightDiff),
)
Expand All @@ -265,14 +271,18 @@ func (suite *KeeperTestSuite) TestChanOpenTry() {
suite.Require().NotNil(cap)

chanCap, ok := suite.chainB.App.GetScopedIBCKeeper().GetCapability(
suite.chainB.GetContext(),
ctx,
host.ChannelCapabilityPath(path.EndpointB.ChannelConfig.PortID, channelID),
)
suite.Require().True(ok, "could not retrieve channel capapbility after successful ChanOpenTry")
suite.Require().Equal(chanCap.String(), cap.String(), "channel capability is not correct")
} else {
suite.Require().Error(err)
}

// Verify events
events := ctx.EventManager().Events()
suite.Require().Len(events, 0) // assert no events emitted
})
}
}
Expand Down Expand Up @@ -434,8 +444,9 @@ func (suite *KeeperTestSuite) TestChanOpenAck() {
channelKey := host.ChannelKey(path.EndpointB.ChannelConfig.PortID, ibctesting.FirstChannelID)
proof, proofHeight := suite.chainB.QueryProof(channelKey)

ctx := suite.chainA.GetContext()
err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.ChanOpenAck(
suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, channelCap, path.EndpointB.ChannelConfig.Version, counterpartyChannelID,
ctx, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, channelCap, path.EndpointB.ChannelConfig.Version, counterpartyChannelID,
proof, malleateHeight(proofHeight, heightDiff),
)

Expand All @@ -444,6 +455,10 @@ func (suite *KeeperTestSuite) TestChanOpenAck() {
} else {
suite.Require().Error(err)
}

// Verify events
events := ctx.EventManager().Events()
suite.Require().Len(events, 0) // assert no events emitted
})
}
}
Expand Down Expand Up @@ -574,8 +589,9 @@ func (suite *KeeperTestSuite) TestChanOpenConfirm() {
channelKey := host.ChannelKey(path.EndpointA.ChannelConfig.PortID, ibctesting.FirstChannelID)
proof, proofHeight := suite.chainA.QueryProof(channelKey)

ctx := suite.chainB.GetContext()
err := suite.chainB.App.GetIBCKeeper().ChannelKeeper.ChanOpenConfirm(
suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, ibctesting.FirstChannelID,
ctx, path.EndpointB.ChannelConfig.PortID, ibctesting.FirstChannelID,
channelCap, proof, malleateHeight(proofHeight, heightDiff),
)

Expand All @@ -584,6 +600,10 @@ func (suite *KeeperTestSuite) TestChanOpenConfirm() {
} else {
suite.Require().Error(err)
}

// Verify events
events := ctx.EventManager().Events()
suite.Require().Len(events, 0) // assert no events emitted
})
}
}
Expand All @@ -594,10 +614,12 @@ func (suite *KeeperTestSuite) TestChanCloseInit() {
var (
path *ibctesting.Path
channelCap *capabilitytypes.Capability
hasEvents bool
)

testCases := []testCase{
{"success", func() {
hasEvents = true
suite.coordinator.Setup(path)
channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
}, true},
Expand Down Expand Up @@ -655,19 +677,42 @@ func (suite *KeeperTestSuite) TestChanCloseInit() {
tc := tc
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset
hasEvents = false
path = ibctesting.NewPath(suite.chainA, suite.chainB)

tc.malleate()

ctx := suite.chainA.GetContext()
err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.ChanCloseInit(
suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, ibctesting.FirstChannelID, channelCap,
ctx, path.EndpointA.ChannelConfig.PortID, ibctesting.FirstChannelID, channelCap,
)

if tc.expPass {
suite.Require().NoError(err)
} else {
suite.Require().Error(err)
}

// Verify events
events := ctx.EventManager().Events()
expEvents := map[string]map[string]string{
"channel_close_init": {
"port_id": path.EndpointA.ChannelConfig.PortID,
"channel_id": path.EndpointA.ChannelID,
"counterparty_port_id": path.EndpointB.ChannelConfig.PortID,
"counterparty_channel_id": path.EndpointB.ChannelID,
"connection_id": path.EndpointA.ConnectionID,
},
"message": {
"module": "ibc_channel",
},
}

if hasEvents {
ibctesting.AssertEvents(&suite.Suite, expEvents, events)
} else {
suite.Require().Len(events, 0)
}
})
}
}
Expand All @@ -680,10 +725,12 @@ func (suite *KeeperTestSuite) TestChanCloseConfirm() {
path *ibctesting.Path
channelCap *capabilitytypes.Capability
heightDiff uint64
hasEvents bool
)

testCases := []testCase{
{"success", func() {
hasEvents = true
suite.coordinator.Setup(path)
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)

Expand Down Expand Up @@ -760,15 +807,17 @@ func (suite *KeeperTestSuite) TestChanCloseConfirm() {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset
heightDiff = 0 // must explicitly be changed
hasEvents = false
path = ibctesting.NewPath(suite.chainA, suite.chainB)

tc.malleate()

channelKey := host.ChannelKey(path.EndpointA.ChannelConfig.PortID, ibctesting.FirstChannelID)
proof, proofHeight := suite.chainA.QueryProof(channelKey)

ctx := suite.chainB.GetContext()
err := suite.chainB.App.GetIBCKeeper().ChannelKeeper.ChanCloseConfirm(
suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, ibctesting.FirstChannelID, channelCap,
ctx, path.EndpointB.ChannelConfig.PortID, ibctesting.FirstChannelID, channelCap,
proof, malleateHeight(proofHeight, heightDiff),
)

Expand All @@ -777,6 +826,27 @@ func (suite *KeeperTestSuite) TestChanCloseConfirm() {
} else {
suite.Require().Error(err)
}

// Verify events
events := ctx.EventManager().Events()
expEvents := map[string]map[string]string{
"channel_close_confirm": {
"port_id": path.EndpointB.ChannelConfig.PortID,
"channel_id": path.EndpointB.ChannelID,
"counterparty_port_id": path.EndpointA.ChannelConfig.PortID,
"counterparty_channel_id": path.EndpointA.ChannelID,
"connection_id": path.EndpointB.ConnectionID,
},
"message": {
"module": "ibc_channel",
},
}

if hasEvents {
ibctesting.AssertEvents(&suite.Suite, expEvents, events)
} else {
suite.Require().Len(events, 0)
}
})
}
}
Expand Down
Loading