From 16a7358880b8363a8c2493997a103d393e49fa9c Mon Sep 17 00:00:00 2001 From: Quentin Mc Gaw Date: Tue, 21 Jan 2025 17:15:06 +0100 Subject: [PATCH 1/4] chore(core/types): header `PostCopy` hook --- core/types/block.go | 47 ---------------------------------------- core/types/header_ext.go | 16 +++++++++++++- core/types/imports.go | 1 + 3 files changed, 16 insertions(+), 48 deletions(-) diff --git a/core/types/block.go b/core/types/block.go index 404edca818..01c723c475 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -126,53 +126,6 @@ func NewBlock( return b } -// CopyHeader creates a deep copy of a block header. -func CopyHeader(h *Header) *Header { - cpy := *h - hExtra := GetHeaderExtra(h) - cpyExtra := &HeaderExtra{ - ExtDataHash: hExtra.ExtDataHash, - } - SetHeaderExtra(&cpy, cpyExtra) - - if cpy.Difficulty = new(big.Int); h.Difficulty != nil { - cpy.Difficulty.Set(h.Difficulty) - } - if cpy.Number = new(big.Int); h.Number != nil { - cpy.Number.Set(h.Number) - } - if h.BaseFee != nil { - cpy.BaseFee = new(big.Int).Set(h.BaseFee) - } - if hExtra.ExtDataGasUsed != nil { - cpyExtra.ExtDataGasUsed = new(big.Int).Set(hExtra.ExtDataGasUsed) - } - if hExtra.BlockGasCost != nil { - cpyExtra.BlockGasCost = new(big.Int).Set(hExtra.BlockGasCost) - } - if len(h.Extra) > 0 { - cpy.Extra = make([]byte, len(h.Extra)) - copy(cpy.Extra, h.Extra) - } - if h.WithdrawalsHash != nil { - cpy.WithdrawalsHash = new(common.Hash) - *cpy.WithdrawalsHash = *h.WithdrawalsHash - } - if h.ExcessBlobGas != nil { - cpy.ExcessBlobGas = new(uint64) - *cpy.ExcessBlobGas = *h.ExcessBlobGas - } - if h.BlobGasUsed != nil { - cpy.BlobGasUsed = new(uint64) - *cpy.BlobGasUsed = *h.BlobGasUsed - } - if h.ParentBeaconRoot != nil { - cpy.ParentBeaconRoot = new(common.Hash) - *cpy.ParentBeaconRoot = *h.ParentBeaconRoot - } - return &cpy -} - // DecodeRLP decodes a block from RLP. func (b *Block) DecodeRLP(s *rlp.Stream) error { var eb extblock diff --git a/core/types/header_ext.go b/core/types/header_ext.go index 1e6ebd5bf8..cab950586d 100644 --- a/core/types/header_ext.go +++ b/core/types/header_ext.go @@ -84,7 +84,21 @@ func (h *HeaderExtra) DecodeJSON(eth *ethtypes.Header, input []byte) error { } func (h *HeaderExtra) PostCopy(dst *ethtypes.Header) { - panic("not implemented") + extraCopy := &HeaderExtra{ + ExtDataHash: h.ExtDataHash, + } + + if h.BlockGasCost != nil { + extraCopy.BlockGasCost = big.NewInt(0) + extraCopy.BlockGasCost.SetBytes(h.BlockGasCost.Bytes()) + } + + if h.ExtDataGasUsed != nil { + extraCopy.ExtDataGasUsed = big.NewInt(0) + extraCopy.ExtDataGasUsed.SetBytes(h.ExtDataGasUsed.Bytes()) + } + + SetHeaderExtra(dst, extraCopy) } func (h *HeaderSerializable) updateFromEth(eth *ethtypes.Header) { diff --git a/core/types/imports.go b/core/types/imports.go index cc25fa9a8e..e630ea5366 100644 --- a/core/types/imports.go +++ b/core/types/imports.go @@ -51,6 +51,7 @@ const ( var ( BloomLookup = ethtypes.BloomLookup BytesToBloom = ethtypes.BytesToBloom + CopyHeader = ethtypes.CopyHeader CreateBloom = ethtypes.CreateBloom EncodeNonce = ethtypes.EncodeNonce FullAccount = ethtypes.FullAccount From 897cf7470a60cc54c8c425f166b8f8da620a26a9 Mon Sep 17 00:00:00 2001 From: Quentin Mc Gaw Date: Wed, 12 Feb 2025 16:10:55 +0100 Subject: [PATCH 2/4] `TestHeaderExtraPostCopy` --- core/types/header_ext_test.go | 52 +++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/core/types/header_ext_test.go b/core/types/header_ext_test.go index 97b22237b3..75517ad645 100644 --- a/core/types/header_ext_test.go +++ b/core/types/header_ext_test.go @@ -168,4 +168,56 @@ func assertNonZero[T interface { } } +func TestHeaderExtraPostCopy(t *testing.T) { + t.Parallel() + + t.Run("empty_extra", func(t *testing.T) { + t.Parallel() + + got := ðtypes.Header{ + ParentHash: common.Hash{1}, + } + extra := &HeaderExtra{} + + extra.PostCopy(got) + + want := ðtypes.Header{ + ParentHash: common.Hash{1}, + } + wantExtra := &HeaderExtra{} + SetHeaderExtra(want, wantExtra) + + assert.Equal(t, want, got) + gotExtra := GetHeaderExtra(got) + assert.Equal(t, wantExtra, gotExtra) + }) + + t.Run("filled_extra", func(t *testing.T) { + got := ðtypes.Header{ + ParentHash: common.Hash{1}, + } + extra := &HeaderExtra{ + ExtDataHash: common.Hash{2}, + ExtDataGasUsed: big.NewInt(3), + BlockGasCost: big.NewInt(4), + } + + extra.PostCopy(got) + + want := ðtypes.Header{ + ParentHash: common.Hash{1}, + } + wantExtra := &HeaderExtra{ + ExtDataHash: common.Hash{2}, + ExtDataGasUsed: big.NewInt(3), + BlockGasCost: big.NewInt(4), + } + _ = WithHeaderExtra(want, wantExtra) + + assert.Equal(t, want, got) + gotExtra := GetHeaderExtra(got) + assert.Equal(t, wantExtra, gotExtra) + }) +} + func ptrTo[T any](x T) *T { return &x } From dec6653224ab519a3c3864c8ce6ba9480a6c797f Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Fri, 21 Feb 2025 16:31:43 +0100 Subject: [PATCH 3/4] Simplify HeaderExtra PostCopy() body Co-authored-by: Arran Schlosberg <519948+ARR4N@users.noreply.github.com> Signed-off-by: Quentin McGaw --- core/types/header_ext.go | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/core/types/header_ext.go b/core/types/header_ext.go index cab950586d..ad83f8ec4b 100644 --- a/core/types/header_ext.go +++ b/core/types/header_ext.go @@ -84,21 +84,16 @@ func (h *HeaderExtra) DecodeJSON(eth *ethtypes.Header, input []byte) error { } func (h *HeaderExtra) PostCopy(dst *ethtypes.Header) { - extraCopy := &HeaderExtra{ + cp := &HeaderExtra{ ExtDataHash: h.ExtDataHash, } - if h.BlockGasCost != nil { - extraCopy.BlockGasCost = big.NewInt(0) - extraCopy.BlockGasCost.SetBytes(h.BlockGasCost.Bytes()) + cp.BlockGasCost = new(big.Int).Set(h.BlockGasCost) } - if h.ExtDataGasUsed != nil { - extraCopy.ExtDataGasUsed = big.NewInt(0) - extraCopy.ExtDataGasUsed.SetBytes(h.ExtDataGasUsed.Bytes()) + cp.ExtDataGasUsed = new(big.Int).Set(h.ExtDataGasUsed) } - - SetHeaderExtra(dst, extraCopy) + SetHeaderExtra(dst, cp) } func (h *HeaderSerializable) updateFromEth(eth *ethtypes.Header) { From 6b133adb072b42c92fb93c74f731e154a2ad890a Mon Sep 17 00:00:00 2001 From: Quentin Mc Gaw Date: Tue, 25 Feb 2025 16:38:34 +0100 Subject: [PATCH 4/4] remove unneeded tests --- core/types/header_ext_test.go | 52 +---------------------------------- 1 file changed, 1 insertion(+), 51 deletions(-) diff --git a/core/types/header_ext_test.go b/core/types/header_ext_test.go index 75517ad645..769052a552 100644 --- a/core/types/header_ext_test.go +++ b/core/types/header_ext_test.go @@ -168,56 +168,6 @@ func assertNonZero[T interface { } } -func TestHeaderExtraPostCopy(t *testing.T) { - t.Parallel() - - t.Run("empty_extra", func(t *testing.T) { - t.Parallel() - - got := ðtypes.Header{ - ParentHash: common.Hash{1}, - } - extra := &HeaderExtra{} - - extra.PostCopy(got) - - want := ðtypes.Header{ - ParentHash: common.Hash{1}, - } - wantExtra := &HeaderExtra{} - SetHeaderExtra(want, wantExtra) - - assert.Equal(t, want, got) - gotExtra := GetHeaderExtra(got) - assert.Equal(t, wantExtra, gotExtra) - }) - - t.Run("filled_extra", func(t *testing.T) { - got := ðtypes.Header{ - ParentHash: common.Hash{1}, - } - extra := &HeaderExtra{ - ExtDataHash: common.Hash{2}, - ExtDataGasUsed: big.NewInt(3), - BlockGasCost: big.NewInt(4), - } - - extra.PostCopy(got) - - want := ðtypes.Header{ - ParentHash: common.Hash{1}, - } - wantExtra := &HeaderExtra{ - ExtDataHash: common.Hash{2}, - ExtDataGasUsed: big.NewInt(3), - BlockGasCost: big.NewInt(4), - } - _ = WithHeaderExtra(want, wantExtra) - - assert.Equal(t, want, got) - gotExtra := GetHeaderExtra(got) - assert.Equal(t, wantExtra, gotExtra) - }) -} +// Note [TestCopyHeader] tests the [HeaderExtra.PostCopy] method. func ptrTo[T any](x T) *T { return &x }