Node: Support VAAs with large payloads (#2518)
* Node: Support VAAs with large payloads Change-Id: Ie09bdc8ea0813ee90f9b686f3fe95365baa4014f * Clean up tests Change-Id: I983a40a2e7763271c8212ac18455b8b45e48fd3e
This commit is contained in:
parent
0dc0aefa08
commit
2658868ba6
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue