mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-07 14:46:44 +00:00
[NOD-1418] Implement DAG Traversal (#953)
* Implement DAG Traversal * Update the DAGTraversalManager interface
This commit is contained in:
parent
9cf1557c37
commit
971d50b684
@ -6,6 +6,6 @@ import "github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|||||||
// in the DAG
|
// in the DAG
|
||||||
type DAGTraversalManager interface {
|
type DAGTraversalManager interface {
|
||||||
HighestChainBlockBelowBlueScore(highHash *externalapi.DomainHash, blueScore uint64) (*externalapi.DomainHash, error)
|
HighestChainBlockBelowBlueScore(highHash *externalapi.DomainHash, blueScore uint64) (*externalapi.DomainHash, error)
|
||||||
SelectedParentIterator(highHash *externalapi.DomainHash) (SelectedParentIterator, error)
|
SelectedParentIterator(highHash *externalapi.DomainHash) SelectedParentIterator
|
||||||
BlueWindow(highHash *externalapi.DomainHash, windowSize uint64) ([]*externalapi.DomainHash, error)
|
BlueWindow(highHash *externalapi.DomainHash, windowSize uint64) ([]*externalapi.DomainHash, error)
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package dagtraversalmanager
|
package dagtraversalmanager
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/kaspanet/kaspad/domain/consensus/model"
|
"github.com/kaspanet/kaspad/domain/consensus/model"
|
||||||
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
)
|
)
|
||||||
@ -14,6 +15,29 @@ type dagTraversalManager struct {
|
|||||||
ghostdagDataStore model.GHOSTDAGDataStore
|
ghostdagDataStore model.GHOSTDAGDataStore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// selectedParentIterator implements the `model.SelectedParentIterator` API
|
||||||
|
type selectedParentIterator struct {
|
||||||
|
databaseContext model.DBReader
|
||||||
|
ghostdagDataStore model.GHOSTDAGDataStore
|
||||||
|
current *externalapi.DomainHash
|
||||||
|
}
|
||||||
|
|
||||||
|
func (spi *selectedParentIterator) Next() bool {
|
||||||
|
if spi.current == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
ghostdagData, err := spi.ghostdagDataStore.Get(spi.databaseContext, spi.current)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("ghostdagDataStore is missing ghostdagData for: %v. '%s' ", spi.current, err))
|
||||||
|
}
|
||||||
|
spi.current = ghostdagData.SelectedParent
|
||||||
|
return spi.current != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (spi *selectedParentIterator) Get() *externalapi.DomainHash {
|
||||||
|
return spi.current
|
||||||
|
}
|
||||||
|
|
||||||
// New instantiates a new DAGTraversalManager
|
// New instantiates a new DAGTraversalManager
|
||||||
func New(
|
func New(
|
||||||
databaseContext model.DBReader,
|
databaseContext model.DBReader,
|
||||||
@ -28,8 +52,12 @@ func New(
|
|||||||
|
|
||||||
// SelectedParentIterator creates an iterator over the selected
|
// SelectedParentIterator creates an iterator over the selected
|
||||||
// parent chain of the given highHash
|
// parent chain of the given highHash
|
||||||
func (dtm *dagTraversalManager) SelectedParentIterator(highHash *externalapi.DomainHash) (model.SelectedParentIterator, error) {
|
func (dtm *dagTraversalManager) SelectedParentIterator(highHash *externalapi.DomainHash) model.SelectedParentIterator {
|
||||||
return nil, nil
|
return &selectedParentIterator{
|
||||||
|
databaseContext: dtm.databaseContext,
|
||||||
|
ghostdagDataStore: dtm.ghostdagDataStore,
|
||||||
|
current: highHash,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// HighestChainBlockBelowBlueScore returns the hash of the
|
// HighestChainBlockBelowBlueScore returns the hash of the
|
||||||
@ -37,5 +65,28 @@ func (dtm *dagTraversalManager) SelectedParentIterator(highHash *externalapi.Dom
|
|||||||
// blueScore in the block with the given highHash's selected
|
// blueScore in the block with the given highHash's selected
|
||||||
// parent chain
|
// parent chain
|
||||||
func (dtm *dagTraversalManager) HighestChainBlockBelowBlueScore(highHash *externalapi.DomainHash, blueScore uint64) (*externalapi.DomainHash, error) {
|
func (dtm *dagTraversalManager) HighestChainBlockBelowBlueScore(highHash *externalapi.DomainHash, blueScore uint64) (*externalapi.DomainHash, error) {
|
||||||
return nil, nil
|
|
||||||
|
blockHash := highHash
|
||||||
|
chainBlock, err := dtm.ghostdagDataStore.Get(dtm.databaseContext, highHash)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if chainBlock.BlueScore < blueScore { // will practically return genesis.
|
||||||
|
blueScore = chainBlock.BlueScore
|
||||||
|
}
|
||||||
|
|
||||||
|
requiredBlueScore := chainBlock.BlueScore - blueScore
|
||||||
|
|
||||||
|
// If we used `SelectedParentIterator` we'd need to do more calls to `ghostdagDataStore` so we can get the blueScore
|
||||||
|
for chainBlock.BlueScore >= requiredBlueScore {
|
||||||
|
if chainBlock.SelectedParent == nil { // genesis
|
||||||
|
return blockHash, nil
|
||||||
|
}
|
||||||
|
blockHash = chainBlock.SelectedParent
|
||||||
|
chainBlock, err = dtm.ghostdagDataStore.Get(dtm.databaseContext, highHash)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return blockHash, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user