mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-24 07:46:45 +00:00

* Get rid of insertMode * Rename AddBlockToVirtual->AddBlock * When F is not in the future of P, enforce finality with P and not with F. * Don't allow blocks with invalid parents or with missing block body * Check finality violation before checking block status * Implement CalculateIndependentPruningPoint * Move checkBlockStatus to validateBlock * Add ValidateBlock to block processor interface * Adjust SetPruningPoint to the new IBD flow * Add pruning store to CSM's constructor * Flip wrong condition on AddHeaderTip * Fix func (hts *headerSelectedTipStore) Has * Fix block stage order * Call to ValidateBodyInContext from validatePostProofOfWork * Enable overrideDAGParams * Update log * Rename SetPruningPoint to ValidateAndInsertPruningPoint and move most of its logic inside block processor * Rename hasValidatedHeader->hasValidatedOnlyHeader * Fix typo * Name return values for fetchMissingUTXOSet * Add comment * Return ErrMissingParents when block body is missing * Add logs and comments * Fix merge error * Fix pruning point calculation to be by virtual selected parent * Replace CalculateIndependentPruningPoint to CalculatePruningPointByHeaderSelectedTip * Fix isAwaitingUTXOSet to check pruning point by headers * Change isAwaitingUTXOSet indication * Remove IsBlockInHeaderPruningPointFuture from BlockInfo * Fix LowestChainBlockAboveOrEqualToBlueScore * Add validateNewPruningPointTransactions * Add validateNewPruningAgainstPastUTXO * Rename set_pruning_utxo_set.go to update_pruning_utxo_set.go * Check missing block body hashes by missing block instead of status * Validate pruning point against past UTXO with the pruning point as block hash * Remove virtualHeaderHash * Fix comment * Fix imports
126 lines
4.1 KiB
Go
126 lines
4.1 KiB
Go
package dagtraversalmanager
|
|
|
|
import (
|
|
"fmt"
|
|
"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
|
|
ghostdagDataStore model.GHOSTDAGDataStore
|
|
ghostdagManager model.GHOSTDAGManager
|
|
}
|
|
|
|
// selectedParentIterator implements the `model.BlockIterator` API
|
|
type selectedParentIterator struct {
|
|
databaseContext model.DBReader
|
|
ghostdagDataStore model.GHOSTDAGDataStore
|
|
current *externalapi.DomainHash
|
|
}
|
|
|
|
func (spi *selectedParentIterator) Next() bool {
|
|
if spi.current == nil {
|
|
return false
|
|
}
|
|
ghostdagData, err := spi.ghostdagDataStore.Get(spi.databaseContext, spi.current)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("ghostdagDataStore is missing ghostdagData for: %v. '%s' ", spi.current, err))
|
|
}
|
|
spi.current = ghostdagData.SelectedParent()
|
|
return spi.current != nil
|
|
}
|
|
|
|
func (spi *selectedParentIterator) Get() *externalapi.DomainHash {
|
|
return spi.current
|
|
}
|
|
|
|
// New instantiates a new DAGTraversalManager
|
|
func New(
|
|
databaseContext model.DBReader,
|
|
dagTopologyManager model.DAGTopologyManager,
|
|
ghostdagDataStore model.GHOSTDAGDataStore,
|
|
ghostdagManager model.GHOSTDAGManager) model.DAGTraversalManager {
|
|
return &dagTraversalManager{
|
|
databaseContext: databaseContext,
|
|
dagTopologyManager: dagTopologyManager,
|
|
ghostdagDataStore: ghostdagDataStore,
|
|
ghostdagManager: ghostdagManager,
|
|
}
|
|
}
|
|
|
|
// SelectedParentIterator creates an iterator over the selected
|
|
// parent chain of the given highHash
|
|
func (dtm *dagTraversalManager) SelectedParentIterator(highHash *externalapi.DomainHash) model.BlockIterator {
|
|
return &selectedParentIterator{
|
|
databaseContext: dtm.databaseContext,
|
|
ghostdagDataStore: dtm.ghostdagDataStore,
|
|
current: highHash,
|
|
}
|
|
}
|
|
|
|
// 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(highHash *externalapi.DomainHash, depth uint64) (*externalapi.DomainHash, error) {
|
|
currentBlockHash := highHash
|
|
highBlockGHOSTDAGData, err := dtm.ghostdagDataStore.Get(dtm.databaseContext, highHash)
|
|
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, currentBlockHash)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return currentBlockHash, nil
|
|
}
|
|
|
|
func (dtm *dagTraversalManager) LowestChainBlockAboveOrEqualToBlueScore(highHash *externalapi.DomainHash, blueScore uint64) (*externalapi.DomainHash, error) {
|
|
highBlockGHOSTDAGData, err := dtm.ghostdagDataStore.Get(dtm.databaseContext, highHash)
|
|
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
|
|
iterator := dtm.SelectedParentIterator(highHash)
|
|
for iterator.Next() {
|
|
selectedParentBlockGHOSTDAGData, err := dtm.ghostdagDataStore.Get(dtm.databaseContext, currentBlockGHOSTDAGData.SelectedParent())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if selectedParentBlockGHOSTDAGData.BlueScore() < blueScore {
|
|
break
|
|
}
|
|
currentHash = currentBlockGHOSTDAGData.SelectedParent()
|
|
currentBlockGHOSTDAGData = selectedParentBlockGHOSTDAGData
|
|
}
|
|
|
|
return currentHash, nil
|
|
}
|