Skip to content

Commit 98c935d

Browse files
lightclientjorgemmsilva
authored andcommitted
all: refactor so NewBlock, WithBody take types.Body (ethereum#29482)
* all: refactor so NewBlock(..) and WithBody(..) take a types.Body * core: fixup comments, remove txs != receipts panic * core/types: add empty withdrawls to body if len == 0
1 parent 996d297 commit 98c935d

File tree

23 files changed

+81
-76
lines changed

23 files changed

+81
-76
lines changed

beacon/engine/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func ExecutableDataToBlock(params ExecutableData, versionedHashes []common.Hash,
250250
BlobGasUsed: params.BlobGasUsed,
251251
ParentBeaconRoot: beaconRoot,
252252
}
253-
block := types.NewBlockWithHeader(header).WithBody(txs, nil /* uncles */).WithWithdrawals(params.Withdrawals)
253+
block := types.NewBlockWithHeader(header).WithBody(types.Body{Transactions: txs, Uncles: nil, Withdrawals: params.Withdrawals})
254254
if block.Hash() != params.BlockHash {
255255
return nil, fmt.Errorf("blockhash mismatch, want %x, got %x", params.BlockHash, block.Hash())
256256
}

beacon/types/exec_payload.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ func convertPayload[T payloadType](payload T, parentRoot *zrntcommon.Root) (*typ
6363
panic("unsupported block type")
6464
}
6565

66-
block := types.NewBlockWithHeader(&header)
67-
block = block.WithBody(transactions, nil)
68-
block = block.WithWithdrawals(withdrawals)
66+
block := types.NewBlockWithHeader(&header).WithBody(types.Body{Transactions: transactions, Withdrawals: withdrawals})
6967
if hash := block.Hash(); hash != expectedHash {
7068
return nil, fmt.Errorf("Sanity check failed, payload hash does not match (expected %x, got %x)", expectedHash, hash)
7169
}

cmd/evm/internal/t8ntool/block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func (i *bbInput) ToBlock() *types.Block {
160160
if i.Header.Difficulty != nil {
161161
header.Difficulty = i.Header.Difficulty
162162
}
163-
return types.NewBlockWithHeader(header).WithBody(i.Txs, i.Ommers).WithWithdrawals(i.Withdrawals)
163+
return types.NewBlockWithHeader(header).WithBody(types.Body{Transactions: i.Txs, Uncles: i.Ommers, Withdrawals: i.Withdrawals})
164164
}
165165

166166
// SealBlock seals the given block using the configured engine.

consensus/beacon/consensus.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
388388
header.Root = state.IntermediateRoot(true)
389389

390390
// Assemble and return the final block.
391-
return types.NewBlockWithWithdrawals(header, body.Transactions, body.Uncles, receipts, body.Withdrawals, trie.NewStackTrie(nil)), nil
391+
return types.NewBlock(header, body, receipts, trie.NewStackTrie(nil)), nil
392392
}
393393

394394
// Seal generates a new sealing request for the given input block and pushes

consensus/clique/clique.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ func (c *Clique) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *
597597
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
598598

599599
// Assemble and return the final block for sealing.
600-
return types.NewBlock(header, body.Transactions, nil, receipts, trie.NewStackTrie(nil)), nil
600+
return types.NewBlock(header, &types.Body{Transactions: body.Transactions}, receipts, trie.NewStackTrie(nil)), nil
601601
}
602602

603603
// Authorize injects a private key into the consensus engine to mint new blocks

consensus/ethash/consensus.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ func (ethash *Ethash) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
520520
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
521521

522522
// Header seems complete, assemble into a block and return
523-
return types.NewBlock(header, body.Transactions, body.Uncles, receipts, trie.NewStackTrie(nil)), nil
523+
return types.NewBlock(header, &types.Body{Transactions: body.Transactions, Uncles: body.Uncles}, receipts, trie.NewStackTrie(nil)), nil
524524
}
525525

526526
// SealHash returns the hash of a block prior to it being sealed.

core/genesis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ func (g *Genesis) ToBlock() *types.Block {
476476
}
477477
}
478478
}
479-
return types.NewBlock(head, nil, nil, nil, trie.NewStackTrie(nil)).WithWithdrawals(withdrawals)
479+
return types.NewBlock(head, &types.Body{Withdrawals: withdrawals}, nil, trie.NewStackTrie(nil))
480480
}
481481

482482
// Commit writes the block and state of a genesis specification to the database.

core/rawdb/accessors_chain.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ func ReadBlock(db ethdb.Reader, hash common.Hash, number uint64) *types.Block {
753753
if body == nil {
754754
return nil
755755
}
756-
return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles).WithWithdrawals(body.Withdrawals)
756+
return types.NewBlockWithHeader(header).WithBody(*body)
757757
}
758758

759759
// WriteBlock serializes a block into the database, header and body separately.
@@ -843,7 +843,11 @@ func ReadBadBlock(db ethdb.Reader, hash common.Hash) *types.Block {
843843
}
844844
for _, bad := range badBlocks {
845845
if bad.Header.Hash() == hash {
846-
return types.NewBlockWithHeader(bad.Header).WithBody(bad.Body.Transactions, bad.Body.Uncles).WithWithdrawals(bad.Body.Withdrawals)
846+
block := types.NewBlockWithHeader(bad.Header)
847+
if bad.Body != nil {
848+
block = block.WithBody(*bad.Body)
849+
}
850+
return block
847851
}
848852
}
849853
return nil
@@ -862,7 +866,11 @@ func ReadAllBadBlocks(db ethdb.Reader) []*types.Block {
862866
}
863867
var blocks []*types.Block
864868
for _, bad := range badBlocks {
865-
blocks = append(blocks, types.NewBlockWithHeader(bad.Header).WithBody(bad.Body.Transactions, bad.Body.Uncles).WithWithdrawals(bad.Body.Withdrawals))
869+
block := types.NewBlockWithHeader(bad.Header)
870+
if bad.Body != nil {
871+
block = block.WithBody(*bad.Body)
872+
}
873+
blocks = append(blocks, block)
866874
}
867875
return blocks
868876
}

core/rawdb/accessors_chain_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ func makeTestBlocks(nblock int, txsPerBlock int) []*types.Block {
640640
Number: big.NewInt(int64(i)),
641641
Extra: []byte("test block"),
642642
}
643-
blocks[i] = types.NewBlockWithHeader(header).WithBody(txs, nil)
643+
blocks[i] = types.NewBlockWithHeader(header).WithBody(types.Body{Transactions: txs})
644644
blocks[i].Hash() // pre-cache the block hash
645645
}
646646
return blocks

core/rawdb/accessors_indexes_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func TestLookupStorage(t *testing.T) {
7676
tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33})
7777
txs := []*types.Transaction{tx1, tx2, tx3}
7878

79-
block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, nil, newTestHasher())
79+
block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, &types.Body{Transactions: txs}, nil, newTestHasher())
8080

8181
// Check that no transactions entries are in a pristine database
8282
for i, tx := range txs {

0 commit comments

Comments
 (0)