Skip to content

Commit c7b3922

Browse files
core: also reduce memory of newChain
1 parent 99c0c18 commit c7b3922

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

core/blockchain.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,7 +2185,7 @@ func (bc *BlockChain) collectLogs(b *types.Block, removed bool) []*types.Log {
21852185
// externally.
21862186
func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
21872187
var (
2188-
newChain types.Blocks
2188+
newChain []*types.Header
21892189
oldChain []*types.Header
21902190
commonBlock *types.Block
21912191

@@ -2210,7 +2210,7 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
22102210
} else {
22112211
// New chain is longer, stash all blocks away for subsequent insertion
22122212
for ; newBlock != nil && newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1) {
2213-
newChain = append(newChain, newBlock)
2213+
newChain = append(newChain, newBlock.Header())
22142214
}
22152215
}
22162216
if oldBlock == nil {
@@ -2232,7 +2232,7 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
22322232
for _, tx := range oldBlock.Transactions() {
22332233
deletedTxs = append(deletedTxs, tx.Hash())
22342234
}
2235-
newChain = append(newChain, newBlock)
2235+
newChain = append(newChain, newBlock.Header())
22362236

22372237
// Step back with both chains
22382238
oldBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1)
@@ -2282,10 +2282,11 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
22822282
// as it will be handled separately outside of this function
22832283
for i := len(newChain) - 1; i >= 1; i-- {
22842284
// Insert the block in the canonical way, re-writing history
2285-
bc.writeHeadBlock(newChain[i])
2285+
newBlock = bc.GetBlock(newChain[i].Hash(), newChain[i].Number.Uint64())
2286+
bc.writeHeadBlock(newBlock)
22862287

22872288
// Collect the new added transactions.
2288-
for _, tx := range newChain[i].Transactions() {
2289+
for _, tx := range newBlock.Transactions() {
22892290
addedTxs = append(addedTxs, tx.Hash())
22902291
}
22912292
}
@@ -2304,7 +2305,7 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
23042305
// markers greater than or equal to new chain head should be deleted.
23052306
number := commonBlock.NumberU64()
23062307
if len(newChain) > 1 {
2307-
number = newChain[1].NumberU64()
2308+
number = newChain[1].Number.Uint64()
23082309
}
23092310
for i := number + 1; ; i++ {
23102311
hash := rawdb.ReadCanonicalHash(bc.db, i)
@@ -2346,7 +2347,8 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
23462347
// New logs:
23472348
var rebirthLogs []*types.Log
23482349
for i := len(newChain) - 1; i >= 1; i-- {
2349-
if logs := bc.collectLogs(newChain[i], false); len(logs) > 0 {
2350+
newBlock = bc.GetBlock(newChain[i].Hash(), newChain[i].Number.Uint64())
2351+
if logs := bc.collectLogs(newBlock, false); len(logs) > 0 {
23502352
rebirthLogs = append(rebirthLogs, logs...)
23512353
}
23522354
if len(rebirthLogs) > 512 {

core/blockchain_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4263,3 +4263,4 @@ func BenchmarkReorg(b *testing.B) {
42634263

42644264
// Master: BenchmarkReorg-8 10000 899591 ns/op 820154 B/op 1440 allocs/op 1549443072 bytes of heap used
42654265
// WithoutOldChain: BenchmarkReorg-8 10000 1147281 ns/op 943163 B/op 1564 allocs/op 1163870208 bytes of heap used
4266+
// WithoutNewChain: BenchmarkReorg-8 10000 1018922 ns/op 943580 B/op 1564 allocs/op 1171890176 bytes of heap used

0 commit comments

Comments
 (0)