-
Notifications
You must be signed in to change notification settings - Fork 43
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
Add discriminator to the account and allow encoding/decoding of non-account types #672
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package codec | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/codec/encodings" | ||
"github.com/smartcontractkit/chainlink-common/pkg/types" | ||
) | ||
|
||
const discriminatorLength = 8 | ||
|
||
func NewDiscriminator(name string) encodings.TypeCodec { | ||
sum := sha256.Sum256([]byte("account:" + name)) | ||
return &discriminator{hashPrefix: sum[:discriminatorLength]} | ||
} | ||
|
||
type discriminator struct { | ||
hashPrefix []byte | ||
} | ||
|
||
func (d discriminator) Encode(value any, into []byte) ([]byte, error) { | ||
if value == nil { | ||
return append(into, d.hashPrefix...), nil | ||
} | ||
|
||
raw, ok := value.(*[]byte) | ||
if !ok { | ||
return nil, fmt.Errorf("%w: value must be a byte slice got %T", types.ErrInvalidType, value) | ||
} | ||
|
||
// inject if not specified | ||
if raw == nil { | ||
return append(into, d.hashPrefix...), nil | ||
} | ||
|
||
// Not sure if we should really be encoding accounts... | ||
if !bytes.Equal(*raw, d.hashPrefix) { | ||
return nil, fmt.Errorf("%w: invalid discriminator expected %x got %x", types.ErrInvalidType, d.hashPrefix, raw) | ||
} | ||
|
||
return append(into, *raw...), nil | ||
} | ||
|
||
func (d discriminator) Decode(encoded []byte) (any, []byte, error) { | ||
raw, remaining, err := encodings.SafeDecode(encoded, discriminatorLength, func(raw []byte) []byte { return raw }) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
if !bytes.Equal(raw, d.hashPrefix) { | ||
return nil, nil, fmt.Errorf("%w: invalid discriminator expected %x got %x", types.ErrInvalidEncoding, d.hashPrefix, raw) | ||
} | ||
|
||
return &raw, remaining, nil | ||
} | ||
|
||
func (d discriminator) GetType() reflect.Type { | ||
// Pointer type so that nil can inject values and so that the NamedCodec won't wrap with no-nil pointer. | ||
return reflect.TypeOf(&[]byte{}) | ||
} | ||
|
||
func (d discriminator) Size(_ int) (int, error) { | ||
return discriminatorLength, nil | ||
} | ||
|
||
func (d discriminator) FixedSize() (int, error) { | ||
return discriminatorLength, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package codec_test | ||
|
||
import ( | ||
"crypto/sha256" | ||
"errors" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/types" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/smartcontractkit/chainlink-solana/pkg/solana/codec" | ||
) | ||
|
||
func TestDiscriminator(t *testing.T) { | ||
t.Run("encode and decode return the discriminator", func(t *testing.T) { | ||
tmp := sha256.Sum256([]byte("account:Foo")) | ||
expected := tmp[:8] | ||
c := codec.NewDiscriminator("Foo") | ||
encoded, err := c.Encode(&expected, nil) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, encoded) | ||
actual, remaining, err := c.Decode(encoded) | ||
require.NoError(t, err) | ||
require.Equal(t, &expected, actual) | ||
require.Len(t, remaining, 0) | ||
}) | ||
|
||
t.Run("encode returns an error if the discriminator is invalid", func(t *testing.T) { | ||
c := codec.NewDiscriminator("Foo") | ||
_, err := c.Encode(&[]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, nil) | ||
require.True(t, errors.Is(err, types.ErrInvalidType)) | ||
}) | ||
|
||
t.Run("encode injects the discriminator if it's not provided", func(t *testing.T) { | ||
tmp := sha256.Sum256([]byte("account:Foo")) | ||
expected := tmp[:8] | ||
c := codec.NewDiscriminator("Foo") | ||
encoded, err := c.Encode(nil, nil) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, encoded) | ||
encoded, err = c.Encode((*[]byte)(nil), nil) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, encoded) | ||
}) | ||
|
||
t.Run("decode returns an error if the encoded value is too short", func(t *testing.T) { | ||
c := codec.NewDiscriminator("Foo") | ||
_, _, err := c.Decode([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06}) | ||
require.True(t, errors.Is(err, types.ErrInvalidEncoding)) | ||
}) | ||
|
||
t.Run("decode returns an error if the discriminator is invalid", func(t *testing.T) { | ||
c := codec.NewDiscriminator("Foo") | ||
_, _, err := c.Decode([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}) | ||
require.True(t, errors.Is(err, types.ErrInvalidEncoding)) | ||
}) | ||
|
||
t.Run("encode returns an error if the value is not a byte slice", func(t *testing.T) { | ||
c := codec.NewDiscriminator("Foo") | ||
_, err := c.Encode(42, nil) | ||
require.True(t, errors.Is(err, types.ErrInvalidType)) | ||
}) | ||
|
||
t.Run("GetType returns the type of the discriminator", func(t *testing.T) { | ||
c := codec.NewDiscriminator("Foo") | ||
require.Equal(t, reflect.TypeOf(&[]byte{}), c.GetType()) | ||
}) | ||
|
||
t.Run("Size returns the length of the discriminator", func(t *testing.T) { | ||
c := codec.NewDiscriminator("Foo") | ||
size, err := c.Size(0) | ||
require.NoError(t, err) | ||
require.Equal(t, 8, size) | ||
}) | ||
|
||
t.Run("FixedSize returns the length of the discriminator", func(t *testing.T) { | ||
c := codec.NewDiscriminator("Foo") | ||
size, err := c.FixedSize() | ||
require.NoError(t, err) | ||
require.Equal(t, 8, size) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a nil pointer concern for dereferencing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't add it in this PR, but I don't think so. The call only gets made if the type is a struct so I expect the fields would be present but empty if there are no fields.
There are a few derefs later on as well.