Ori Newman d207888b67
Implement pruned headers node (#1787)
* 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
2021-07-26 12:24:07 +03:00

205 lines
7.6 KiB
Go

package difficultymanager
import (
"math/big"
"time"
"github.com/kaspanet/kaspad/infrastructure/logger"
"github.com/kaspanet/kaspad/util/math"
"github.com/kaspanet/kaspad/util/difficulty"
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
)
// DifficultyManager provides a method to resolve the
// difficulty value of a block
type difficultyManager struct {
databaseContext model.DBReader
ghostdagManager model.GHOSTDAGManager
ghostdagStore model.GHOSTDAGDataStore
headerStore model.BlockHeaderStore
daaBlocksStore model.DAABlocksStore
dagTopologyManager model.DAGTopologyManager
dagTraversalManager model.DAGTraversalManager
genesisHash *externalapi.DomainHash
powMax *big.Int
difficultyAdjustmentWindowSize int
disableDifficultyAdjustment bool
targetTimePerBlock time.Duration
genesisBits uint32
}
// New instantiates a new DifficultyManager
func New(databaseContext model.DBReader,
ghostdagManager model.GHOSTDAGManager,
ghostdagStore model.GHOSTDAGDataStore,
headerStore model.BlockHeaderStore,
daaBlocksStore model.DAABlocksStore,
dagTopologyManager model.DAGTopologyManager,
dagTraversalManager model.DAGTraversalManager,
powMax *big.Int,
difficultyAdjustmentWindowSize int,
disableDifficultyAdjustment bool,
targetTimePerBlock time.Duration,
genesisHash *externalapi.DomainHash,
genesisBits uint32) model.DifficultyManager {
return &difficultyManager{
databaseContext: databaseContext,
ghostdagManager: ghostdagManager,
ghostdagStore: ghostdagStore,
headerStore: headerStore,
daaBlocksStore: daaBlocksStore,
dagTopologyManager: dagTopologyManager,
dagTraversalManager: dagTraversalManager,
powMax: powMax,
difficultyAdjustmentWindowSize: difficultyAdjustmentWindowSize,
disableDifficultyAdjustment: disableDifficultyAdjustment,
targetTimePerBlock: targetTimePerBlock,
genesisHash: genesisHash,
genesisBits: genesisBits,
}
}
// StageDAADataAndReturnRequiredDifficulty calculates the DAA window, stages the DAA score and DAA added
// blocks, and returns the required difficulty for the given block.
// The reason this function both stages DAA data and returns the difficulty is because in order to calculate
// both of them we need to calculate the DAA window, which is a relatively heavy operation, so we reuse the
// block window instead of recalculating it for the two purposes.
// For cases where no staging should happen and the caller only needs to know the difficulty he should
// use RequiredDifficulty.
func (dm *difficultyManager) StageDAADataAndReturnRequiredDifficulty(
stagingArea *model.StagingArea,
blockHash *externalapi.DomainHash,
isBlockWithTrustedData bool) (uint32, error) {
onEnd := logger.LogAndMeasureExecutionTime(log, "StageDAADataAndReturnRequiredDifficulty")
defer onEnd()
targetsWindow, windowHashes, err := dm.blockWindow(stagingArea, blockHash, dm.difficultyAdjustmentWindowSize)
if err != nil {
return 0, err
}
err = dm.stageDAAScoreAndAddedBlocks(stagingArea, blockHash, windowHashes, isBlockWithTrustedData)
if err != nil {
return 0, err
}
return dm.requiredDifficultyFromTargetsWindow(targetsWindow)
}
// RequiredDifficulty returns the difficulty required for some block
func (dm *difficultyManager) RequiredDifficulty(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (uint32, error) {
targetsWindow, _, err := dm.blockWindow(stagingArea, blockHash, dm.difficultyAdjustmentWindowSize)
if err != nil {
return 0, err
}
return dm.requiredDifficultyFromTargetsWindow(targetsWindow)
}
func (dm *difficultyManager) requiredDifficultyFromTargetsWindow(targetsWindow blockWindow) (uint32, error) {
if dm.disableDifficultyAdjustment {
return dm.genesisBits, nil
}
// We need at least 2 blocks to get a timestamp interval
// We could instead clamp the timestamp difference to `targetTimePerBlock`,
// but then everything will cancel out and we'll get the target from the last block, which will be the same as genesis.
if len(targetsWindow) < 2 {
return dm.genesisBits, nil
}
windowMinTimestamp, windowMaxTimeStamp, windowsMinIndex, _ := targetsWindow.minMaxTimestamps()
// Remove the last block from the window so to calculate the average target of dag.difficultyAdjustmentWindowSize blocks
targetsWindow.remove(windowsMinIndex)
// Calculate new target difficulty as:
// averageWindowTarget * (windowMinTimestamp / (targetTimePerBlock * windowSize))
// The result uses integer division which means it will be slightly
// rounded down.
div := new(big.Int)
newTarget := targetsWindow.averageTarget()
newTarget.
// We need to clamp the timestamp difference to 1 so that we'll never get a 0 target.
Mul(newTarget, div.SetInt64(math.MaxInt64(windowMaxTimeStamp-windowMinTimestamp, 1))).
Div(newTarget, div.SetInt64(dm.targetTimePerBlock.Milliseconds())).
Div(newTarget, div.SetUint64(uint64(len(targetsWindow))))
if newTarget.Cmp(dm.powMax) > 0 {
return difficulty.BigToCompact(dm.powMax), nil
}
newTargetBits := difficulty.BigToCompact(newTarget)
return newTargetBits, nil
}
func (dm *difficultyManager) stageDAAScoreAndAddedBlocks(stagingArea *model.StagingArea,
blockHash *externalapi.DomainHash,
windowHashes []*externalapi.DomainHash,
isBlockWithTrustedData bool) error {
onEnd := logger.LogAndMeasureExecutionTime(log, "stageDAAScoreAndAddedBlocks")
defer onEnd()
daaScore, addedBlocks, err := dm.calculateDaaScoreAndAddedBlocks(stagingArea, blockHash, windowHashes, isBlockWithTrustedData)
if err != nil {
return err
}
dm.daaBlocksStore.StageDAAScore(stagingArea, blockHash, daaScore)
dm.daaBlocksStore.StageBlockDAAAddedBlocks(stagingArea, blockHash, addedBlocks)
return nil
}
func (dm *difficultyManager) calculateDaaScoreAndAddedBlocks(stagingArea *model.StagingArea,
blockHash *externalapi.DomainHash,
windowHashes []*externalapi.DomainHash,
isBlockWithTrustedData bool) (uint64, []*externalapi.DomainHash, error) {
if blockHash.Equal(dm.genesisHash) {
return 0, nil, nil
}
ghostdagData, err := dm.ghostdagStore.Get(dm.databaseContext, stagingArea, blockHash, false)
if err != nil {
return 0, nil, err
}
mergeSetLength := len(ghostdagData.MergeSetBlues()) + len(ghostdagData.MergeSetReds())
mergeSet := make(map[externalapi.DomainHash]struct{}, mergeSetLength)
for _, hash := range ghostdagData.MergeSetBlues() {
mergeSet[*hash] = struct{}{}
}
for _, hash := range ghostdagData.MergeSetReds() {
mergeSet[*hash] = struct{}{}
}
// TODO: Consider optimizing by breaking the loop once you arrive to the
// window block with blue work higher than all non-added merge set blocks.
daaAddedBlocks := make([]*externalapi.DomainHash, 0, len(mergeSet))
for _, hash := range windowHashes {
if _, exists := mergeSet[*hash]; exists {
daaAddedBlocks = append(daaAddedBlocks, hash)
if len(daaAddedBlocks) == len(mergeSet) {
break
}
}
}
var daaScore uint64
if isBlockWithTrustedData {
daaScore, err = dm.daaBlocksStore.DAAScore(dm.databaseContext, stagingArea, blockHash)
if err != nil {
return 0, nil, err
}
} else {
selectedParentDAAScore, err := dm.daaBlocksStore.DAAScore(dm.databaseContext, stagingArea, ghostdagData.SelectedParent())
if err != nil {
return 0, nil, err
}
daaScore = selectedParentDAAScore + uint64(len(daaAddedBlocks))
}
return daaScore, daaAddedBlocks, nil
}