diff --git a/domain/consensus/factory.go b/domain/consensus/factory.go index a64635c42..0275a3aed 100644 --- a/domain/consensus/factory.go +++ b/domain/consensus/factory.go @@ -52,6 +52,7 @@ func (f *factory) NewConsensus(dagParams *dagconfig.Params, databaseContext *dba blockRelationStore, reachabilityDataStore) dagTopologyManager := dagtopologymanager.New( + databaseContext, reachabilityTree, blockRelationStore) ghostdagManager := ghostdagmanager.New( diff --git a/domain/consensus/processes/dagtopologymanager/dagtopologymanager.go b/domain/consensus/processes/dagtopologymanager/dagtopologymanager.go index 46fb644fe..36c70ffe2 100644 --- a/domain/consensus/processes/dagtopologymanager/dagtopologymanager.go +++ b/domain/consensus/processes/dagtopologymanager/dagtopologymanager.go @@ -3,6 +3,7 @@ package dagtopologymanager import ( "github.com/kaspanet/kaspad/domain/consensus/datastructures" "github.com/kaspanet/kaspad/domain/consensus/processes" + "github.com/kaspanet/kaspad/infrastructure/db/dbaccess" "github.com/kaspanet/kaspad/util/daghash" ) @@ -11,13 +12,16 @@ import ( type DAGTopologyManager struct { reachabilityTree processes.ReachabilityTree blockRelationStore datastructures.BlockRelationStore + databaseContext *dbaccess.DatabaseContext } // New instantiates a new DAGTopologyManager func New( + databaseContext *dbaccess.DatabaseContext, reachabilityTree processes.ReachabilityTree, blockRelationStore datastructures.BlockRelationStore) *DAGTopologyManager { return &DAGTopologyManager{ + databaseContext: databaseContext, reachabilityTree: reachabilityTree, blockRelationStore: blockRelationStore, } @@ -25,30 +29,39 @@ func New( // Parents returns the DAG parents of the given blockHash 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 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 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 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 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 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 }