Skip to content

Commit

Permalink
Add a simple Codec test (smartcontractkit#12006)
Browse files Browse the repository at this point in the history
Hopefully a clear minimal example.
  • Loading branch information
bolekk authored Feb 13, 2024
1 parent 2380220 commit 85e8858
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
25 changes: 25 additions & 0 deletions core/services/relay/evm/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,31 @@ func TestCodec(t *testing.T) {
})
}

func TestCodec_SimpleEncode(t *testing.T) {
codecName := "my_codec"
input := map[string]any{
"Report": int32(6),
"Meta": "abcdefg",
}
evmEncoderConfig := `[{"Name":"Report","Type":"int32"},{"Name":"Meta","Type":"string"}]`

codecConfig := types.CodecConfig{Configs: map[string]types.ChainCodecConfig{
codecName: {TypeABI: evmEncoderConfig},
}}
c, err := evm.NewCodec(codecConfig)
require.NoError(t, err)

result, err := c.Encode(testutils.Context(t), input, codecName)
require.NoError(t, err)
expected :=
"0000000000000000000000000000000000000000000000000000000000000006" + // int32(6)
"0000000000000000000000000000000000000000000000000000000000000040" + // total bytes occupied by the string (64)
"0000000000000000000000000000000000000000000000000000000000000007" + // length of the string (7 chars)
"6162636465666700000000000000000000000000000000000000000000000000" // actual string

require.Equal(t, expected, hexutil.Encode(result)[2:])
}

type codecInterfaceTester struct{}

func (it *codecInterfaceTester) Setup(_ *testing.T) {}
Expand Down
2 changes: 1 addition & 1 deletion core/services/relay/evm/types/codec_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func getNativeAndCheckedTypes(curType *abi.Type) (reflect.Type, reflect.Type, er
func createTupleType(curType *abi.Type, converter func(reflect.Type) reflect.Type) (reflect.Type, reflect.Type, error) {
if len(curType.TupleElems) == 0 {
if curType.TupleType == nil {
return nil, nil, fmt.Errorf("%w: unsupported solitidy type: %v", commontypes.ErrInvalidType, curType.String())
return nil, nil, fmt.Errorf("%w: unsupported solidity type: %v", commontypes.ErrInvalidType, curType.String())
}
return curType.TupleType, curType.TupleType, nil
}
Expand Down

0 comments on commit 85e8858

Please sign in to comment.