From dc9e4f85990e1e2c8c3f5cc84a995b90743249d5 Mon Sep 17 00:00:00 2001 From: Quentin Mc Gaw Date: Fri, 17 Jan 2025 14:46:00 +0100 Subject: [PATCH] feat(core/types): `BodyHooks` for RLP overrides --- core/state/state.libevm_test.go | 1 + core/state/state_object.libevm_test.go | 3 ++ core/types/block.libevm_test.go | 31 ++++++++++++++++++- .../types/rlp_backwards_compat.libevm_test.go | 1 + core/types/state_account.libevm_test.go | 2 ++ .../state_account_storage.libevm_test.go | 4 +++ 6 files changed, 41 insertions(+), 1 deletion(-) diff --git a/core/state/state.libevm_test.go b/core/state/state.libevm_test.go index 17a236245587..54493fdb8aca 100644 --- a/core/state/state.libevm_test.go +++ b/core/state/state.libevm_test.go @@ -48,6 +48,7 @@ func TestGetSetExtra(t *testing.T) { payloads := types.RegisterExtras[ types.NOOPHeaderHooks, *types.NOOPHeaderHooks, types.NOOPBlockHooks, *types.NOOPBlockHooks, + types.NOOPBodyHooks, *types.NOOPBodyHooks, *accountExtra]().StateAccount rng := ethtest.NewPseudoRand(42) diff --git a/core/state/state_object.libevm_test.go b/core/state/state_object.libevm_test.go index 29b46a8a3994..36293033abd9 100644 --- a/core/state/state_object.libevm_test.go +++ b/core/state/state_object.libevm_test.go @@ -49,6 +49,7 @@ func TestStateObjectEmpty(t *testing.T) { types.RegisterExtras[ types.NOOPHeaderHooks, *types.NOOPHeaderHooks, types.NOOPBlockHooks, *types.NOOPBlockHooks, + types.NOOPBodyHooks, *types.NOOPBodyHooks, bool]().StateAccount.Set(acc, false) }, wantEmpty: true, @@ -59,6 +60,7 @@ func TestStateObjectEmpty(t *testing.T) { types.RegisterExtras[ types.NOOPHeaderHooks, *types.NOOPHeaderHooks, types.NOOPBlockHooks, *types.NOOPBlockHooks, + types.NOOPBodyHooks, *types.NOOPBodyHooks, bool]() }, wantEmpty: true, @@ -69,6 +71,7 @@ func TestStateObjectEmpty(t *testing.T) { types.RegisterExtras[ types.NOOPHeaderHooks, *types.NOOPHeaderHooks, types.NOOPBlockHooks, *types.NOOPBlockHooks, + types.NOOPBodyHooks, *types.NOOPBodyHooks, bool]().StateAccount.Set(acc, true) }, wantEmpty: false, diff --git a/core/types/block.libevm_test.go b/core/types/block.libevm_test.go index 5966539ccfdd..5bb9ae6c4424 100644 --- a/core/types/block.libevm_test.go +++ b/core/types/block.libevm_test.go @@ -104,11 +104,40 @@ func (bh *stubBlockHooks) DecodeRLP(b *Block, s *rlp.Stream) error { return bh.errDecode } +type stubBodyHooks struct { + encoding []byte + gotRawRLPToDecode []byte + setBodyToOnUnmarshalOrDecode Body + + errEncode, errDecode error +} + +func (bh *stubBodyHooks) EncodeRLP(b *Body, w io.Writer) error { + if _, err := w.Write(bh.encoding); err != nil { + return err + } + return bh.errEncode +} + +func (bh *stubBodyHooks) DecodeRLP(b *Body, s *rlp.Stream) error { + r, err := s.Raw() + if err != nil { + return err + } + bh.gotRawRLPToDecode = r + *b = bh.setBodyToOnUnmarshalOrDecode + return bh.errDecode +} + func TestHeaderHooks(t *testing.T) { TestOnlyClearRegisteredExtras() defer TestOnlyClearRegisteredExtras() - extras := RegisterExtras[stubHeaderHooks, *stubHeaderHooks, stubBlockHooks, *stubBlockHooks, struct{}]() + extras := RegisterExtras[ + stubHeaderHooks, *stubHeaderHooks, + stubBlockHooks, *stubBlockHooks, + stubBodyHooks, *stubBodyHooks, + struct{}]() rng := ethtest.NewPseudoRand(13579) suffix := rng.Bytes(8) diff --git a/core/types/rlp_backwards_compat.libevm_test.go b/core/types/rlp_backwards_compat.libevm_test.go index 1401ebc0400e..b44af96524e3 100644 --- a/core/types/rlp_backwards_compat.libevm_test.go +++ b/core/types/rlp_backwards_compat.libevm_test.go @@ -43,6 +43,7 @@ func TestHeaderRLPBackwardsCompatibility(t *testing.T) { RegisterExtras[ NOOPHeaderHooks, *NOOPHeaderHooks, NOOPBlockHooks, *NOOPBlockHooks, + NOOPBodyHooks, *NOOPBodyHooks, struct{}]() }, }, diff --git a/core/types/state_account.libevm_test.go b/core/types/state_account.libevm_test.go index 34614b282580..0d2d3b4dec9d 100644 --- a/core/types/state_account.libevm_test.go +++ b/core/types/state_account.libevm_test.go @@ -49,6 +49,7 @@ func TestStateAccountRLP(t *testing.T) { RegisterExtras[ NOOPHeaderHooks, *NOOPHeaderHooks, NOOPBlockHooks, *NOOPBlockHooks, + NOOPBodyHooks, *NOOPBodyHooks, bool]() }, acc: &StateAccount{ @@ -82,6 +83,7 @@ func TestStateAccountRLP(t *testing.T) { RegisterExtras[ NOOPHeaderHooks, *NOOPHeaderHooks, NOOPBlockHooks, *NOOPBlockHooks, + NOOPBodyHooks, *NOOPBodyHooks, bool]() }, acc: &StateAccount{ diff --git a/core/types/state_account_storage.libevm_test.go b/core/types/state_account_storage.libevm_test.go index f6197b86bb99..10cd530c32bd 100644 --- a/core/types/state_account_storage.libevm_test.go +++ b/core/types/state_account_storage.libevm_test.go @@ -76,6 +76,7 @@ func TestStateAccountExtraViaTrieStorage(t *testing.T) { e := types.RegisterExtras[ types.NOOPHeaderHooks, *types.NOOPHeaderHooks, types.NOOPBlockHooks, *types.NOOPBlockHooks, + types.NOOPBodyHooks, *types.NOOPBodyHooks, bool]() e.StateAccount.Set(a, true) return a, func(t *testing.T, got *types.StateAccount) { //nolint:thelper @@ -90,6 +91,7 @@ func TestStateAccountExtraViaTrieStorage(t *testing.T) { e := types.RegisterExtras[ types.NOOPHeaderHooks, *types.NOOPHeaderHooks, types.NOOPBlockHooks, *types.NOOPBlockHooks, + types.NOOPBodyHooks, *types.NOOPBodyHooks, bool]() e.StateAccount.Set(a, false) // the explicit part @@ -105,6 +107,7 @@ func TestStateAccountExtraViaTrieStorage(t *testing.T) { e := types.RegisterExtras[ types.NOOPHeaderHooks, *types.NOOPHeaderHooks, types.NOOPBlockHooks, *types.NOOPBlockHooks, + types.NOOPBodyHooks, *types.NOOPBodyHooks, bool]() // Note that `a` is reflected, unchanged (the implicit part). return a, func(t *testing.T, got *types.StateAccount) { //nolint:thelper @@ -119,6 +122,7 @@ func TestStateAccountExtraViaTrieStorage(t *testing.T) { e := types.RegisterExtras[ types.NOOPHeaderHooks, *types.NOOPHeaderHooks, types.NOOPBlockHooks, *types.NOOPBlockHooks, + types.NOOPBodyHooks, *types.NOOPBodyHooks, arbitraryPayload]() p := arbitraryPayload{arbitraryData} e.StateAccount.Set(a, p)