Skip to content

Commit

Permalink
feat(core/types): BodyHooks for RLP overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jan 17, 2025
1 parent 1541e34 commit dc9e4f8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions core/state/state.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions core/state/state_object.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
31 changes: 30 additions & 1 deletion core/types/block.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions core/types/rlp_backwards_compat.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestHeaderRLPBackwardsCompatibility(t *testing.T) {
RegisterExtras[
NOOPHeaderHooks, *NOOPHeaderHooks,
NOOPBlockHooks, *NOOPBlockHooks,
NOOPBodyHooks, *NOOPBodyHooks,
struct{}]()
},
},
Expand Down
2 changes: 2 additions & 0 deletions core/types/state_account.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestStateAccountRLP(t *testing.T) {
RegisterExtras[
NOOPHeaderHooks, *NOOPHeaderHooks,
NOOPBlockHooks, *NOOPBlockHooks,
NOOPBodyHooks, *NOOPBodyHooks,
bool]()
},
acc: &StateAccount{
Expand Down Expand Up @@ -82,6 +83,7 @@ func TestStateAccountRLP(t *testing.T) {
RegisterExtras[
NOOPHeaderHooks, *NOOPHeaderHooks,
NOOPBlockHooks, *NOOPBlockHooks,
NOOPBodyHooks, *NOOPBodyHooks,
bool]()
},
acc: &StateAccount{
Expand Down
4 changes: 4 additions & 0 deletions core/types/state_account_storage.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit dc9e4f8

Please sign in to comment.