-
Notifications
You must be signed in to change notification settings - Fork 3
feat(core/types): Body
RLP codec hooks
#107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a8ffd92
d10c4a3
0508241
b9e5186
7f6afca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ type HeaderHooks interface { | |
UnmarshalJSON(*Header, []byte) error //nolint:govet | ||
EncodeRLP(*Header, io.Writer) error | ||
DecodeRLP(*Header, *rlp.Stream) error | ||
Copy(*Header) *Header | ||
} | ||
|
||
// hooks returns the Header's registered HeaderHooks, if any, otherwise a | ||
|
@@ -43,7 +44,7 @@ func (h *Header) hooks() HeaderHooks { | |
return new(NOOPHeaderHooks) | ||
} | ||
|
||
func (e ExtraPayloads[HPtr, SA]) hooksFromHeader(h *Header) HeaderHooks { | ||
func (e ExtraPayloads[HPtr, BodyExtraPtr, SA]) hooksFromHeader(h *Header) HeaderHooks { | ||
qdm12 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return e.Header.Get(h) | ||
} | ||
|
||
|
@@ -108,3 +109,75 @@ func (*NOOPHeaderHooks) DecodeRLP(h *Header, s *rlp.Stream) error { | |
type withoutMethods Header | ||
return s.Decode((*withoutMethods)(h)) | ||
} | ||
|
||
func (n *NOOPHeaderHooks) Copy(h *Header) *Header { | ||
return CopyEthHeader(h) | ||
} | ||
|
||
// CopyHeader creates a deep copy of a block header. | ||
func CopyHeader(h *Header) *Header { | ||
return h.hooks().Copy(h) | ||
} | ||
|
||
// BodyHooks are required for all types registered with [RegisterExtras] for | ||
// [Body] payloads. | ||
type BodyHooks interface { | ||
EncodeRLP(*Body, io.Writer) error | ||
DecodeRLP(*Body, *rlp.Stream) error | ||
} | ||
|
||
// hooks returns the Body's registered BodyHooks, if any, otherwise a | ||
// [*NOOPBodyHooks] suitable for running the default behaviour. | ||
func (b *Body) hooks() BodyHooks { | ||
if r := registeredExtras; r.Registered() { | ||
return r.Get().hooks.hooksFromBody(b) | ||
} | ||
return new(NOOPBodyHooks) | ||
} | ||
|
||
func (e ExtraPayloads[HPtr, BodyExtraPtr, SA]) hooksFromBody(b *Body) BodyHooks { | ||
return e.Body.Get(b) | ||
} | ||
|
||
var _ interface { | ||
rlp.Encoder | ||
rlp.Decoder | ||
} = (*Body)(nil) | ||
|
||
// EncodeRLP implements the [rlp.Encoder] interface. | ||
func (b *Body) EncodeRLP(w io.Writer) error { | ||
return b.hooks().EncodeRLP(b, w) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Completely overriding RLP {en,de}coding is a blunt tool that we used for My greatest concern with a complete override is that the user still has to implement a lot of the upstream functionality, reducing the benefit of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Progress on #109 suggests that this should be possible, with a hook that only needs to write fields to an |
||
} | ||
|
||
// DecodeRLP implements the [rlp.Decoder] interface. | ||
func (b *Body) DecodeRLP(s *rlp.Stream) error { | ||
return b.hooks().DecodeRLP(b, s) | ||
} | ||
|
||
func (b *Body) extraPayload() *pseudo.Type { | ||
qdm12 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
r := registeredExtras | ||
if !r.Registered() { | ||
// See params.ChainConfig.extraPayload() for panic rationale. | ||
panic(fmt.Sprintf("%T.extraPayload() called before RegisterExtras()", r)) | ||
} | ||
if b.extra == nil { | ||
b.extra = r.Get().newBody() | ||
} | ||
return b.extra | ||
} | ||
|
||
// NOOPBodyHooks implements [BodyHooks] such that they are equivalent to | ||
// no type having been registered. | ||
type NOOPBodyHooks struct{} | ||
|
||
var _ BodyHooks = (*NOOPBodyHooks)(nil) | ||
|
||
func (*NOOPBodyHooks) EncodeRLP(b *Body, w io.Writer) error { | ||
type withoutMethods Body | ||
return rlp.Encode(w, (*withoutMethods)(b)) | ||
} | ||
|
||
func (*NOOPBodyHooks) DecodeRLP(b *Body, s *rlp.Stream) error { | ||
type withoutMethods Body | ||
return s.Decode((*withoutMethods)(b)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note this change is compulsory since there are more fields present in the struct than these 3.