Skip to content

Commit 739ba84

Browse files
qdm12ARR4N
andauthored
feat(rlp/rlpgen): support alias types (#154)
## Why this should be merged To be able to generate RLP code using type aliases (defined e.g. in `coreth`). This can and should be PR-ed upstream as well I think. ## How this works Call `types.Unalias()` at the start of `buildContext.makeOp()`. The alternative of adding a `case` to the switch statement adds unnecessary recursive calls while `types.Unalias()` is a no-op if not an alias. ## How this was tested New `rlpgen` unit test with aliases of well-known types that have special handling—these make their proper handling easy to spot when inspecting generated code. A recursive alias in the same test also demonstrates full alias resolution. --------- Co-authored-by: Arran Schlosberg <[email protected]>
1 parent 9f410ec commit 739ba84

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

rlp/rlpgen/gen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ func (op sliceOp) genDecode(ctx *genContext) (string, string) {
673673
}
674674

675675
func (bctx *buildContext) makeOp(name *types.Named, typ types.Type, tags rlpstruct.Tags) (op, error) {
676-
switch typ := typ.(type) {
676+
switch typ := types.Unalias(typ).(type) {
677677
case *types.Named:
678678
if isBigInt(typ) {
679679
return bigIntOp{}, nil

rlp/rlpgen/gen_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func init() {
4747
}
4848
}
4949

50-
var tests = []string{"uints", "nil", "rawvalue", "optional", "bigint", "uint256"}
50+
var tests = []string{"uints", "nil", "rawvalue", "optional", "bigint", "uint256", "alias"}
5151

5252
func TestOutput(t *testing.T) {
5353
for _, test := range tests {

rlp/rlpgen/testdata/alias.in.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// -*- mode: go -*-
2+
3+
package test
4+
5+
import (
6+
"math/big"
7+
"github.com/holiman/uint256"
8+
)
9+
10+
// Alias types chosen because their originals have special handling that is easy
11+
// to spot when inspecting generated output.
12+
type (
13+
Big = big.Int
14+
// Demonstrate recursive unaliasing
15+
intermediate = uint256.Int
16+
Uint256 = intermediate
17+
)
18+
19+
type Test struct {
20+
BigAlias Big
21+
Uint256Alias Uint256
22+
}

rlp/rlpgen/testdata/alias.out.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package test
2+
3+
import "github.com/ava-labs/libevm/rlp"
4+
import "github.com/holiman/uint256"
5+
import "io"
6+
7+
func (obj *Test) EncodeRLP(_w io.Writer) error {
8+
w := rlp.NewEncoderBuffer(_w)
9+
_tmp0 := w.List()
10+
if obj.BigAlias.Sign() == -1 {
11+
return rlp.ErrNegativeBigInt
12+
}
13+
w.WriteBigInt(&obj.BigAlias)
14+
w.WriteUint256(&obj.Uint256Alias)
15+
w.ListEnd(_tmp0)
16+
return w.Flush()
17+
}
18+
19+
func (obj *Test) DecodeRLP(dec *rlp.Stream) error {
20+
var _tmp0 Test
21+
{
22+
if _, err := dec.List(); err != nil {
23+
return err
24+
}
25+
// BigAlias:
26+
_tmp1, err := dec.BigInt()
27+
if err != nil {
28+
return err
29+
}
30+
_tmp0.BigAlias = (*_tmp1)
31+
// Uint256Alias:
32+
var _tmp2 uint256.Int
33+
if err := dec.ReadUint256(&_tmp2); err != nil {
34+
return err
35+
}
36+
_tmp0.Uint256Alias = _tmp2
37+
if err := dec.ListEnd(); err != nil {
38+
return err
39+
}
40+
}
41+
*obj = _tmp0
42+
return nil
43+
}

0 commit comments

Comments
 (0)