From d9dedd76d496bb6a77d2e3bc9cbf5ed88fd345d8 Mon Sep 17 00:00:00 2001 From: Arran Schlosberg Date: Thu, 22 Aug 2024 19:46:28 +0100 Subject: [PATCH] refactor: payloads only available through `params.ExtraPayloadGetter` --- libevm/examples/go.mod | 13 ------------- libevm/examples/go.sum | 14 -------------- params/config.libevm.go | 18 +++++++++--------- params/config.libevm_test.go | 8 ++++---- .../example.libevm_test.go | 0 5 files changed, 13 insertions(+), 40 deletions(-) delete mode 100644 libevm/examples/go.mod delete mode 100644 libevm/examples/go.sum rename libevm/examples/extraparams/extraparams.go => params/example.libevm_test.go (100%) diff --git a/libevm/examples/go.mod b/libevm/examples/go.mod deleted file mode 100644 index 84e13d9b63..0000000000 --- a/libevm/examples/go.mod +++ /dev/null @@ -1,13 +0,0 @@ -module libevm/examples - -go 1.22.4 - -replace github.com/ethereum/go-ethereum => ../../ - -require github.com/ethereum/go-ethereum v0.0.0-00010101000000-000000000000 - -require ( - github.com/holiman/uint256 v1.3.1 // indirect - golang.org/x/crypto v0.22.0 // indirect - golang.org/x/sys v0.22.0 // indirect -) diff --git a/libevm/examples/go.sum b/libevm/examples/go.sum deleted file mode 100644 index 145acea3bb..0000000000 --- a/libevm/examples/go.sum +++ /dev/null @@ -1,14 +0,0 @@ -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/holiman/uint256 v1.3.1 h1:JfTzmih28bittyHM8z360dCjIA9dbPIBlcTI6lmctQs= -github.com/holiman/uint256 v1.3.1/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= -golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= -golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= -golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/params/config.libevm.go b/params/config.libevm.go index 8fe40e709d..23384b6de9 100644 --- a/params/config.libevm.go +++ b/params/config.libevm.go @@ -32,8 +32,8 @@ type Extras[C any, R any] struct { // [ChainConfig.Rules] will call the `NewForRules` function of the registered // [Extras] to create a new `*R`. // -// The payloads can be accessed via the [ChainConfig.ExtraPayload] and -// [Rules.ExtraPayload] methods, which will always return a `*C` or `*R` +// The payloads can be accessed via the [ChainConfig.extraPayload] and +// [Rules.extraPayload] methods, which will always return a `*C` or `*R` // respectively however these pointers may themselves be nil. // // As the `ExtraPayload()` methods are not generic and return `any`, their @@ -56,12 +56,12 @@ type ExtraPayloadGetter[C any, R any] struct{} // FromChainConfig ... func (ExtraPayloadGetter[C, R]) FromChainConfig(c *ChainConfig) *C { - return pseudo.NewValueUnsafe[*C](c.ExtraPayload()).Get() + return pseudo.NewValueUnsafe[*C](c.extraPayload()).Get() } // FromRules ... func (ExtraPayloadGetter[C, R]) FromRules(r *Rules) *R { - return pseudo.NewValueUnsafe[*R](r.ExtraPayload()).Get() + return pseudo.NewValueUnsafe[*R](r.extraPayload()).Get() } func mustBeStruct[T any]() { @@ -130,12 +130,12 @@ func (c *ChainConfig) addRulesExtra(r *Rules, blockNum *big.Int, isMerge bool, t } } -// ExtraPayload returns the extra payload carried by the ChainConfig and can +// extraPayload returns the extra payload carried by the ChainConfig and can // only be called if [RegisterExtras] was called. The returned value is always // of type `*C` as registered, but may be nil. Callers MUST immediately // type-assert the returned value to `*C` to avoid typed-nil bugs. See the // example for the intended usage pattern. -func (c *ChainConfig) ExtraPayload() *pseudo.Type { +func (c *ChainConfig) extraPayload() *pseudo.Type { if registeredExtras == nil { panic(fmt.Sprintf("%T.ExtraPayload() called before RegisterExtras()", c)) } @@ -145,12 +145,12 @@ func (c *ChainConfig) ExtraPayload() *pseudo.Type { return c.extra } -// ExtraPayload returns the extra payload carried by the Rules and can only be +// extraPayload returns the extra payload carried by the Rules and can only be // called if [RegisterExtras] was called. The returned value is always of type // `*R` as registered, but may be nil. Callers MUST immediately type-assert the // returned value to `*R` to avoid typed-nil bugs. See the example on -// [ChainConfig.ExtraPayload] for the intended usage pattern. -func (r *Rules) ExtraPayload() *pseudo.Type { +// [ChainConfig.extraPayload] for the intended usage pattern. +func (r *Rules) extraPayload() *pseudo.Type { if registeredExtras == nil { panic(fmt.Sprintf("%T.ExtraPayload() called before RegisterExtras()", r)) } diff --git a/params/config.libevm_test.go b/params/config.libevm_test.go index 26f14d2cef..4f78888db3 100644 --- a/params/config.libevm_test.go +++ b/params/config.libevm_test.go @@ -184,13 +184,13 @@ func TestRegisterExtras(t *testing.T) { got := new(ChainConfig) require.NoError(t, json.Unmarshal(buf, got)) - assert.Equal(t, tt.ccExtra.Interface(), got.ExtraPayload().Interface()) + assert.Equal(t, tt.ccExtra.Interface(), got.extraPayload().Interface()) assert.Equal(t, in, got) // TODO: do we need an explicit test of the JSON output, or is a // Marshal-Unmarshal round trip sufficient? gotRules := got.Rules(nil, false, 0) - assert.Equal(t, tt.wantRulesExtra, gotRules.ExtraPayload().Interface()) + assert.Equal(t, tt.wantRulesExtra, gotRules.extraPayload().Interface()) }) } } @@ -212,14 +212,14 @@ func TestExtrasPanic(t *testing.T) { assertPanics( t, func() { - new(ChainConfig).ExtraPayload() + new(ChainConfig).extraPayload() }, "before RegisterExtras", ) assertPanics( t, func() { - new(Rules).ExtraPayload() + new(Rules).extraPayload() }, "before RegisterExtras", ) diff --git a/libevm/examples/extraparams/extraparams.go b/params/example.libevm_test.go similarity index 100% rename from libevm/examples/extraparams/extraparams.go rename to params/example.libevm_test.go