eth: quick canceling block inserting when debug_setHead is invoked (#32067)
Some checks are pending
/ Linux Build (push) Waiting to run
/ Linux Build (arm) (push) Waiting to run
/ Docker Image (push) Waiting to run

If Geth is engaged in a long-run block synchronization, such as a full
syncing over a large number of blocks, invoking `debug_setHead` will
cause `downloader.Cancel` to wait for all fetchers to stop first.
This can be time-consuming, particularly for the block processing
thread.

To address this, we manually call `blockchain.StopInsert` to interrupt
the blocking processing thread and allow it to exit immediately, and
after that call `blockchain.ResumeInsert` to resume the block
downloading process.

Additionally, we add a sanity check for the input block number of
`debug_setHead` to ensure its validity.

---------

Signed-off-by: jsvisa <delweng@gmail.com>
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
This commit is contained in:
Delweng 2025-06-23 14:04:21 +08:00 committed by GitHub
parent 21920207e4
commit 78b6059341
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 12 deletions

View file

@ -1244,7 +1244,7 @@ func (bc *BlockChain) stopWithoutSaving() {
bc.scope.Close() bc.scope.Close()
// Signal shutdown to all goroutines. // Signal shutdown to all goroutines.
bc.StopInsert() bc.InterruptInsert(true)
// Now wait for all chain modifications to end and persistent goroutines to exit. // Now wait for all chain modifications to end and persistent goroutines to exit.
// //
@ -1318,11 +1318,15 @@ func (bc *BlockChain) Stop() {
log.Info("Blockchain stopped") log.Info("Blockchain stopped")
} }
// StopInsert interrupts all insertion methods, causing them to return // InterruptInsert interrupts all insertion methods, causing them to return
// errInsertionInterrupted as soon as possible. Insertion is permanently disabled after // errInsertionInterrupted as soon as possible, or resume the chain insertion
// calling this method. // if required.
func (bc *BlockChain) StopInsert() { func (bc *BlockChain) InterruptInsert(on bool) {
bc.procInterrupt.Store(true) if on {
bc.procInterrupt.Store(true)
} else {
bc.procInterrupt.Store(false)
}
} }
// insertStopped returns true after StopInsert has been called. // insertStopped returns true after StopInsert has been called.

View file

@ -199,8 +199,8 @@ type BlockChain interface {
// InsertChain inserts a batch of blocks into the local chain. // InsertChain inserts a batch of blocks into the local chain.
InsertChain(types.Blocks) (int, error) InsertChain(types.Blocks) (int, error)
// StopInsert interrupts the inserting process. // InterruptInsert whether disables the chain insertion.
StopInsert() InterruptInsert(on bool)
// InsertReceiptChain inserts a batch of blocks along with their receipts // InsertReceiptChain inserts a batch of blocks along with their receipts
// into the local chain. Blocks older than the specified `ancientLimit` // into the local chain. Blocks older than the specified `ancientLimit`
@ -630,16 +630,15 @@ func (d *Downloader) cancel() {
// Cancel aborts all of the operations and waits for all download goroutines to // Cancel aborts all of the operations and waits for all download goroutines to
// finish before returning. // finish before returning.
func (d *Downloader) Cancel() { func (d *Downloader) Cancel() {
d.blockchain.InterruptInsert(true)
d.cancel() d.cancel()
d.cancelWg.Wait() d.cancelWg.Wait()
d.blockchain.InterruptInsert(false)
} }
// Terminate interrupts the downloader, canceling all pending operations. // Terminate interrupts the downloader, canceling all pending operations.
// The downloader cannot be reused after calling Terminate. // The downloader cannot be reused after calling Terminate.
func (d *Downloader) Terminate() { func (d *Downloader) Terminate() {
// Signal to stop inserting in-flight blocks
d.blockchain.StopInsert()
// Close the termination channel (make sure double close is allowed) // Close the termination channel (make sure double close is allowed)
d.quitLock.Lock() d.quitLock.Lock()
select { select {

View file

@ -1831,8 +1831,16 @@ func (api *DebugAPI) ChaindbCompact() error {
} }
// SetHead rewinds the head of the blockchain to a previous block. // SetHead rewinds the head of the blockchain to a previous block.
func (api *DebugAPI) SetHead(number hexutil.Uint64) { func (api *DebugAPI) SetHead(number hexutil.Uint64) error {
header := api.b.CurrentHeader()
if header == nil {
return errors.New("current header is not available")
}
if header.Number.Uint64() <= uint64(number) {
return errors.New("not allowed to rewind to a future block")
}
api.b.SetHead(uint64(number)) api.b.SetHead(uint64(number))
return nil
} }
// NetAPI offers network related RPC methods // NetAPI offers network related RPC methods