Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
86af7887
Unverified
Commit
86af7887
authored
May 30, 2022
by
Marius van der Wijden
Committed by
GitHub
May 30, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core: use less memory during reorgs (#24616)
This PR significantly reduces the memory consumption of a long reorg
parent
be974272
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
13 deletions
+43
-13
blockchain.go
core/blockchain.go
+25
-13
transaction.go
core/types/transaction.go
+18
-0
No files found.
core/blockchain.go
View file @
86af7887
...
...
@@ -1965,8 +1965,8 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
oldChain
types
.
Blocks
commonBlock
*
types
.
Block
deletedTxs
types
.
Transactions
addedTxs
types
.
Transactions
deletedTxs
[]
common
.
Hash
addedTxs
[]
common
.
Hash
deletedLogs
[][]
*
types
.
Log
rebirthLogs
[][]
*
types
.
Log
...
...
@@ -1976,7 +1976,9 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
// Old chain is longer, gather all transactions and logs as deleted ones
for
;
oldBlock
!=
nil
&&
oldBlock
.
NumberU64
()
!=
newBlock
.
NumberU64
();
oldBlock
=
bc
.
GetBlock
(
oldBlock
.
ParentHash
(),
oldBlock
.
NumberU64
()
-
1
)
{
oldChain
=
append
(
oldChain
,
oldBlock
)
deletedTxs
=
append
(
deletedTxs
,
oldBlock
.
Transactions
()
...
)
for
_
,
tx
:=
range
oldBlock
.
Transactions
()
{
deletedTxs
=
append
(
deletedTxs
,
tx
.
Hash
())
}
// Collect deleted logs for notification
logs
:=
bc
.
collectLogs
(
oldBlock
.
Hash
(),
true
)
...
...
@@ -2006,7 +2008,9 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
}
// Remove an old block as well as stash away a new block
oldChain
=
append
(
oldChain
,
oldBlock
)
deletedTxs
=
append
(
deletedTxs
,
oldBlock
.
Transactions
()
...
)
for
_
,
tx
:=
range
oldBlock
.
Transactions
()
{
deletedTxs
=
append
(
deletedTxs
,
tx
.
Hash
())
}
// Collect deleted logs for notification
logs
:=
bc
.
collectLogs
(
oldBlock
.
Hash
(),
true
)
...
...
@@ -2025,6 +2029,7 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
return
fmt
.
Errorf
(
"invalid new chain"
)
}
}
// Ensure the user sees large reorgs
if
len
(
oldChain
)
>
0
&&
len
(
newChain
)
>
0
{
logFn
:=
log
.
Info
...
...
@@ -2041,7 +2046,7 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
}
else
if
len
(
newChain
)
>
0
{
// Special case happens in the post merge stage that current head is
// the ancestor of new head while these two blocks are not consecutive
log
.
Info
(
"Extend chain"
,
"add"
,
len
(
newChain
),
"number"
,
newChain
[
0
]
.
Number
U64
(),
"hash"
,
newChain
[
0
]
.
Hash
())
log
.
Info
(
"Extend chain"
,
"add"
,
len
(
newChain
),
"number"
,
newChain
[
0
]
.
Number
(),
"hash"
,
newChain
[
0
]
.
Hash
())
blockReorgAddMeter
.
Mark
(
int64
(
len
(
newChain
)))
}
else
{
// len(newChain) == 0 && len(oldChain) > 0
...
...
@@ -2054,19 +2059,17 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
// Insert the block in the canonical way, re-writing history
bc
.
writeHeadBlock
(
newChain
[
i
])
// Collect reborn logs due to chain reorg
logs
:=
bc
.
collectLogs
(
newChain
[
i
]
.
Hash
(),
false
)
if
len
(
logs
)
>
0
{
rebirthLogs
=
append
(
rebirthLogs
,
logs
)
}
// Collect the new added transactions.
addedTxs
=
append
(
addedTxs
,
newChain
[
i
]
.
Transactions
()
...
)
for
_
,
tx
:=
range
newChain
[
i
]
.
Transactions
()
{
addedTxs
=
append
(
addedTxs
,
tx
.
Hash
())
}
}
// Delete useless indexes right now which includes the non-canonical
// transaction indexes, canonical chain indexes which above the head.
indexesBatch
:=
bc
.
db
.
NewBatch
()
for
_
,
tx
:=
range
types
.
Tx
Difference
(
deletedTxs
,
addedTxs
)
{
rawdb
.
DeleteTxLookupEntry
(
indexesBatch
,
tx
.
Hash
()
)
for
_
,
tx
:=
range
types
.
Hash
Difference
(
deletedTxs
,
addedTxs
)
{
rawdb
.
DeleteTxLookupEntry
(
indexesBatch
,
tx
)
}
// Delete any canonical number assignments above the new head
number
:=
bc
.
CurrentBlock
()
.
NumberU64
()
...
...
@@ -2080,6 +2083,15 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
if
err
:=
indexesBatch
.
Write
();
err
!=
nil
{
log
.
Crit
(
"Failed to delete useless indexes"
,
"err"
,
err
)
}
// Collect the logs
for
i
:=
len
(
newChain
)
-
1
;
i
>=
1
;
i
--
{
// Collect reborn logs due to chain reorg
logs
:=
bc
.
collectLogs
(
newChain
[
i
]
.
Hash
(),
false
)
if
len
(
logs
)
>
0
{
rebirthLogs
=
append
(
rebirthLogs
,
logs
)
}
}
// If any logs need to be fired, do it now. In theory we could avoid creating
// this goroutine if there are no events to fire, but realistcally that only
// ever happens if we're reorging empty blocks, which will only happen on idle
...
...
core/types/transaction.go
View file @
86af7887
...
...
@@ -432,6 +432,24 @@ func TxDifference(a, b Transactions) Transactions {
return
keep
}
// HashDifference returns a new set which is the difference between a and b.
func
HashDifference
(
a
,
b
[]
common
.
Hash
)
[]
common
.
Hash
{
keep
:=
make
([]
common
.
Hash
,
0
,
len
(
a
))
remove
:=
make
(
map
[
common
.
Hash
]
struct
{})
for
_
,
hash
:=
range
b
{
remove
[
hash
]
=
struct
{}{}
}
for
_
,
hash
:=
range
a
{
if
_
,
ok
:=
remove
[
hash
];
!
ok
{
keep
=
append
(
keep
,
hash
)
}
}
return
keep
}
// TxByNonce implements the sort interface to allow sorting a list of transactions
// by their nonces. This is usually only useful for sorting transactions from a
// single account, otherwise a nonce comparison doesn't make much sense.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment