Skip to content

Commit

Permalink
Add event testing to handshake, packets and timeouts functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Anmol1696 committed Nov 26, 2022
1 parent 4cbefc7 commit 756f79e
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 34 deletions.
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
43 changes: 24 additions & 19 deletions modules/core/04-channel/keeper/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ func (suite *KeeperTestSuite) TestRecvPacket() {
packet = types.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)
// attempts to receive packet 2 without receiving packet 1
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)
hasEvents = true
}, false},
{"channel not found", func() {
expError = types.ErrChannelNotFound
Expand Down Expand Up @@ -489,6 +488,7 @@ func (suite *KeeperTestSuite) TestRecvPacket() {
suite.chainB.App.GetIBCKeeper().ChannelKeeper.SetPacketReceipt(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, sequence)
packet = types.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)
hasEvents = true
}, false},
{"validation failed", func() {
// skip error code check, downstream error code is used from light-client implementations
Expand Down Expand Up @@ -671,26 +671,28 @@ func (suite *KeeperTestSuite) TestWriteAcknowledgement() {

// Verify events
events := ctx.EventManager().Events()
expEvents := map[string]map[string]string{
"write_acknowledgement": {
"packet_data": string(packet.GetData()),
"packet_data_hex": hex.EncodeToString(packet.GetData()),
"packet_timeout_height": packet.GetTimeoutHeight().String(),
"packet_timeout_timestamp": fmt.Sprintf("%d", packet.GetTimeoutTimestamp()),
"packet_sequence": fmt.Sprintf("%d", packet.GetSequence()),
"packet_src_port": path.EndpointB.ChannelConfig.PortID,
"packet_src_channel": path.EndpointB.ChannelID,
"packet_dst_port": path.EndpointA.ChannelConfig.PortID,
"packet_dst_channel": path.EndpointA.ChannelID,
"packet_channel_ordering": path.EndpointB.ChannelConfig.Order.String(),
"packet_connection": path.EndpointB.ConnectionID,
},
"message": {
"module": "ibc_channel",
},
}

if hasEvents {
expEvents := map[string]map[string]string{
"write_acknowledgement": {
"packet_data": string(packet.GetData()),
"packet_data_hex": hex.EncodeToString(packet.GetData()),
"packet_timeout_height": packet.GetTimeoutHeight().String(),
"packet_timeout_timestamp": fmt.Sprintf("%d", packet.GetTimeoutTimestamp()),
"packet_sequence": fmt.Sprintf("%d", packet.GetSequence()),
"packet_src_port": path.EndpointB.ChannelConfig.PortID,
"packet_src_channel": path.EndpointB.ChannelID,
"packet_dst_port": path.EndpointA.ChannelConfig.PortID,
"packet_dst_channel": path.EndpointA.ChannelID,
"packet_ack": string(ack.Acknowledgement()),
"packet_ack_hex": hex.EncodeToString(ack.Acknowledgement()),
"packet_connection": path.EndpointB.ConnectionID,
},
"message": {
"module": "ibc_channel",
},
}

ibctesting.AssertEvents(suite.Suite, expEvents, events)
} else {
suite.Require().Len(events, 0)
Expand Down Expand Up @@ -763,6 +765,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {

err = path.EndpointA.AcknowledgePacket(packet, ack.Acknowledgement())
suite.Require().NoError(err)
hasEvents = true
}, false},
{"packet already acknowledged unordered channel (no-op)", func() {
expError = types.ErrNoOpMsg
Expand All @@ -783,6 +786,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {

err = path.EndpointA.AcknowledgePacket(packet, ack.Acknowledgement())
suite.Require().NoError(err)
hasEvents = true
}, false},
{"channel not found", func() {
expError = types.ErrChannelNotFound
Expand Down Expand Up @@ -872,6 +876,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {
suite.coordinator.Setup(path)
packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)
channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
hasEvents = true
}, false},
{"packet ack verification failed", func() {
// skip error code check since error occurs in light-clients
Expand Down
Loading

0 comments on commit 756f79e

Please sign in to comment.