Skip to content

Commit

Permalink
(fix): Allow pointers to bytes in PreCodec modifier (#975)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinkaseman authored Dec 14, 2024
1 parent edc5dee commit b403079
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/codec/precodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewPreCodec(fields map[string]string, codecs map[string]types.RemoteCodec)
}

m.modifyFieldForInput = func(_ string, field *reflect.StructField, _ string, typeDef string) error {
if field.Type != reflect.SliceOf(reflect.TypeFor[uint8]()) {
if field.Type != reflect.SliceOf(reflect.TypeFor[uint8]()) && field.Type != reflect.PointerTo(reflect.SliceOf(reflect.TypeFor[uint8]())) {
return fmt.Errorf("can only decode []byte from on-chain: %s", field.Type)
}

Expand Down
27 changes: 26 additions & 1 deletion pkg/codec/precodec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ type testStructOn struct {
Bid int
}

type testStructOnPointer struct {
Ask *[]byte
Bid int
}

type nestedTestStructOn struct {
Report []byte
FeedID [32]byte
Expand All @@ -84,6 +89,12 @@ func TestPreCodec(t *testing.T) {
)
require.NoError(t, err)

pointerPreCodec, err := codec.NewPreCodec(
map[string]string{"Ask": "uint256"},
map[string]types.RemoteCodec{"uint256": ExampleCodec{offChainType: int(0)}},
)
require.NoError(t, err)

nestedPreCodec, err := codec.NewPreCodec(
map[string]string{"Report": TestStructOffDef},
map[string]types.RemoteCodec{TestStructOffDef: ExampleCodec{offChainType: testStructOff{}}},
Expand Down Expand Up @@ -122,7 +133,7 @@ func TestPreCodec(t *testing.T) {
assert.Equal(t, reflect.TypeOf(int(0)), field1.Type)
})

t.Run("RetypeToOffChain works on pointers", func(t *testing.T) {
t.Run("RetypeToOffChain works on pointers to type", func(t *testing.T) {
offChainType, err := preCodec.RetypeToOffChain(reflect.PointerTo(reflect.TypeOf(testStructOn{})), "")
require.NoError(t, err)
assert.Equal(t, reflect.Ptr, offChainType.Kind())
Expand All @@ -136,6 +147,20 @@ func TestPreCodec(t *testing.T) {
assert.Equal(t, reflect.TypeOf(int(0)), field1.Type)
})

t.Run("RetypeToOffChain works on pointers", func(t *testing.T) {
offChainType, err := pointerPreCodec.RetypeToOffChain(reflect.PointerTo(reflect.TypeOf(testStructOnPointer{})), "")
require.NoError(t, err)
assert.Equal(t, reflect.Ptr, offChainType.Kind())
elem := offChainType.Elem()
require.Equal(t, 2, elem.NumField())
field0 := elem.Field(0)
assert.Equal(t, "Ask", field0.Name)
assert.Equal(t, reflect.TypeOf(int(0)), field0.Type)
field1 := elem.Field(1)
assert.Equal(t, "Bid", field1.Name)
assert.Equal(t, reflect.TypeOf(int(0)), field1.Type)
})

t.Run("RetypeToOffChain works on slices", func(t *testing.T) {
offChainType, err := preCodec.RetypeToOffChain(reflect.SliceOf(reflect.TypeOf(testStructOn{})), "")
require.NoError(t, err)
Expand Down

0 comments on commit b403079

Please sign in to comment.