mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-05 13:46:42 +00:00
[NOD-1419] Implement DAG topology (#948)
* [NOD-1419] Implement DAG topology * [NOD-1419] Add isHashInSlice
This commit is contained in:
parent
9181481fc8
commit
74d13e271e
@ -52,6 +52,7 @@ func (f *factory) NewConsensus(dagParams *dagconfig.Params, databaseContext *dba
|
||||
blockRelationStore,
|
||||
reachabilityDataStore)
|
||||
dagTopologyManager := dagtopologymanager.New(
|
||||
databaseContext,
|
||||
reachabilityTree,
|
||||
blockRelationStore)
|
||||
ghostdagManager := ghostdagmanager.New(
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user