Skip to content

Commit 67f879a

Browse files
authored
refactor!(core/types): Block method WithBody(Body) signature (#110)
Breaking change refactoring `core/types.Block.WithBody()` method from signature WithBody(transactions []*Transaction, uncles []*Header) *Block to signature WithBody(body Body) *Block such that block and body extras can be used within `WithBody`. Note `geth` made the same change in method signature so the deltas introduced in this PR will disappear once we sync.
1 parent 2580bd1 commit 67f879a

File tree

13 files changed

+47
-22
lines changed

13 files changed

+47
-22
lines changed

beacon/engine/types.go

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

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}).WithWithdrawals(i.Withdrawals)
164164
}
165165

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

core/rawdb/accessors_chain.go

Lines changed: 13 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).WithWithdrawals(body.Withdrawals)
757757
}
758758

759759
// WriteBlock serializes a block into the database, header and body separately.
@@ -843,7 +843,12 @@ 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+
block = block.WithWithdrawals(bad.Body.Withdrawals)
850+
}
851+
return block
847852
}
848853
}
849854
return nil
@@ -862,7 +867,12 @@ func ReadAllBadBlocks(db ethdb.Reader) []*types.Block {
862867
}
863868
var blocks []*types.Block
864869
for _, bad := range badBlocks {
865-
blocks = append(blocks, types.NewBlockWithHeader(bad.Header).WithBody(bad.Body.Transactions, bad.Body.Uncles).WithWithdrawals(bad.Body.Withdrawals))
870+
block := types.NewBlockWithHeader(bad.Header)
871+
if bad.Body != nil {
872+
block = block.WithBody(*bad.Body)
873+
block = block.WithWithdrawals(bad.Body.Withdrawals)
874+
}
875+
blocks = append(blocks, block)
866876
}
867877
return blocks
868878
}

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/types/block.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -460,16 +460,16 @@ func (b *Block) WithSeal(header *Header) *Block {
460460
}
461461

462462
// WithBody returns a copy of the block with the given transaction and uncle contents.
463-
func (b *Block) WithBody(transactions []*Transaction, uncles []*Header) *Block {
463+
func (b *Block) WithBody(body Body) *Block {
464464
block := &Block{
465465
header: b.header,
466-
transactions: make([]*Transaction, len(transactions)),
467-
uncles: make([]*Header, len(uncles)),
466+
transactions: make([]*Transaction, len(body.Transactions)),
467+
uncles: make([]*Header, len(body.Uncles)),
468468
withdrawals: b.withdrawals,
469469
}
470-
copy(block.transactions, transactions)
471-
for i := range uncles {
472-
block.uncles[i] = CopyHeader(uncles[i])
470+
copy(block.transactions, body.Transactions)
471+
for i := range body.Uncles {
472+
block.uncles[i] = CopyHeader(body.Uncles[i])
473473
}
474474
return block
475475
}

eth/catalyst/api_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ func setBlockhash(data *engine.ExecutableData) *engine.ExecutableData {
778778
Extra: data.ExtraData,
779779
MixDigest: data.Random,
780780
}
781-
block := types.NewBlockWithHeader(header).WithBody(txs, nil /* uncles */)
781+
block := types.NewBlockWithHeader(header).WithBody(types.Body{Transactions: txs})
782782
data.BlockHash = block.Hash()
783783
return data
784784
}
@@ -935,7 +935,7 @@ func TestNewPayloadOnInvalidTerminalBlock(t *testing.T) {
935935
Extra: data.ExtraData,
936936
MixDigest: data.Random,
937937
}
938-
block := types.NewBlockWithHeader(header).WithBody(txs, nil /* uncles */)
938+
block := types.NewBlockWithHeader(header).WithBody(types.Body{Transactions: txs})
939939
data.BlockHash = block.Hash()
940940
// Send the new payload
941941
resp2, err := api.NewPayloadV1(data)

eth/downloader/downloader.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,7 @@ func (d *Downloader) importBlockResults(results []*fetchResult) error {
15001500
)
15011501
blocks := make([]*types.Block, len(results))
15021502
for i, result := range results {
1503-
blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles).WithWithdrawals(result.Withdrawals)
1503+
blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.body()).WithWithdrawals(result.Withdrawals)
15041504
}
15051505
// Downloaded blocks are always regarded as trusted after the
15061506
// transition. Because the downloaded chain is guided by the
@@ -1718,7 +1718,7 @@ func (d *Downloader) commitSnapSyncData(results []*fetchResult, stateSync *state
17181718
blocks := make([]*types.Block, len(results))
17191719
receipts := make([]types.Receipts, len(results))
17201720
for i, result := range results {
1721-
blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles).WithWithdrawals(result.Withdrawals)
1721+
blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.body()).WithWithdrawals(result.Withdrawals)
17221722
receipts[i] = result.Receipts
17231723
}
17241724
if index, err := d.blockchain.InsertReceiptChain(blocks, receipts, d.ancientLimit); err != nil {
@@ -1729,7 +1729,7 @@ func (d *Downloader) commitSnapSyncData(results []*fetchResult, stateSync *state
17291729
}
17301730

17311731
func (d *Downloader) commitPivotBlock(result *fetchResult) error {
1732-
block := types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles).WithWithdrawals(result.Withdrawals)
1732+
block := types.NewBlockWithHeader(result.Header).WithBody(result.body()).WithWithdrawals(result.Withdrawals)
17331733
log.Debug("Committing snap sync pivot as new head", "number", block.Number(), "hash", block.Hash())
17341734

17351735
// Commit the pivot block as the new head, will require full sync from here on

eth/downloader/queue.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ func newFetchResult(header *types.Header, fastSync bool) *fetchResult {
8787
return item
8888
}
8989

90+
// body returns a representation of the fetch result as a types.Body object.
91+
func (f *fetchResult) body() types.Body {
92+
return types.Body{
93+
Transactions: f.Transactions,
94+
Uncles: f.Uncles,
95+
Withdrawals: f.Withdrawals,
96+
}
97+
}
98+
9099
// SetBodyDone flags the body as finished.
91100
func (f *fetchResult) SetBodyDone() {
92101
if v := f.pending.Load(); (v & (1 << bodyType)) != 0 {

eth/fetcher/block_fetcher.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,8 @@ func (f *BlockFetcher) loop() {
686686
// Mark the body matched, reassemble if still unknown
687687
matched = true
688688
if f.getBlock(hash) == nil {
689-
block := types.NewBlockWithHeader(announce.header).WithBody(task.transactions[i], task.uncles[i])
689+
body := types.Body{Transactions: task.transactions[i], Uncles: task.uncles[i]}
690+
block := types.NewBlockWithHeader(announce.header).WithBody(body)
690691
block.ReceivedAt = task.time
691692
blocks = append(blocks, block)
692693
} else {

eth/handler_eth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ func testBroadcastMalformedBlock(t *testing.T, protocol uint) {
586586

587587
// Try to broadcast all malformations and ensure they all get discarded
588588
for _, header := range []*types.Header{malformedUncles, malformedTransactions, malformedEverything} {
589-
block := types.NewBlockWithHeader(header).WithBody(block.Transactions(), block.Uncles())
589+
block := types.NewBlockWithHeader(header).WithBody(*block.Body())
590590
if err := src.SendNewBlock(block, big.NewInt(131136)); err != nil {
591591
t.Fatalf("failed to broadcast block: %v", err)
592592
}

0 commit comments

Comments
 (0)