@@ -47,27 +47,27 @@ type Extras[C ChainConfigHooks, R RulesHooks] struct {
47
47
// Calls to [ChainConfig.Rules] will call the `NewRules` function of the
48
48
// registered [Extras] to create a new `R`.
49
49
//
50
- // The payloads can be accessed via the [ExtraPayloadGetter .FromChainConfig] and
51
- // [ExtraPayloadGetter .FromRules] methods of the getter returned by
52
- // RegisterExtras. Where stated in the interface definitions, they will also be
53
- // used as hooks to alter Ethereum behaviour; if this isn't desired then they
54
- // can embed [NOOPHooks] to satisfy either interface.
55
- func RegisterExtras [C ChainConfigHooks , R RulesHooks ](e Extras [C , R ]) ExtraPayloadGetter [C , R ] {
50
+ // The payloads can be accessed via the [ExtraPayloads .FromChainConfig] and
51
+ // [ExtraPayloads .FromRules] methods of the accessor returned by RegisterExtras.
52
+ // Where stated in the interface definitions, they will also be used as hooks to
53
+ // alter Ethereum behaviour; if this isn't desired then they can embed
54
+ // [NOOPHooks] to satisfy either interface.
55
+ func RegisterExtras [C ChainConfigHooks , R RulesHooks ](e Extras [C , R ]) ExtraPayloads [C , R ] {
56
56
if registeredExtras != nil {
57
57
panic ("re-registration of Extras" )
58
58
}
59
59
mustBeStructOrPointerToOne [C ]()
60
60
mustBeStructOrPointerToOne [R ]()
61
61
62
- getter := e .getter ()
62
+ payloads := e .payloads ()
63
63
registeredExtras = & extraConstructors {
64
64
newChainConfig : pseudo .NewConstructor [C ]().Zero ,
65
65
newRules : pseudo .NewConstructor [R ]().Zero ,
66
66
reuseJSONRoot : e .ReuseJSONRoot ,
67
67
newForRules : e .newForRules ,
68
- getter : getter ,
68
+ payloads : payloads ,
69
69
}
70
- return getter
70
+ return payloads
71
71
}
72
72
73
73
// TestOnlyClearRegisteredExtras clears the [Extras] previously passed to
@@ -102,7 +102,7 @@ type extraConstructors struct {
102
102
newForRules func (_ * ChainConfig , _ * Rules , blockNum * big.Int , isMerge bool , timestamp uint64 ) * pseudo.Type
103
103
// use top-level hooksFrom<X>() functions instead of these as they handle
104
104
// instances where no [Extras] were registered.
105
- getter interface {
105
+ payloads interface {
106
106
hooksFromChainConfig (* ChainConfig ) ChainConfigHooks
107
107
hooksFromRules (* Rules ) RulesHooks
108
108
}
@@ -112,11 +112,11 @@ func (e *Extras[C, R]) newForRules(c *ChainConfig, r *Rules, blockNum *big.Int,
112
112
if e .NewRules == nil {
113
113
return registeredExtras .newRules ()
114
114
}
115
- rExtra := e .NewRules (c , r , e .getter ().FromChainConfig (c ), blockNum , isMerge , timestamp )
115
+ rExtra := e .NewRules (c , r , e .payloads ().FromChainConfig (c ), blockNum , isMerge , timestamp )
116
116
return pseudo .From (rExtra ).Type
117
117
}
118
118
119
- func (* Extras [C , R ]) getter () (g ExtraPayloadGetter [C , R ]) { return }
119
+ func (* Extras [C , R ]) payloads () (g ExtraPayloads [C , R ]) { return }
120
120
121
121
// mustBeStructOrPointerToOne panics if `T` isn't a struct or a *struct.
122
122
func mustBeStructOrPointerToOne [T any ]() {
@@ -140,44 +140,56 @@ func notStructMessage[T any]() string {
140
140
return fmt .Sprintf ("%T is not a struct nor a pointer to a struct" , x )
141
141
}
142
142
143
- // An ExtraPayloadGettter provides strongly typed access to the extra payloads
144
- // carried by [ChainConfig] and [Rules] structs. The only valid way to construct
145
- // a getter is by a call to [RegisterExtras].
146
- type ExtraPayloadGetter [C ChainConfigHooks , R RulesHooks ] struct {
147
- _ struct {} // make godoc show unexported fields so nobody tries to make their own getter ;)
143
+ // ExtraPayloads provides strongly typed access to the extra payloads carried by
144
+ // [ChainConfig] and [Rules] structs. The only valid way to construct an
145
+ // instance is by a call to [RegisterExtras].
146
+ type ExtraPayloads [C ChainConfigHooks , R RulesHooks ] struct {
147
+ _ struct {} // make godoc show unexported fields so nobody tries to make their own instance ;)
148
148
}
149
149
150
150
// FromChainConfig returns the ChainConfig's extra payload.
151
- func (ExtraPayloadGetter [C , R ]) FromChainConfig (c * ChainConfig ) C {
151
+ func (ExtraPayloads [C , R ]) FromChainConfig (c * ChainConfig ) C {
152
152
return pseudo.MustNewValue [C ](c .extraPayload ()).Get ()
153
153
}
154
154
155
155
// PointerFromChainConfig returns a pointer to the ChainConfig's extra payload.
156
156
// This is guaranteed to be non-nil.
157
- func (ExtraPayloadGetter [C , R ]) PointerFromChainConfig (c * ChainConfig ) * C {
157
+ func (ExtraPayloads [C , R ]) PointerFromChainConfig (c * ChainConfig ) * C {
158
158
return pseudo.MustPointerTo [C ](c .extraPayload ()).Value .Get ()
159
159
}
160
160
161
+ // SetOnChainConfig sets the ChainConfig's extra payload. It is equivalent to
162
+ // `*e.PointerFromChainConfig(cc) = val`.
163
+ func (e ExtraPayloads [C , R ]) SetOnChainConfig (cc * ChainConfig , val C ) {
164
+ * e .PointerFromChainConfig (cc ) = val
165
+ }
166
+
161
167
// hooksFromChainConfig is equivalent to FromChainConfig(), but returns an
162
168
// interface instead of the concrete type implementing it; this allows it to be
163
169
// used in non-generic code.
164
- func (e ExtraPayloadGetter [C , R ]) hooksFromChainConfig (c * ChainConfig ) ChainConfigHooks {
170
+ func (e ExtraPayloads [C , R ]) hooksFromChainConfig (c * ChainConfig ) ChainConfigHooks {
165
171
return e .FromChainConfig (c )
166
172
}
167
173
168
174
// FromRules returns the Rules' extra payload.
169
- func (ExtraPayloadGetter [C , R ]) FromRules (r * Rules ) R {
175
+ func (ExtraPayloads [C , R ]) FromRules (r * Rules ) R {
170
176
return pseudo.MustNewValue [R ](r .extraPayload ()).Get ()
171
177
}
172
178
173
179
// PointerFromRules returns a pointer to the Rules's extra payload. This is
174
180
// guaranteed to be non-nil.
175
- func (ExtraPayloadGetter [C , R ]) PointerFromRules (r * Rules ) * R {
181
+ func (ExtraPayloads [C , R ]) PointerFromRules (r * Rules ) * R {
176
182
return pseudo.MustPointerTo [R ](r .extraPayload ()).Value .Get ()
177
183
}
178
184
185
+ // SetOnRules sets the Rules' extra payload. It is equivalent to
186
+ // `*e.PointerFromRules(r) = val`.
187
+ func (e ExtraPayloads [C , R ]) SetOnRules (r * Rules , val R ) {
188
+ * e .PointerFromRules (r ) = val
189
+ }
190
+
179
191
// hooksFromRules is the [RulesHooks] equivalent of hooksFromChainConfig().
180
- func (e ExtraPayloadGetter [C , R ]) hooksFromRules (r * Rules ) RulesHooks {
192
+ func (e ExtraPayloads [C , R ]) hooksFromRules (r * Rules ) RulesHooks {
181
193
return e .FromRules (r )
182
194
}
183
195
@@ -195,7 +207,7 @@ func (c *ChainConfig) addRulesExtra(r *Rules, blockNum *big.Int, isMerge bool, t
195
207
// unmarshalling of JSON), a nil value is constructed and returned.
196
208
func (c * ChainConfig ) extraPayload () * pseudo.Type {
197
209
if registeredExtras == nil {
198
- // This will only happen if someone constructs an [ExtraPayloadGetter ]
210
+ // This will only happen if someone constructs an [ExtraPayloads ]
199
211
// directly, without a call to [RegisterExtras].
200
212
//
201
213
// See https://google.github.io/styleguide/go/best-practices#when-to-panic
0 commit comments