core, eth: polish

This commit is contained in:
Gary Rong 2025-06-23 13:56:07 +08:00
parent bc3314aed9
commit 0119765c35
3 changed files with 14 additions and 18 deletions

View file

@ -1226,7 +1226,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.
// //
@ -1300,16 +1300,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 {
// ResumeInsert re-enables the blockchain insertion, which is used to rewinding the chain to bc.procInterrupt.Store(false)
func (bc *BlockChain) ResumeInsert() { }
bc.procInterrupt.Store(false)
} }
// insertStopped returns true after StopInsert has been called. // insertStopped returns true after StopInsert has been called.

View file

@ -62,10 +62,8 @@ func (b *EthAPIBackend) CurrentBlock() *types.Header {
} }
func (b *EthAPIBackend) SetHead(number uint64) { func (b *EthAPIBackend) SetHead(number uint64) {
b.eth.blockchain.StopInsert()
b.eth.handler.downloader.Cancel() b.eth.handler.downloader.Cancel()
b.eth.blockchain.SetHead(number) b.eth.blockchain.SetHead(number)
b.eth.blockchain.ResumeInsert()
} }
func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {

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 {