Skip to content

Commit aa183c5

Browse files
authored
refactor: generate internal Header.encodeRLP() for override (#86)
## Why this should be merged This is a precursor to being able to override `types.Header` RLP {en,de}coding. As there is already a `Header.EncodeRLP()` method we either have to modify the generated code or rename the generated method—this PR does the latter. ## How this works The `rlpgen -internal_methods` flag changes the generated methods from `EncodeRLP()` and `DecodeRLP()` to `encodeRLP()` and `decodeRLP()`, respectively. A new CI job checks that generated code is up to date. We can then implement our own `Header.EncodeRLP()` that either overrides or falls back on the original. It appears that `core/gen_genesis.go` was out of date but only because of formatting. ## How this was tested I deliberately excluded the change to `core/types/gen_header_rlp.go` to confirm that the new workflow [detects](https://github.com/ava-labs/libevm/actions/runs/12259667481/job/34202386378?pr=86#step:5:92) the change and fails. The actual change can be inspected via the code diff.
1 parent 380aa31 commit aa183c5

File tree

8 files changed

+119
-32
lines changed

8 files changed

+119
-32
lines changed

.github/workflows/go.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,20 @@ jobs:
2424
- name: Run non-flaky tests concurrently
2525
run: |
2626
go test -short $(go list ./... | grep -Pv "${FLAKY_REGEX}");
27+
28+
go_generate:
29+
env:
30+
EXCLUDE_REGEX: 'ava-labs/libevm/(accounts/usbwallet/trezor)$'
31+
runs-on: ubuntu-latest
32+
steps:
33+
- uses: actions/checkout@v4
34+
- name: Set up Go
35+
uses: actions/setup-go@v5
36+
with:
37+
go-version: 1.21.4
38+
39+
- name: Run `go generate`
40+
run: go list ./... | grep -Pv "${EXCLUDE_REGEX}" | xargs go generate;
41+
42+
- name: git diff
43+
run: git diff --exit-code

core/gen_genesis.go

Lines changed: 28 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/types/block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (n *BlockNonce) UnmarshalText(input []byte) error {
5959
}
6060

6161
//go:generate go run github.com/fjl/gencodec -type Header -field-override headerMarshaling -out gen_header_json.go
62-
//go:generate go run ../../rlp/rlpgen -type Header -out gen_header_rlp.go
62+
//go:generate go run ../../rlp/rlpgen -type Header -internal_methods -out gen_header_rlp.go
6363

6464
// Header represents a block header in the Ethereum blockchain.
6565
type Header struct {

core/types/block.libevm.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2024 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
package types
18+
19+
import (
20+
"io"
21+
22+
"github.com/ava-labs/libevm/rlp"
23+
)
24+
25+
func (h *Header) EncodeRLP(w io.Writer) error {
26+
return h.encodeRLP(w)
27+
}
28+
29+
var _ rlp.Encoder = (*Header)(nil)

core/types/gen_header_rlp.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rlp/rlpgen/gen.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type buildContext struct {
3535
rawValueType *types.Named
3636

3737
typeToStructCache map[types.Type]*rlpstruct.Type
38+
39+
internalMethods bool
3840
}
3941

4042
func newBuildContext(packageRLP *types.Package) *buildContext {
@@ -98,6 +100,8 @@ type genContext struct {
98100
inPackage *types.Package
99101
imports map[string]struct{}
100102
tempCounter int
103+
104+
internalMethods bool
101105
}
102106

103107
func newGenContext(inPackage *types.Package) *genContext {
@@ -736,7 +740,7 @@ func generateDecoder(ctx *genContext, typ string, op op) []byte {
736740

737741
result, code := op.genDecode(ctx)
738742
var b bytes.Buffer
739-
fmt.Fprintf(&b, "func (obj *%s) DecodeRLP(dec *rlp.Stream) error {\n", typ)
743+
fmt.Fprintf(&b, "func (obj *%s) %s(dec *rlp.Stream) error {\n", typ, ctx.decoderMethod())
740744
fmt.Fprint(&b, code)
741745
fmt.Fprintf(&b, " *obj = %s\n", result)
742746
fmt.Fprintf(&b, " return nil\n")
@@ -751,7 +755,7 @@ func generateEncoder(ctx *genContext, typ string, op op) []byte {
751755
ctx.addImport(pathOfPackageRLP)
752756

753757
var b bytes.Buffer
754-
fmt.Fprintf(&b, "func (obj *%s) EncodeRLP(_w io.Writer) error {\n", typ)
758+
fmt.Fprintf(&b, "func (obj *%s) %s(_w io.Writer) error {\n", typ, ctx.encoderMethod())
755759
fmt.Fprintf(&b, " w := rlp.NewEncoderBuffer(_w)\n")
756760
fmt.Fprint(&b, op.genWrite(ctx, "obj"))
757761
fmt.Fprintf(&b, " return w.Flush()\n")
@@ -773,6 +777,7 @@ func (bctx *buildContext) generate(typ *types.Named, encoder, decoder bool) ([]b
773777
encSource []byte
774778
decSource []byte
775779
)
780+
ctx.internalMethods = bctx.internalMethods
776781
if encoder {
777782
encSource = generateEncoder(ctx, typ.Obj().Name(), op)
778783
}

rlp/rlpgen/gen.libevm.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2024 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
package main
18+
19+
func (ctx *genContext) encoderMethod() string {
20+
if ctx.internalMethods {
21+
return "encodeRLP"
22+
}
23+
return "EncodeRLP"
24+
}
25+
26+
func (ctx *genContext) decoderMethod() string {
27+
if ctx.internalMethods {
28+
return "decodeRLP"
29+
}
30+
return "DecodeRLP"
31+
}

rlp/rlpgen/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func main() {
3636
genEncoder = flag.Bool("encoder", true, "generate EncodeRLP?")
3737
genDecoder = flag.Bool("decoder", false, "generate DecodeRLP?")
3838
typename = flag.String("type", "", "type to generate methods for")
39+
internal = flag.Bool("internal_methods", false, "generate internal (lower-case) method names")
3940
)
4041
flag.Parse()
4142

@@ -44,6 +45,7 @@ func main() {
4445
Type: *typename,
4546
GenerateEncoder: *genEncoder,
4647
GenerateDecoder: *genDecoder,
48+
InternalMethods: *internal,
4749
}
4850
code, err := cfg.process()
4951
if err != nil {
@@ -67,6 +69,8 @@ type Config struct {
6769

6870
GenerateEncoder bool
6971
GenerateDecoder bool
72+
73+
InternalMethods bool
7074
}
7175

7276
// process generates the Go code.
@@ -101,6 +105,7 @@ func (cfg *Config) process() (code []byte, err error) {
101105
}
102106
}
103107
bctx := newBuildContext(packageRLP)
108+
bctx.internalMethods = cfg.InternalMethods
104109

105110
// Find the type and generate.
106111
typ, err := lookupStructType(pkg.Scope(), cfg.Type)

0 commit comments

Comments
 (0)