refactor: payloads only available through params.ExtraPayloadGetter

This commit is contained in:
Arran Schlosberg 2024-08-22 19:46:28 +01:00
parent 0bd2bfc667
commit d9dedd76d4
No known key found for this signature in database
GPG key ID: 8A30F7E4344B4EF3
5 changed files with 13 additions and 40 deletions

View file

@ -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
)

View file

@ -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=

View file

@ -32,8 +32,8 @@ type Extras[C any, R any] struct {
// [ChainConfig.Rules] will call the `NewForRules` function of the registered // [ChainConfig.Rules] will call the `NewForRules` function of the registered
// [Extras] to create a new `*R`. // [Extras] to create a new `*R`.
// //
// The payloads can be accessed via the [ChainConfig.ExtraPayload] and // The payloads can be accessed via the [ChainConfig.extraPayload] and
// [Rules.ExtraPayload] methods, which will always return a `*C` or `*R` // [Rules.extraPayload] methods, which will always return a `*C` or `*R`
// respectively however these pointers may themselves be nil. // respectively however these pointers may themselves be nil.
// //
// As the `ExtraPayload()` methods are not generic and return `any`, their // As the `ExtraPayload()` methods are not generic and return `any`, their
@ -56,12 +56,12 @@ type ExtraPayloadGetter[C any, R any] struct{}
// FromChainConfig ... // FromChainConfig ...
func (ExtraPayloadGetter[C, R]) FromChainConfig(c *ChainConfig) *C { func (ExtraPayloadGetter[C, R]) FromChainConfig(c *ChainConfig) *C {
return pseudo.NewValueUnsafe[*C](c.ExtraPayload()).Get() return pseudo.NewValueUnsafe[*C](c.extraPayload()).Get()
} }
// FromRules ... // FromRules ...
func (ExtraPayloadGetter[C, R]) FromRules(r *Rules) *R { 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]() { 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 // only be called if [RegisterExtras] was called. The returned value is always
// of type `*C` as registered, but may be nil. Callers MUST immediately // 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 // type-assert the returned value to `*C` to avoid typed-nil bugs. See the
// example for the intended usage pattern. // example for the intended usage pattern.
func (c *ChainConfig) ExtraPayload() *pseudo.Type { func (c *ChainConfig) extraPayload() *pseudo.Type {
if registeredExtras == nil { if registeredExtras == nil {
panic(fmt.Sprintf("%T.ExtraPayload() called before RegisterExtras()", c)) panic(fmt.Sprintf("%T.ExtraPayload() called before RegisterExtras()", c))
} }
@ -145,12 +145,12 @@ func (c *ChainConfig) ExtraPayload() *pseudo.Type {
return c.extra 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 // 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 // `*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 // returned value to `*R` to avoid typed-nil bugs. See the example on
// [ChainConfig.ExtraPayload] for the intended usage pattern. // [ChainConfig.extraPayload] for the intended usage pattern.
func (r *Rules) ExtraPayload() *pseudo.Type { func (r *Rules) extraPayload() *pseudo.Type {
if registeredExtras == nil { if registeredExtras == nil {
panic(fmt.Sprintf("%T.ExtraPayload() called before RegisterExtras()", r)) panic(fmt.Sprintf("%T.ExtraPayload() called before RegisterExtras()", r))
} }

View file

@ -184,13 +184,13 @@ func TestRegisterExtras(t *testing.T) {
got := new(ChainConfig) got := new(ChainConfig)
require.NoError(t, json.Unmarshal(buf, got)) 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) assert.Equal(t, in, got)
// TODO: do we need an explicit test of the JSON output, or is a // TODO: do we need an explicit test of the JSON output, or is a
// Marshal-Unmarshal round trip sufficient? // Marshal-Unmarshal round trip sufficient?
gotRules := got.Rules(nil, false, 0) 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( assertPanics(
t, func() { t, func() {
new(ChainConfig).ExtraPayload() new(ChainConfig).extraPayload()
}, },
"before RegisterExtras", "before RegisterExtras",
) )
assertPanics( assertPanics(
t, func() { t, func() {
new(Rules).ExtraPayload() new(Rules).extraPayload()
}, },
"before RegisterExtras", "before RegisterExtras",
) )