mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-11-29 00:23: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
157 lines
5.4 KiB
Go
157 lines
5.4 KiB
Go
package dagtraversalmanager
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/domain/consensus/model"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// dagTraversalManager exposes methods for travering blocks
|
|
// in the DAG
|
|
type dagTraversalManager struct {
|
|
databaseContext model.DBReader
|
|
|
|
dagTopologyManager model.DAGTopologyManager
|
|
ghostdagManager model.GHOSTDAGManager
|
|
ghostdagDataStore model.GHOSTDAGDataStore
|
|
reachabilityDataStore model.ReachabilityDataStore
|
|
consensusStateStore model.ConsensusStateStore
|
|
daaWindowStore model.BlocksWithTrustedDataDAAWindowStore
|
|
genesisHash *externalapi.DomainHash
|
|
}
|
|
|
|
// New instantiates a new DAGTraversalManager
|
|
func New(
|
|
databaseContext model.DBReader,
|
|
dagTopologyManager model.DAGTopologyManager,
|
|
ghostdagDataStore model.GHOSTDAGDataStore,
|
|
reachabilityDataStore model.ReachabilityDataStore,
|
|
ghostdagManager model.GHOSTDAGManager,
|
|
conssensusStateStore model.ConsensusStateStore,
|
|
daaWindowStore model.BlocksWithTrustedDataDAAWindowStore,
|
|
genesisHash *externalapi.DomainHash) model.DAGTraversalManager {
|
|
return &dagTraversalManager{
|
|
databaseContext: databaseContext,
|
|
dagTopologyManager: dagTopologyManager,
|
|
ghostdagDataStore: ghostdagDataStore,
|
|
reachabilityDataStore: reachabilityDataStore,
|
|
ghostdagManager: ghostdagManager,
|
|
consensusStateStore: conssensusStateStore,
|
|
daaWindowStore: daaWindowStore,
|
|
|
|
genesisHash: genesisHash,
|
|
}
|
|
}
|
|
|
|
// BlockAtDepth returns the hash of the highest block with a blue score
|
|
// lower than (highHash.blueSore - depth) in the selected-parent-chain
|
|
// of the block with the given highHash's selected parent chain.
|
|
func (dtm *dagTraversalManager) BlockAtDepth(stagingArea *model.StagingArea, highHash *externalapi.DomainHash, depth uint64) (*externalapi.DomainHash, error) {
|
|
currentBlockHash := highHash
|
|
highBlockGHOSTDAGData, err := dtm.ghostdagDataStore.Get(dtm.databaseContext, stagingArea, highHash, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
requiredBlueScore := uint64(0)
|
|
if highBlockGHOSTDAGData.BlueScore() > depth {
|
|
requiredBlueScore = highBlockGHOSTDAGData.BlueScore() - depth
|
|
}
|
|
|
|
currentBlockGHOSTDAGData := highBlockGHOSTDAGData
|
|
// If we used `BlockIterator` we'd need to do more calls to `ghostdagDataStore` so we can get the blueScore
|
|
for currentBlockGHOSTDAGData.BlueScore() >= requiredBlueScore {
|
|
if currentBlockGHOSTDAGData.SelectedParent() == nil { // genesis
|
|
return currentBlockHash, nil
|
|
}
|
|
currentBlockHash = currentBlockGHOSTDAGData.SelectedParent()
|
|
currentBlockGHOSTDAGData, err = dtm.ghostdagDataStore.Get(dtm.databaseContext, stagingArea, currentBlockHash, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return currentBlockHash, nil
|
|
}
|
|
|
|
func (dtm *dagTraversalManager) LowestChainBlockAboveOrEqualToBlueScore(stagingArea *model.StagingArea, highHash *externalapi.DomainHash, blueScore uint64) (*externalapi.DomainHash, error) {
|
|
highBlockGHOSTDAGData, err := dtm.ghostdagDataStore.Get(dtm.databaseContext, stagingArea, highHash, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if highBlockGHOSTDAGData.BlueScore() < blueScore {
|
|
return nil, errors.Errorf("the given blue score %d is higher than block %s blue score of %d",
|
|
blueScore, highHash, highBlockGHOSTDAGData.BlueScore())
|
|
}
|
|
|
|
currentHash := highHash
|
|
currentBlockGHOSTDAGData := highBlockGHOSTDAGData
|
|
|
|
for !currentHash.Equal(dtm.genesisHash) {
|
|
selectedParentBlockGHOSTDAGData, err := dtm.ghostdagDataStore.Get(dtm.databaseContext, stagingArea,
|
|
currentBlockGHOSTDAGData.SelectedParent(), false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if selectedParentBlockGHOSTDAGData.BlueScore() < blueScore {
|
|
break
|
|
}
|
|
currentHash = currentBlockGHOSTDAGData.SelectedParent()
|
|
currentBlockGHOSTDAGData = selectedParentBlockGHOSTDAGData
|
|
}
|
|
|
|
return currentHash, nil
|
|
}
|
|
|
|
func (dtm *dagTraversalManager) CalculateChainPath(stagingArea *model.StagingArea,
|
|
fromBlockHash, toBlockHash *externalapi.DomainHash) (*externalapi.SelectedChainPath, error) {
|
|
|
|
// Walk down from fromBlockHash until we reach the common selected
|
|
// parent chain ancestor of fromBlockHash and toBlockHash. Note
|
|
// that this slice will be empty if fromBlockHash is the selected
|
|
// parent of toBlockHash
|
|
var removed []*externalapi.DomainHash
|
|
current := fromBlockHash
|
|
for {
|
|
isCurrentInTheSelectedParentChainOfNewVirtualSelectedParent, err :=
|
|
dtm.dagTopologyManager.IsInSelectedParentChainOf(stagingArea, current, toBlockHash)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if isCurrentInTheSelectedParentChainOfNewVirtualSelectedParent {
|
|
break
|
|
}
|
|
removed = append(removed, current)
|
|
|
|
currentGHOSTDAGData, err := dtm.ghostdagDataStore.Get(dtm.databaseContext, stagingArea, current, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
current = currentGHOSTDAGData.SelectedParent()
|
|
}
|
|
commonAncestor := current
|
|
|
|
// Walk down from the toBlockHash to the common ancestor
|
|
var added []*externalapi.DomainHash
|
|
current = toBlockHash
|
|
for !current.Equal(commonAncestor) {
|
|
added = append(added, current)
|
|
currentGHOSTDAGData, err := dtm.ghostdagDataStore.Get(dtm.databaseContext, stagingArea, current, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
current = currentGHOSTDAGData.SelectedParent()
|
|
}
|
|
|
|
// Reverse the order of `added` so that it's sorted from low hash to high hash
|
|
for i, j := 0, len(added)-1; i < j; i, j = i+1, j-1 {
|
|
added[i], added[j] = added[j], added[i]
|
|
}
|
|
|
|
return &externalapi.SelectedChainPath{
|
|
Added: added,
|
|
Removed: removed,
|
|
}, nil
|
|
}
|