mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-20 13:44:31 +00:00
feat(core/types): Header hook PostCopy (#106)
This commit is contained in:
parent
e2b0abbe53
commit
ee93f60829
3 changed files with 25 additions and 0 deletions
|
|
@ -309,6 +309,7 @@ func CopyHeader(h *Header) *Header {
|
|||
cpy.ParentBeaconRoot = new(common.Hash)
|
||||
*cpy.ParentBeaconRoot = *h.ParentBeaconRoot
|
||||
}
|
||||
h.hooks().PostCopy(&cpy)
|
||||
return &cpy
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ type HeaderHooks interface {
|
|||
UnmarshalJSON(*Header, []byte) error //nolint:govet
|
||||
EncodeRLP(*Header, io.Writer) error
|
||||
DecodeRLP(*Header, *rlp.Stream) error
|
||||
PostCopy(dst *Header)
|
||||
}
|
||||
|
||||
// hooks returns the Header's registered HeaderHooks, if any, otherwise a
|
||||
|
|
@ -108,3 +109,5 @@ func (*NOOPHeaderHooks) DecodeRLP(h *Header, s *rlp.Stream) error {
|
|||
type withoutMethods Header
|
||||
return s.Decode((*withoutMethods)(h))
|
||||
}
|
||||
|
||||
func (n *NOOPHeaderHooks) PostCopy(dst *Header) {}
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ import (
|
|||
. "github.com/ava-labs/libevm/core/types"
|
||||
"github.com/ava-labs/libevm/crypto"
|
||||
"github.com/ava-labs/libevm/libevm/ethtest"
|
||||
"github.com/ava-labs/libevm/libevm/pseudo"
|
||||
"github.com/ava-labs/libevm/rlp"
|
||||
)
|
||||
|
||||
|
|
@ -36,6 +37,8 @@ type stubHeaderHooks struct {
|
|||
suffix []byte
|
||||
gotRawJSONToUnmarshal, gotRawRLPToDecode []byte
|
||||
setHeaderToOnUnmarshalOrDecode Header
|
||||
accessor pseudo.Accessor[*Header, *stubHeaderHooks]
|
||||
toCopy *stubHeaderHooks
|
||||
|
||||
errMarshal, errUnmarshal, errEncode, errDecode error
|
||||
}
|
||||
|
|
@ -75,6 +78,10 @@ func (hh *stubHeaderHooks) DecodeRLP(h *Header, s *rlp.Stream) error {
|
|||
return hh.errDecode
|
||||
}
|
||||
|
||||
func (hh *stubHeaderHooks) PostCopy(dst *Header) {
|
||||
hh.accessor.Set(dst, hh.toCopy)
|
||||
}
|
||||
|
||||
func TestHeaderHooks(t *testing.T) {
|
||||
TestOnlyClearRegisteredExtras()
|
||||
defer TestOnlyClearRegisteredExtras()
|
||||
|
|
@ -135,6 +142,20 @@ func TestHeaderHooks(t *testing.T) {
|
|||
assert.Equalf(t, &stub.setHeaderToOnUnmarshalOrDecode, hdr, "%T after RLP decoding with hook", hdr)
|
||||
})
|
||||
|
||||
t.Run("PostCopy", func(t *testing.T) {
|
||||
hdr := new(Header)
|
||||
stub := &stubHeaderHooks{
|
||||
accessor: extras.Header,
|
||||
toCopy: &stubHeaderHooks{
|
||||
suffix: []byte("copied"),
|
||||
},
|
||||
}
|
||||
extras.Header.Set(hdr, stub)
|
||||
|
||||
got := extras.Header.Get(CopyHeader(hdr))
|
||||
assert.Equal(t, stub.toCopy, got)
|
||||
})
|
||||
|
||||
t.Run("error_propagation", func(t *testing.T) {
|
||||
errMarshal := errors.New("whoops")
|
||||
errUnmarshal := errors.New("is it broken?")
|
||||
|
|
|
|||
Loading…
Reference in a new issue