Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix): Allow pointers to bytes in PreCodec modifier #975

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading