From 2658868ba6a7295a575c503e0539114b1fc1a757 Mon Sep 17 00:00:00 2001 From: bruce-riley <96066700+bruce-riley@users.noreply.github.com> Date: Tue, 14 Mar 2023 13:32:30 -0500 Subject: [PATCH] Node: Support VAAs with large payloads (#2518) * Node: Support VAAs with large payloads Change-Id: Ie09bdc8ea0813ee90f9b686f3fe95365baa4014f * Clean up tests Change-Id: I983a40a2e7763271c8212ac18455b8b45e48fd3e --- node/pkg/common/chainlock.go | 2 +- node/pkg/common/chainlock_test.go | 31 +++++++++++++++++++++++++++++++ sdk/vaa/structs.go | 4 +--- sdk/vaa/structs_test.go | 22 ++++++++-------------- 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/node/pkg/common/chainlock.go b/node/pkg/common/chainlock.go index 6c93388ae..c0e7595f5 100644 --- a/node/pkg/common/chainlock.go +++ b/node/pkg/common/chainlock.go @@ -98,7 +98,7 @@ func UnmarshalMessagePublication(data []byte) (*MessagePublication, error) { } msg.EmitterAddress = emitterAddress - payload := make([]byte, vaa.InternalTruncatedPayloadSafetyLimit) + payload := make([]byte, reader.Len()) n, err := reader.Read(payload) if err != nil || n == 0 { return nil, fmt.Errorf("failed to read payload [%d]: %w", n, err) diff --git a/node/pkg/common/chainlock_test.go b/node/pkg/common/chainlock_test.go index c0952f621..2ec27793f 100644 --- a/node/pkg/common/chainlock_test.go +++ b/node/pkg/common/chainlock_test.go @@ -74,6 +74,37 @@ func TestSerializeAndDeserializeOfMessagePublication(t *testing.T) { assert.Equal(t, payload1, payload2) } +func TestSerializeAndDeserializeOfMessagePublicationWithBigPayload(t *testing.T) { + tokenBridgeAddress, err := vaa.StringToAddress("0x707f9118e33a9b8998bea41dd0d46f38bb963fc8") + require.NoError(t, err) + + // Create a payload of more than 1000 bytes. + var payload1 []byte + for i := 0; i < 2000; i++ { + ch := i % 255 + payload1 = append(payload1, byte(ch)) + } + + msg1 := &MessagePublication{ + TxHash: eth_common.HexToHash("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"), + Timestamp: time.Unix(int64(1654516425), 0), + Nonce: 123456, + Sequence: 789101112131415, + EmitterChain: vaa.ChainIDEthereum, + EmitterAddress: tokenBridgeAddress, + Payload: payload1, + ConsistencyLevel: 32, + } + + bytes, err := msg1.Marshal() + require.NoError(t, err) + + msg2, err := UnmarshalMessagePublication(bytes) + require.NoError(t, err) + + assert.Equal(t, msg1, msg2) +} + func TestMarshalUnmarshalJSONOfMessagePublication(t *testing.T) { originAddress, err := vaa.StringToAddress("0xDDb64fE46a91D46ee29420539FC25FD07c5FEa3E") //nolint:gosec require.NoError(t, err) diff --git a/sdk/vaa/structs.go b/sdk/vaa/structs.go index 60bfe9185..0c1e53596 100644 --- a/sdk/vaa/structs.go +++ b/sdk/vaa/structs.go @@ -405,8 +405,6 @@ const ( SupportedVAAVersion = 0x01 BatchVAAVersion = 0x02 - - InternalTruncatedPayloadSafetyLimit = 1000 ) // UnmarshalBody deserializes the binary representation of a VAA's "BODY" properties @@ -445,7 +443,7 @@ func UnmarshalBody(data []byte, reader *bytes.Reader, v *VAA) (*VAA, error) { // Make sure to only read the payload if the VAA has one; VAAs may have a 0 length payload if reader.Len() != 0 { - payload := make([]byte, InternalTruncatedPayloadSafetyLimit) + payload := make([]byte, reader.Len()) n, err := reader.Read(payload) if err != nil { return nil, fmt.Errorf("failed to read payload [%d]: %w", n, err) diff --git a/sdk/vaa/structs_test.go b/sdk/vaa/structs_test.go index b3c297486..79f7b8fad 100644 --- a/sdk/vaa/structs_test.go +++ b/sdk/vaa/structs_test.go @@ -367,30 +367,24 @@ func TestUnmarshalNoPayload(t *testing.T) { assert.Equal(t, &vaa1, vaa2) } -func TestUnmarshalTooBig(t *testing.T) { +func TestUnmarshalBigPayload(t *testing.T) { vaa := getVaa() - // Overwrite an oversized payload for the VAA that we cannot unmarshal + // Create a payload of more than 1000 bytes. var payload []byte for i := 0; i < 2000; i++ { - payload = append(payload, 'a') + ch := i % 255 + payload = append(payload, byte(ch)) } vaa.Payload = payload - // Let's marshal the VAA to bytes to unmarshaled marshalBytes, err := vaa.Marshal() - assert.Nil(t, err) + require.NoError(t, err) - // Let's now unmarshal the oversized VAA and cause it to panic - vaa2, err2 := Unmarshal(marshalBytes) - assert.Nil(t, err2) + vaa2, err := Unmarshal(marshalBytes) + require.NoError(t, err) - // Marshal the VAA - marshalBytes2, err3 := vaa2.Marshal() - assert.Nil(t, err3) - - // Verify that it's truncated at to 1057 (57 byte header + 1000 byte payload) - assert.Equal(t, marshalBytes[:1057], marshalBytes2) + assert.Equal(t, vaa, *vaa2) } func TestVerifySignatures(t *testing.T) {