core: optimize SnapSyncCommitHead to accept block instead of hash

This commit is contained in:
allen 2025-10-08 11:48:40 -04:00
parent 1e4b39ed12
commit b9f6aeb137
2 changed files with 6 additions and 7 deletions

View file

@ -1107,13 +1107,12 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
return rootNumber, bc.loadLastState() return rootNumber, bc.loadLastState()
} }
// SnapSyncCommitHead sets the current head block to the one defined by the hash // SnapSyncCommitHead sets the current head block to the one defined by the block
// irrelevant what the chain contents were prior. // irrelevant what the chain contents were prior.
func (bc *BlockChain) SnapSyncCommitHead(hash common.Hash) error { func (bc *BlockChain) SnapSyncCommitHead(block *types.Block) error {
// Make sure that both the block as well at its state trie exists // Make sure that both the block as well at its state trie exists
block := bc.GetBlockByHash(hash)
if block == nil { if block == nil {
return fmt.Errorf("non existent block [%x..]", hash[:4]) return fmt.Errorf("non existent block")
} }
// Reset the trie database with the fresh snap synced state. // Reset the trie database with the fresh snap synced state.
root := block.Root() root := block.Root()
@ -1138,7 +1137,7 @@ func (bc *BlockChain) SnapSyncCommitHead(hash common.Hash) error {
if bc.snaps != nil { if bc.snaps != nil {
bc.snaps.Rebuild(root) bc.snaps.Rebuild(root)
} }
log.Info("Committed new head block", "number", block.Number(), "hash", hash) log.Info("Committed new head block", "number", block.Number(), "hash", block.Hash())
return nil return nil
} }

View file

@ -190,7 +190,7 @@ type BlockChain interface {
CurrentSnapBlock() *types.Header CurrentSnapBlock() *types.Header
// SnapSyncCommitHead directly commits the head block to a certain entity. // SnapSyncCommitHead directly commits the head block to a certain entity.
SnapSyncCommitHead(common.Hash) error SnapSyncCommitHead(block *types.Block) error
// InsertHeadersBeforeCutoff inserts a batch of headers before the configured // InsertHeadersBeforeCutoff inserts a batch of headers before the configured
// chain cutoff into the ancient store. // chain cutoff into the ancient store.
@ -1066,7 +1066,7 @@ func (d *Downloader) commitPivotBlock(result *fetchResult) error {
if _, err := d.blockchain.InsertReceiptChain([]*types.Block{block}, []rlp.RawValue{result.Receipts}, d.ancientLimit); err != nil { if _, err := d.blockchain.InsertReceiptChain([]*types.Block{block}, []rlp.RawValue{result.Receipts}, d.ancientLimit); err != nil {
return err return err
} }
if err := d.blockchain.SnapSyncCommitHead(block.Hash()); err != nil { if err := d.blockchain.SnapSyncCommitHead(block); err != nil {
return err return err
} }
d.committed.Store(true) d.committed.Store(true)