Skip to content

Commit

Permalink
feat: params.ExtraPayloadGetter[C,R].PointerFromChainConfig(...) *C
Browse files Browse the repository at this point in the history
… and `Rules => *R` equiv
  • Loading branch information
ARR4N committed Sep 11, 2024
1 parent cc36bed commit 5d99903
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
12 changes: 12 additions & 0 deletions params/config.libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ func (ExtraPayloadGetter[C, R]) FromChainConfig(c *ChainConfig) C {
return pseudo.MustNewValue[C](c.extraPayload()).Get()
}

// PointerFromChainConfig returns a pointer to the ChainConfig's extra payload.
// This is guaranteed to be non-nil.
func (ExtraPayloadGetter[C, R]) PointerFromChainConfig(c *ChainConfig) *C {
return pseudo.MustPointerTo[C](c.extraPayload()).Value.Get()
}

// hooksFromChainConfig is equivalent to FromChainConfig(), but returns an
// interface instead of the concrete type implementing it; this allows it to be
// used in non-generic code.
Expand All @@ -164,6 +170,12 @@ func (ExtraPayloadGetter[C, R]) FromRules(r *Rules) R {
return pseudo.MustNewValue[R](r.extraPayload()).Get()
}

// PointerFromRules returns a pointer to the Rules's extra payload. This is
// guaranteed to be non-nil.
func (ExtraPayloadGetter[C, R]) PointerFromRules(r *Rules) *R {
return pseudo.MustPointerTo[R](r.extraPayload()).Value.Get()
}

// hooksFromRules is the [RulesHooks] equivalent of hooksFromChainConfig().
func (e ExtraPayloadGetter[C, R]) hooksFromRules(r *Rules) RulesHooks {
return e.FromRules(r)
Expand Down
51 changes: 51 additions & 0 deletions params/config.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,57 @@ func TestRegisterExtras(t *testing.T) {
}
}

func TestZeroExtrasAndPointers(t *testing.T) {
type (
ccExtra struct {
X int
NOOPHooks
}
rulesExtra struct {
X int
NOOPHooks
}
)

TestOnlyClearRegisteredExtras()
t.Cleanup(TestOnlyClearRegisteredExtras)
getter := RegisterExtras(Extras[ccExtra, rulesExtra]{})

var (
config ChainConfig
rules Rules
)
// These assertion helpers are defined before any modifications so that the
// closure is demonstrably over the original zero values.
assertChainConfigExtra := func(t *testing.T, want ccExtra, msg string) {
t.Helper()
assert.Equalf(t, want, getter.FromChainConfig(&config), "%T: "+msg, &config)
}
assertRulesExtra := func(t *testing.T, want rulesExtra, msg string) {
t.Helper()
assert.Equalf(t, want, getter.FromRules(&rules), "%T: "+msg, &rules)
}

assertChainConfigExtra(t, ccExtra{}, "zero value")
assertRulesExtra(t, rulesExtra{}, "zero value")

const answer = 42
getter.PointerFromChainConfig(&config).X = answer
assertChainConfigExtra(t, ccExtra{X: answer}, "after setting via pointer field")

const pi = 314159
getter.PointerFromRules(&rules).X = pi
assertRulesExtra(t, rulesExtra{X: pi}, "after setting via pointer field")

ccReplace := ccExtra{X: 142857}
*getter.PointerFromChainConfig(&config) = ccReplace
assertChainConfigExtra(t, ccReplace, "after replacement of entire extra via `*pointer = x`")

rulesReplace := rulesExtra{X: 18101986}
*getter.PointerFromRules(&rules) = rulesReplace
assertRulesExtra(t, rulesReplace, "after replacement of entire extra via `*pointer = x`")
}

func TestExtrasPanic(t *testing.T) {
TestOnlyClearRegisteredExtras()
defer TestOnlyClearRegisteredExtras()
Expand Down

0 comments on commit 5d99903

Please sign in to comment.