Skip to content

Commit df20255

Browse files
committed
feat: HeaderHooks JSON round-tripping
1 parent 606881b commit df20255

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

core/types/block.libevm.go

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,25 @@ import (
2828
// HeaderHooks are required for all types registered with [RegisterExtras] for
2929
// [Header] payloads.
3030
type HeaderHooks interface {
31+
MarshalJSON(*Header) ([]byte, error) //nolint:govet // Type-specific override hook
32+
UnmarshalJSON(*Header, []byte) error //nolint:govet
3133
EncodeRLP(*Header, io.Writer) error
3234
DecodeRLP(*Header, *rlp.Stream) error
3335
}
3436

37+
// hooks returns the Header's registered HeaderHooks, if any, otherwise a
38+
// [NOOPHeaderHooks] suitable for running default behaviour.
39+
func (h *Header) hooks() HeaderHooks {
40+
if r := registeredExtras; r.Registered() {
41+
return r.Get().hooks.hooksFromHeader(h)
42+
}
43+
return new(NOOPHeaderHooks)
44+
}
45+
46+
func (e ExtraPayloads[HPtr, SA]) hooksFromHeader(h *Header) HeaderHooks {
47+
return e.Header.Get(h)
48+
}
49+
3550
var _ interface {
3651
rlp.Encoder
3752
rlp.Decoder
@@ -41,39 +56,22 @@ var _ interface {
4156

4257
// MarshalJSON implements the [json.Marshaler] interface.
4358
func (h *Header) MarshalJSON() ([]byte, error) {
44-
return h.marshalJSON()
59+
return h.hooks().MarshalJSON(h)
4560
}
4661

4762
// UnmarshalJSON implements the [json.Unmarshaler] interface.
4863
func (h *Header) UnmarshalJSON(b []byte) error {
49-
return h.unmarshalJSON(b)
64+
return h.hooks().UnmarshalJSON(h, b)
5065
}
5166

5267
// EncodeRLP implements the [rlp.Encoder] interface.
5368
func (h *Header) EncodeRLP(w io.Writer) error {
54-
if r := registeredExtras; r.Registered() {
55-
return r.Get().hooks.hooksFromHeader(h).EncodeRLP(h, w)
56-
}
57-
return h.encodeRLP(w)
58-
}
59-
60-
// decodeHeaderRLPDirectly bypasses the [Header.DecodeRLP] method to avoid
61-
// infinite recursion.
62-
func decodeHeaderRLPDirectly(h *Header, s *rlp.Stream) error {
63-
type withoutMethods Header
64-
return s.Decode((*withoutMethods)(h))
69+
return h.hooks().EncodeRLP(h, w)
6570
}
6671

6772
// DecodeRLP implements the [rlp.Decoder] interface.
6873
func (h *Header) DecodeRLP(s *rlp.Stream) error {
69-
if r := registeredExtras; r.Registered() {
70-
return r.Get().hooks.hooksFromHeader(h).DecodeRLP(h, s)
71-
}
72-
return decodeHeaderRLPDirectly(h, s)
73-
}
74-
75-
func (e ExtraPayloads[HPtr, SA]) hooksFromHeader(h *Header) HeaderHooks {
76-
return e.Header.Get(h)
74+
return h.hooks().DecodeRLP(h, s)
7775
}
7876

7977
func (h *Header) extraPayload() *pseudo.Type {
@@ -94,10 +92,19 @@ type NOOPHeaderHooks struct{}
9492

9593
var _ HeaderHooks = (*NOOPHeaderHooks)(nil)
9694

95+
func (*NOOPHeaderHooks) MarshalJSON(h *Header) ([]byte, error) { //nolint:govet
96+
return h.marshalJSON()
97+
}
98+
99+
func (*NOOPHeaderHooks) UnmarshalJSON(h *Header, b []byte) error { //nolint:govet
100+
return h.unmarshalJSON(b)
101+
}
102+
97103
func (*NOOPHeaderHooks) EncodeRLP(h *Header, w io.Writer) error {
98104
return h.encodeRLP(w)
99105
}
100106

101107
func (*NOOPHeaderHooks) DecodeRLP(h *Header, s *rlp.Stream) error {
102-
return decodeHeaderRLPDirectly(h, s)
108+
type withoutMethods Header
109+
return s.Decode((*withoutMethods)(h))
103110
}

core/types/block.libevm_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ func fakeHeaderRLP(h *Header, suffix []byte) []byte {
4242
return append(crypto.Keccak256(h.ParentHash[:]), suffix...)
4343
}
4444

45+
func (hh *stubHeaderHooks) MarshalJSON(h *Header) ([]byte, error) { //nolint:govet
46+
return nil, errors.New("TODO")
47+
}
48+
49+
func (hh *stubHeaderHooks) UnmarshalJSON(h *Header, b []byte) error { //nolint:govet
50+
return errors.New("TODO")
51+
}
52+
4553
func (hh *stubHeaderHooks) EncodeRLP(h *Header, w io.Writer) error {
4654
if _, err := w.Write(fakeHeaderRLP(h, hh.rlpSuffix)); err != nil {
4755
return err

0 commit comments

Comments
 (0)