Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth: reset miner, engine, and txpool when calling SetHead to prevent crashes #16713

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package eth

import (
"context"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/consensus/clique"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/bloombits"
"github.com/ethereum/go-ethereum/core/rawdb"
Expand All @@ -33,6 +35,7 @@ import (
"github.com/ethereum/go-ethereum/eth/gasprice"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/miner"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
)
Expand All @@ -53,7 +56,28 @@ func (b *EthAPIBackend) CurrentBlock() *types.Block {

func (b *EthAPIBackend) SetHead(number uint64) {
b.eth.protocolManager.downloader.Cancel()

// Note: Suddenly changing the state of the blockchain can break other parts
// of the codebase that are holding on to some state, in some cases causing
// crashes. In order to prevent that, we need to stop and reset the miner,
// txpool, and consensus engine.
wasMining := b.eth.miner.Mining()
if wasMining {
b.eth.miner.Stop()
}
b.eth.txPool.Stop()
b.eth.blockchain.SetHead(number)
b.eth.txPool = core.NewTxPool(b.eth.config.TxPool, b.eth.chainConfig, b.eth.blockchain)
if b.eth.chainConfig.Clique != nil {
Copy link
Author

@albrow albrow May 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At my company, we use a single node network for testing with Clique as the consensus engine. I'm not sure how to handle Ethash or other consensus engines here. Is that something we need to consider?

b.eth.engine = clique.New(b.eth.chainConfig.Clique, b.eth.chainDb)
}
b.eth.miner = miner.New(b.eth, b.eth.chainConfig, b.eth.EventMux(), b.eth.engine)

if wasMining {
if err := b.eth.StartMining(true); err != nil {
panic(fmt.Errorf("could not start miner: %s", err.Error()))
}
}
}

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