-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
codec: fix parsing of
optional
values, add a special type codec for…
… OnRampAddress (#1109) * codec: Add the ability to customize parsing for specific types * codec: Properly parse option types
- Loading branch information
Showing
5 changed files
with
144 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package codec | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/codec/encodings" | ||
"github.com/smartcontractkit/chainlink-common/pkg/types" | ||
) | ||
|
||
func NewOnRampAddress(builder encodings.Builder) encodings.TypeCodec { | ||
return &onRampAddress{ | ||
intEncoder: builder.Uint32(), | ||
} | ||
} | ||
|
||
type onRampAddress struct { | ||
intEncoder encodings.TypeCodec | ||
} | ||
|
||
var _ encodings.TypeCodec = &onRampAddress{} | ||
|
||
func (d *onRampAddress) Encode(value any, into []byte) ([]byte, error) { | ||
bi, ok := value.([]byte) | ||
if !ok { | ||
return nil, fmt.Errorf("%w: expected []byte, got %T", types.ErrInvalidType, value) | ||
} | ||
|
||
length := len(bi) | ||
if length > 64 { | ||
return nil, fmt.Errorf("%w: expected []byte to be 64 bytes or less, got %v", types.ErrInvalidType, length) | ||
} | ||
// assert 64 bytes or less | ||
var buf [64]byte | ||
copy(buf[:], bi) | ||
|
||
// 64 bytes, padded, then len u32 | ||
into = append(into, buf[:]...) | ||
return d.intEncoder.Encode(uint32(length), into) | ||
} | ||
|
||
func (d *onRampAddress) Decode(encoded []byte) (any, []byte, error) { | ||
buf := encoded[0:64] | ||
encoded = encoded[64:] | ||
|
||
// decode uint32 len | ||
l, bytes, err := d.intEncoder.Decode(encoded) | ||
if err != nil { | ||
return nil, bytes, err | ||
} | ||
|
||
length, ok := l.(uint32) | ||
if !ok { | ||
return nil, bytes, fmt.Errorf("expected uint32, got %T", l) | ||
} | ||
|
||
return buf[:length], bytes, nil | ||
} | ||
|
||
func (d *onRampAddress) GetType() reflect.Type { | ||
return reflect.TypeOf([]byte{}) | ||
} | ||
|
||
func (d *onRampAddress) Size(val int) (int, error) { | ||
// 64 bytes + uint32 | ||
return 64 + 4, nil | ||
} | ||
|
||
func (d *onRampAddress) FixedSize() (int, error) { | ||
// 64 bytes + uint32 | ||
return 64 + 4, 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,59 @@ | ||
package codec | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/codec/encodings" | ||
commonencodings "github.com/smartcontractkit/chainlink-common/pkg/codec/encodings" | ||
) | ||
|
||
func NewOption(codec commonencodings.TypeCodec) encodings.TypeCodec { | ||
return &option{ | ||
codec, | ||
} | ||
} | ||
|
||
type option struct { | ||
codec encodings.TypeCodec | ||
} | ||
|
||
var _ encodings.TypeCodec = &option{} | ||
|
||
func (d *option) Encode(value any, into []byte) ([]byte, error) { | ||
// encoding is either 0 for None, or 1, bytes... for Some(val) | ||
if value == nil { | ||
return append(into, 0), nil | ||
} | ||
|
||
into = append(into, 1) | ||
return d.codec.Encode(value, into) | ||
} | ||
|
||
func (d *option) Decode(encoded []byte) (any, []byte, error) { | ||
prefix := encoded[0] | ||
bytes := encoded[1:] | ||
|
||
// encoding is either 0 for None, or 1, bytes... for Some(val) | ||
if prefix == 0 { | ||
return reflect.Zero(d.codec.GetType()).Interface(), encoded[1:], nil | ||
} | ||
|
||
if prefix != 1 { | ||
return nil, encoded, fmt.Errorf("expected either 0 or 1, got %v", prefix) | ||
} | ||
|
||
return d.codec.Decode(bytes) | ||
} | ||
|
||
func (d *option) GetType() reflect.Type { | ||
return d.codec.GetType() | ||
} | ||
|
||
func (d *option) Size(val int) (int, error) { | ||
return d.codec.Size(val) | ||
} | ||
|
||
func (d *option) FixedSize() (int, error) { | ||
return d.codec.FixedSize() | ||
} |
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