Skip to content

Commit 0573adb

Browse files
authored
Merge pull request #2019 from CortexFoundation/dev
refactor so NewBlock, WithBody take types.Body
2 parents 1a74865 + 8bd9af3 commit 0573adb

File tree

17 files changed

+70
-43
lines changed

17 files changed

+70
-43
lines changed

client/ctxc_client.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,11 @@ func (ec *Client) getBlock(ctx context.Context, method string, args ...any) (*ty
181181
}
182182
txs[i] = tx.tx
183183
}
184-
return types.NewBlockWithHeader(head).WithBody(txs, uncles), nil
184+
return types.NewBlockWithHeader(head).WithBody(
185+
types.Body{
186+
Transactions: txs,
187+
Uncles: uncles,
188+
}), nil
185189
}
186190

187191
// HeaderByHash returns the block header with the given hash.

consensus/clique/clique.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ func (c *Clique) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *
611611
c.Finalize(chain, header, state, txs, uncles)
612612

613613
// Assemble and return the final block for sealing
614-
return types.NewBlock(header, txs, nil, receipts, trie.NewStackTrie(nil)), nil
614+
return types.NewBlock(header, &types.Body{Transactions: txs}, receipts, trie.NewStackTrie(nil)), nil
615615
}
616616

617617
// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
@@ -621,8 +621,8 @@ func (c *Clique) FinalizeWithoutParent(chain consensus.ChainHeaderReader, header
621621
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
622622
header.UncleHash = types.CalcUncleHash(nil)
623623

624-
// Assemble and return the final block for sealing
625-
return types.NewBlock(header, txs, nil, receipts, trie.NewStackTrie(nil)), nil
624+
// Assemble and return the final block for sealing.
625+
return types.NewBlock(header, &types.Body{Transactions: txs}, receipts, trie.NewStackTrie(nil)), nil
626626
}
627627

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

consensus/cuckoo/consensus.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ func (cuckoo *Cuckoo) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
746746
}
747747

748748
// Header seems complete, assemble into a block and return
749-
return types.NewBlock(header, txs, uncles, receipts, trie.NewStackTrie(nil)), nil
749+
return types.NewBlock(header, &types.Body{Transactions: txs, Uncles: uncles}, receipts, trie.NewStackTrie(nil)), nil
750750
}
751751

752752
// FinalizeAndAssemble implements consensus.Engine, accumulating the block and
@@ -757,7 +757,7 @@ func (cuckoo *Cuckoo) FinalizeWithoutParent(chain consensus.ChainHeaderReader, h
757757
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
758758

759759
// Header seems complete, assemble into a block and return
760-
return types.NewBlock(header, txs, uncles, receipts, trie.NewStackTrie(nil)), nil
760+
return types.NewBlock(header, &types.Body{Transactions: txs, Uncles: uncles}, receipts, trie.NewStackTrie(nil)), nil
761761
}
762762

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

consensus/cuckoo/cuckoo_faker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ func (cuckoo *CuckooFake) Close() error {
6565
}
6666

6767
func (cuckoo *CuckooFake) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
68-
return types.NewBlock(header, txs, uncles, receipts, trie.NewStackTrie(nil)), nil
68+
return types.NewBlock(header, &types.Body{Transactions: txs, Uncles: uncles}, receipts, trie.NewStackTrie(nil)), nil
6969
}
7070

7171
func (cuckoo *CuckooFake) FinalizeWithoutParent(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
72-
return types.NewBlock(header, txs, uncles, receipts, trie.NewStackTrie(nil)), nil
72+
return types.NewBlock(header, &types.Body{Transactions: txs, Uncles: uncles}, receipts, trie.NewStackTrie(nil)), nil
7373
}
7474

