forked from cerc-io/plugeth
core/blockchain: fast to snap in comments (#27722)
This commit is contained in:
parent
e1fe6bc846
commit
c40ab6af72
@ -344,7 +344,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
|
|||||||
needRewind = true
|
needRewind = true
|
||||||
low = fullBlock.Number.Uint64()
|
low = fullBlock.Number.Uint64()
|
||||||
}
|
}
|
||||||
// In fast sync, it may happen that ancient data has been written to the
|
// In snap sync, it may happen that ancient data has been written to the
|
||||||
// ancient store, but the LastFastBlock has not been updated, truncate the
|
// ancient store, but the LastFastBlock has not been updated, truncate the
|
||||||
// extra data here.
|
// extra data here.
|
||||||
snapBlock := bc.CurrentSnapBlock()
|
snapBlock := bc.CurrentSnapBlock()
|
||||||
@ -472,7 +472,7 @@ func (bc *BlockChain) loadLastState() error {
|
|||||||
}
|
}
|
||||||
bc.hc.SetCurrentHeader(headHeader)
|
bc.hc.SetCurrentHeader(headHeader)
|
||||||
|
|
||||||
// Restore the last known head fast block
|
// Restore the last known head snap block
|
||||||
bc.currentSnapBlock.Store(headBlock.Header())
|
bc.currentSnapBlock.Store(headBlock.Header())
|
||||||
headFastBlockGauge.Update(int64(headBlock.NumberU64()))
|
headFastBlockGauge.Update(int64(headBlock.NumberU64()))
|
||||||
|
|
||||||
@ -507,21 +507,21 @@ func (bc *BlockChain) loadLastState() error {
|
|||||||
}
|
}
|
||||||
log.Info("Loaded most recent local block", "number", headBlock.Number(), "hash", headBlock.Hash(), "td", blockTd, "age", common.PrettyAge(time.Unix(int64(headBlock.Time()), 0)))
|
log.Info("Loaded most recent local block", "number", headBlock.Number(), "hash", headBlock.Hash(), "td", blockTd, "age", common.PrettyAge(time.Unix(int64(headBlock.Time()), 0)))
|
||||||
if headBlock.Hash() != currentSnapBlock.Hash() {
|
if headBlock.Hash() != currentSnapBlock.Hash() {
|
||||||
fastTd := bc.GetTd(currentSnapBlock.Hash(), currentSnapBlock.Number.Uint64())
|
snapTd := bc.GetTd(currentSnapBlock.Hash(), currentSnapBlock.Number.Uint64())
|
||||||
log.Info("Loaded most recent local snap block", "number", currentSnapBlock.Number, "hash", currentSnapBlock.Hash(), "td", fastTd, "age", common.PrettyAge(time.Unix(int64(currentSnapBlock.Time), 0)))
|
log.Info("Loaded most recent local snap block", "number", currentSnapBlock.Number, "hash", currentSnapBlock.Hash(), "td", snapTd, "age", common.PrettyAge(time.Unix(int64(currentSnapBlock.Time), 0)))
|
||||||
}
|
}
|
||||||
if currentFinalBlock != nil {
|
if currentFinalBlock != nil {
|
||||||
finalTd := bc.GetTd(currentFinalBlock.Hash(), currentFinalBlock.Number.Uint64())
|
finalTd := bc.GetTd(currentFinalBlock.Hash(), currentFinalBlock.Number.Uint64())
|
||||||
log.Info("Loaded most recent local finalized block", "number", currentFinalBlock.Number, "hash", currentFinalBlock.Hash(), "td", finalTd, "age", common.PrettyAge(time.Unix(int64(currentFinalBlock.Time), 0)))
|
log.Info("Loaded most recent local finalized block", "number", currentFinalBlock.Number, "hash", currentFinalBlock.Hash(), "td", finalTd, "age", common.PrettyAge(time.Unix(int64(currentFinalBlock.Time), 0)))
|
||||||
}
|
}
|
||||||
if pivot := rawdb.ReadLastPivotNumber(bc.db); pivot != nil {
|
if pivot := rawdb.ReadLastPivotNumber(bc.db); pivot != nil {
|
||||||
log.Info("Loaded last fast-sync pivot marker", "number", *pivot)
|
log.Info("Loaded last snap-sync pivot marker", "number", *pivot)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetHead rewinds the local chain to a new head. Depending on whether the node
|
// SetHead rewinds the local chain to a new head. Depending on whether the node
|
||||||
// was fast synced or full synced and in which state, the method will try to
|
// was snap synced or full synced and in which state, the method will try to
|
||||||
// delete minimal data from disk whilst retaining chain consistency.
|
// delete minimal data from disk whilst retaining chain consistency.
|
||||||
func (bc *BlockChain) SetHead(head uint64) error {
|
func (bc *BlockChain) SetHead(head uint64) error {
|
||||||
if _, err := bc.setHeadBeyondRoot(head, 0, common.Hash{}, false); err != nil {
|
if _, err := bc.setHeadBeyondRoot(head, 0, common.Hash{}, false); err != nil {
|
||||||
@ -542,7 +542,7 @@ func (bc *BlockChain) SetHead(head uint64) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SetHeadWithTimestamp rewinds the local chain to a new head that has at max
|
// SetHeadWithTimestamp rewinds the local chain to a new head that has at max
|
||||||
// the given timestamp. Depending on whether the node was fast synced or full
|
// the given timestamp. Depending on whether the node was snap synced or full
|
||||||
// synced and in which state, the method will try to delete minimal data from
|
// synced and in which state, the method will try to delete minimal data from
|
||||||
// disk whilst retaining chain consistency.
|
// disk whilst retaining chain consistency.
|
||||||
func (bc *BlockChain) SetHeadWithTimestamp(timestamp uint64) error {
|
func (bc *BlockChain) SetHeadWithTimestamp(timestamp uint64) error {
|
||||||
@ -588,7 +588,7 @@ func (bc *BlockChain) SetSafe(header *types.Header) {
|
|||||||
// setHeadBeyondRoot rewinds the local chain to a new head with the extra condition
|
// setHeadBeyondRoot rewinds the local chain to a new head with the extra condition
|
||||||
// that the rewind must pass the specified state root. This method is meant to be
|
// that the rewind must pass the specified state root. This method is meant to be
|
||||||
// used when rewinding with snapshots enabled to ensure that we go back further than
|
// used when rewinding with snapshots enabled to ensure that we go back further than
|
||||||
// persistent disk layer. Depending on whether the node was fast synced or full, and
|
// persistent disk layer. Depending on whether the node was snap synced or full, and
|
||||||
// in which state, the method will try to delete minimal data from disk whilst
|
// in which state, the method will try to delete minimal data from disk whilst
|
||||||
// retaining chain consistency.
|
// retaining chain consistency.
|
||||||
//
|
//
|
||||||
@ -676,7 +676,7 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
|
|||||||
bc.currentBlock.Store(newHeadBlock.Header())
|
bc.currentBlock.Store(newHeadBlock.Header())
|
||||||
headBlockGauge.Update(int64(newHeadBlock.NumberU64()))
|
headBlockGauge.Update(int64(newHeadBlock.NumberU64()))
|
||||||
}
|
}
|
||||||
// Rewind the fast block in a simpleton way to the target head
|
// Rewind the snap block in a simpleton way to the target head
|
||||||
if currentSnapBlock := bc.CurrentSnapBlock(); currentSnapBlock != nil && header.Number.Uint64() < currentSnapBlock.Number.Uint64() {
|
if currentSnapBlock := bc.CurrentSnapBlock(); currentSnapBlock != nil && header.Number.Uint64() < currentSnapBlock.Number.Uint64() {
|
||||||
newHeadSnapBlock := bc.GetBlock(header.Hash(), header.Number.Uint64())
|
newHeadSnapBlock := bc.GetBlock(header.Hash(), header.Number.Uint64())
|
||||||
// If either blocks reached nil, reset to the genesis state
|
// If either blocks reached nil, reset to the genesis state
|
||||||
@ -734,7 +734,7 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Rewind the chain to the requested head and keep going backwards until a
|
// Rewind the chain to the requested head and keep going backwards until a
|
||||||
// block with a state is found or fast sync pivot is passed
|
// block with a state is found or snap sync pivot is passed
|
||||||
if time > 0 {
|
if time > 0 {
|
||||||
log.Warn("Rewinding blockchain to timestamp", "target", time)
|
log.Warn("Rewinding blockchain to timestamp", "target", time)
|
||||||
bc.hc.SetHeadWithTimestamp(time, updateFn, delFn)
|
bc.hc.SetHeadWithTimestamp(time, updateFn, delFn)
|
||||||
@ -868,7 +868,7 @@ func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error {
|
|||||||
|
|
||||||
// writeHeadBlock injects a new head block into the current block chain. This method
|
// writeHeadBlock injects a new head block into the current block chain. This method
|
||||||
// assumes that the block is indeed a true head. It will also reset the head
|
// assumes that the block is indeed a true head. It will also reset the head
|
||||||
// header and the head fast sync block to this very same block if they are older
|
// header and the head snap sync block to this very same block if they are older
|
||||||
// or if they are on a different side chain.
|
// or if they are on a different side chain.
|
||||||
//
|
//
|
||||||
// Note, this function assumes that the `mu` mutex is held!
|
// Note, this function assumes that the `mu` mutex is held!
|
||||||
@ -1049,7 +1049,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
|
|||||||
size = int64(0)
|
size = int64(0)
|
||||||
)
|
)
|
||||||
|
|
||||||
// updateHead updates the head fast sync block if the inserted blocks are better
|
// updateHead updates the head snap sync block if the inserted blocks are better
|
||||||
// and returns an indicator whether the inserted blocks are canonical.
|
// and returns an indicator whether the inserted blocks are canonical.
|
||||||
updateHead := func(head *types.Block) bool {
|
updateHead := func(head *types.Block) bool {
|
||||||
if !bc.chainmu.TryLock() {
|
if !bc.chainmu.TryLock() {
|
||||||
@ -1148,7 +1148,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
|
|||||||
if err := bc.db.Sync(); err != nil {
|
if err := bc.db.Sync(); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
// Update the current fast block because all block data is now present in DB.
|
// Update the current snap block because all block data is now present in DB.
|
||||||
previousSnapBlock := bc.CurrentSnapBlock().Number.Uint64()
|
previousSnapBlock := bc.CurrentSnapBlock().Number.Uint64()
|
||||||
if !updateHead(blockChain[len(blockChain)-1]) {
|
if !updateHead(blockChain[len(blockChain)-1]) {
|
||||||
// We end up here if the header chain has reorg'ed, and the blocks/receipts
|
// We end up here if the header chain has reorg'ed, and the blocks/receipts
|
||||||
@ -1578,11 +1578,11 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
|
|||||||
block, err = it.next()
|
block, err = it.next()
|
||||||
}
|
}
|
||||||
// The remaining blocks are still known blocks, the only scenario here is:
|
// The remaining blocks are still known blocks, the only scenario here is:
|
||||||
// During the fast sync, the pivot point is already submitted but rollback
|
// During the snap sync, the pivot point is already submitted but rollback
|
||||||
// happens. Then node resets the head full block to a lower height via `rollback`
|
// happens. Then node resets the head full block to a lower height via `rollback`
|
||||||
// and leaves a few known blocks in the database.
|
// and leaves a few known blocks in the database.
|
||||||
//
|
//
|
||||||
// When node runs a fast sync again, it can re-import a batch of known blocks via
|
// When node runs a snap sync again, it can re-import a batch of known blocks via
|
||||||
// `insertChain` while a part of them have higher total difficulty than current
|
// `insertChain` while a part of them have higher total difficulty than current
|
||||||
// head full block(new pivot point).
|
// head full block(new pivot point).
|
||||||
for block != nil && bc.skipBlock(err, it) {
|
for block != nil && bc.skipBlock(err, it) {
|
||||||
|
Loading…
Reference in New Issue
Block a user