mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-06 22:26:47 +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,
|
blockRelationStore,
|
||||||
reachabilityDataStore)
|
reachabilityDataStore)
|
||||||
dagTopologyManager := dagtopologymanager.New(
|
dagTopologyManager := dagtopologymanager.New(
|
||||||
|
databaseContext,
|
||||||
reachabilityTree,
|
reachabilityTree,
|
||||||
blockRelationStore)
|
blockRelationStore)
|
||||||
ghostdagManager := ghostdagmanager.New(
|
ghostdagManager := ghostdagmanager.New(
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user