@@ -74,11 +74,10 @@ func RegisterExtras[
74
74
// The [ExtraPayloads] that we returns is based on [HPtr,BPtr,SA], not
75
75
// [H,B,SA] so our constructors MUST match that. This guarantees that calls to
76
76
// the [HeaderHooks] and [BodyHooks] methods will never be performed on a nil pointer.
77
- newHeader : pseudo .NewConstructor [H ]().NewPointer , // i.e. non-nil HPtr
78
- newBody : pseudo .NewConstructor [B ]().NewPointer , // i.e. non-nil BPtr
79
- newStateAccount : pseudo .NewConstructor [SA ]().Zero ,
80
- cloneStateAccount : extra .cloneStateAccount ,
81
- hooks : extra ,
77
+ newHeader : pseudo .NewConstructor [H ]().NewPointer , // i.e. non-nil HPtr
78
+ newBody : pseudo .NewConstructor [B ]().NewPointer , // i.e. non-nil BPtr
79
+ newStateAccount : pseudo .NewConstructor [SA ]().Zero ,
80
+ hooks : extra ,
82
81
})
83
82
return extra
84
83
}
@@ -96,23 +95,65 @@ func TestOnlyClearRegisteredExtras() {
96
95
var registeredExtras register.AtMostOnce [* extraConstructors ]
97
96
98
97
type extraConstructors struct {
99
- stateAccountType string
100
- newHeader func () * pseudo.Type
101
- newBody func () * pseudo.Type
102
- newStateAccount func () * pseudo.Type
103
- cloneStateAccount func (* StateAccountExtra ) * StateAccountExtra
104
- hooks interface {
98
+ stateAccountType string
99
+ newHeader func () * pseudo.Type
100
+ newBody func () * pseudo.Type
101
+ newStateAccount func () * pseudo.Type
102
+ hooks interface {
105
103
hooksFromHeader (* Header ) HeaderHooks
106
104
hooksFromBody (* Body ) BodyHooks
105
+ cloneStateAccount (* StateAccountExtra ) * StateAccountExtra
107
106
}
108
107
}
109
108
109
+ func extraPayloadOrSetDefault (field * * pseudo.Type , construct func (* extraConstructors ) * pseudo.Type ) * pseudo.Type {
110
+ r := registeredExtras
111
+ if ! r .Registered () {
112
+ // See params.ChainConfig.extraPayload() for panic rationale.
113
+ panic ("<T>.extraPayload() called before RegisterExtras()" )
114
+ }
115
+ if * field == nil {
116
+ * field = construct (r .Get ())
117
+ }
118
+ return * field
119
+ }
120
+
121
+ func (h * Header ) extraPayload () * pseudo.Type {
122
+ return extraPayloadOrSetDefault (& h .extra , func (c * extraConstructors ) * pseudo.Type {
123
+ return c .newHeader ()
124
+ })
125
+ }
126
+
127
+ func (b * Body ) extraPayload () * pseudo.Type {
128
+ return extraPayloadOrSetDefault (& b .extra , func (c * extraConstructors ) * pseudo.Type {
129
+ return c .newBody ()
130
+ })
131
+ }
132
+
133
+ // hooks returns the [Header]'s registered [HeaderHooks], if any, otherwise a
134
+ // [NOOPHeaderHooks] suitable for running default behaviour.
135
+ func (h * Header ) hooks () HeaderHooks {
136
+ if r := registeredExtras ; r .Registered () {
137
+ return r .Get ().hooks .hooksFromHeader (h )
138
+ }
139
+ return new (NOOPHeaderHooks )
140
+ }
141
+
142
+ // hooks returns the [Body]'s registered [BodyHooks], if any, otherwise a
143
+ // [NOOPBodyHooks] suitable for running default behaviour.
144
+ func (b * Body ) hooks () BodyHooks {
145
+ if r := registeredExtras ; r .Registered () {
146
+ return r .Get ().hooks .hooksFromBody (b )
147
+ }
148
+ return NOOPBodyHooks {}
149
+ }
150
+
110
151
func (e * StateAccountExtra ) clone () * StateAccountExtra {
111
152
switch r := registeredExtras ; {
112
153
case ! r .Registered (), e == nil :
113
154
return nil
114
155
default :
115
- return r .Get ().cloneStateAccount (e )
156
+ return r .Get ().hooks . cloneStateAccount (e )
116
157
}
117
158
}
118
159
@@ -125,6 +166,9 @@ type ExtraPayloads[HPtr HeaderHooks, BPtr BodyHooks, SA any] struct {
125
166
StateAccount pseudo.Accessor [StateOrSlimAccount , SA ] // Also provides [SlimAccount] access.
126
167
}
127
168
169
+ func (e ExtraPayloads [HPtr , BPtr , SA ]) hooksFromHeader (h * Header ) HeaderHooks { return e .Header .Get (h ) }
170
+ func (e ExtraPayloads [HPtr , BPtr , SA ]) hooksFromBody (b * Body ) BodyHooks { return e .Body .Get (b ) }
171
+
128
172
func (ExtraPayloads [HPtr , BPtr , SA ]) cloneStateAccount (s * StateAccountExtra ) * StateAccountExtra {
129
173
v := pseudo.MustNewValue [SA ](s .t )
130
174
return & StateAccountExtra {
0 commit comments