From d022be1053ebba9c11d5d3d5c51755b3705b4c03 Mon Sep 17 00:00:00 2001 From: Charlie Chen Date: Tue, 15 Oct 2024 15:11:20 -0500 Subject: [PATCH] move bits.go to bits folder; type defines for OpCode and EncodingFormat; add more func descriptions --- pkg/math/{ => bits}/bits.go | 4 + pkg/math/{ => bits}/bits_test.go | 0 pkg/memo/codec.go | 20 ++--- pkg/memo/codec_compact.go | 5 +- pkg/memo/codec_compact_test.go | 128 +++++++++++++++---------------- pkg/memo/codec_test.go | 36 ++++----- pkg/memo/fields.go | 6 +- pkg/memo/fields_v0.go | 44 +++++------ pkg/memo/fields_v0_test.go | 124 +++++++++++++++--------------- pkg/memo/header.go | 54 ++++++------- pkg/memo/header_test.go | 30 ++++---- pkg/memo/memo.go | 4 +- pkg/memo/memo_test.go | 65 +++++++++------- testutil/sample/memo.go | 2 +- 14 files changed, 271 insertions(+), 251 deletions(-) rename pkg/math/{ => bits}/bits.go (86%) rename pkg/math/{ => bits}/bits_test.go (100%) diff --git a/pkg/math/bits.go b/pkg/math/bits/bits.go similarity index 86% rename from pkg/math/bits.go rename to pkg/math/bits/bits.go index b00e1539b0..a7a7865793 100644 --- a/pkg/math/bits.go +++ b/pkg/math/bits/bits.go @@ -9,6 +9,10 @@ func SetBit(b *byte, position uint8) { if position > 7 { return } + + // Example: given b = 0b00000000 and position = 3 + // step-1: shift value 1 to left by 3 times: 1 << 3 = 0b00001000 + // step-2: make an OR operation with original byte to set the bit: 0b00000000 | 0b00001000 = 0b00001000 *b |= 1 << position } diff --git a/pkg/math/bits_test.go b/pkg/math/bits/bits_test.go similarity index 100% rename from pkg/math/bits_test.go rename to pkg/math/bits/bits_test.go diff --git a/pkg/memo/codec.go b/pkg/memo/codec.go index d1f592f4b3..ea60fa24c1 100644 --- a/pkg/memo/codec.go +++ b/pkg/memo/codec.go @@ -4,19 +4,21 @@ import ( "fmt" ) +type EncodingFormat uint8 + // Enum for non-EVM chain memo encoding format (2 bits) const ( // EncodingFmtABI represents ABI encoding format - EncodingFmtABI uint8 = 0b0000 + EncodingFmtABI EncodingFormat = 0b0000 // EncodingFmtCompactShort represents 'compact short' encoding format - EncodingFmtCompactShort uint8 = 0b0001 + EncodingFmtCompactShort EncodingFormat = 0b0001 // EncodingFmtCompactLong represents 'compact long' encoding format - EncodingFmtCompactLong uint8 = 0b0010 + EncodingFmtCompactLong EncodingFormat = 0b0010 // EncodingFmtInvalid represents invalid encoding format - EncodingFmtInvalid uint8 = 0b0011 + EncodingFmtInvalid EncodingFormat = 0b0011 ) // Enum for length of bytes used to encode compact data @@ -38,7 +40,7 @@ type Codec interface { } // GetLenBytes returns the number of bytes used to encode the length of the data -func GetLenBytes(encodingFmt uint8) (int, error) { +func GetLenBytes(encodingFmt EncodingFormat) (int, error) { switch encodingFmt { case EncodingFmtCompactShort: return LenBytesShort, nil @@ -50,13 +52,13 @@ func GetLenBytes(encodingFmt uint8) (int, error) { } // GetCodec returns the codec based on the encoding format -func GetCodec(encodingFormat uint8) (Codec, error) { - switch encodingFormat { +func GetCodec(encodingFmt EncodingFormat) (Codec, error) { + switch encodingFmt { case EncodingFmtABI: return NewCodecABI(), nil case EncodingFmtCompactShort, EncodingFmtCompactLong: - return NewCodecCompact(encodingFormat) + return NewCodecCompact(encodingFmt) default: - return nil, fmt.Errorf("invalid encoding format %d", encodingFormat) + return nil, fmt.Errorf("invalid encoding format %d", encodingFmt) } } diff --git a/pkg/memo/codec_compact.go b/pkg/memo/codec_compact.go index 379e627a8f..bcb49d3f92 100644 --- a/pkg/memo/codec_compact.go +++ b/pkg/memo/codec_compact.go @@ -12,6 +12,9 @@ import ( var _ Codec = (*CodecCompact)(nil) // CodecCompact is a coder/decoder for compact encoded memo fields +// +// This encoding format concatenates the memo fields into a single byte array +// with zero padding to minimize the total size of the memo. type CodecCompact struct { // lenBytes is the number of bytes used to encode the length of the data lenBytes int @@ -21,7 +24,7 @@ type CodecCompact struct { } // NewCodecCompact creates a new compact codec -func NewCodecCompact(encodingFmt uint8) (*CodecCompact, error) { +func NewCodecCompact(encodingFmt EncodingFormat) (*CodecCompact, error) { lenBytes, err := GetLenBytes(encodingFmt) if err != nil { return nil, err diff --git a/pkg/memo/codec_compact_test.go b/pkg/memo/codec_compact_test.go index 72600bf54f..9cec7d122d 100644 --- a/pkg/memo/codec_compact_test.go +++ b/pkg/memo/codec_compact_test.go @@ -12,24 +12,24 @@ import ( func Test_NewCodecCompact(t *testing.T) { tests := []struct { - name string - encodingFmt uint8 - fail bool + name string + encodeFmt memo.EncodingFormat + fail bool }{ { - name: "create codec compact successfully", - encodingFmt: memo.EncodingFmtCompactShort, + name: "create codec compact successfully", + encodeFmt: memo.EncodingFmtCompactShort, }, { - name: "create codec compact failed on invalid encoding format", - encodingFmt: 0b11, - fail: true, + name: "create codec compact failed on invalid encoding format", + encodeFmt: 0b11, + fail: true, }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - codec, err := memo.NewCodecCompact(tc.encodingFmt) + codec, err := memo.NewCodecCompact(tc.encodeFmt) if tc.fail { require.Error(t, err) require.Nil(t, codec) @@ -64,14 +64,14 @@ func Test_CodecCompact_PackArguments(t *testing.T) { // test cases tests := []struct { name string - encodingFmt uint8 + encodeFmt memo.EncodingFormat args []memo.CodecArg expectedLen int errMsg string }{ { - name: "pack arguments of [address, bytes, string] in compact-short format", - encodingFmt: memo.EncodingFmtCompactShort, + name: "pack arguments of [address, bytes, string] in compact-short format", + encodeFmt: memo.EncodingFmtCompactShort, args: []memo.CodecArg{ memo.ArgReceiver(argAddress), memo.ArgPayload(argBytes), @@ -80,8 +80,8 @@ func Test_CodecCompact_PackArguments(t *testing.T) { expectedLen: 20 + 1 + len(argBytes) + 1 + len([]byte(argString)), }, { - name: "pack arguments of [string, address, bytes] in compact-long format", - encodingFmt: memo.EncodingFmtCompactLong, + name: "pack arguments of [string, address, bytes] in compact-long format", + encodeFmt: memo.EncodingFmtCompactLong, args: []memo.CodecArg{ memo.ArgRevertAddress(argString), memo.ArgReceiver(argAddress), @@ -90,32 +90,32 @@ func Test_CodecCompact_PackArguments(t *testing.T) { expectedLen: 2 + len([]byte(argString)) + 20 + 2 + len(argBytes), }, { - name: "pack long string (> 255 bytes) with compact-long format", - encodingFmt: memo.EncodingFmtCompactLong, + name: "pack long string (> 255 bytes) with compact-long format", + encodeFmt: memo.EncodingFmtCompactLong, args: []memo.CodecArg{ memo.ArgPayload([]byte(sample.StringRandom(sample.Rand(), 256))), }, expectedLen: 2 + 256, }, { - name: "pack long string (> 255 bytes) with compact-short format should fail", - encodingFmt: memo.EncodingFmtCompactShort, + name: "pack long string (> 255 bytes) with compact-short format should fail", + encodeFmt: memo.EncodingFmtCompactShort, args: []memo.CodecArg{ memo.ArgPayload([]byte(sample.StringRandom(sample.Rand(), 256))), }, errMsg: "exceeds 255 bytes", }, { - name: "pack long string (> 65535 bytes) with compact-long format should fail", - encodingFmt: memo.EncodingFmtCompactLong, + name: "pack long string (> 65535 bytes) with compact-long format should fail", + encodeFmt: memo.EncodingFmtCompactLong, args: []memo.CodecArg{ memo.ArgPayload([]byte(sample.StringRandom(sample.Rand(), 65536))), }, errMsg: "exceeds 65535 bytes", }, { - name: "pack empty byte array and string arguments", - encodingFmt: memo.EncodingFmtCompactShort, + name: "pack empty byte array and string arguments", + encodeFmt: memo.EncodingFmtCompactShort, args: []memo.CodecArg{ memo.ArgPayload([]byte{}), memo.ArgRevertAddress(""), @@ -123,32 +123,32 @@ func Test_CodecCompact_PackArguments(t *testing.T) { expectedLen: 2, }, { - name: "failed to pack bytes argument if string is passed", - encodingFmt: memo.EncodingFmtCompactShort, + name: "failed to pack bytes argument if string is passed", + encodeFmt: memo.EncodingFmtCompactShort, args: []memo.CodecArg{ memo.ArgPayload(argString), // expect bytes type, but passed string }, errMsg: "argument is not of type []byte", }, { - name: "failed to pack address argument if bytes is passed", - encodingFmt: memo.EncodingFmtCompactShort, + name: "failed to pack address argument if bytes is passed", + encodeFmt: memo.EncodingFmtCompactShort, args: []memo.CodecArg{ memo.ArgReceiver(argBytes), // expect address type, but passed bytes }, errMsg: "argument is not of type common.Address", }, { - name: "failed to pack string argument if bytes is passed", - encodingFmt: memo.EncodingFmtCompactShort, + name: "failed to pack string argument if bytes is passed", + encodeFmt: memo.EncodingFmtCompactShort, args: []memo.CodecArg{ memo.ArgRevertAddress(argBytes), // expect string type, but passed bytes }, errMsg: "argument is not of type string", }, { - name: "failed to pack unsupported argument type", - encodingFmt: memo.EncodingFmtCompactShort, + name: "failed to pack unsupported argument type", + encodeFmt: memo.EncodingFmtCompactShort, args: []memo.CodecArg{ memo.NewArg("receiver", memo.ArgType("unknown"), nil), }, @@ -160,7 +160,7 @@ func Test_CodecCompact_PackArguments(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { // create a new compact codec and add arguments - codec, err := memo.NewCodecCompact(tc.encodingFmt) + codec, err := memo.NewCodecCompact(tc.encodeFmt) require.NoError(t, err) codec.AddArguments(tc.args...) @@ -176,7 +176,7 @@ func Test_CodecCompact_PackArguments(t *testing.T) { require.Equal(t, tc.expectedLen, len(packedData)) // calc expected data for comparison - expectedData := sample.CompactPack(tc.encodingFmt, tc.args...) + expectedData := sample.CompactPack(tc.encodeFmt, tc.args...) // validate the packed data require.True(t, bytes.Equal(expectedData, packedData), "compact encoded data mismatch") @@ -192,15 +192,15 @@ func Test_CodecCompact_UnpackArguments(t *testing.T) { // test cases tests := []struct { - name string - encodingFmt uint8 - data []byte - expected []memo.CodecArg - errMsg string + name string + encodeFmt memo.EncodingFormat + data []byte + expected []memo.CodecArg + errMsg string }{ { - name: "unpack arguments of [address, bytes, string] in compact-short format", - encodingFmt: memo.EncodingFmtCompactShort, + name: "unpack arguments of [address, bytes, string] in compact-short format", + encodeFmt: memo.EncodingFmtCompactShort, data: sample.CompactPack( memo.EncodingFmtCompactShort, memo.ArgReceiver(argAddress), @@ -214,8 +214,8 @@ func Test_CodecCompact_UnpackArguments(t *testing.T) { }, }, { - name: "unpack arguments of [string, address, bytes] in compact-long format", - encodingFmt: memo.EncodingFmtCompactLong, + name: "unpack arguments of [string, address, bytes] in compact-long format", + encodeFmt: memo.EncodingFmtCompactLong, data: sample.CompactPack( memo.EncodingFmtCompactLong, memo.ArgRevertAddress(argString), @@ -229,8 +229,8 @@ func Test_CodecCompact_UnpackArguments(t *testing.T) { }, }, { - name: "unpack empty byte array and string argument", - encodingFmt: memo.EncodingFmtCompactShort, + name: "unpack empty byte array and string argument", + encodeFmt: memo.EncodingFmtCompactShort, data: sample.CompactPack( memo.EncodingFmtCompactShort, memo.ArgPayload([]byte{}), @@ -242,35 +242,35 @@ func Test_CodecCompact_UnpackArguments(t *testing.T) { }, }, { - name: "failed to unpack address if data length < 20 bytes", - encodingFmt: memo.EncodingFmtCompactShort, - data: []byte{0x01, 0x02, 0x03, 0x04, 0x05}, + name: "failed to unpack address if data length < 20 bytes", + encodeFmt: memo.EncodingFmtCompactShort, + data: []byte{0x01, 0x02, 0x03, 0x04, 0x05}, expected: []memo.CodecArg{ memo.ArgReceiver(argAddress), }, errMsg: "expected address, got 5 bytes", }, { - name: "failed to unpack string if data length < 1 byte", - encodingFmt: memo.EncodingFmtCompactShort, - data: []byte{}, + name: "failed to unpack string if data length < 1 byte", + encodeFmt: memo.EncodingFmtCompactShort, + data: []byte{}, expected: []memo.CodecArg{ memo.ArgRevertAddress(argString), }, errMsg: "expected 1 bytes to decode length", }, { - name: "failed to unpack string if actual data is less than decoded length", - encodingFmt: memo.EncodingFmtCompactShort, - data: []byte{0x05, 0x0a, 0x0b, 0x0c, 0x0d}, // length = 5, but only 4 bytes provided + name: "failed to unpack string if actual data is less than decoded length", + encodeFmt: memo.EncodingFmtCompactShort, + data: []byte{0x05, 0x0a, 0x0b, 0x0c, 0x0d}, // length = 5, but only 4 bytes provided expected: []memo.CodecArg{ memo.ArgPayload(argBytes), }, errMsg: "expected 5 bytes, got 4", }, { - name: "failed to unpack bytes argument if string is passed", - encodingFmt: memo.EncodingFmtCompactShort, + name: "failed to unpack bytes argument if string is passed", + encodeFmt: memo.EncodingFmtCompactShort, data: sample.CompactPack( memo.EncodingFmtCompactShort, memo.ArgPayload(argBytes), @@ -281,8 +281,8 @@ func Test_CodecCompact_UnpackArguments(t *testing.T) { errMsg: "argument is not of type *[]byte", }, { - name: "failed to unpack address argument if bytes is passed", - encodingFmt: memo.EncodingFmtCompactShort, + name: "failed to unpack address argument if bytes is passed", + encodeFmt: memo.EncodingFmtCompactShort, data: sample.CompactPack( memo.EncodingFmtCompactShort, memo.ArgReceiver(argAddress), @@ -293,8 +293,8 @@ func Test_CodecCompact_UnpackArguments(t *testing.T) { errMsg: "argument is not of type *common.Address", }, { - name: "failed to unpack string argument if address is passed", - encodingFmt: memo.EncodingFmtCompactShort, + name: "failed to unpack string argument if address is passed", + encodeFmt: memo.EncodingFmtCompactShort, data: sample.CompactPack( memo.EncodingFmtCompactShort, memo.ArgRevertAddress(argString), @@ -305,17 +305,17 @@ func Test_CodecCompact_UnpackArguments(t *testing.T) { errMsg: "argument is not of type *string", }, { - name: "failed to unpack unsupported argument type", - encodingFmt: memo.EncodingFmtCompactShort, - data: []byte{}, + name: "failed to unpack unsupported argument type", + encodeFmt: memo.EncodingFmtCompactShort, + data: []byte{}, expected: []memo.CodecArg{ memo.NewArg("payload", memo.ArgType("unknown"), nil), }, errMsg: "unsupported argument (payload) type", }, { - name: "unpacking should fail if not all data is consumed", - encodingFmt: memo.EncodingFmtCompactShort, + name: "unpacking should fail if not all data is consumed", + encodeFmt: memo.EncodingFmtCompactShort, data: func() []byte { data := sample.CompactPack( memo.EncodingFmtCompactShort, @@ -337,7 +337,7 @@ func Test_CodecCompact_UnpackArguments(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { // create a new compact codec and add arguments - codec, err := memo.NewCodecCompact(tc.encodingFmt) + codec, err := memo.NewCodecCompact(tc.encodeFmt) require.NoError(t, err) // add output arguments diff --git a/pkg/memo/codec_test.go b/pkg/memo/codec_test.go index 6ac3a3cd0d..aa27667967 100644 --- a/pkg/memo/codec_test.go +++ b/pkg/memo/codec_test.go @@ -11,23 +11,23 @@ func Test_GetLenBytes(t *testing.T) { // Define table-driven test cases tests := []struct { name string - encodingFmt uint8 + encodeFmt memo.EncodingFormat expectedLen int expectErr bool }{ { name: "compact short", - encodingFmt: memo.EncodingFmtCompactShort, + encodeFmt: memo.EncodingFmtCompactShort, expectedLen: 1, }, { name: "compact long", - encodingFmt: memo.EncodingFmtCompactLong, + encodeFmt: memo.EncodingFmtCompactLong, expectedLen: 2, }, { name: "non-compact encoding format", - encodingFmt: memo.EncodingFmtABI, + encodeFmt: memo.EncodingFmtABI, expectedLen: 0, expectErr: true, }, @@ -36,7 +36,7 @@ func Test_GetLenBytes(t *testing.T) { // Loop through each test case for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - length, err := memo.GetLenBytes(tc.encodingFmt) + length, err := memo.GetLenBytes(tc.encodeFmt) // Check if error is expected if tc.expectErr { @@ -53,33 +53,33 @@ func Test_GetLenBytes(t *testing.T) { func Test_GetCodec(t *testing.T) { // Define table-driven test cases tests := []struct { - name string - encodingFmt uint8 - errMsg string + name string + encodeFmt memo.EncodingFormat + errMsg string }{ { - name: "should get ABI codec", - encodingFmt: memo.EncodingFmtABI, + name: "should get ABI codec", + encodeFmt: memo.EncodingFmtABI, }, { - name: "should get compact codec", - encodingFmt: memo.EncodingFmtCompactShort, + name: "should get compact codec", + encodeFmt: memo.EncodingFmtCompactShort, }, { - name: "should get compact codec", - encodingFmt: memo.EncodingFmtCompactLong, + name: "should get compact codec", + encodeFmt: memo.EncodingFmtCompactLong, }, { - name: "should fail to get codec", - encodingFmt: 0b0011, - errMsg: "invalid encoding format", + name: "should fail to get codec", + encodeFmt: 0b0011, + errMsg: "invalid encoding format", }, } // Loop through each test case for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - codec, err := memo.GetCodec(tc.encodingFmt) + codec, err := memo.GetCodec(tc.encodeFmt) if tc.errMsg != "" { require.Error(t, err) require.Nil(t, codec) diff --git a/pkg/memo/fields.go b/pkg/memo/fields.go index 9baeab19af..fff853f955 100644 --- a/pkg/memo/fields.go +++ b/pkg/memo/fields.go @@ -3,13 +3,13 @@ package memo // Fields is the interface for memo fields type Fields interface { // Pack encodes the memo fields - Pack(opCode, encodingFormat, dataFlags uint8) ([]byte, error) + Pack(opCode OpCode, encodingFmt EncodingFormat, dataFlags uint8) ([]byte, error) // Unpack decodes the memo fields - Unpack(opCode, encodingFormat, dataFlags uint8, data []byte) error + Unpack(opCode OpCode, encodingFmt EncodingFormat, dataFlags uint8, data []byte) error // Validate checks if the fields are valid - Validate(opCode, dataFlags uint8) error + Validate(opCode OpCode, dataFlags uint8) error // DataFlags build the data flags for the fields DataFlags() uint8 diff --git a/pkg/memo/fields_v0.go b/pkg/memo/fields_v0.go index e6b6a5f986..a8f79d99ba 100644 --- a/pkg/memo/fields_v0.go +++ b/pkg/memo/fields_v0.go @@ -5,7 +5,7 @@ import ( "github.com/pkg/errors" "github.com/zeta-chain/node/pkg/crypto" - zetamath "github.com/zeta-chain/node/pkg/math" + zetabits "github.com/zeta-chain/node/pkg/math/bits" crosschaintypes "github.com/zeta-chain/node/x/crosschain/types" ) @@ -38,14 +38,14 @@ type FieldsV0 struct { } // Pack encodes the memo fields -func (f *FieldsV0) Pack(opCode uint8, encodingFormat uint8, dataFlags uint8) ([]byte, error) { +func (f *FieldsV0) Pack(opCode OpCode, encodingFmt EncodingFormat, dataFlags uint8) ([]byte, error) { // validate fields err := f.Validate(opCode, dataFlags) if err != nil { return nil, err } - codec, err := GetCodec(encodingFormat) + codec, err := GetCodec(encodingFmt) if err != nil { return nil, errors.Wrap(err, "unable to get codec") } @@ -54,8 +54,8 @@ func (f *FieldsV0) Pack(opCode uint8, encodingFormat uint8, dataFlags uint8) ([] } // Unpack decodes the memo fields -func (f *FieldsV0) Unpack(opCode uint8, encodingFormat uint8, dataFlags uint8, data []byte) error { - codec, err := GetCodec(encodingFormat) +func (f *FieldsV0) Unpack(opCode OpCode, encodingFmt EncodingFormat, dataFlags uint8, data []byte) error { + codec, err := GetCodec(encodingFmt) if err != nil { return errors.Wrap(err, "unable to get codec") } @@ -69,9 +69,9 @@ func (f *FieldsV0) Unpack(opCode uint8, encodingFormat uint8, dataFlags uint8, d } // Validate checks if the fields are valid -func (f *FieldsV0) Validate(opCode uint8, dataFlags uint8) error { +func (f *FieldsV0) Validate(opCode OpCode, dataFlags uint8) error { // receiver address must be a valid address - if zetamath.IsBitSet(dataFlags, bitPosReceiver) && crypto.IsEmptyAddress(f.Receiver) { + if zetabits.IsBitSet(dataFlags, bitPosReceiver) && crypto.IsEmptyAddress(f.Receiver) { return errors.New("receiver address is empty") } @@ -81,7 +81,7 @@ func (f *FieldsV0) Validate(opCode uint8, dataFlags uint8) error { } // abort address must be a valid address - if zetamath.IsBitSet(dataFlags, bitPosAbortAddress) && !common.IsHexAddress(f.RevertOptions.AbortAddress) { + if zetabits.IsBitSet(dataFlags, bitPosAbortAddress) && !common.IsHexAddress(f.RevertOptions.AbortAddress) { return errors.New("invalid abort address") } @@ -101,27 +101,27 @@ func (f *FieldsV0) DataFlags() uint8 { // set 'receiver' flag if provided if !crypto.IsEmptyAddress(f.Receiver) { - zetamath.SetBit(&dataFlags, bitPosReceiver) + zetabits.SetBit(&dataFlags, bitPosReceiver) } // set 'payload' flag if provided if len(f.Payload) > 0 { - zetamath.SetBit(&dataFlags, bitPosPayload) + zetabits.SetBit(&dataFlags, bitPosPayload) } // set 'revertAddress' flag if provided if f.RevertOptions.RevertAddress != "" { - zetamath.SetBit(&dataFlags, bitPosRevertAddress) + zetabits.SetBit(&dataFlags, bitPosRevertAddress) } // set 'abortAddress' flag if provided if f.RevertOptions.AbortAddress != "" { - zetamath.SetBit(&dataFlags, bitPosAbortAddress) + zetabits.SetBit(&dataFlags, bitPosAbortAddress) } // set 'revertMessage' flag if provided if f.RevertOptions.CallOnRevert { - zetamath.SetBit(&dataFlags, bitPosRevertMessage) + zetabits.SetBit(&dataFlags, bitPosRevertMessage) } return dataFlags @@ -130,23 +130,23 @@ func (f *FieldsV0) DataFlags() uint8 { // packFieldsV0 packs the memo fields for version 0 func (f *FieldsV0) packFields(codec Codec, dataFlags uint8) ([]byte, error) { // add 'receiver' argument optionally - if zetamath.IsBitSet(dataFlags, bitPosReceiver) { + if zetabits.IsBitSet(dataFlags, bitPosReceiver) { codec.AddArguments(ArgReceiver(f.Receiver)) } // add 'payload' argument optionally - if zetamath.IsBitSet(dataFlags, bitPosPayload) { + if zetabits.IsBitSet(dataFlags, bitPosPayload) { codec.AddArguments(ArgPayload(f.Payload)) } // add 'revertAddress' argument optionally - if zetamath.IsBitSet(dataFlags, bitPosRevertAddress) { + if zetabits.IsBitSet(dataFlags, bitPosRevertAddress) { codec.AddArguments(ArgRevertAddress(f.RevertOptions.RevertAddress)) } // add 'abortAddress' argument optionally abortAddress := common.HexToAddress(f.RevertOptions.AbortAddress) - if zetamath.IsBitSet(dataFlags, bitPosAbortAddress) { + if zetabits.IsBitSet(dataFlags, bitPosAbortAddress) { codec.AddArguments(ArgAbortAddress(abortAddress)) } @@ -167,28 +167,28 @@ func (f *FieldsV0) packFields(codec Codec, dataFlags uint8) ([]byte, error) { // unpackFields unpacks the memo fields for version 0 func (f *FieldsV0) unpackFields(codec Codec, dataFlags byte, data []byte) error { // add 'receiver' argument optionally - if zetamath.IsBitSet(dataFlags, bitPosReceiver) { + if zetabits.IsBitSet(dataFlags, bitPosReceiver) { codec.AddArguments(ArgReceiver(&f.Receiver)) } // add 'payload' argument optionally - if zetamath.IsBitSet(dataFlags, bitPosPayload) { + if zetabits.IsBitSet(dataFlags, bitPosPayload) { codec.AddArguments(ArgPayload(&f.Payload)) } // add 'revertAddress' argument optionally - if zetamath.IsBitSet(dataFlags, bitPosRevertAddress) { + if zetabits.IsBitSet(dataFlags, bitPosRevertAddress) { codec.AddArguments(ArgRevertAddress(&f.RevertOptions.RevertAddress)) } // add 'abortAddress' argument optionally var abortAddress common.Address - if zetamath.IsBitSet(dataFlags, bitPosAbortAddress) { + if zetabits.IsBitSet(dataFlags, bitPosAbortAddress) { codec.AddArguments(ArgAbortAddress(&abortAddress)) } // add 'revertMessage' argument optionally - f.RevertOptions.CallOnRevert = zetamath.IsBitSet(dataFlags, bitPosRevertMessage) + f.RevertOptions.CallOnRevert = zetabits.IsBitSet(dataFlags, bitPosRevertMessage) if f.RevertOptions.CallOnRevert { codec.AddArguments(ArgRevertMessage(&f.RevertOptions.RevertMessage)) } diff --git a/pkg/memo/fields_v0_test.go b/pkg/memo/fields_v0_test.go index 124b7af70f..1339f4370a 100644 --- a/pkg/memo/fields_v0_test.go +++ b/pkg/memo/fields_v0_test.go @@ -23,20 +23,20 @@ func Test_V0_Pack(t *testing.T) { fString := "this_is_a_string_field" tests := []struct { - name string - opCode uint8 - encodingFormat uint8 - dataFlags uint8 - fields memo.FieldsV0 - expectedFlags byte - expectedData []byte - errMsg string + name string + opCode memo.OpCode + encodeFmt memo.EncodingFormat + dataFlags uint8 + fields memo.FieldsV0 + expectedFlags byte + expectedData []byte + errMsg string }{ { - name: "pack all fields with ABI encoding", - opCode: memo.OpCodeDepositAndCall, - encodingFormat: memo.EncodingFmtABI, - dataFlags: flagsAllFieldsSet, // all fields are set + name: "pack all fields with ABI encoding", + opCode: memo.OpCodeDepositAndCall, + encodeFmt: memo.EncodingFmtABI, + dataFlags: flagsAllFieldsSet, // all fields are set fields: memo.FieldsV0{ Receiver: fAddress, Payload: fBytes, @@ -55,10 +55,10 @@ func Test_V0_Pack(t *testing.T) { memo.ArgRevertMessage(fBytes)), }, { - name: "pack all fields with compact encoding", - opCode: memo.OpCodeDepositAndCall, - encodingFormat: memo.EncodingFmtCompactShort, - dataFlags: flagsAllFieldsSet, // all fields are set + name: "pack all fields with compact encoding", + opCode: memo.OpCodeDepositAndCall, + encodeFmt: memo.EncodingFmtCompactShort, + dataFlags: flagsAllFieldsSet, // all fields are set fields: memo.FieldsV0{ Receiver: fAddress, Payload: fBytes, @@ -78,10 +78,10 @@ func Test_V0_Pack(t *testing.T) { memo.ArgRevertMessage(fBytes)), }, { - name: "fields validation failed due to empty receiver address", - opCode: memo.OpCodeDepositAndCall, - encodingFormat: memo.EncodingFmtABI, - dataFlags: 0b00000001, // receiver flag is set + name: "fields validation failed due to empty receiver address", + opCode: memo.OpCodeDepositAndCall, + encodeFmt: memo.EncodingFmtABI, + dataFlags: 0b00000001, // receiver flag is set fields: memo.FieldsV0{ Receiver: common.Address{}, }, @@ -93,15 +93,15 @@ func Test_V0_Pack(t *testing.T) { fields: memo.FieldsV0{ Receiver: fAddress, }, - encodingFormat: 0x0F, - errMsg: "unable to get codec", + encodeFmt: 0x0F, + errMsg: "unable to get codec", }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { // pack the fields - data, err := tc.fields.Pack(tc.opCode, tc.encodingFormat, tc.dataFlags) + data, err := tc.fields.Pack(tc.opCode, tc.encodeFmt, tc.dataFlags) // validate the error message if tc.errMsg != "" { @@ -124,19 +124,19 @@ func Test_V0_Unpack(t *testing.T) { fString := "this_is_a_string_field" tests := []struct { - name string - opCode uint8 - encodingFormat uint8 - dataFlags byte - data []byte - expected memo.FieldsV0 - errMsg string + name string + opCode memo.OpCode + encodeFmt memo.EncodingFormat + dataFlags byte + data []byte + expected memo.FieldsV0 + errMsg string }{ { - name: "unpack all fields with ABI encoding", - opCode: memo.OpCodeDepositAndCall, - encodingFormat: memo.EncodingFmtABI, - dataFlags: flagsAllFieldsSet, // all fields are set + name: "unpack all fields with ABI encoding", + opCode: memo.OpCodeDepositAndCall, + encodeFmt: memo.EncodingFmtABI, + dataFlags: flagsAllFieldsSet, // all fields are set data: sample.ABIPack(t, memo.ArgReceiver(fAddress), memo.ArgPayload(fBytes), @@ -155,10 +155,10 @@ func Test_V0_Unpack(t *testing.T) { }, }, { - name: "unpack all fields with compact encoding", - opCode: memo.OpCodeDepositAndCall, - encodingFormat: memo.EncodingFmtCompactShort, - dataFlags: flagsAllFieldsSet, // all fields are set + name: "unpack all fields with compact encoding", + opCode: memo.OpCodeDepositAndCall, + encodeFmt: memo.EncodingFmtCompactShort, + dataFlags: flagsAllFieldsSet, // all fields are set data: sample.CompactPack( memo.EncodingFmtCompactShort, memo.ArgReceiver(fAddress), @@ -178,47 +178,47 @@ func Test_V0_Unpack(t *testing.T) { }, }, { - name: "unpack empty ABI encoded payload if flag is set", - opCode: memo.OpCodeDepositAndCall, - encodingFormat: memo.EncodingFmtABI, - dataFlags: 0b00000010, // payload flags are set + name: "unpack empty ABI encoded payload if flag is set", + opCode: memo.OpCodeDepositAndCall, + encodeFmt: memo.EncodingFmtABI, + dataFlags: 0b00000010, // payload flags are set data: sample.ABIPack(t, memo.ArgPayload([]byte{})), // empty payload expected: memo.FieldsV0{}, }, { - name: "unpack empty compact encoded payload if flag is set", - opCode: memo.OpCodeDepositAndCall, - encodingFormat: memo.EncodingFmtCompactShort, - dataFlags: 0b00000010, // payload flag is set + name: "unpack empty compact encoded payload if flag is set", + opCode: memo.OpCodeDepositAndCall, + encodeFmt: memo.EncodingFmtCompactShort, + dataFlags: 0b00000010, // payload flag is set data: sample.CompactPack( memo.EncodingFmtCompactShort, memo.ArgPayload([]byte{})), // empty payload expected: memo.FieldsV0{}, }, { - name: "unable to get codec on invalid encoding format", - opCode: memo.OpCodeDepositAndCall, - encodingFormat: 0x0F, - dataFlags: 0b00000001, - data: []byte{}, - errMsg: "unable to get codec", + name: "unable to get codec on invalid encoding format", + opCode: memo.OpCodeDepositAndCall, + encodeFmt: 0x0F, + dataFlags: 0b00000001, + data: []byte{}, + errMsg: "unable to get codec", }, { - name: "failed to unpack ABI encoded data with compact encoding format", - opCode: memo.OpCodeDepositAndCall, - encodingFormat: memo.EncodingFmtCompactShort, - dataFlags: 0b00000011, // receiver and payload flags are set + name: "failed to unpack ABI encoded data with compact encoding format", + opCode: memo.OpCodeDepositAndCall, + encodeFmt: memo.EncodingFmtCompactShort, + dataFlags: 0b00000011, // receiver and payload flags are set data: sample.ABIPack(t, memo.ArgReceiver(fAddress), memo.ArgPayload(fBytes)), errMsg: "failed to unpack arguments", }, { - name: "fields validation failed due to empty receiver address", - opCode: memo.OpCodeDepositAndCall, - encodingFormat: memo.EncodingFmtABI, - dataFlags: 0b00000011, // receiver and payload flags are set + name: "fields validation failed due to empty receiver address", + opCode: memo.OpCodeDepositAndCall, + encodeFmt: memo.EncodingFmtABI, + dataFlags: 0b00000011, // receiver and payload flags are set data: sample.ABIPack(t, memo.ArgReceiver(common.Address{}), memo.ArgPayload(fBytes)), @@ -230,7 +230,7 @@ func Test_V0_Unpack(t *testing.T) { t.Run(tc.name, func(t *testing.T) { // unpack the fields fields := memo.FieldsV0{} - err := fields.Unpack(tc.opCode, tc.encodingFormat, tc.dataFlags, tc.data) + err := fields.Unpack(tc.opCode, tc.encodeFmt, tc.dataFlags, tc.data) // validate the error message if tc.errMsg != "" { @@ -253,7 +253,7 @@ func Test_V0_Validate(t *testing.T) { tests := []struct { name string - opCode uint8 + opCode memo.OpCode dataFlags uint8 fields memo.FieldsV0 errMsg string diff --git a/pkg/memo/header.go b/pkg/memo/header.go index fc614eacab..5f9bcd1e14 100644 --- a/pkg/memo/header.go +++ b/pkg/memo/header.go @@ -5,9 +5,11 @@ import ( "github.com/pkg/errors" - zetamath "github.com/zeta-chain/node/pkg/math" + zetabits "github.com/zeta-chain/node/pkg/math/bits" ) +type OpCode uint8 + const ( // Identifier is the ASCII code of 'Z' (0x5A) Identifier byte = 0x5A @@ -15,25 +17,25 @@ const ( // HeaderSize is the size of the memo header: [identifier + ctrlByte1+ ctrlByte2 + dataFlags] HeaderSize = 4 - // MaskVersion is the mask for the version bits(upper 4 bits) - MaskVersion byte = 0b11110000 + // maskVersion is the mask for the version bits(upper 4 bits) + maskVersion byte = 0b11110000 - // MaskEncodingFormat is the mask for the encoding format bits(lower 4 bits) - MaskEncodingFormat byte = 0b00001111 + // maskEncodingFormat is the mask for the encoding format bits(lower 4 bits) + maskEncodingFormat byte = 0b00001111 - // MaskOpCode is the mask for the operation code bits(upper 4 bits) - MaskOpCode byte = 0b11110000 + // maskOpCode is the mask for the operation code bits(upper 4 bits) + maskOpCode byte = 0b11110000 - // MaskCtrlReserved is the mask for reserved control bits (lower 4 bits) - MaskCtrlReserved byte = 0b00001111 + // maskCtrlReserved is the mask for reserved control bits (lower 4 bits) + maskCtrlReserved byte = 0b00001111 ) // Enum for non-EVM chain inbound operation code (4 bits) const ( - OpCodeDeposit uint8 = 0b0000 // operation 'deposit' - OpCodeDepositAndCall uint8 = 0b0001 // operation 'deposit_and_call' - OpCodeCall uint8 = 0b0010 // operation 'call' - OpCodeInvalid uint8 = 0b0011 // invalid operation code + OpCodeDeposit OpCode = 0b0000 // operation 'deposit' + OpCodeDepositAndCall OpCode = 0b0001 // operation 'deposit_and_call' + OpCodeCall OpCode = 0b0010 // operation 'call' + OpCodeInvalid OpCode = 0b0011 // invalid operation code ) // Header represent the memo header @@ -41,11 +43,11 @@ type Header struct { // Version is the memo Version Version uint8 - // EncodingFormat is the memo encoding format - EncodingFormat uint8 + // EncodingFmt is the memo encoding format + EncodingFmt EncodingFormat // OpCode is the inbound operation code - OpCode uint8 + OpCode OpCode // Reserved is the reserved control bits Reserved uint8 @@ -69,14 +71,14 @@ func (h *Header) EncodeToBytes() ([]byte, error) { // set version #, encoding format var ctrlByte1 byte - ctrlByte1 = zetamath.SetBits(ctrlByte1, MaskVersion, h.Version) - ctrlByte1 = zetamath.SetBits(ctrlByte1, MaskEncodingFormat, h.EncodingFormat) + ctrlByte1 = zetabits.SetBits(ctrlByte1, maskVersion, h.Version) + ctrlByte1 = zetabits.SetBits(ctrlByte1, maskEncodingFormat, byte(h.EncodingFmt)) data[1] = ctrlByte1 // set operation code, reserved bits var ctrlByte2 byte - ctrlByte2 = zetamath.SetBits(ctrlByte2, MaskOpCode, h.OpCode) - ctrlByte2 = zetamath.SetBits(ctrlByte2, MaskCtrlReserved, h.Reserved) + ctrlByte2 = zetabits.SetBits(ctrlByte2, maskOpCode, byte(h.OpCode)) + ctrlByte2 = zetabits.SetBits(ctrlByte2, maskCtrlReserved, h.Reserved) data[2] = ctrlByte2 // set data flags @@ -99,13 +101,13 @@ func (h *Header) DecodeFromBytes(data []byte) error { // extract version #, encoding format ctrlByte1 := data[1] - h.Version = zetamath.GetBits(ctrlByte1, MaskVersion) - h.EncodingFormat = zetamath.GetBits(ctrlByte1, MaskEncodingFormat) + h.Version = zetabits.GetBits(ctrlByte1, maskVersion) + h.EncodingFmt = EncodingFormat(zetabits.GetBits(ctrlByte1, maskEncodingFormat)) // extract operation code, reserved bits ctrlByte2 := data[2] - h.OpCode = zetamath.GetBits(ctrlByte2, MaskOpCode) - h.Reserved = zetamath.GetBits(ctrlByte2, MaskCtrlReserved) + h.OpCode = OpCode(zetabits.GetBits(ctrlByte2, maskOpCode)) + h.Reserved = zetabits.GetBits(ctrlByte2, maskCtrlReserved) // extract data flags h.DataFlags = data[3] @@ -120,8 +122,8 @@ func (h *Header) Validate() error { return fmt.Errorf("invalid memo version: %d", h.Version) } - if h.EncodingFormat >= EncodingFmtInvalid { - return fmt.Errorf("invalid encoding format: %d", h.EncodingFormat) + if h.EncodingFmt >= EncodingFmtInvalid { + return fmt.Errorf("invalid encoding format: %d", h.EncodingFmt) } if h.OpCode >= OpCodeInvalid { diff --git a/pkg/memo/header_test.go b/pkg/memo/header_test.go index fbb706fc8e..31693a7c18 100644 --- a/pkg/memo/header_test.go +++ b/pkg/memo/header_test.go @@ -18,10 +18,10 @@ func Test_Header_EncodeToBytes(t *testing.T) { { name: "it works", header: memo.Header{ - Version: 0, - EncodingFormat: memo.EncodingFmtABI, - OpCode: memo.OpCodeCall, - DataFlags: 0b00011111, + Version: 0, + EncodingFmt: memo.EncodingFmtABI, + OpCode: memo.OpCodeCall, + DataFlags: 0b00011111, }, expected: []byte{memo.Identifier, 0b00000000, 0b00100000, 0b00011111}, }, @@ -57,12 +57,14 @@ func Test_Header_DecodeFromBytes(t *testing.T) { }{ { name: "it works", - data: append(sample.MemoHead(0, memo.EncodingFmtABI, memo.OpCodeCall, 0, 0), []byte{0x01, 0x02}...), + data: append( + sample.MemoHead(0, uint8(memo.EncodingFmtABI), uint8(memo.OpCodeCall), 0, 0), + []byte{0x01, 0x02}...), expected: memo.Header{ - Version: 0, - EncodingFormat: memo.EncodingFmtABI, - OpCode: memo.OpCodeCall, - Reserved: 0, + Version: 0, + EncodingFmt: memo.EncodingFmtABI, + OpCode: memo.OpCodeCall, + Reserved: 0, }, }, { @@ -78,7 +80,7 @@ func Test_Header_DecodeFromBytes(t *testing.T) { { name: "header validation failed", data: append( - sample.MemoHead(0, memo.EncodingFmtInvalid, memo.OpCodeCall, 0, 0), + sample.MemoHead(0, uint8(memo.EncodingFmtInvalid), uint8(memo.OpCodeCall), 0, 0), []byte{0x01, 0x02}...), // invalid encoding format errMsg: "invalid encoding format", }, @@ -107,9 +109,9 @@ func Test_Header_Validate(t *testing.T) { { name: "valid header", header: memo.Header{ - Version: 0, - EncodingFormat: memo.EncodingFmtCompactShort, - OpCode: memo.OpCodeDepositAndCall, + Version: 0, + EncodingFmt: memo.EncodingFmtCompactShort, + OpCode: memo.OpCodeDepositAndCall, }, }, { @@ -122,7 +124,7 @@ func Test_Header_Validate(t *testing.T) { { name: "invalid encoding format", header: memo.Header{ - EncodingFormat: memo.EncodingFmtInvalid, + EncodingFmt: memo.EncodingFmtInvalid, }, errMsg: "invalid encoding format", }, diff --git a/pkg/memo/memo.go b/pkg/memo/memo.go index 152b078c5e..420bcac6fa 100644 --- a/pkg/memo/memo.go +++ b/pkg/memo/memo.go @@ -36,7 +36,7 @@ func (m *InboundMemo) EncodeToBytes() ([]byte, error) { var data []byte switch m.Version { case 0: - data, err = m.FieldsV0.Pack(m.OpCode, m.EncodingFormat, dataFlags) + data, err = m.FieldsV0.Pack(m.OpCode, m.EncodingFmt, dataFlags) default: return nil, fmt.Errorf("invalid memo version: %d", m.Version) } @@ -62,7 +62,7 @@ func DecodeFromBytes(data []byte) (*InboundMemo, error) { // decode fields based on version switch memo.Version { case 0: - err = memo.FieldsV0.Unpack(memo.OpCode, memo.EncodingFormat, memo.Header.DataFlags, data[HeaderSize:]) + err = memo.FieldsV0.Unpack(memo.OpCode, memo.EncodingFmt, memo.Header.DataFlags, data[HeaderSize:]) default: return nil, fmt.Errorf("invalid memo version: %d", memo.Version) } diff --git a/pkg/memo/memo_test.go b/pkg/memo/memo_test.go index 10c8094e5c..1de361e267 100644 --- a/pkg/memo/memo_test.go +++ b/pkg/memo/memo_test.go @@ -26,9 +26,9 @@ func Test_Memo_EncodeToBytes(t *testing.T) { name: "encode memo with ABI encoding", memo: &memo.InboundMemo{ Header: memo.Header{ - Version: 0, - EncodingFormat: memo.EncodingFmtABI, - OpCode: memo.OpCodeDepositAndCall, + Version: 0, + EncodingFmt: memo.EncodingFmtABI, + OpCode: memo.OpCodeDepositAndCall, }, FieldsV0: memo.FieldsV0{ Receiver: fAddress, @@ -43,8 +43,8 @@ func Test_Memo_EncodeToBytes(t *testing.T) { }, expectedHead: sample.MemoHead( 0, - memo.EncodingFmtABI, - memo.OpCodeDepositAndCall, + uint8(memo.EncodingFmtABI), + uint8(memo.OpCodeDepositAndCall), 0, flagsAllFieldsSet, // all fields are set ), @@ -59,9 +59,9 @@ func Test_Memo_EncodeToBytes(t *testing.T) { name: "encode memo with compact encoding", memo: &memo.InboundMemo{ Header: memo.Header{ - Version: 0, - EncodingFormat: memo.EncodingFmtCompactShort, - OpCode: memo.OpCodeDepositAndCall, + Version: 0, + EncodingFmt: memo.EncodingFmtCompactShort, + OpCode: memo.OpCodeDepositAndCall, }, FieldsV0: memo.FieldsV0{ Receiver: fAddress, @@ -76,8 +76,8 @@ func Test_Memo_EncodeToBytes(t *testing.T) { }, expectedHead: sample.MemoHead( 0, - memo.EncodingFmtCompactShort, - memo.OpCodeDepositAndCall, + uint8(memo.EncodingFmtCompactShort), + uint8(memo.OpCodeDepositAndCall), 0, flagsAllFieldsSet, // all fields are set ), @@ -111,9 +111,9 @@ func Test_Memo_EncodeToBytes(t *testing.T) { name: "failed to pack memo fields", memo: &memo.InboundMemo{ Header: memo.Header{ - Version: 0, - EncodingFormat: memo.EncodingFmtABI, - OpCode: memo.OpCodeDeposit, + Version: 0, + EncodingFmt: memo.EncodingFmtABI, + OpCode: memo.OpCodeDeposit, }, FieldsV0: memo.FieldsV0{ Receiver: fAddress, @@ -134,6 +134,11 @@ func Test_Memo_EncodeToBytes(t *testing.T) { } require.NoError(t, err) require.Equal(t, append(tt.expectedHead, tt.expectedData...), data) + + // decode the memo and compare with the original + decodedMemo, err := memo.DecodeFromBytes(data) + require.NoError(t, err) + require.Equal(t, tt.memo, decodedMemo) }) } } @@ -148,6 +153,8 @@ func Test_Memo_DecodeFromBytes(t *testing.T) { name string head []byte data []byte + headHex string + dataHex string expectedMemo memo.InboundMemo errMsg string }{ @@ -155,8 +162,8 @@ func Test_Memo_DecodeFromBytes(t *testing.T) { name: "decode memo with ABI encoding", head: sample.MemoHead( 0, - memo.EncodingFmtABI, - memo.OpCodeDepositAndCall, + uint8(memo.EncodingFmtABI), + uint8(memo.OpCodeDepositAndCall), 0, flagsAllFieldsSet, // all fields are set ), @@ -168,10 +175,10 @@ func Test_Memo_DecodeFromBytes(t *testing.T) { memo.ArgRevertMessage(fBytes)), expectedMemo: memo.InboundMemo{ Header: memo.Header{ - Version: 0, - EncodingFormat: memo.EncodingFmtABI, - OpCode: memo.OpCodeDepositAndCall, - DataFlags: 0b00011111, + Version: 0, + EncodingFmt: memo.EncodingFmtABI, + OpCode: memo.OpCodeDepositAndCall, + DataFlags: 0b00011111, }, FieldsV0: memo.FieldsV0{ Receiver: fAddress, @@ -189,8 +196,8 @@ func Test_Memo_DecodeFromBytes(t *testing.T) { name: "decode memo with compact encoding", head: sample.MemoHead( 0, - memo.EncodingFmtCompactLong, - memo.OpCodeDepositAndCall, + uint8(memo.EncodingFmtCompactLong), + uint8(memo.OpCodeDepositAndCall), 0, flagsAllFieldsSet, // all fields are set ), @@ -203,10 +210,10 @@ func Test_Memo_DecodeFromBytes(t *testing.T) { memo.ArgRevertMessage(fBytes)), expectedMemo: memo.InboundMemo{ Header: memo.Header{ - Version: 0, - EncodingFormat: memo.EncodingFmtCompactLong, - OpCode: memo.OpCodeDepositAndCall, - DataFlags: 0b00011111, + Version: 0, + EncodingFmt: memo.EncodingFmtCompactLong, + OpCode: memo.OpCodeDepositAndCall, + DataFlags: 0b00011111, }, FieldsV0: memo.FieldsV0{ Receiver: fAddress, @@ -222,13 +229,13 @@ func Test_Memo_DecodeFromBytes(t *testing.T) { }, { name: "failed to decode memo header", - head: sample.MemoHead(0, memo.EncodingFmtABI, memo.OpCodeInvalid, 0, 0), + head: sample.MemoHead(0, uint8(memo.EncodingFmtABI), uint8(memo.OpCodeInvalid), 0, 0), data: sample.ABIPack(t, memo.ArgReceiver(fAddress)), errMsg: "failed to decode memo header", }, { name: "failed to decode if version is invalid", - head: sample.MemoHead(1, memo.EncodingFmtABI, memo.OpCodeDeposit, 0, 0), + head: sample.MemoHead(1, uint8(memo.EncodingFmtABI), uint8(memo.OpCodeDeposit), 0, 0), data: sample.ABIPack(t, memo.ArgReceiver(fAddress)), errMsg: "invalid memo version", }, @@ -236,8 +243,8 @@ func Test_Memo_DecodeFromBytes(t *testing.T) { name: "failed to decode compact encoded data with ABI encoding format", head: sample.MemoHead( 0, - memo.EncodingFmtABI, - memo.OpCodeDepositAndCall, + uint8(memo.EncodingFmtABI), + uint8(memo.OpCodeDepositAndCall), 0, 0, ), // header says ABI encoding diff --git a/testutil/sample/memo.go b/testutil/sample/memo.go index eb6a2351fa..a70ce3537c 100644 --- a/testutil/sample/memo.go +++ b/testutil/sample/memo.go @@ -81,7 +81,7 @@ func ABIPack(t *testing.T, args ...memo.CodecArg) []byte { // CompactPack is a helper function to pack arguments into compact encoded data // Note: all arguments are assumed to be <= 65535 bytes for simplicity. -func CompactPack(encodingFmt uint8, args ...memo.CodecArg) []byte { +func CompactPack(encodingFmt memo.EncodingFormat, args ...memo.CodecArg) []byte { var ( length int packedData []byte