Skip to content

Commit 217edb2

Browse files
committed
eth: reset miner, engine, and txpool when calling SetHead
1 parent 7beccb2 commit 217edb2

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

eth/api_backend.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ package eth
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"math/big"
2223

2324
"github.com/ethereum/go-ethereum/accounts"
2425
"github.com/ethereum/go-ethereum/common"
2526
"github.com/ethereum/go-ethereum/common/math"
27+
"github.com/ethereum/go-ethereum/consensus/clique"
2628
"github.com/ethereum/go-ethereum/core"
2729
"github.com/ethereum/go-ethereum/core/bloombits"
2830
"github.com/ethereum/go-ethereum/core/rawdb"
@@ -33,6 +35,7 @@ import (
3335
"github.com/ethereum/go-ethereum/eth/gasprice"
3436
"github.com/ethereum/go-ethereum/ethdb"
3537
"github.com/ethereum/go-ethereum/event"
38+
"github.com/ethereum/go-ethereum/miner"
3639
"github.com/ethereum/go-ethereum/params"
3740
"github.com/ethereum/go-ethereum/rpc"
3841
)
@@ -53,7 +56,28 @@ func (b *EthAPIBackend) CurrentBlock() *types.Block {
5356

5457
func (b *EthAPIBackend) SetHead(number uint64) {
5558
b.eth.protocolManager.downloader.Cancel()
59+
60+
// Note: Suddenly changing the state of the blockchain can break other parts
61+
// of the codebase that are holding on to some state, in some cases causing
62+
// crashes. In order to prevent that, we need to stop and reset the miner,
63+
// txpool, and consensus engine.
64+
wasMining := b.eth.miner.Mining()
65+
if wasMining {
66+
b.eth.miner.Stop()
67+
}
68+
b.eth.txPool.Stop()
5669
b.eth.blockchain.SetHead(number)
70+
b.eth.txPool = core.NewTxPool(b.eth.config.TxPool, b.eth.chainConfig, b.eth.blockchain)
71+
if b.eth.chainConfig.Clique != nil {
72+
b.eth.engine = clique.New(b.eth.chainConfig.Clique, b.eth.chainDb)
73+
}
74+
b.eth.miner = miner.New(b.eth, b.eth.chainConfig, b.eth.EventMux(), b.eth.engine)
75+
76+
if wasMining {
77+
if err := b.eth.StartMining(true); err != nil {
78+
panic(fmt.Errorf("could not start miner: %s", err.Error()))
79+
}
80+
}
5781
}
5882

5983
func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {

0 commit comments

Comments
 (0)