mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-09-14 05:20:11 +00:00

* Add StagingArea struct * Implemented staging areas in blockStore * Move blockStagingShard to separate folder * Apply staging shard to acceptanceDataStore * Update blockHeaderStore with StagingArea * Add StagingArea to BlockRelationStore * Add StagingArea to blockStatusStore * Add StagingArea to consensusStateStore * Add StagingArea to daaBlocksStore * Add StagingArea to finalityStore * Add StagingArea to ghostdagDataStore * Add StagingArea to headersSelectedChainStore and headersSelectedTipStore * Add StagingArea to multisetStore * Add StagingArea to pruningStore * Add StagingArea to reachabilityDataStore * Add StagingArea to utxoDiffStore * Fix forgotten compilation error * Update reachability manager and some more things with StagingArea * Add StagingArea to dagTopologyManager, and some more * Add StagingArea to GHOSTDAGManager, and some more * Add StagingArea to difficultyManager, and some more * Add StagingArea to dagTraversalManager, and some more * Add StagingArea to headerTipsManager, and some more * Add StagingArea to constnsusStateManager, pastMedianTimeManager * Add StagingArea to transactionValidator * Add StagingArea to finalityManager * Add StagingArea to mergeDepthManager * Add StagingArea to pruningManager * Add StagingArea to rest of ValidateAndInsertBlock * Add StagingArea to blockValidator * Add StagingArea to coinbaseManager * Add StagingArea to syncManager * Add StagingArea to blockBuilder * Update consensus with StagingArea * Add StagingArea to ghostdag2 * Fix remaining compilation errors * Update names of stagingShards * Fix forgotten stagingArea passing * Mark stagingShard.isCommited = true once commited * Move isStaged to stagingShard, so that it's available without going through store * Make blockHeaderStore count be avilable from stagingShard * Fix remaining forgotten stagingArea passing * commitAllChanges should call dbTx.Commit in the end * Fix all tests tests in blockValidator * Fix all tests in consensusStateManager and some more * Fix all tests in pruningManager * Add many missing stagingAreas in tests * Fix many tests * Fix most of all other tests * Fix ghostdag_test.go * Add comment to StagingArea * Make list of StagingShards an array * Add comment to StagingShardID * Make sure all staging shards are pointer-receiver * Undo bucket rename in block_store * Typo: isCommited -> isCommitted * Add comment explaining why stagingArea.shards is an array
88 lines
2.4 KiB
Go
88 lines
2.4 KiB
Go
package difficultymanager
|
|
|
|
import (
|
|
"math"
|
|
"math/big"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/model"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/util/difficulty"
|
|
)
|
|
|
|
type difficultyBlock struct {
|
|
timeInMilliseconds int64
|
|
Bits uint32
|
|
}
|
|
|
|
type blockWindow []difficultyBlock
|
|
|
|
func (dm *difficultyManager) getDifficultyBlock(
|
|
stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (difficultyBlock, error) {
|
|
|
|
header, err := dm.headerStore.BlockHeader(dm.databaseContext, stagingArea, blockHash)
|
|
if err != nil {
|
|
return difficultyBlock{}, err
|
|
}
|
|
return difficultyBlock{
|
|
timeInMilliseconds: header.TimeInMilliseconds(),
|
|
Bits: header.Bits(),
|
|
}, nil
|
|
}
|
|
|
|
// blockWindow returns a blockWindow of the given size that contains the
|
|
// blocks in the past of startindNode, the sorting is unspecified.
|
|
// If the number of blocks in the past of startingNode is less then windowSize,
|
|
// the window will be padded by genesis blocks to achieve a size of windowSize.
|
|
func (dm *difficultyManager) blockWindow(stagingArea *model.StagingArea, startingNode *externalapi.DomainHash, windowSize int) (blockWindow,
|
|
[]*externalapi.DomainHash, error) {
|
|
|
|
window := make(blockWindow, 0, windowSize)
|
|
windowHashes, err := dm.dagTraversalManager.BlockWindow(stagingArea, startingNode, windowSize)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
for _, hash := range windowHashes {
|
|
block, err := dm.getDifficultyBlock(stagingArea, hash)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
window = append(window, block)
|
|
}
|
|
return window, windowHashes, nil
|
|
}
|
|
|
|
func (window blockWindow) minMaxTimestamps() (min, max int64, minIndex, maxIndex int) {
|
|
min = math.MaxInt64
|
|
minIndex = math.MaxInt64
|
|
max = 0
|
|
maxIndex = 0
|
|
for i, block := range window {
|
|
if block.timeInMilliseconds < min {
|
|
min = block.timeInMilliseconds
|
|
minIndex = i
|
|
}
|
|
if block.timeInMilliseconds > max {
|
|
max = block.timeInMilliseconds
|
|
maxIndex = i
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (window *blockWindow) remove(n int) {
|
|
(*window)[n] = (*window)[len(*window)-1]
|
|
*window = (*window)[:len(*window)-1]
|
|
}
|
|
|
|
func (window blockWindow) averageTarget() *big.Int {
|
|
averageTarget := new(big.Int)
|
|
targetTmp := new(big.Int)
|
|
for _, block := range window {
|
|
difficulty.CompactToBigWithDestination(block.Bits, targetTmp)
|
|
averageTarget.Add(averageTarget, targetTmp)
|
|
}
|
|
return averageTarget.Div(averageTarget, big.NewInt(int64(len(window))))
|
|
}
|