From e377cea51dfe1416d9365dbe19e21d03bd09b6f0 Mon Sep 17 00:00:00 2001 From: Andrew Ashikhmin <34320705+yperbasis@users.noreply.github.com> Date: Fri, 11 Aug 2023 20:30:02 +0200 Subject: [PATCH] Fix CopyTxs for BlobTxWrapper (EIP-4844) (#8002) --- core/types/block.go | 15 ++++++++++----- core/types/block_test.go | 26 ++++++++++++++++++++++++++ turbo/builder/latest_block_built.go | 4 ++-- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/core/types/block.go b/core/types/block.go index 748cbcb7a..e1824595f 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -1594,11 +1594,16 @@ func CopyTxs(in Transactions) Transactions { if err != nil { panic(fmt.Errorf("MarshalTransactionsBinary failed: %w", err)) } - out, err := DecodeTransactions(transactionsData) - if err != nil { - panic(fmt.Errorf("DecodeTransactions failed: %w", err)) - } - for i := 0; i < len(in); i++ { + out := make([]Transaction, len(in)) + for i, tx := range in { + if _, ok := tx.(*BlobTxWrapper); ok { + out[i], err = UnmarshalWrappedTransactionFromBinary(transactionsData[i]) + } else { + out[i], err = UnmarshalTransactionFromBinary(transactionsData[i]) + } + if err != nil { + panic(fmt.Errorf("DecodeTransactions failed: %w", err)) + } if s, ok := in[i].GetSender(); ok { out[i].SetSender(s) } diff --git a/core/types/block_test.go b/core/types/block_test.go index d42fe6b08..be05c716b 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -486,3 +486,29 @@ func TestBlockRawBodyPostShanghaiWithdrawals(t *testing.T) { require.Equal(0, len(body.Transactions)) require.Equal(2, len(body.Withdrawals)) } + +func TestCopyTxs(t *testing.T) { + var txs Transactions + txs = append(txs, &LegacyTx{ + CommonTx: CommonTx{ + Nonce: 0, + Value: new(uint256.Int).SetUint64(10000), + Gas: 50000, + Data: []byte("Sparta"), + }, + GasPrice: new(uint256.Int).SetUint64(10), + }) + + populateBlobTxs() + for _, tx := range dummyBlobTxs { + txs = append(txs, tx) + } + + populateBlobWrapperTxs() + for _, tx := range dummyBlobWrapperTxs { + txs = append(txs, tx) + } + + copies := CopyTxs(txs) + assert.Equal(t, txs, copies) +} diff --git a/turbo/builder/latest_block_built.go b/turbo/builder/latest_block_built.go index 950784349..0e9e06288 100644 --- a/turbo/builder/latest_block_built.go +++ b/turbo/builder/latest_block_built.go @@ -19,11 +19,11 @@ func NewLatestBlockBuiltStore() *LatestBlockBuiltStore { func (s *LatestBlockBuiltStore) AddBlockBuilt(block *types.Block) { s.lock.Lock() defer s.lock.Unlock() - s.block = block.Copy() + s.block = block } func (s *LatestBlockBuiltStore) BlockBuilt() *types.Block { s.lock.Lock() defer s.lock.Unlock() - return s.block.Copy() + return s.block }