Skip to content

Commit

Permalink
Header Copy hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jan 20, 2025
1 parent 8e7cc54 commit 7306184
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
6 changes: 1 addition & 5 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,7 @@ func NewBlockWithWithdrawals(header *Header, txs []*Transaction, uncles []*Heade
return b.WithWithdrawals(withdrawals)
}

// CopyHeader creates a deep copy of a block header.
func CopyHeader(h *Header) *Header {
func copyHeader(h *Header) *Header {
cpy := *h
if cpy.Difficulty = new(big.Int); h.Difficulty != nil {
cpy.Difficulty.Set(h.Difficulty)
Expand Down Expand Up @@ -313,9 +312,6 @@ func CopyHeader(h *Header) *Header {
cpy.ParentBeaconRoot = new(common.Hash)
*cpy.ParentBeaconRoot = *h.ParentBeaconRoot
}
if h.extra != nil {
cpy.extra = h.extra
}
return &cpy
}

Expand Down
18 changes: 16 additions & 2 deletions core/types/block.libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

// hooks returns the Header's registered HeaderHooks, if any, otherwise a
Expand All @@ -40,7 +41,9 @@ func (h *Header) hooks() HeaderHooks {
if r := registeredExtras; r.Registered() {
return r.Get().hooks.hooksFromHeader(h)
}
return new(NOOPHeaderHooks)
return &NOOPHeaderHooks{
header: h,
}
}

func (e ExtraPayloads[HPtr, BlockExtraPtr, BodyExtraPtr, SA]) hooksFromHeader(h *Header) HeaderHooks {
Expand Down Expand Up @@ -88,7 +91,9 @@ func (h *Header) extraPayload() *pseudo.Type {

// NOOPHeaderHooks implements [HeaderHooks] such that they are equivalent to
// no type having been registered.
type NOOPHeaderHooks struct{}
type NOOPHeaderHooks struct {
header *Header
}

var _ HeaderHooks = (*NOOPHeaderHooks)(nil)

Expand All @@ -109,6 +114,15 @@ func (*NOOPHeaderHooks) DecodeRLP(h *Header, s *rlp.Stream) error {
return s.Decode((*withoutMethods)(h))
}

func (n *NOOPHeaderHooks) Copy() *Header {
return copyHeader(n.header)
}

// CopyHeader creates a deep copy of a block header.
func CopyHeader(h *Header) *Header {
return h.hooks().Copy()
}

// BlockHooks are required for all types registered with [RegisterExtras] for
// [Block] payloads.
type BlockHooks interface {
Expand Down
4 changes: 4 additions & 0 deletions core/types/block.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func (hh *stubHeaderHooks) DecodeRLP(h *Header, s *rlp.Stream) error {
return hh.errDecode
}

func (hh *stubHeaderHooks) Copy() *Header {
return nil
}

type stubBlockHooks struct {
suffix []byte
gotRawRLPToDecode []byte
Expand Down

0 comments on commit 7306184

Please sign in to comment.