7575
func (cuckoo *CuckooFake) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error {

core/chain_makers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ func (cuckoo *CuckooFakeForTest) FinalizeAndAssemble(chain consensus.ChainHeader
6565
// No block rewards in PoA, so the state remains as is and uncles are dropped
6666
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
6767
header.UncleHash = types.CalcUncleHash(nil)
68-
return types.NewBlock(header, txs, uncles, receipts, trie.NewStackTrie(nil)), nil
68+
return types.NewBlock(header, &types.Body{Transactions: txs, Uncles: uncles}, receipts, trie.NewStackTrie(nil)), nil
6969
}
7070

7171
func (cuckoo *CuckooFakeForTest) FinalizeWithoutParent(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
7272
// No block rewards in PoA, so the state remains as is and uncles are dropped
7373
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
7474
header.UncleHash = types.CalcUncleHash(nil)
75-
return types.NewBlock(header, txs, uncles, receipts, trie.NewStackTrie(nil)), nil
75+
return types.NewBlock(header, &types.Body{Transactions: txs, Uncles: uncles}, receipts, trie.NewStackTrie(nil)), nil
7676
}
7777

7878
func (cuckoo *CuckooFakeForTest) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error {

core/genesis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func (g *Genesis) ToBlock(db ctxcdb.Database) *types.Block {
292292

293293
statedb.Database().TrieDB().Commit(root, true)
294294

295-
return types.NewBlock(head, nil, nil, nil, trie.NewStackTrie(nil))
295+
return types.NewBlock(head, &types.Body{}, nil, trie.NewStackTrie(nil))
296296
}
297297

298298
// 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
@@ -775,7 +775,7 @@ func ReadBlock(db ctxcdb.Reader, hash common.Hash, number uint64) *types.Block {
775775
if body == nil {
776776
return nil
777777
}
778-
return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles)
778+
return types.NewBlockWithHeader(header).WithBody(*body)
779779
}
780780

781781
// WriteBlock serializes a block into the database, header and body separately.
@@ -865,7 +865,11 @@ func ReadBadBlock(db ctxcdb.Reader, hash common.Hash) *types.Block {
865865
}
866866
for _, bad := range badBlocks {
867867
if bad.Header.Hash() == hash {
868-
return types.NewBlockWithHeader(bad.Header).WithBody(bad.Body.Transactions, bad.Body.Uncles)
868+
block := types.NewBlockWithHeader(bad.Header)
869+
if bad.Body != nil {
870+
block = block.WithBody(*bad.Body)
871+
}
872+
return block
869873
}
870874
}
871875
return nil
@@ -884,7 +888,11 @@ func ReadAllBadBlocks(db ctxcdb.Reader) []*types.Block {
884888
}
885889
var blocks []*types.Block
886890
for _, bad := range badBlocks {
887-
blocks = append(blocks, types.NewBlockWithHeader(bad.Header).WithBody(bad.Body.Transactions, bad.Body.Uncles))
891+
block := types.NewBlockWithHeader(bad.Header)
892+
if bad.Body != nil {
893+
block = block.WithBody(*bad.Body)
894+
}
895+
blocks = append(blocks, block)
888896
}
889897
return blocks
890898
}

core/rawdb/accessors_indexes_test.go

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

101-
block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, nil, newHasher())
101+
block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, &types.Body{Transactions: txs}, nil, newHasher())
102102

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

core/rawdb/chain_iterator_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ func TestChainIterator(t *testing.T) {
3535
var txs []*types.Transaction
3636
for i := uint64(0); i <= 10; i++ {
3737
if i == 0 {
38-
block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, nil, nil, nil, newHasher()) // Empty genesis block
38+
block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, &types.Body{}, nil, newHasher()) // Empty genesis block
3939
} else {
4040
tx := types.NewTransaction(i, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
4141
txs = append(txs, tx)
42-
block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, []*types.Transaction{tx}, nil, nil, newHasher())
42+
block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, &types.Body{Transactions: []*types.Transaction{tx}}, nil, newHasher())
4343
}
4444
WriteBlock(chainDb, block)
4545
WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64())
@@ -90,11 +90,11 @@ func TestIndexTransactions(t *testing.T) {
9090
var txs []*types.Transaction
9191
for i := uint64(0); i <= 10; i++ {
9292
if i == 0 {
93-
block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, nil, nil, nil, newHasher()) // Empty genesis block
93+
block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, &types.Body{}, nil, newHasher()) // Empty genesis block
9494
} else {
9595
tx := types.NewTransaction(i, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
9696
txs = append(txs, tx)
97-
block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, []*types.Transaction{tx}, nil, nil, newHasher())
97+
block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, &types.Body{Transactions: []*types.Transaction{tx}}, nil, newHasher())
9898
}
9999
WriteBlock(chainDb, block)
100100
WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64())

