Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
26 changes: 26 additions & 0 deletions rlp/rlpgen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,29 @@ func (op uint256Op) genDecode(ctx *genContext) (string, string) {
return result, b.String()
}

func (bctx *buildContext) makeNamedBasicOp(named *types.Named) (op, error) {
underlying := named.Underlying()
basic, ok := underlying.(*types.Basic)
if !ok {
return nil, fmt.Errorf("expected basic type, got %T", underlying)
}

// Use the existing makeBasicOp function to get the base operation
baseOp, err := bctx.makeBasicOp(basic)
if err != nil {
return nil, err
}

// Cast to basicOp and modify the typ field to use the named type
op, ok := baseOp.(basicOp)
if !ok {
return nil, fmt.Errorf("expected basicOp, got %T", baseOp)
}
op.typ = named // Use the named type as the main type instead of the underlying basic type

return op, nil
}

// encoderDecoderOp handles rlp.Encoder and rlp.Decoder.
// In order to be used with this, the type must implement both interfaces.
// This restriction may be lifted in the future by creating separate ops for
Expand Down Expand Up @@ -684,6 +707,9 @@ func (bctx *buildContext) makeOp(name *types.Named, typ types.Type, tags rlpstru
if typ == bctx.rawValueType {
return bctx.makeRawValueOp(), nil
}
if isNamedWithBasicUnderlying(typ) {
return bctx.makeNamedBasicOp(typ)
}
if bctx.isDecoder(typ) {
return nil, fmt.Errorf("type %v implements rlp.Decoder with non-pointer receiver", typ)
}
Expand Down
4 changes: 2 additions & 2 deletions rlp/rlpgen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func init() {
}
}

var tests = []string{"uints", "nil", "rawvalue", "optional", "bigint", "uint256", "alias"}
var tests = []string{"uints", "nil", "rawvalue", "optional", "bigint", "uint256", "alias", "named"}

func TestOutput(t *testing.T) {
for _, test := range tests {
Expand All @@ -66,7 +66,7 @@ func TestOutput(t *testing.T) {

// Set this environment variable to regenerate the test outputs.
if os.Getenv("WRITE_TEST_FILES") != "" {
os.WriteFile(outputFile, output, 0644)
os.WriteFile(outputFile, output, 0o644)
}

// Check if output matches.
Expand Down
15 changes: 15 additions & 0 deletions rlp/rlpgen/testdata/named.in.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// -*- mode: go -*-

package test

type (
BoolT bool
Uint64T uint64
StringT string
)

type Test struct {
BoolNewT BoolT
Uint64NewT Uint64T
StringNewT StringT
}
49 changes: 49 additions & 0 deletions rlp/rlpgen/testdata/named.out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package test

import "github.com/ava-labs/libevm/rlp"
import "io"

func (obj *Test) EncodeRLP(_w io.Writer) error {
w := rlp.NewEncoderBuffer(_w)
_tmp0 := w.List()
w.WriteBool(bool(obj.BoolNewT))
w.WriteUint64(uint64(obj.Uint64NewT))
w.WriteString(string(obj.StringNewT))
w.ListEnd(_tmp0)
return w.Flush()
}

func (obj *Test) DecodeRLP(dec *rlp.Stream) error {
var _tmp0 Test
{
if _, err := dec.List(); err != nil {
return err
}
// BoolNewT:
_tmp1, err := dec.Bool()
if err != nil {
return err
}
_tmp2 := BoolT(_tmp1)
_tmp0.BoolNewT = _tmp2
// Uint64NewT:
_tmp3, err := dec.Uint64()
if err != nil {
return err
}
_tmp4 := Uint64T(_tmp3)
_tmp0.Uint64NewT = _tmp4
// StringNewT:
_tmp5, err := dec.String()
if err != nil {
return err
}
_tmp6 := StringT(_tmp5)
_tmp0.StringNewT = _tmp6
if err := dec.ListEnd(); err != nil {
return err
}
}
*obj = _tmp0
return nil
}
10 changes: 10 additions & 0 deletions rlp/rlpgen/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ func isByte(typ types.Type) bool {
return ok && basic.Kind() == types.Uint8
}

// isNamedWithBasicUnderlying checks whether 'typ' is a named type with an underlying basic type.
func isNamedWithBasicUnderlying(typ types.Type) bool {
named, ok := typ.(*types.Named)
if !ok {
return false
}
_, ok = named.Underlying().(*types.Basic)
return ok
}

func resolveUnderlying(typ types.Type) types.Type {
for {
t := typ.Underlying()
Expand Down