[NOD-1419] Implement DAG topology (#948)

* [NOD-1419] Implement DAG topology

* [NOD-1419] Add isHashInSlice
This commit is contained in:
Ori Newman 2020-10-08 04:00:25 -07:00 committed by GitHub
parent 9181481fc8
commit 74d13e271e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 5 deletions

View File

@ -52,6 +52,7 @@ func (f *factory) NewConsensus(dagParams *dagconfig.Params, databaseContext *dba
blockRelationStore, blockRelationStore,
reachabilityDataStore) reachabilityDataStore)
dagTopologyManager := dagtopologymanager.New( dagTopologyManager := dagtopologymanager.New(
databaseContext,
reachabilityTree, reachabilityTree,
blockRelationStore) blockRelationStore)
ghostdagManager := ghostdagmanager.New( ghostdagManager := ghostdagmanager.New(

View File

@ -3,6 +3,7 @@ package dagtopologymanager
import ( import (
"github.com/kaspanet/kaspad/domain/consensus/datastructures" "github.com/kaspanet/kaspad/domain/consensus/datastructures"
"github.com/kaspanet/kaspad/domain/consensus/processes" "github.com/kaspanet/kaspad/domain/consensus/processes"
"github.com/kaspanet/kaspad/infrastructure/db/dbaccess"
"github.com/kaspanet/kaspad/util/daghash" "github.com/kaspanet/kaspad/util/daghash"
) )
@ -11,13 +12,16 @@ import (
type DAGTopologyManager struct { type DAGTopologyManager struct {
reachabilityTree processes.ReachabilityTree reachabilityTree processes.ReachabilityTree
blockRelationStore datastructures.BlockRelationStore blockRelationStore datastructures.BlockRelationStore
databaseContext *dbaccess.DatabaseContext
} }
// New instantiates a new DAGTopologyManager // New instantiates a new DAGTopologyManager
func New( func New(
databaseContext *dbaccess.DatabaseContext,
reachabilityTree processes.ReachabilityTree, reachabilityTree processes.ReachabilityTree,
blockRelationStore datastructures.BlockRelationStore) *DAGTopologyManager { blockRelationStore datastructures.BlockRelationStore) *DAGTopologyManager {
return &DAGTopologyManager{ return &DAGTopologyManager{
databaseContext: databaseContext,
reachabilityTree: reachabilityTree, reachabilityTree: reachabilityTree,
blockRelationStore: blockRelationStore, blockRelationStore: blockRelationStore,
} }
@ -25,30 +29,39 @@ func New(
// Parents returns the DAG parents of the given blockHash // Parents returns the DAG parents of the given blockHash
func (dtm *DAGTopologyManager) Parents(blockHash *daghash.Hash) []*daghash.Hash { func (dtm *DAGTopologyManager) Parents(blockHash *daghash.Hash) []*daghash.Hash {
return nil return dtm.blockRelationStore.Get(dtm.databaseContext, blockHash).Parents
} }
// Children returns the DAG children of the given blockHash // Children returns the DAG children of the given blockHash
func (dtm *DAGTopologyManager) Children(blockHash *daghash.Hash) []*daghash.Hash { func (dtm *DAGTopologyManager) Children(blockHash *daghash.Hash) []*daghash.Hash {
return nil return dtm.blockRelationStore.Get(dtm.databaseContext, blockHash).Children
} }
// IsParentOf returns true if blockHashA is a direct DAG parent of blockHashB // IsParentOf returns true if blockHashA is a direct DAG parent of blockHashB
func (dtm *DAGTopologyManager) IsParentOf(blockHashA *daghash.Hash, blockHashB *daghash.Hash) bool { func (dtm *DAGTopologyManager) IsParentOf(blockHashA *daghash.Hash, blockHashB *daghash.Hash) bool {
return false return isHashInSlice(blockHashA, dtm.blockRelationStore.Get(dtm.databaseContext, blockHashB).Parents)
} }
// IsChildOf returns true if blockHashA is a direct DAG child of blockHashB // IsChildOf returns true if blockHashA is a direct DAG child of blockHashB
func (dtm *DAGTopologyManager) IsChildOf(blockHashA *daghash.Hash, blockHashB *daghash.Hash) bool { func (dtm *DAGTopologyManager) IsChildOf(blockHashA *daghash.Hash, blockHashB *daghash.Hash) bool {
return false return isHashInSlice(blockHashA, dtm.blockRelationStore.Get(dtm.databaseContext, blockHashB).Children)
} }
// IsAncestorOf returns true if blockHashA is a DAG ancestor of blockHashB // IsAncestorOf returns true if blockHashA is a DAG ancestor of blockHashB
func (dtm *DAGTopologyManager) IsAncestorOf(blockHashA *daghash.Hash, blockHashB *daghash.Hash) bool { func (dtm *DAGTopologyManager) IsAncestorOf(blockHashA *daghash.Hash, blockHashB *daghash.Hash) bool {
return false return dtm.reachabilityTree.IsDAGAncestorOf(blockHashA, blockHashB)
} }
// IsDescendantOf returns true if blockHashA is a DAG descendant of blockHashB // IsDescendantOf returns true if blockHashA is a DAG descendant of blockHashB
func (dtm *DAGTopologyManager) IsDescendantOf(blockHashA *daghash.Hash, blockHashB *daghash.Hash) bool { func (dtm *DAGTopologyManager) IsDescendantOf(blockHashA *daghash.Hash, blockHashB *daghash.Hash) bool {
return dtm.reachabilityTree.IsDAGAncestorOf(blockHashB, blockHashA)
}
func isHashInSlice(hash *daghash.Hash, hashes []*daghash.Hash) bool {
for _, h := range hashes {
if *h == *hash {
return true
}
}
return false return false
} }