core/txpool/txpool_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type testBlockChain struct {
6363
func (bc *testBlockChain) CurrentBlock() *types.Block {
6464
return types.NewBlock(&types.Header{
6565
GasLimit: bc.gasLimit.Load(),
66-
}, nil, nil, nil, trie.NewStackTrie(nil))
66+
}, &types.Body{}, nil, trie.NewStackTrie(nil))
6767
}
6868

6969
func newTestBlockChain(gasLimit uint64, statedb *state.StateDB, chainHeadFeed *event.Feed) *testBlockChain {

core/types/block.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io"
2424
"math/big"
2525
"reflect"
26+
"slices"
2627
"sync"
2728
"sync/atomic"
2829
"time"
@@ -256,13 +257,17 @@ type storageblock struct {
256257
// changes to header and to the field values will not affect the
257258
// block.
258259
//
259-
// The values of TxHash, UncleHash, ReceiptHash and Bloom in header
260-
// are ignored and set to values derived from the given txs, uncles
261-
// and receipts.
262-
func NewBlock(header *Header, txs []*Transaction, uncles []*Header, receipts []*Receipt, hasher Hasher) *Block {
263-
b := &Block{header: CopyHeader(header), td: new(big.Int)}
260+
// relevant portions of the header.
261+
func NewBlock(header *Header, body *Body, receipts []*Receipt, hasher Hasher) *Block {
262+
if body == nil {
263+
body = &Body{}
264+
}
265+
var (
266+
b = NewBlockWithHeader(header)
267+
txs = body.Transactions
268+
uncles = body.Uncles
269+
)
264270

265-
// TODO: panic if len(txs) != len(receipts)
266271
if len(txs) == 0 {
267272
b.header.TxHash = EmptyRootHash
268273
} else {
@@ -431,16 +436,16 @@ func (b *Block) WithSeal(header *Header) *Block {
431436
}
432437
}
433438

434-
// WithBody returns a new block with the given transaction and uncle contents.
435-
func (b *Block) WithBody(transactions []*Transaction, uncles []*Header) *Block {
439+
// WithBody returns a new block with the original header and a deep copy of the
440+
// provided body.
441+
func (b *Block) WithBody(body Body) *Block {
436442
block := &Block{
437-
header: CopyHeader(b.header),
438-
transactions: make([]*Transaction, len(transactions)),
439-
uncles: make([]*Header, len(uncles)),
443+
header: b.header,
444+
transactions: slices.Clone(body.Transactions),
445+
uncles: make([]*Header, len(body.Uncles)),
440446
}
441-
copy(block.transactions, transactions)
442-
for i := range uncles {
443-
block.uncles[i] = CopyHeader(uncles[i])
447+
for i := range body.Uncles {
448+
block.uncles[i] = CopyHeader(body.Uncles[i])
444449
}
445450
return block
446451
}

core/types/block_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,5 +206,5 @@ func makeBenchBlock() *Block {
206206
Extra: []byte("benchmark uncle"),
207207
}
208208
}
209-
return NewBlock(header, txs, uncles, receipts, newHasher())
209+
return NewBlock(header, &Body{Transactions: txs, Uncles: uncles}, receipts, newHasher())
210210
}

ctxc/downloader/downloader.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,7 +1639,7 @@ func (d *Downloader) importBlockResults(results []*fetchResult) error {
16391639
)
16401640
blocks := make([]*types.Block, len(results))
16411641
for i, result := range results {
1642-
blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles)
1642+
blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.body())
16431643
}
16441644
if index, err := d.blockchain.InsertChain(blocks); err != nil {
16451645
if index < len(results) {
@@ -1831,7 +1831,7 @@ func (d *Downloader) commitFastSyncData(results []*fetchResult, stateSync *state
18311831
blocks := make([]*types.Block, len(results))
18321832
receipts := make([]types.Receipts, len(results))
18331833
for i, result := range results {
1834-
blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles)
1834+
blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.body())
18351835
receipts[i] = result.Receipts
18361836
}
18371837
if index, err := d.blockchain.InsertReceiptChain(blocks, receipts, d.ancientLimit); err != nil {
@@ -1842,8 +1842,10 @@ func (d *Downloader) commitFastSyncData(results []*fetchResult, stateSync *state
18421842
}
18431843

18441844
func (d *Downloader) commitPivotBlock(result *fetchResult) error {
1845-
block := types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles)
1846-
log.Debug("Committing fast sync pivot as new head", "number", block.Number(), "hash", block.Hash())
1845+
block := types.NewBlockWithHeader(result.Header).WithBody(result.body())
1846+
log.Debug("Committing snap sync pivot as new head", "number", block.Number(), "hash", block.Hash())
1847+
1848+
// Commit the pivot block as the new head, will require full sync from here on
18471849
if _, err := d.blockchain.InsertReceiptChain([]*types.Block{block}, []types.Receipts{result.Receipts}, d.ancientLimit); err != nil {
18481850
return err
18491851
}

ctxc/downloader/queue.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ func newFetchResult(header *types.Header, fastSync bool) *fetchResult {
8383
return item
8484
}
8585

86+
// body returns a representation of the fetch result as a types.Body object.
87+
func (f *fetchResult) body() types.Body {
88+
return types.Body{
89+
Transactions: f.Transactions,
90+
Uncles: f.Uncles,
91+
}
92+
}
93+
8694
// SetBodyDone flags the body as finished.
8795
func (f *fetchResult) SetBodyDone() {
8896
if v := f.pending.Load(); (v & (1 << bodyType)) != 0 {

ctxc/fetcher/block_fetcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ func (f *Fetcher) loop() {
588588
// Mark the body matched, reassemble if still unknown
589589
matched = true
590590
if f.getBlock(hash) == nil {
591-
block := types.NewBlockWithHeader(announce.header).WithBody(task.transactions[i], task.uncles[i])
591+
block := types.NewBlockWithHeader(announce.header).WithBody(types.Body{Transactions: task.transactions[i], Uncles: task.uncles[i]})
592592
block.ReceivedAt = task.time
593593
blocks = append(blocks, block)
594594
} else {

ctxc/fetcher/fetcher_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var (
3939
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
4040
testAddress = crypto.PubkeyToAddress(testKey.PublicKey)
4141
genesis = core.GenesisBlockForTesting(testdb, testAddress, big.NewInt(1000000000))
42-
unknownBlock = types.NewBlock(&types.Header{GasLimit: params.GenesisGasLimit}, nil, nil, nil, trie.NewStackTrie(nil))
42+
unknownBlock = types.NewBlock(&types.Header{GasLimit: params.GenesisGasLimit}, &types.Body{}, nil, trie.NewStackTrie(nil))
4343
)
4444

4545
// makeChain creates a chain of n blocks starting at and including parent.

miner/worker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,8 +770,8 @@ func (w *worker) updateSnapshot() {
770770

771771
w.snapshotBlock = types.NewBlock(
772772
w.current.header,
773-
w.current.txs,
774-
uncles,
773+
&types.Body{Transactions: w.current.txs,
774+
Uncles: uncles},
775775
w.current.receipts,
776776
trie.NewStackTrie(nil),
777777
)

0 commit comments

Comments
 (0)