mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* [NOD-1006] Make CompactToBig take an out param so that we can reuse the same big.Int in averageTarget. * [NOD-1006] Fix merge errors. * [NOD-1006] Use CompactToBigWithDestination only in averageTarget. * [NOD-1006] Fix refactor errors. * [NOD-1006] Fix refactor errors. * [NOD-1006] Optimize averageTarget with a big.Int pool. * [NOD-1006] Defer releasing bigInts. * [NOD-1006] Use a pool for requiredDifficulty as well. * [NOD-1006] Move the big int pool to utils. * [NOD-1006] Remove unnecessary line.
83 lines
2.1 KiB
Go
83 lines
2.1 KiB
Go
package blockdag
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/util"
|
|
"github.com/kaspanet/kaspad/util/bigintpool"
|
|
"github.com/pkg/errors"
|
|
"math"
|
|
"math/big"
|
|
"sort"
|
|
)
|
|
|
|
type blockWindow []*blockNode
|
|
|
|
// blueBlockWindow returns a blockWindow of the given size that contains the
|
|
// blues in the past of startindNode, sorted by GHOSTDAG order.
|
|
// If the number of blues in the past of startingNode is less then windowSize,
|
|
// the window will be padded by genesis blocks to achieve a size of windowSize.
|
|
func blueBlockWindow(startingNode *blockNode, windowSize uint64) blockWindow {
|
|
window := make(blockWindow, 0, windowSize)
|
|
currentNode := startingNode
|
|
for uint64(len(window)) < windowSize && currentNode.selectedParent != nil {
|
|
if currentNode.selectedParent != nil {
|
|
for _, blue := range currentNode.blues {
|
|
window = append(window, blue)
|
|
if uint64(len(window)) == windowSize {
|
|
break
|
|
}
|
|
}
|
|
currentNode = currentNode.selectedParent
|
|
}
|
|
}
|
|
|
|
if uint64(len(window)) < windowSize {
|
|
genesis := currentNode
|
|
for uint64(len(window)) < windowSize {
|
|
window = append(window, genesis)
|
|
}
|
|
}
|
|
|
|
return window
|
|
}
|
|
|
|
func (window blockWindow) minMaxTimestamps() (min, max int64) {
|
|
min = math.MaxInt64
|
|
max = 0
|
|
for _, node := range window {
|
|
if node.timestamp < min {
|
|
min = node.timestamp
|
|
}
|
|
if node.timestamp > max {
|
|
max = node.timestamp
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (window blockWindow) averageTarget(averageTarget *big.Int) {
|
|
averageTarget.SetInt64(0)
|
|
|
|
target := bigintpool.Acquire(0)
|
|
defer bigintpool.Release(target)
|
|
for _, node := range window {
|
|
util.CompactToBigWithDestination(node.bits, target)
|
|
averageTarget.Add(averageTarget, target)
|
|
}
|
|
|
|
windowLen := bigintpool.Acquire(int64(len(window)))
|
|
defer bigintpool.Release(windowLen)
|
|
averageTarget.Div(averageTarget, windowLen)
|
|
}
|
|
|
|
func (window blockWindow) medianTimestamp() (int64, error) {
|
|
if len(window) == 0 {
|
|
return 0, errors.New("Cannot calculate median timestamp for an empty block window")
|
|
}
|
|
timestamps := make([]int64, len(window))
|
|
for i, node := range window {
|
|
timestamps[i] = node.timestamp
|
|
}
|
|
sort.Sort(timeSorter(timestamps))
|
|
return timestamps[len(timestamps)/2], nil
|
|
}
|