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 21, 2025
1 parent 8e7cc54 commit 0a85fe1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 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
10 changes: 10 additions & 0 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) *Header
}

// hooks returns the Header's registered HeaderHooks, if any, otherwise a
Expand Down Expand Up @@ -109,6 +110,15 @@ func (*NOOPHeaderHooks) DecodeRLP(h *Header, s *rlp.Stream) error {
return s.Decode((*withoutMethods)(h))
}

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

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

// 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) *Header {
return nil
}

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

0 comments on commit 0a85fe1

Please sign in to comment.