core: also reduce memory of newChain

This commit is contained in:
Marius van der Wijden 2024-10-15 11:12:00 +02:00
parent 99c0c1818a
commit c7b3922ed6
2 changed files with 10 additions and 7 deletions

View file

@ -2185,7 +2185,7 @@ func (bc *BlockChain) collectLogs(b *types.Block, removed bool) []*types.Log {
// externally. // externally.
func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error { func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
var ( var (
newChain types.Blocks newChain []*types.Header
oldChain []*types.Header oldChain []*types.Header
commonBlock *types.Block commonBlock *types.Block
@ -2210,7 +2210,7 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
} else { } else {
// New chain is longer, stash all blocks away for subsequent insertion // New chain is longer, stash all blocks away for subsequent insertion
for ; newBlock != nil && newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1) { for ; newBlock != nil && newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1) {
newChain = append(newChain, newBlock) newChain = append(newChain, newBlock.Header())
} }
} }
if oldBlock == nil { if oldBlock == nil {
@ -2232,7 +2232,7 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
for _, tx := range oldBlock.Transactions() { for _, tx := range oldBlock.Transactions() {
deletedTxs = append(deletedTxs, tx.Hash()) deletedTxs = append(deletedTxs, tx.Hash())
} }
newChain = append(newChain, newBlock) newChain = append(newChain, newBlock.Header())
// Step back with both chains // Step back with both chains
oldBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1) oldBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1)
@ -2282,10 +2282,11 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
// as it will be handled separately outside of this function // as it will be handled separately outside of this function
for i := len(newChain) - 1; i >= 1; i-- { for i := len(newChain) - 1; i >= 1; i-- {
// Insert the block in the canonical way, re-writing history // Insert the block in the canonical way, re-writing history
bc.writeHeadBlock(newChain[i]) newBlock = bc.GetBlock(newChain[i].Hash(), newChain[i].Number.Uint64())
bc.writeHeadBlock(newBlock)
// Collect the new added transactions. // Collect the new added transactions.
for _, tx := range newChain[i].Transactions() { for _, tx := range newBlock.Transactions() {
addedTxs = append(addedTxs, tx.Hash()) addedTxs = append(addedTxs, tx.Hash())
} }
} }
@ -2304,7 +2305,7 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
// markers greater than or equal to new chain head should be deleted. // markers greater than or equal to new chain head should be deleted.
number := commonBlock.NumberU64() number := commonBlock.NumberU64()
if len(newChain) > 1 { if len(newChain) > 1 {
number = newChain[1].NumberU64() number = newChain[1].Number.Uint64()
} }
for i := number + 1; ; i++ { for i := number + 1; ; i++ {
hash := rawdb.ReadCanonicalHash(bc.db, i) hash := rawdb.ReadCanonicalHash(bc.db, i)
@ -2346,7 +2347,8 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
// New logs: // New logs:
var rebirthLogs []*types.Log var rebirthLogs []*types.Log
for i := len(newChain) - 1; i >= 1; i-- { for i := len(newChain) - 1; i >= 1; i-- {
if logs := bc.collectLogs(newChain[i], false); len(logs) > 0 { newBlock = bc.GetBlock(newChain[i].Hash(), newChain[i].Number.Uint64())
if logs := bc.collectLogs(newBlock, false); len(logs) > 0 {
rebirthLogs = append(rebirthLogs, logs...) rebirthLogs = append(rebirthLogs, logs...)
} }
if len(rebirthLogs) > 512 { if len(rebirthLogs) > 512 {

View file

@ -4263,3 +4263,4 @@ func BenchmarkReorg(b *testing.B) {
// Master: BenchmarkReorg-8 10000 899591 ns/op 820154 B/op 1440 allocs/op 1549443072 bytes of heap used // Master: BenchmarkReorg-8 10000 899591 ns/op 820154 B/op 1440 allocs/op 1549443072 bytes of heap used
// WithoutOldChain: BenchmarkReorg-8 10000 1147281 ns/op 943163 B/op 1564 allocs/op 1163870208 bytes of heap used // WithoutOldChain: BenchmarkReorg-8 10000 1147281 ns/op 943163 B/op 1564 allocs/op 1163870208 bytes of heap used
// WithoutNewChain: BenchmarkReorg-8 10000 1018922 ns/op 943580 B/op 1564 allocs/op 1171890176 bytes of heap used