mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-16 09:50:45 +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 = new(common.Hash)
|
||||||
*cpy.ParentBeaconRoot = *h.ParentBeaconRoot
|
*cpy.ParentBeaconRoot = *h.ParentBeaconRoot
|
||||||
}
|
}
|
||||||
|
h.hooks().PostCopy(&cpy)
|
||||||
return &cpy
|
return &cpy
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ type HeaderHooks interface {
|
||||||
UnmarshalJSON(*Header, []byte) error //nolint:govet
|
UnmarshalJSON(*Header, []byte) error //nolint:govet
|
||||||
EncodeRLP(*Header, io.Writer) error
|
EncodeRLP(*Header, io.Writer) error
|
||||||
DecodeRLP(*Header, *rlp.Stream) error
|
DecodeRLP(*Header, *rlp.Stream) error
|
||||||
|
PostCopy(dst *Header)
|
||||||
}
|
}
|
||||||
|
|
||||||
// hooks returns the Header's registered HeaderHooks, if any, otherwise a
|
// 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
|
type withoutMethods Header
|
||||||
return s.Decode((*withoutMethods)(h))
|
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/core/types"
|
||||||
"github.com/ava-labs/libevm/crypto"
|
"github.com/ava-labs/libevm/crypto"
|
||||||
"github.com/ava-labs/libevm/libevm/ethtest"
|
"github.com/ava-labs/libevm/libevm/ethtest"
|
||||||
|
"github.com/ava-labs/libevm/libevm/pseudo"
|
||||||
"github.com/ava-labs/libevm/rlp"
|
"github.com/ava-labs/libevm/rlp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -36,6 +37,8 @@ type stubHeaderHooks struct {
|
||||||
suffix []byte
|
suffix []byte
|
||||||
gotRawJSONToUnmarshal, gotRawRLPToDecode []byte
|
gotRawJSONToUnmarshal, gotRawRLPToDecode []byte
|
||||||
setHeaderToOnUnmarshalOrDecode Header
|
setHeaderToOnUnmarshalOrDecode Header
|
||||||
|
accessor pseudo.Accessor[*Header, *stubHeaderHooks]
|
||||||
|
toCopy *stubHeaderHooks
|
||||||
|
|
||||||
errMarshal, errUnmarshal, errEncode, errDecode error
|
errMarshal, errUnmarshal, errEncode, errDecode error
|
||||||
}
|
}
|
||||||
|
|
@ -75,6 +78,10 @@ func (hh *stubHeaderHooks) DecodeRLP(h *Header, s *rlp.Stream) error {
|
||||||
return hh.errDecode
|
return hh.errDecode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (hh *stubHeaderHooks) PostCopy(dst *Header) {
|
||||||
|
hh.accessor.Set(dst, hh.toCopy)
|
||||||
|
}
|
||||||
|
|
||||||
func TestHeaderHooks(t *testing.T) {
|
func TestHeaderHooks(t *testing.T) {
|
||||||
TestOnlyClearRegisteredExtras()
|
TestOnlyClearRegisteredExtras()
|
||||||
defer 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)
|
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) {
|
t.Run("error_propagation", func(t *testing.T) {
|
||||||
errMarshal := errors.New("whoops")
|
errMarshal := errors.New("whoops")
|
||||||
errUnmarshal := errors.New("is it broken?")
|
errUnmarshal := errors.New("is it broken?")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue