Optimize block locator using finality store (#1386)

* Make sure block locator doesn't include a hash lower than the lowHash in
the block locator

* Use finalityStore to optimize LowestChainBlockAboveOrEqualToBlueScore
This commit is contained in:
Elichai Turkel 2021-01-10 13:36:02 +02:00 committed by GitHub
parent 0f2d0d45b5
commit 2cc0bf1639
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 5 deletions

View File

@ -111,7 +111,9 @@ func (f *factory) NewConsensus(dagParams *dagconfig.Params, db infrastructuredat
dagTopologyManager,
ghostdagDataStore,
reachabilityDataStore,
ghostdagManager)
ghostdagManager,
finalityStore,
dagParams.FinalityDepth())
pastMedianTimeManager := pastmediantimemanager.New(
dagParams.TimestampDeviationTolerance,
dbManager,

View File

@ -13,9 +13,11 @@ type dagTraversalManager struct {
databaseContext model.DBReader
dagTopologyManager model.DAGTopologyManager
ghostdagManager model.GHOSTDAGManager
ghostdagDataStore model.GHOSTDAGDataStore
reachabilityDataStore model.ReachabilityDataStore
ghostdagManager model.GHOSTDAGManager
finalityStore model.FinalityStore
finalityDepth uint64
}
// selectedParentIterator implements the `model.BlockIterator` API
@ -47,13 +49,17 @@ func New(
dagTopologyManager model.DAGTopologyManager,
ghostdagDataStore model.GHOSTDAGDataStore,
reachabilityDataStore model.ReachabilityDataStore,
ghostdagManager model.GHOSTDAGManager) model.DAGTraversalManager {
ghostdagManager model.GHOSTDAGManager,
finalityStore model.FinalityStore,
finalityDepth uint64) model.DAGTraversalManager {
return &dagTraversalManager{
databaseContext: databaseContext,
dagTopologyManager: dagTopologyManager,
ghostdagDataStore: ghostdagDataStore,
reachabilityDataStore: reachabilityDataStore,
ghostdagManager: ghostdagManager,
finalityStore: finalityStore,
finalityDepth: finalityDepth,
}
}
@ -110,6 +116,20 @@ func (dtm *dagTraversalManager) LowestChainBlockAboveOrEqualToBlueScore(highHash
currentHash := highHash
currentBlockGHOSTDAGData := highBlockGHOSTDAGData
// Use the finality Store to jump `finalityDepth` blue scores down the selected chain.
// this should be much faster than stepping through the whole chain.
for currentBlockGHOSTDAGData.BlueScore()-blueScore >= dtm.finalityDepth {
currentHash, err = dtm.finalityStore.FinalityPoint(dtm.databaseContext, currentHash)
if err != nil {
return nil, err
}
currentBlockGHOSTDAGData, err = dtm.ghostdagDataStore.Get(dtm.databaseContext, currentHash)
if err != nil {
return nil, err
}
}
for currentBlockGHOSTDAGData.SelectedParent() != nil {
selectedParentBlockGHOSTDAGData, err := dtm.ghostdagDataStore.Get(dtm.databaseContext, currentBlockGHOSTDAGData.SelectedParent())
if err != nil {

View File

@ -25,7 +25,8 @@ func (sm *syncManager) createBlockLocator(lowHash, highHash *externalapi.DomainH
currentHash := highHash
step := uint64(1)
locator := make(externalapi.BlockLocator, 0)
for currentHash != nil {
// The loop will break if we reached the limit or if we got to lowHash.
for {
locator = append(locator, currentHash)
// Stop if we've reached the limit (if it's set)
@ -55,7 +56,7 @@ func (sm *syncManager) createBlockLocator(lowHash, highHash *externalapi.DomainH
// Calculate blueScore of previous node to include ensuring the
// final node is lowNode.
nextBlueScore := currentBlockBlueScore - step
if currentBlockBlueScore < step {
if currentBlockBlueScore < step || nextBlueScore < lowBlockGHOSTDAGData.BlueScore() {
nextBlueScore = lowBlockGHOSTDAGData.BlueScore()
}