Skip to content

Commit 611f11d

Browse files
committed
quick canceling block inserting when debug_setHead is invoked
1 parent 0f6910c commit 611f11d

5 files changed

Lines changed: 38 additions & 17 deletions

File tree

core/blockchain.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ var (
8383
blockReorgAddMeter = metrics.NewRegisteredMeter("chain/reorg/add", nil)
8484
blockReorgDropMeter = metrics.NewRegisteredMeter("chain/reorg/drop", nil)
8585

86-
blockPrefetchExecuteTimer = metrics.NewRegisteredTimer("chain/prefetch/executes", nil)
87-
blockPrefetchInterruptMeter = metrics.NewRegisteredMeter("chain/prefetch/interrupts", nil)
86+
blockPrefetchExecuteTimer = metrics.NewRegisteredTimer("chain/prefetch/executes", nil)
87+
blockPrefetchInterruptMeter = metrics.NewRegisteredMeter("chain/prefetch/interrupts", nil)
88+
blockPrefetchTxsInvalidMeter = metrics.NewRegisteredMeter("chain/prefetch/txs/invalid", nil)
89+
blockPrefetchTxsValidMeter = metrics.NewRegisteredMeter("chain/prefetch/txs/valid", nil)
8890

8991
errInsertionInterrupted = errors.New("insertion is interrupted")
9092
errChainStopped = errors.New("blockchain is stopped")
@@ -1020,7 +1022,7 @@ func (bc *BlockChain) stopWithoutSaving() {
10201022

10211023
// Signal shutdown to all goroutines.
10221024
close(bc.quit)
1023-
bc.StopInsert()
1025+
bc.InterruptInsert(true)
10241026

10251027
// Now wait for all chain modifications to end and persistent goroutines to exit.
10261028
//
@@ -1102,11 +1104,15 @@ func (bc *BlockChain) Stop() {
11021104
log.Info("Blockchain stopped")
11031105
}
11041106

1105-
// StopInsert interrupts all insertion methods, causing them to return
1106-
// errInsertionInterrupted as soon as possible. Insertion is permanently disabled after
1107-
// calling this method.
1108-
func (bc *BlockChain) StopInsert() {
1109-
bc.procInterrupt.Store(true)
1107+
// InterruptInsert interrupts all insertion methods, causing them to return
1108+
// errInsertionInterrupted as soon as possible, or resume the chain insertion
1109+
// if required.
1110+
func (bc *BlockChain) InterruptInsert(on bool) {
1111+
if on {
1112+
bc.procInterrupt.Store(true)
1113+
} else {
1114+
bc.procInterrupt.Store(false)
1115+
}
11101116
}
11111117

11121118
// insertStopped returns true after StopInsert has been called.

ctxc/downloader/downloader.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ type BlockChain interface {
187187
// InsertChain inserts a batch of blocks into the local chain.
188188
InsertChain(types.Blocks) (int, error)
189189

190+
// InterruptInsert whether disables the chain insertion.
191+
InterruptInsert(on bool)
192+
190193
// InsertReceiptChain inserts a batch of receipts into the local chain.
191194
InsertReceiptChain(types.Blocks, []types.Receipts, uint64) (int, error)
192195
}
@@ -564,8 +567,10 @@ func (d *Downloader) cancel() {
564567
// Cancel aborts all of the operations and waits for all download goroutines to
565568
// finish before returning.
566569
func (d *Downloader) Cancel() {
570+
d.blockchain.InterruptInsert(true)
567571
d.cancel()
568572
d.cancelWg.Wait()
573+
d.blockchain.InterruptInsert(false)
569574
}
570575

571576
// Terminate interrupts the downloader, canceling all pending operations.

ctxc/downloader/downloader_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,9 @@ func (dl *downloadTester) SetHead(head uint64) error {
384384
func (dl *downloadTester) Rollback(hashes []common.Hash) {
385385
}
386386

387+
func (bc *downloadTester) InterruptInsert(on bool) {
388+
}
389+
387390
// newPeer registers a new block download source into the downloader.
388391
func (dl *downloadTester) newPeer(id string, version int, chain *testChain) error {
389392
dl.lock.Lock()

ctxc/sync.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ func (cs *chainSyncer) loop() {
224224
// Disable all insertion on the blockchain. This needs to happen before
225225
// terminating the downloader because the downloader waits for blockchain
226226
// inserts, and these can take a long time to finish.
227-
cs.pm.blockchain.StopInsert()
228227
cs.pm.downloader.Terminate()
229228
if cs.doneCh != nil {
230229
// Wait for the current sync to end.

internal/ctxcapi/api.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -867,13 +867,13 @@ func (args *CallArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) *core.Mes
867867

868868
// Create new call message
869869
msg := &core.Message{
870-
From: addr,
871-
To: args.To,
872-
Value: args.Value.ToInt(),
873-
GasLimit: gas,
874-
GasPrice: gasPrice,
875-
Data: args.Data,
876-
SkipAccountChecks: true,
870+
From: addr,
871+
To: args.To,
872+
Value: args.Value.ToInt(),
873+
GasLimit: gas,
874+
GasPrice: gasPrice,
875+
Data: args.Data,
876+
SkipNonceChecks: true,
877877
}
878878
return msg
879879
}
@@ -1818,8 +1818,16 @@ func (api *PrivateDebugAPI) ChaindbCompact() error {
18181818
}
18191819

18201820
// SetHead rewinds the head of the blockchain to a previous block.
1821-
func (api *PrivateDebugAPI) SetHead(number hexutil.Uint64) {
1821+
func (api *PrivateDebugAPI) SetHead(number hexutil.Uint64) error {
1822+
header := api.b.CurrentHeader()
1823+
if header == nil {
1824+
return errors.New("current header is not available")
1825+
}
1826+
if header.Number.Uint64() <= uint64(number) {
1827+
return errors.New("not allowed to rewind to a future block")
1828+
}
18221829
api.b.SetHead(uint64(number))
1830+
return nil
18231831
}
18241832

18251833
// PrivateFsAPI offers torrentfs related RPC methods

0 commit comments

Comments
 (0)