Skip to content

Commit

Permalink
e2e: add timeoutOnClose test
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Sep 24, 2023
1 parent 9f7366c commit c419adc
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 29 deletions.
63 changes: 39 additions & 24 deletions pkg/testing/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ func (chain *Chain) TimeoutPacket(
}

var proof []byte
proofHeight := counterparty.LatestLCInputData.Header().Number
if counterpartyCh.Ordering == uint8(channeltypes.ORDERED) {
panic("not implemented")
} else {
Expand All @@ -779,14 +780,11 @@ func (chain *Chain) TimeoutPacket(
if ok {
return fmt.Errorf("packet receipt exists: port=%v channel=%v sequence=%v", packet.DestinationPort, packet.DestinationChannel, packet.Sequence)
}
p, err := counterparty.QueryPacketReceiptProof(chain, channel.ClientID, packet, counterparty.LatestLCInputData.Header().Number)
p, err := counterparty.QueryPacketReceiptProof(chain, channel.ClientID, packet, proofHeight)
if err != nil {
return err
}
proof = p.Data
if uint64(counterparty.LatestLCInputData.Header().Number.Int64()) != p.Height.RevisionHeight {
return fmt.Errorf("revision height mismatch: expected=%v actual=%v", counterparty.LatestLCInputData.Header().Number.Int64(), p.Height.RevisionHeight)
}
}

return chain.WaitIfNoError(ctx)(
Expand All @@ -797,7 +795,7 @@ func (chain *Chain) TimeoutPacket(
Proof: proof,
ProofHeight: ibchandler.HeightData{
RevisionNumber: 0,
RevisionHeight: uint64(counterparty.LatestLCInputData.Header().Number.Int64()),
RevisionHeight: proofHeight.Uint64(),
},
},
),
Expand All @@ -808,13 +806,15 @@ func (chain *Chain) TimeoutOnClose(
ctx context.Context,
packet channeltypes.Packet,
counterparty *Chain,
channel TestChannel,
counterpartyChannel TestChannel,
) error {
var (
proofClose []byte
proofUnreceived []byte
)

channel, found, err := counterparty.IBCHandler.GetChannel(
counterpartyCh, found, err := counterparty.IBCHandler.GetChannel(
counterparty.CallOpts(ctx, RelayerKeyIndex),
packet.DestinationPort,
packet.DestinationChannel,
Expand All @@ -825,25 +825,40 @@ func (chain *Chain) TimeoutOnClose(
if !found {
return fmt.Errorf("channel not found: port=%v channel=%v", packet.DestinationPort, packet.DestinationChannel)
}
if channel.State != uint8(channeltypes.CLOSED) {
if counterpartyCh.State != uint8(channeltypes.CLOSED) {
return fmt.Errorf("channel is not closed: port=%v channel=%v", packet.DestinationPort, packet.DestinationChannel)
}

switch chain.ClientType() {
case ibcclient.MockClient:
bz, err := proto.Marshal(channelToPB(channel))
if err != nil {
return err
}
h := sha256.Sum256(bz)
proofClose = h[:]
if channel.Ordering == uint8(channeltypes.ORDERED) {
panic("not implemented")
} else {
proofHeight := counterparty.LatestLCInputData.Header().Number

p, err := counterparty.QueryChannelProof(chain, counterpartyChannel.ClientID, counterpartyChannel, proofHeight)
if err != nil {
return err
}
proofClose = p.Data

if counterpartyCh.Ordering == uint8(channeltypes.ORDERED) {
panic("not implemented")
} else {
if chain.ClientType() == ibcclient.MockClient {
proofUnreceived = []byte{}
} else {
ok, err := counterparty.IBCHandler.HasPacketReceipt(
counterparty.CallOpts(ctx, RelayerKeyIndex),
packet.DestinationPort, packet.DestinationChannel, packet.Sequence,
)
if err != nil {
return err
}
if ok {
return fmt.Errorf("packet receipt exists: port=%v channel=%v sequence=%v", packet.DestinationPort, packet.DestinationChannel, packet.Sequence)
}
p, err := counterparty.QueryPacketReceiptProof(chain, channel.ClientID, packet, proofHeight)
if err != nil {
return err
}
proofUnreceived = p.Data
}
default:
return errors.New("TimeoutOnClose: unsupported client type")
}

return chain.WaitIfNoError(ctx)(
Expand Down Expand Up @@ -1136,21 +1151,21 @@ func (counterparty *Chain) QueryConnectionProof(chain *Chain, counterpartyClient
return proof, nil
}

func (counterparty *Chain) QueryChannelProof(chain *Chain, counterpartyClientID string, channel TestChannel, height *big.Int) (*Proof, error) {
proof, err := counterparty.QueryProof(chain, counterpartyClientID, commitment.ChannelStateCommitmentSlot(channel.PortID, channel.ID), height)
func (counterparty *Chain) QueryChannelProof(chain *Chain, counterpartyClientID string, counterpartyChannel TestChannel, height *big.Int) (*Proof, error) {
proof, err := counterparty.QueryProof(chain, counterpartyClientID, commitment.ChannelStateCommitmentSlot(counterpartyChannel.PortID, counterpartyChannel.ID), height)
if err != nil {
return nil, err
}
switch counterparty.ClientType() {
case ibcclient.MockClient:
ch, found, err := counterparty.IBCHandler.GetChannel(
counterparty.CallOpts(context.Background(), RelayerKeyIndex),
channel.PortID, channel.ID,
counterpartyChannel.PortID, counterpartyChannel.ID,
)
if err != nil {
return nil, err
} else if !found {
return nil, fmt.Errorf("channel not found: %v", channel)
return nil, fmt.Errorf("channel not found: %v", counterpartyChannel)
}
bz, err := proto.Marshal(channelToPB(ch))
if err != nil {
Expand Down
27 changes: 23 additions & 4 deletions tests/e2e/chains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (suite *ChainTestSuite) TestPacketRelay() {
chainA.ICS20Transfer.SendTransfer(
chainA.TxOpts(ctx, aliceA),
baseDenom,
100,
50,
chainB.CallOpts(ctx, bobB).From,
chanA.PortID, chanA.ID,
uint64(chainB.LastHeader().Number.Int64())+1,
Expand All @@ -126,6 +126,28 @@ func (suite *ChainTestSuite) TestPacketRelay() {
suite.Require().NoError(chainA.EnsurePacketCommitmentExistence(ctx, false, transferPacket.SourcePort, transferPacket.SourceChannel, transferPacket.Sequence))
}

{
// try to transfer the token to chainB
suite.Require().NoError(chainA.WaitIfNoError(ctx)(
chainA.ICS20Transfer.SendTransfer(
chainA.TxOpts(ctx, aliceA),
strings.ToLower(chainA.ContractConfig.ERC20TokenAddress.String()),
50,
chainB.CallOpts(ctx, bobB).From,
chanA.PortID, chanA.ID,
uint64(chainB.LastHeader().Number.Int64())+1000,
),
))

transferPacket, err := chainA.GetLastSentPacket(ctx, chanA.PortID, chanA.ID)
suite.Require().NoError(err)

// close channel
suite.Require().NoError(coordinator.ChanCloseInit(ctx, chainB, chainA, chanB))
suite.Require().NoError(chainA.TimeoutOnClose(ctx, *transferPacket, chainB, chanA, chanB))
suite.Require().NoError(coordinator.ChanCloseConfirm(ctx, chainA, chainB, chanA, chanB))
}

// TODO uncomment this after implementing `onTimeoutPacket` for ICS20Transfer
// // withdraw tokens from the bank
// suite.Require().NoError(chainA.WaitIfNoError(ctx)(
Expand All @@ -140,9 +162,6 @@ func (suite *ChainTestSuite) TestPacketRelay() {
// afterBalanceA, err := chainA.ERC20.BalanceOf(chainA.CallOpts(ctx, relayer), chainA.CallOpts(ctx, deployerA).From)
// suite.Require().NoError(err)
// suite.Require().Equal(beforeBalanceA.Int64(), afterBalanceA.Int64())

// close channel
coordinator.CloseChannel(ctx, chainA, chainB, chanA, chanB)
}

func (suite *ChainTestSuite) TestPacketRelayWithDelay() {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (suite *ContractTestSuite) TestTimeoutOnClose() {
suite.Require().NoError(err)

suite.Require().NoError(suite.coordinator.ChanCloseInit(ctx, chainB, chainA, chanB))
suite.Require().NoError(suite.chainA.TimeoutOnClose(ctx, *transferPacket, chainB))
suite.Require().NoError(suite.chainA.TimeoutOnClose(ctx, *transferPacket, chainB, chanA, chanB))
}

func TestContractTestSuite(t *testing.T) {
Expand Down

0 comments on commit c419adc

Please sign in to comment.