Skip to content
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

refactor: generate internal Header.encodeRLP() for override #86

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,20 @@ jobs:
- name: Run non-flaky tests concurrently
run: |
go test -short $(go list ./... | grep -Pv "${FLAKY_REGEX}");

go_generate:
env:
EXCLUDE_REGEX: 'ava-labs/libevm/(accounts/usbwallet/trezor)$'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.21.4
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit shouldn't we stay on 1.20 given that's what in the go.mod? 🤔

Copy link
Collaborator Author

@ARR4N ARR4N Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both the go.mod and the go.yml inherit their versions from upstream v1.13.14 for now. I'm happy to look at changing them but think we should consider it more thoroughly first.

If you want to send a PR that uses a matrix strategy for multiple versions I think that will be the best of both worlds. Please try to keep the pull_request to main workflow trigger fast / limited to one version and leave the multi-version tests to main push + everything to release/* (I'm not 100% sure this is possible, but it would be nice for PRs to not have to wait half an hour).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we want to support/check it works on versions above the one in go.mod though? When we later sync with geth, we can just bump all CI versions to the geth go.mod version (and CI may or may not fail). I'm creating (slowly) an issue to list CI checks we could add for post-syncing, for example keeping go-version of CI jobs in sync with go.mod (perhaps dependabot does it). Maybe a more appropriate PR would be to pin all go-version of jobs to 1.20? 🤔


- name: Run `go generate`
run: go list ./... | grep -Pv "${EXCLUDE_REGEX}" | xargs go generate;
Comment on lines +39 to +40
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the -skip flag I think:

Suggested change
- name: Run `go generate`
run: go list ./... | grep -Pv "${EXCLUDE_REGEX}" | xargs go generate;
- run: go generate -skip "${EXCLUDE_REGEX}" ./...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, TIL about -skip. This doesn't work for me locally though (I suspect because the regex needs to change):

$ go generate -skip 'ava-labs/libevm/(accounts/usbwallet/trezor)$' ./...
/usr/local/include: warning: directory does not exist.
protoc-gen-go: no such flag -import_path
--go_out: protoc-gen-go: Plugin failed with status code 1.
accounts/usbwallet/trezor/trezor.go:45: running "protoc": exit status 1

If you add the matrix strategy and switching to -skip is simple then please include it, but I think it should be timeboxed as it's low yield.

Copy link
Collaborator

@qdm12 qdm12 Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My apologies, similarly to -run, it's for the source text in the //go:generate statement, not the file path. Better to keep it as it is!


- name: git diff
run: git diff --exit-code
56 changes: 28 additions & 28 deletions core/gen_genesis.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (n *BlockNonce) UnmarshalText(input []byte) error {
}

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

// Header represents a block header in the Ethereum blockchain.
type Header struct {
Expand Down
29 changes: 29 additions & 0 deletions core/types/block.libevm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2024 the libevm authors.
//
// The libevm additions to go-ethereum are free software: you can redistribute
// them and/or modify them under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The libevm additions are distributed in the hope that they will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see
// <http://www.gnu.org/licenses/>.

package types

import (
"io"

"github.com/ava-labs/libevm/rlp"
)

func (h *Header) EncodeRLP(w io.Writer) error {
return h.encodeRLP(w)
}

var _ rlp.Encoder = (*Header)(nil)
2 changes: 1 addition & 1 deletion core/types/gen_header_rlp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions rlp/rlpgen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type buildContext struct {
rawValueType *types.Named

typeToStructCache map[types.Type]*rlpstruct.Type

internalMethods bool
ARR4N marked this conversation as resolved.
Show resolved Hide resolved
}

func newBuildContext(packageRLP *types.Package) *buildContext {
Expand Down Expand Up @@ -98,6 +100,8 @@ type genContext struct {
inPackage *types.Package
imports map[string]struct{}
tempCounter int

internalMethods bool
}

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

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

var b bytes.Buffer
fmt.Fprintf(&b, "func (obj *%s) EncodeRLP(_w io.Writer) error {\n", typ)
fmt.Fprintf(&b, "func (obj *%s) %s(_w io.Writer) error {\n", typ, ctx.encoderMethod())
fmt.Fprintf(&b, " w := rlp.NewEncoderBuffer(_w)\n")
fmt.Fprint(&b, op.genWrite(ctx, "obj"))
fmt.Fprintf(&b, " return w.Flush()\n")
Expand All @@ -773,6 +777,7 @@ func (bctx *buildContext) generate(typ *types.Named, encoder, decoder bool) ([]b
encSource []byte
decSource []byte
)
ctx.internalMethods = bctx.internalMethods
if encoder {
encSource = generateEncoder(ctx, typ.Obj().Name(), op)
}
Expand Down
31 changes: 31 additions & 0 deletions rlp/rlpgen/gen.libevm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 the libevm authors.
//
// The libevm additions to go-ethereum are free software: you can redistribute
// them and/or modify them under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The libevm additions are distributed in the hope that they will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see
// <http://www.gnu.org/licenses/>.

package main

func (ctx *genContext) encoderMethod() string {
if ctx.internalMethods {
return "encodeRLP"
}
return "EncodeRLP"
}

func (ctx *genContext) decoderMethod() string {
if ctx.internalMethods {
return "decodeRLP"
}
return "DecodeRLP"
}
5 changes: 5 additions & 0 deletions rlp/rlpgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func main() {
genEncoder = flag.Bool("encoder", true, "generate EncodeRLP?")
genDecoder = flag.Bool("decoder", false, "generate DecodeRLP?")
typename = flag.String("type", "", "type to generate methods for")
internal = flag.Bool("internal_methods", false, "generate internal (lower-case) method names")
)
flag.Parse()

Expand All @@ -44,6 +45,7 @@ func main() {
Type: *typename,
GenerateEncoder: *genEncoder,
GenerateDecoder: *genDecoder,
InternalMethods: *internal,
}
code, err := cfg.process()
if err != nil {
Expand All @@ -67,6 +69,8 @@ type Config struct {

GenerateEncoder bool
GenerateDecoder bool

InternalMethods bool
}

// process generates the Go code.
Expand Down Expand Up @@ -101,6 +105,7 @@ func (cfg *Config) process() (code []byte, err error) {
}
}
bctx := newBuildContext(packageRLP)
bctx.internalMethods = cfg.InternalMethods

// Find the type and generate.
typ, err := lookupStructType(pkg.Scope(), cfg.Type)
Expand Down
Loading