-
Notifications
You must be signed in to change notification settings - Fork 672
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
testing: implement packet parsing from events in MsgSendPacketWithSender #8012
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,40 @@ | ||
package ibctesting | ||
|
||
import ( | ||
"github.com/cosmos/gogoproto/proto" | ||
"encoding/hex" | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/gogoproto/proto" | ||
|
||
abci "github.com/cometbft/cometbft/abci/types" | ||
clientv2types "github.com/cosmos/ibc-go/v10/modules/core/02-client/v2/types" | ||
channeltypesv2 "github.com/cosmos/ibc-go/v10/modules/core/04-channel/v2/types" | ||
hostv2 "github.com/cosmos/ibc-go/v10/modules/core/24-host/v2" | ||
) | ||
|
||
// ParsePacketFromEventsV2 parses a packet from events using the encoded packet hex attribute. | ||
// It returns the parsed packet and an error if the packet cannot be found or decoded. | ||
func ParsePacketFromEventsV2(events []abci.Event) (channeltypesv2.Packet, error) { | ||
for _, event := range events { | ||
if event.Type == channeltypesv2.EventTypeSendPacket { | ||
for _, attr := range event.Attributes { | ||
if string(attr.Key) == channeltypesv2.AttributeKeyEncodedPacketHex { | ||
packetBytes, err := hex.DecodeString(string(attr.Value)) | ||
if err != nil { | ||
return channeltypesv2.Packet{}, fmt.Errorf("failed to decode packet bytes: %w", err) | ||
} | ||
var packet channeltypesv2.Packet | ||
if err := proto.Unmarshal(packetBytes, &packet); err != nil { | ||
return channeltypesv2.Packet{}, fmt.Errorf("failed to unmarshal packet: %w", err) | ||
} | ||
return packet, nil | ||
} | ||
} | ||
} | ||
} | ||
return channeltypesv2.Packet{}, fmt.Errorf("packet not found in events") | ||
} | ||
|
||
// RegisterCounterparty will construct and execute a MsgRegisterCounterparty on the associated endpoint. | ||
func (endpoint *Endpoint) RegisterCounterparty() (err error) { | ||
msg := clientv2types.NewMsgRegisterCounterparty(endpoint.ClientID, endpoint.Counterparty.MerklePathPrefix.KeyPath, endpoint.Counterparty.ClientID, endpoint.Chain.SenderAccount.GetAddress().String()) | ||
|
@@ -43,22 +68,23 @@ func (endpoint *Endpoint) MsgSendPacketWithSender(timeoutTimestamp uint64, paylo | |
return channeltypesv2.Packet{}, err | ||
} | ||
|
||
// TODO: parse the packet from events instead of from the response. https://github.com/cosmos/ibc-go/issues/7459 | ||
// get sequence from msg response | ||
var msgData sdk.TxMsgData | ||
err = proto.Unmarshal(res.Data, &msgData) | ||
if err != nil { | ||
return channeltypesv2.Packet{}, err | ||
} | ||
msgResponse := msgData.MsgResponses[0] | ||
var sendResponse channeltypesv2.MsgSendPacketResponse | ||
err = proto.Unmarshal(msgResponse.Value, &sendResponse) | ||
if err != nil { | ||
return channeltypesv2.Packet{}, err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be implemented as a function as is specified in the original issue: #7856 |
||
// Convert sdk.Events to abci.Events | ||
abciEvents := make([]abci.Event, len(res.Events)) | ||
for i, event := range res.Events { | ||
attributes := make([]abci.EventAttribute, len(event.Attributes)) | ||
for j, attr := range event.Attributes { | ||
attributes[j] = abci.EventAttribute{ | ||
Key: attr.Key, | ||
Value: attr.Value, | ||
} | ||
} | ||
abciEvents[i] = abci.Event{ | ||
Type: event.Type, | ||
Attributes: attributes, | ||
} | ||
} | ||
packet := channeltypesv2.NewPacket(sendResponse.Sequence, endpoint.ClientID, endpoint.Counterparty.ClientID, timeoutTimestamp, payload) | ||
|
||
return packet, nil | ||
return ParsePacketFromEventsV2(abciEvents) | ||
} | ||
|
||
// MsgRecvPacket sends a MsgRecvPacket on the associated endpoint with the provided packet. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package ibctesting | ||
|
||
import ( | ||
"encoding/hex" | ||
"testing" | ||
|
||
"github.com/cosmos/gogoproto/proto" | ||
"github.com/stretchr/testify/require" | ||
|
||
abci "github.com/cometbft/cometbft/abci/types" | ||
channeltypesv2 "github.com/cosmos/ibc-go/v10/modules/core/04-channel/v2/types" | ||
) | ||
|
||
func TestParsePacketFromEventsV2(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you check the tests we have for the previous version of this in |
||
testCases := []struct { | ||
name string | ||
events []abci.Event | ||
expectedError string | ||
}{ | ||
{ | ||
name: "successful packet parsing", | ||
events: []abci.Event{ | ||
{ | ||
Type: channeltypesv2.EventTypeSendPacket, | ||
Attributes: []abci.EventAttribute{ | ||
{ | ||
Key: channeltypesv2.AttributeKeyEncodedPacketHex, | ||
Value: hex.EncodeToString(func() []byte { | ||
packet := channeltypesv2.Packet{ | ||
SourceClient: "client-0", | ||
DestinationClient: "client-1", | ||
Sequence: 1, | ||
TimeoutTimestamp: 100, | ||
Payloads: []channeltypesv2.Payload{ | ||
{ | ||
SourcePort: "transfer", | ||
DestinationPort: "transfer", | ||
Version: "1.0", | ||
Encoding: "proto3", | ||
Value: []byte("test data"), | ||
}, | ||
}, | ||
} | ||
bz, err := proto.Marshal(&packet) | ||
require.NoError(t, err) | ||
return bz | ||
}()), | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "empty events", | ||
events: []abci.Event{}, | ||
expectedError: "packet not found in events", | ||
}, | ||
{ | ||
name: "invalid hex encoding", | ||
events: []abci.Event{ | ||
{ | ||
Type: channeltypesv2.EventTypeSendPacket, | ||
Attributes: []abci.EventAttribute{ | ||
{ | ||
Key: channeltypesv2.AttributeKeyEncodedPacketHex, | ||
Value: "invalid hex", | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedError: "failed to decode packet bytes", | ||
}, | ||
{ | ||
name: "invalid proto encoding", | ||
events: []abci.Event{ | ||
{ | ||
Type: channeltypesv2.EventTypeSendPacket, | ||
Attributes: []abci.EventAttribute{ | ||
{ | ||
Key: channeltypesv2.AttributeKeyEncodedPacketHex, | ||
Value: hex.EncodeToString([]byte("invalid proto")), | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedError: "failed to unmarshal packet", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
packet, err := ParsePacketFromEventsV2(tc.events) | ||
if tc.expectedError == "" { | ||
require.NoError(t, err) | ||
require.NotEmpty(t, packet) | ||
require.Equal(t, "client-0", packet.SourceClient) | ||
require.Equal(t, "client-1", packet.DestinationClient) | ||
require.Equal(t, uint64(1), packet.Sequence) | ||
require.Equal(t, uint64(100), packet.TimeoutTimestamp) | ||
require.Len(t, packet.Payloads, 1) | ||
require.Equal(t, "transfer", packet.Payloads[0].SourcePort) | ||
require.Equal(t, "transfer", packet.Payloads[0].DestinationPort) | ||
} else { | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), tc.expectedError) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestMsgSendPacketWithSender(t *testing.T) { | ||
coordinator := NewCoordinator(t, 2) | ||
chainA := coordinator.GetChain(GetChainID(1)) | ||
chainB := coordinator.GetChain(GetChainID(2)) | ||
coordinator.CommitBlock(chainA, chainB) | ||
|
||
path := NewPath(chainA, chainB) | ||
coordinator.SetupConnections(path) | ||
|
||
// Create a mock payload | ||
payload := channeltypesv2.Payload{ | ||
SourcePort: "sourcePort", | ||
DestinationPort: "destPort", | ||
Version: "1.0", | ||
Encoding: "proto3", | ||
Value: []byte("test data"), | ||
} | ||
|
||
// Create sender account | ||
senderAccount := SenderAccount{ | ||
SenderPrivKey: chainA.SenderPrivKey, | ||
SenderAccount: chainA.SenderAccount, | ||
} | ||
|
||
t.Run("successful packet send", func(t *testing.T) { | ||
// Send packet | ||
timeoutTimestamp := chainA.GetTimeoutTimestamp() | ||
packet, err := path.EndpointA.MsgSendPacketWithSender(timeoutTimestamp, payload, senderAccount) | ||
require.NoError(t, err) | ||
|
||
// Verify packet fields | ||
require.Equal(t, path.EndpointA.ClientID, packet.SourceClient) | ||
require.Equal(t, path.EndpointB.ClientID, packet.DestinationClient) | ||
require.Equal(t, timeoutTimestamp, packet.TimeoutTimestamp) | ||
require.Len(t, packet.Payloads, 1) | ||
require.Equal(t, payload, packet.Payloads[0]) | ||
}) | ||
|
||
t.Run("packet not found in events", func(t *testing.T) { | ||
// Mock a failed send by using an invalid payload that will cause the event to be missing | ||
invalidPayload := channeltypesv2.Payload{ | ||
SourcePort: "", // Invalid empty source port | ||
DestinationPort: "", // Invalid empty destination port | ||
Version: "", | ||
Encoding: "", | ||
Value: nil, | ||
} | ||
|
||
timeoutTimestamp := chainA.GetTimeoutTimestamp() | ||
_, err := path.EndpointA.MsgSendPacketWithSender(timeoutTimestamp, invalidPayload, senderAccount) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "packet not found in events") | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please take a look at
testing/events.go
. There is a ParsePacketFromEvents there. This one should be moved there.