mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-08-23 02:53:14 +00:00

* Pruning headers p2p basic structure * Remove headers-first * Fix consensus tests except TestValidateAndInsertPruningPointWithSideBlocks and TestValidateAndInsertImportedPruningPoint * Add virtual genesis * Implement PruningPointAndItsAnticoneWithMetaData * Start fixing TestValidateAndInsertImportedPruningPoint * Fix TestValidateAndInsertImportedPruningPoint * Fix BlockWindow * Update p2p and gRPC * Fix all tests except TestHandleRelayInvs * Delete TestHandleRelayInvs parts that cover the old IBD flow * Fix lint errors * Add p2p_request_ibd_blocks.go * Clean code * Make MsgBlockWithMetaData implement its own representation * Remove redundant check if highest share block is below the pruning point * Fix TestCheckLockTimeVerifyConditionedByAbsoluteTimeWithWrongLockTime * Fix comments, errors ane names * Fix window size to the real value * Check reindex root after each block at TestUpdateReindexRoot * Remove irrelevant check * Renames and comments * Remove redundant argument from sendGetBlockLocator * Don't delete staging on non-recoverable errors * Renames and comments * Remove redundant code * Commit changes inside ResolveVirtual * Add comment to IsRecoverableError * Remove blocksWithMetaDataGHOSTDAGDataStore * Increase windows pagefile * Move DeleteStagingConsensus outside of defer * Get rid of mustAccepted in receiveBlockWithMetaData * Ban on invalid pruning point * Rename interface_datastructures_daawindowstore.go to interface_datastructures_blocks_with_meta_data_daa_window_store.go * * Change GetVirtualSelectedParentChainFromBlockResponseMessage and VirtualSelectedParentChainChangedNotificationMessage to show only added block hashes * Remove ResolveVirtual * Use externalapi.ConsensusWrapper inside MiningManager * Fix pruningmanager.blockwithmetadata * Set pruning point selected child when importing the pruning point UTXO set * Change virtual genesis hash * replace the selected parent with virtual genesis on removePrunedBlocksFromGHOSTDAGData * Get rid of low hash in block locators * Remove +1 from everywhere we use difficultyAdjustmentWindowSize and increase the default value by one * Add comments about consensus wrapper * Don't use separate staging area when resolving resolveBlockStatus * Fix netsync stability test * Fix checkResolveVirtual * Rename ConsensusWrapper->ConsensusReference * Get rid of blockHeapNode * Add comment to defaultDifficultyAdjustmentWindowSize * Add SelectedChild to DAGTraversalManager * Remove redundant copy * Rename blockWindowHeap->calculateBlockWindowHeap * Move isVirtualGenesisOnlyParent to utils * Change BlockWithMetaData->BlockWithTrustedData * Get rid of maxReasonLength * Split IBD to 100 blocks each time * Fix a bug in calculateBlockWindowHeap * Switch to trusted data when encountering virtual genesis in blockWithTrustedData * Move ConsensusReference to domain * Update ConsensusReference comment * Add comment * Rename shouldNotAddGenesis->skipAddingGenesis
211 lines
7.8 KiB
Go
211 lines
7.8 KiB
Go
package dagtopologymanager
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/domain/consensus/model"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// dagTopologyManager exposes methods for querying relationships
|
|
// between blocks in the DAG
|
|
type dagTopologyManager struct {
|
|
reachabilityManager model.ReachabilityManager
|
|
blockRelationStore model.BlockRelationStore
|
|
ghostdagStore model.GHOSTDAGDataStore
|
|
databaseContext model.DBReader
|
|
}
|
|
|
|
// New instantiates a new DAGTopologyManager
|
|
func New(
|
|
databaseContext model.DBReader,
|
|
reachabilityManager model.ReachabilityManager,
|
|
blockRelationStore model.BlockRelationStore,
|
|
ghostdagStore model.GHOSTDAGDataStore) model.DAGTopologyManager {
|
|
|
|
return &dagTopologyManager{
|
|
databaseContext: databaseContext,
|
|
reachabilityManager: reachabilityManager,
|
|
blockRelationStore: blockRelationStore,
|
|
ghostdagStore: ghostdagStore,
|
|
}
|
|
}
|
|
|
|
// Parents returns the DAG parents of the given blockHash
|
|
func (dtm *dagTopologyManager) Parents(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) ([]*externalapi.DomainHash, error) {
|
|
blockRelations, err := dtm.blockRelationStore.BlockRelation(dtm.databaseContext, stagingArea, blockHash)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return blockRelations.Parents, nil
|
|
}
|
|
|
|
// Children returns the DAG children of the given blockHash
|
|
func (dtm *dagTopologyManager) Children(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) ([]*externalapi.DomainHash, error) {
|
|
blockRelations, err := dtm.blockRelationStore.BlockRelation(dtm.databaseContext, stagingArea, blockHash)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return blockRelations.Children, nil
|
|
}
|
|
|
|
// IsParentOf returns true if blockHashA is a direct DAG parent of blockHashB
|
|
func (dtm *dagTopologyManager) IsParentOf(stagingArea *model.StagingArea, blockHashA *externalapi.DomainHash, blockHashB *externalapi.DomainHash) (bool, error) {
|
|
blockRelations, err := dtm.blockRelationStore.BlockRelation(dtm.databaseContext, stagingArea, blockHashB)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return isHashInSlice(blockHashA, blockRelations.Parents), nil
|
|
}
|
|
|
|
// IsChildOf returns true if blockHashA is a direct DAG child of blockHashB
|
|
func (dtm *dagTopologyManager) IsChildOf(stagingArea *model.StagingArea, blockHashA *externalapi.DomainHash, blockHashB *externalapi.DomainHash) (bool, error) {
|
|
blockRelations, err := dtm.blockRelationStore.BlockRelation(dtm.databaseContext, stagingArea, blockHashB)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return isHashInSlice(blockHashA, blockRelations.Children), nil
|
|
}
|
|
|
|
// IsAncestorOf returns true if blockHashA is a DAG ancestor of blockHashB
|
|
func (dtm *dagTopologyManager) IsAncestorOf(stagingArea *model.StagingArea, blockHashA *externalapi.DomainHash, blockHashB *externalapi.DomainHash) (bool, error) {
|
|
return dtm.reachabilityManager.IsDAGAncestorOf(stagingArea, blockHashA, blockHashB)
|
|
}
|
|
|
|
// IsAncestorOfAny returns true if `blockHash` is an ancestor of at least one of `potentialDescendants`
|
|
func (dtm *dagTopologyManager) IsAncestorOfAny(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash, potentialDescendants []*externalapi.DomainHash) (bool, error) {
|
|
for _, potentialDescendant := range potentialDescendants {
|
|
isAncestorOf, err := dtm.IsAncestorOf(stagingArea, blockHash, potentialDescendant)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
if isAncestorOf {
|
|
return true, nil
|
|
}
|
|
}
|
|
|
|
return false, nil
|
|
}
|
|
|
|
// IsAnyAncestorOf returns true if at least one of `potentialAncestors` is an ancestor of `blockHash`
|
|
func (dtm *dagTopologyManager) IsAnyAncestorOf(stagingArea *model.StagingArea, potentialAncestors []*externalapi.DomainHash, blockHash *externalapi.DomainHash) (bool, error) {
|
|
for _, potentialAncestor := range potentialAncestors {
|
|
isAncestorOf, err := dtm.IsAncestorOf(stagingArea, potentialAncestor, blockHash)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
if isAncestorOf {
|
|
return true, nil
|
|
}
|
|
}
|
|
|
|
return false, nil
|
|
}
|
|
|
|
// IsInSelectedParentChainOf returns true if blockHashA is in the selected parent chain of blockHashB
|
|
func (dtm *dagTopologyManager) IsInSelectedParentChainOf(stagingArea *model.StagingArea, blockHashA *externalapi.DomainHash, blockHashB *externalapi.DomainHash) (bool, error) {
|
|
return dtm.reachabilityManager.IsReachabilityTreeAncestorOf(stagingArea, blockHashA, blockHashB)
|
|
}
|
|
|
|
func isHashInSlice(hash *externalapi.DomainHash, hashes []*externalapi.DomainHash) bool {
|
|
for _, h := range hashes {
|
|
if h.Equal(hash) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (dtm *dagTopologyManager) SetParents(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash, parentHashes []*externalapi.DomainHash) error {
|
|
hasRelations, err := dtm.blockRelationStore.Has(dtm.databaseContext, stagingArea, blockHash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if hasRelations {
|
|
// Go over the block's current relations (if they exist), and remove the block from all its current parents
|
|
// Note: In theory we should also remove the block from all its children, however, in practice no block
|
|
// ever has its relations updated after getting any children, therefore we skip this step
|
|
|
|
currentRelations, err := dtm.blockRelationStore.BlockRelation(dtm.databaseContext, stagingArea, blockHash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, currentParent := range currentRelations.Parents {
|
|
parentRelations, err := dtm.blockRelationStore.BlockRelation(dtm.databaseContext, stagingArea, currentParent)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for i, parentChild := range parentRelations.Children {
|
|
if parentChild.Equal(blockHash) {
|
|
parentRelations.Children = append(parentRelations.Children[:i], parentRelations.Children[i+1:]...)
|
|
dtm.blockRelationStore.StageBlockRelation(stagingArea, currentParent, parentRelations)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Go over all new parents and add block as their child
|
|
for _, parent := range parentHashes {
|
|
parentRelations, err := dtm.blockRelationStore.BlockRelation(dtm.databaseContext, stagingArea, parent)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
isBlockAlreadyInChildren := false
|
|
for _, parentChild := range parentRelations.Children {
|
|
if parentChild.Equal(blockHash) {
|
|
isBlockAlreadyInChildren = true
|
|
break
|
|
}
|
|
}
|
|
if !isBlockAlreadyInChildren {
|
|
parentRelations.Children = append(parentRelations.Children, blockHash)
|
|
dtm.blockRelationStore.StageBlockRelation(stagingArea, parent, parentRelations)
|
|
}
|
|
}
|
|
|
|
// Finally - create the relations for the block itself
|
|
dtm.blockRelationStore.StageBlockRelation(stagingArea, blockHash, &model.BlockRelations{
|
|
Parents: parentHashes,
|
|
Children: []*externalapi.DomainHash{},
|
|
})
|
|
|
|
return nil
|
|
}
|
|
|
|
// ChildInSelectedParentChainOf returns the child of `context` that is in the selected-parent-chain of `highHash`
|
|
func (dtm *dagTopologyManager) ChildInSelectedParentChainOf(stagingArea *model.StagingArea,
|
|
context, highHash *externalapi.DomainHash) (*externalapi.DomainHash, error) {
|
|
|
|
// Virtual doesn't have reachability data, therefore, it should be treated as a special case -
|
|
// use it's selected parent as highHash.
|
|
specifiedHighHash := highHash
|
|
if highHash == model.VirtualBlockHash {
|
|
ghostdagData, err := dtm.ghostdagStore.Get(dtm.databaseContext, stagingArea, highHash, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
selectedParent := ghostdagData.SelectedParent()
|
|
|
|
// In case where `context` is an immediate parent of `highHash`
|
|
if context.Equal(selectedParent) {
|
|
return highHash, nil
|
|
}
|
|
highHash = selectedParent
|
|
}
|
|
|
|
isInSelectedParentChain, err := dtm.IsInSelectedParentChainOf(stagingArea, context, highHash)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !isInSelectedParentChain {
|
|
return nil, errors.Errorf("context(%s) is not in the selected-parent-chain of highHash(%s)",
|
|
context, specifiedHighHash)
|
|
}
|
|
|
|
return dtm.reachabilityManager.FindNextAncestor(stagingArea, highHash, context)
|
|
}
|