Skip to content

Commit 160add8

Browse files
authored
Merge pull request #15095 from karalabe/txpool-avoid-deep-reorg
core: use blocks and avoid deep reorgs in txpool
2 parents 1e67378 + cc313e7 commit 160add8

2 files changed

Lines changed: 55 additions & 45 deletions

File tree

‎core/tx_pool.go‎

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package core
1919
import (
2020
"errors"
2121
"fmt"
22+
"math"
2223
"math/big"
2324
"sort"
2425
"sync"
@@ -105,11 +106,11 @@ var (
105106
// blockChain provides the state of blockchain and current gas limit to do
106107
// some pre checks in tx pool and event subscribers.
107108
type blockChain interface {
108-
CurrentHeader() *types.Header
109-
SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription
110-
109+
CurrentBlock() *types.Block
111110
GetBlock(hash common.Hash, number uint64) *types.Block
112111
StateAt(root common.Hash) (*state.StateDB, error)
112+
113+
SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription
113114
}
114115

115116
// TxPoolConfig are the configuration parameters of the transaction pool.
@@ -223,7 +224,7 @@ func NewTxPool(config TxPoolConfig, chainconfig *params.ChainConfig, chain block
223224
}
224225
pool.locals = newAccountSet(pool.signer)
225226
pool.priced = newTxPricedList(&pool.all)
226-
pool.reset(nil, chain.CurrentHeader())
227+
pool.reset(nil, chain.CurrentBlock().Header())
227228

228229
// If local transactions and journaling is enabled, load from disk
229230
if !config.NoLocals && config.Journal != "" {
@@ -265,7 +266,7 @@ func (pool *TxPool) loop() {
265266
defer journal.Stop()
266267

267268
// Track the previous head headers for transaction reorgs
268-
head := pool.chain.CurrentHeader()
269+
head := pool.chain.CurrentBlock()
269270

270271
// Keep waiting for and reacting to the various events
271272
for {
@@ -277,8 +278,8 @@ func (pool *TxPool) loop() {
277278
if pool.chainconfig.IsHomestead(ev.Block.Number()) {
278279
pool.homestead = true
279280
}
280-
pool.reset(head, ev.Block.Header())
281-
head = ev.Block.Header()
281+
pool.reset(head.Header(), ev.Block.Header())
282+
head = ev.Block
282283

283284
pool.mu.Unlock()
284285
}
@@ -344,43 +345,52 @@ func (pool *TxPool) reset(oldHead, newHead *types.Header) {
344345
var reinject types.Transactions
345346

346347
if oldHead != nil && oldHead.Hash() != newHead.ParentHash {
347-
var discarded, included types.Transactions
348-
349-
var (
350-
rem = pool.chain.GetBlock(oldHead.Hash(), oldHead.Number.Uint64())
351-
add = pool.chain.GetBlock(newHead.Hash(), newHead.Number.Uint64())
352-
)
353-
for rem.NumberU64() > add.NumberU64() {
354-
discarded = append(discarded, rem.Transactions()...)
355-
if rem = pool.chain.GetBlock(rem.ParentHash(), rem.NumberU64()-1); rem == nil {
356-
log.Error("Unrooted old chain seen by tx pool", "block", oldHead.Number, "hash", oldHead.Hash())
357-
return
358-
}
359-
}
360-
for add.NumberU64() > rem.NumberU64() {
361-
included = append(included, add.Transactions()...)
362-
if add = pool.chain.GetBlock(add.ParentHash(), add.NumberU64()-1); add == nil {
363-
log.Error("Unrooted new chain seen by tx pool", "block", newHead.Number, "hash", newHead.Hash())
364-
return
348+
// If the reorg is too deep, avoid doing it (will happen during fast sync)
349+
oldNum := oldHead.Number.Uint64()
350+
newNum := newHead.Number.Uint64()
351+
352+
if depth := uint64(math.Abs(float64(oldNum) - float64(newNum))); depth > 64 {
353+
log.Warn("Skipping deep transaction reorg", "depth", depth)
354+
} else {
355+
// Reorg seems shallow enough to pull in all transactions into memory
356+
var discarded, included types.Transactions
357+
358+
var (
359+
rem = pool.chain.GetBlock(oldHead.Hash(), oldHead.Number.Uint64())
360+
add = pool.chain.GetBlock(newHead.Hash(), newHead.Number.Uint64())
361+
)
362+
for rem.NumberU64() > add.NumberU64() {
363+
discarded = append(discarded, rem.Transactions()...)
364+
if rem = pool.chain.GetBlock(rem.ParentHash(), rem.NumberU64()-1); rem == nil {
365+
log.Error("Unrooted old chain seen by tx pool", "block", oldHead.Number, "hash", oldHead.Hash())
366+
return
367+
}
365368
}
366-
}
367-
for rem.Hash() != add.Hash() {
368-
discarded = append(discarded, rem.Transactions()...)
369-
if rem = pool.chain.GetBlock(rem.ParentHash(), rem.NumberU64()-1); rem == nil {
370-
log.Error("Unrooted old chain seen by tx pool", "block", oldHead.Number, "hash", oldHead.Hash())
371-
return
369+
for add.NumberU64() > rem.NumberU64() {
370+
included = append(included, add.Transactions()...)
371+
if add = pool.chain.GetBlock(add.ParentHash(), add.NumberU64()-1); add == nil {
372+
log.Error("Unrooted new chain seen by tx pool", "block", newHead.Number, "hash", newHead.Hash())
373+
return
374+
}
372375
}
373-
included = append(included, add.Transactions()...)
374-
if add = pool.chain.GetBlock(add.ParentHash(), add.NumberU64()-1); add == nil {
375-
log.Error("Unrooted new chain seen by tx pool", "block", newHead.Number, "hash", newHead.Hash())
376-
return
376+
for rem.Hash() != add.Hash() {
377+
discarded = append(discarded, rem.Transactions()...)
378+
if rem = pool.chain.GetBlock(rem.ParentHash(), rem.NumberU64()-1); rem == nil {
379+
log.Error("Unrooted old chain seen by tx pool", "block", oldHead.Number, "hash", oldHead.Hash())
380+
return
381+
}
382+
included = append(included, add.Transactions()...)
383+
if add = pool.chain.GetBlock(add.ParentHash(), add.NumberU64()-1); add == nil {
384+
log.Error("Unrooted new chain seen by tx pool", "block", newHead.Number, "hash", newHead.Hash())
385+
return
386+
}
377387
}
388+
reinject = types.TxDifference(discarded, included)
378389
}
379-
reinject = types.TxDifference(discarded, included)
380390
}
381391
// Initialize the internal state to the current head
382392
if newHead == nil {
383-
newHead = pool.chain.CurrentHeader() // Special case during testing
393+
newHead = pool.chain.CurrentBlock().Header() // Special case during testing
384394
}
385395
statedb, err := pool.chain.StateAt(newHead.Root)
386396
if err != nil {

‎core/tx_pool_test.go‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,24 @@ type testBlockChain struct {
5050
chainHeadFeed *event.Feed
5151
}
5252

53-
func (bc *testBlockChain) CurrentHeader() *types.Header {
54-
return &types.Header{
53+
func (bc *testBlockChain) CurrentBlock() *types.Block {
54+
return types.NewBlock(&types.Header{
5555
GasLimit: bc.gasLimit,
56-
}
57-
}
58-
59-
func (bc *testBlockChain) SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription {
60-
return bc.chainHeadFeed.Subscribe(ch)
56+
}, nil, nil, nil)
6157
}
6258

6359
func (bc *testBlockChain) GetBlock(hash common.Hash, number uint64) *types.Block {
64-
return types.NewBlock(bc.CurrentHeader(), nil, nil, nil)
60+
return bc.CurrentBlock()
6561
}
6662

6763
func (bc *testBlockChain) StateAt(common.Hash) (*state.StateDB, error) {
6864
return bc.statedb, nil
6965
}
7066

67+
func (bc *testBlockChain) SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription {
68+
return bc.chainHeadFeed.Subscribe(ch)
69+
}
70+
7171
func transaction(nonce uint64, gaslimit *big.Int, key *ecdsa.PrivateKey) *types.Transaction {
7272
return pricedTransaction(nonce, gaslimit, big.NewInt(1), key)
7373
}

0 commit comments

Comments
 (0)