2017-01-31 01:32:50 +01:00
|
|
|
// Copyright (c) 2013-2017 The btcsuite developers
|
2013-07-18 16:49:28 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-30 21:54:30 +01:00
|
|
|
package blockchain
|
2013-07-18 16:49:28 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"time"
|
2014-07-02 18:04:59 +02:00
|
|
|
|
2016-08-08 21:04:33 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2013-07-18 16:49:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// bigOne is 1 represented as a big.Int. It is defined here to avoid
|
|
|
|
// the overhead of creating it multiple times.
|
|
|
|
bigOne = big.NewInt(1)
|
|
|
|
|
|
|
|
// oneLsh256 is 1 shifted left 256 bits. It is defined here to avoid
|
|
|
|
// the overhead of creating it multiple times.
|
|
|
|
oneLsh256 = new(big.Int).Lsh(bigOne, 256)
|
|
|
|
)
|
|
|
|
|
2016-08-08 21:04:33 +02:00
|
|
|
// HashToBig converts a chainhash.Hash into a big.Int that can be used to
|
2013-07-18 16:49:28 +02:00
|
|
|
// perform math comparisons.
|
2016-08-08 21:04:33 +02:00
|
|
|
func HashToBig(hash *chainhash.Hash) *big.Int {
|
|
|
|
// A Hash is in little-endian, but the big package wants the bytes in
|
|
|
|
// big-endian, so reverse them.
|
2015-04-04 20:25:49 +02:00
|
|
|
buf := *hash
|
2013-07-18 16:49:28 +02:00
|
|
|
blen := len(buf)
|
|
|
|
for i := 0; i < blen/2; i++ {
|
|
|
|
buf[i], buf[blen-1-i] = buf[blen-1-i], buf[i]
|
|
|
|
}
|
|
|
|
|
2015-04-04 20:25:49 +02:00
|
|
|
return new(big.Int).SetBytes(buf[:])
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CompactToBig converts a compact representation of a whole number N to an
|
|
|
|
// unsigned 32-bit number. The representation is similar to IEEE754 floating
|
|
|
|
// point numbers.
|
|
|
|
//
|
|
|
|
// Like IEEE754 floating point, there are three basic components: the sign,
|
|
|
|
// the exponent, and the mantissa. They are broken out as follows:
|
|
|
|
//
|
2023-05-16 06:05:07 +02:00
|
|
|
// - the most significant 8 bits represent the unsigned base 256 exponent
|
|
|
|
// - bit 23 (the 24th bit) represents the sign bit
|
|
|
|
// - the least significant 23 bits represent the mantissa
|
2013-07-18 16:49:28 +02:00
|
|
|
//
|
|
|
|
// -------------------------------------------------
|
|
|
|
// | Exponent | Sign | Mantissa |
|
|
|
|
// -------------------------------------------------
|
|
|
|
// | 8 bits [31-24] | 1 bit [23] | 23 bits [22-00] |
|
|
|
|
// -------------------------------------------------
|
|
|
|
//
|
|
|
|
// The formula to calculate N is:
|
2023-05-16 06:05:07 +02:00
|
|
|
//
|
|
|
|
// N = (-1^sign) * mantissa * 256^(exponent-3)
|
2013-07-18 16:49:28 +02:00
|
|
|
//
|
|
|
|
// This compact form is only used in bitcoin to encode unsigned 256-bit numbers
|
|
|
|
// which represent difficulty targets, thus there really is not a need for a
|
|
|
|
// sign bit, but it is implemented here to stay consistent with bitcoind.
|
|
|
|
func CompactToBig(compact uint32) *big.Int {
|
|
|
|
// Extract the mantissa, sign bit, and exponent.
|
|
|
|
mantissa := compact & 0x007fffff
|
|
|
|
isNegative := compact&0x00800000 != 0
|
|
|
|
exponent := uint(compact >> 24)
|
|
|
|
|
|
|
|
// Since the base for the exponent is 256, the exponent can be treated
|
|
|
|
// as the number of bytes to represent the full 256-bit number. So,
|
|
|
|
// treat the exponent as the number of bytes and shift the mantissa
|
|
|
|
// right or left accordingly. This is equivalent to:
|
|
|
|
// N = mantissa * 256^(exponent-3)
|
|
|
|
var bn *big.Int
|
|
|
|
if exponent <= 3 {
|
|
|
|
mantissa >>= 8 * (3 - exponent)
|
|
|
|
bn = big.NewInt(int64(mantissa))
|
|
|
|
} else {
|
|
|
|
bn = big.NewInt(int64(mantissa))
|
|
|
|
bn.Lsh(bn, 8*(exponent-3))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make it negative if the sign bit is set.
|
|
|
|
if isNegative {
|
|
|
|
bn = bn.Neg(bn)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bn
|
|
|
|
}
|
|
|
|
|
|
|
|
// BigToCompact converts a whole number N to a compact representation using
|
|
|
|
// an unsigned 32-bit number. The compact representation only provides 23 bits
|
|
|
|
// of precision, so values larger than (2^23 - 1) only encode the most
|
|
|
|
// significant digits of the number. See CompactToBig for details.
|
|
|
|
func BigToCompact(n *big.Int) uint32 {
|
|
|
|
// No need to do any work if it's zero.
|
|
|
|
if n.Sign() == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since the base for the exponent is 256, the exponent can be treated
|
|
|
|
// as the number of bytes. So, shift the number right or left
|
|
|
|
// accordingly. This is equivalent to:
|
|
|
|
// mantissa = mantissa / 256^(exponent-3)
|
|
|
|
var mantissa uint32
|
|
|
|
exponent := uint(len(n.Bytes()))
|
|
|
|
if exponent <= 3 {
|
|
|
|
mantissa = uint32(n.Bits()[0])
|
|
|
|
mantissa <<= 8 * (3 - exponent)
|
|
|
|
} else {
|
|
|
|
// Use a copy to avoid modifying the caller's original number.
|
|
|
|
tn := new(big.Int).Set(n)
|
|
|
|
mantissa = uint32(tn.Rsh(tn, 8*(exponent-3)).Bits()[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the mantissa already has the sign bit set, the number is too
|
|
|
|
// large to fit into the available 23-bits, so divide the number by 256
|
|
|
|
// and increment the exponent accordingly.
|
|
|
|
if mantissa&0x00800000 != 0 {
|
|
|
|
mantissa >>= 8
|
|
|
|
exponent++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pack the exponent, sign bit, and mantissa into an unsigned 32-bit
|
|
|
|
// int and return it.
|
|
|
|
compact := uint32(exponent<<24) | mantissa
|
|
|
|
if n.Sign() < 0 {
|
|
|
|
compact |= 0x00800000
|
|
|
|
}
|
|
|
|
return compact
|
|
|
|
}
|
|
|
|
|
2014-02-07 23:23:11 +01:00
|
|
|
// CalcWork calculates a work value from difficulty bits. Bitcoin increases
|
2013-07-18 16:49:28 +02:00
|
|
|
// the difficulty for generating a block by decreasing the value which the
|
|
|
|
// generated hash must be less than. This difficulty target is stored in each
|
2016-02-25 18:17:12 +01:00
|
|
|
// block header using a compact representation as described in the documentation
|
2013-07-18 16:49:28 +02:00
|
|
|
// for CompactToBig. The main chain is selected by choosing the chain that has
|
|
|
|
// the most proof of work (highest difficulty). Since a lower target difficulty
|
|
|
|
// value equates to higher actual difficulty, the work value which will be
|
|
|
|
// accumulated must be the inverse of the difficulty. Also, in order to avoid
|
2014-02-07 23:23:11 +01:00
|
|
|
// potential division by zero and really small floating point numbers, the
|
|
|
|
// result adds 1 to the denominator and multiplies the numerator by 2^256.
|
|
|
|
func CalcWork(bits uint32) *big.Int {
|
2013-07-31 19:57:54 +02:00
|
|
|
// Return a work value of zero if the passed difficulty bits represent
|
|
|
|
// a negative number. Note this should not happen in practice with valid
|
|
|
|
// blocks, but an invalid block could trigger it.
|
2013-07-18 16:49:28 +02:00
|
|
|
difficultyNum := CompactToBig(bits)
|
2013-07-31 19:57:54 +02:00
|
|
|
if difficultyNum.Sign() <= 0 {
|
|
|
|
return big.NewInt(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// (1 << 256) / (difficultyNum + 1)
|
2013-07-18 16:49:28 +02:00
|
|
|
denominator := new(big.Int).Add(difficultyNum, bigOne)
|
2013-07-31 19:57:54 +02:00
|
|
|
return new(big.Int).Div(oneLsh256, denominator)
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// calcEasiestDifficulty calculates the easiest possible difficulty that a block
|
|
|
|
// can have given starting difficulty bits and a duration. It is mainly used to
|
|
|
|
// verify that claimed proof of work by a block is sane as compared to a
|
|
|
|
// known good checkpoint.
|
2013-07-24 18:37:45 +02:00
|
|
|
func (b *BlockChain) calcEasiestDifficulty(bits uint32, duration time.Duration) uint32 {
|
2013-07-18 16:49:28 +02:00
|
|
|
// Convert types used in the calculations below.
|
2017-01-31 01:32:50 +01:00
|
|
|
durationVal := int64(duration / time.Second)
|
2016-08-10 23:02:23 +02:00
|
|
|
adjustmentFactor := big.NewInt(b.chainParams.RetargetAdjustmentFactor)
|
2013-07-18 16:49:28 +02:00
|
|
|
|
2013-07-24 23:43:39 +02:00
|
|
|
// The test network rules allow minimum difficulty blocks after more
|
|
|
|
// than twice the desired amount of time needed to generate a block has
|
|
|
|
// elapsed.
|
2016-08-10 23:02:23 +02:00
|
|
|
if b.chainParams.ReduceMinDifficulty {
|
2017-01-31 01:32:50 +01:00
|
|
|
reductionTime := int64(b.chainParams.MinDiffReductionTime /
|
|
|
|
time.Second)
|
|
|
|
if durationVal > reductionTime {
|
2015-02-06 06:18:27 +01:00
|
|
|
return b.chainParams.PowLimitBits
|
2013-07-24 23:43:39 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-18 16:49:28 +02:00
|
|
|
|
|
|
|
// Since easier difficulty equates to higher numbers, the easiest
|
|
|
|
// difficulty for a given duration is the largest value possible given
|
|
|
|
// the number of retargets for the duration and starting difficulty
|
|
|
|
// multiplied by the max adjustment factor.
|
|
|
|
newTarget := CompactToBig(bits)
|
2015-02-06 06:18:27 +01:00
|
|
|
for durationVal > 0 && newTarget.Cmp(b.chainParams.PowLimit) < 0 {
|
2013-07-18 16:49:28 +02:00
|
|
|
newTarget.Mul(newTarget, adjustmentFactor)
|
2016-08-10 23:02:23 +02:00
|
|
|
durationVal -= b.maxRetargetTimespan
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Limit new value to the proof of work limit.
|
2015-02-06 06:18:27 +01:00
|
|
|
if newTarget.Cmp(b.chainParams.PowLimit) > 0 {
|
|
|
|
newTarget.Set(b.chainParams.PowLimit)
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return BigToCompact(newTarget)
|
|
|
|
}
|
|
|
|
|
2013-07-24 23:43:39 +02:00
|
|
|
// findPrevTestNetDifficulty returns the difficulty of the previous block which
|
|
|
|
// did not have the special testnet minimum difficulty rule applied.
|
2022-11-28 18:54:47 +01:00
|
|
|
func findPrevTestNetDifficulty(startNode HeaderCtx, c ChainCtx) uint32 {
|
2013-07-24 23:43:39 +02:00
|
|
|
// Search backwards through the chain for the last block without
|
|
|
|
// the special rule applied.
|
|
|
|
iterNode := startNode
|
2022-11-28 18:54:47 +01:00
|
|
|
for iterNode != nil && iterNode.Height()%c.BlocksPerRetarget() != 0 &&
|
|
|
|
iterNode.Bits() == c.ChainParams().PowLimitBits {
|
2014-05-26 17:27:50 +02:00
|
|
|
|
2022-11-28 18:54:47 +01:00
|
|
|
iterNode = iterNode.Parent()
|
2013-07-24 23:43:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the found difficulty or the minimum difficulty if no
|
|
|
|
// appropriate block was found.
|
2022-11-28 18:54:47 +01:00
|
|
|
lastBits := c.ChainParams().PowLimitBits
|
2013-07-24 23:43:39 +02:00
|
|
|
if iterNode != nil {
|
2022-11-28 18:54:47 +01:00
|
|
|
lastBits = iterNode.Bits()
|
2013-07-24 23:43:39 +02:00
|
|
|
}
|
2017-02-03 19:13:53 +01:00
|
|
|
return lastBits
|
2013-07-24 23:43:39 +02:00
|
|
|
}
|
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// calcNextRequiredDifficulty calculates the required difficulty for the block
|
2022-11-28 18:54:47 +01:00
|
|
|
// after the passed previous HeaderCtx based on the difficulty retarget rules.
|
2014-03-02 19:17:36 +01:00
|
|
|
// This function differs from the exported CalcNextRequiredDifficulty in that
|
2022-11-28 18:54:47 +01:00
|
|
|
// the exported version uses the current best chain as the previous HeaderCtx
|
|
|
|
// while this function accepts any block node. This function accepts a ChainCtx
|
|
|
|
// parameter that gives the necessary difficulty context variables.
|
|
|
|
func calcNextRequiredDifficulty(lastNode HeaderCtx, newBlockTime time.Time,
|
|
|
|
c ChainCtx) (uint32, error) {
|
2023-05-23 15:33:55 +02:00
|
|
|
|
|
|
|
// Emulate the same behavior as Bitcoin Core that for regtest there is
|
|
|
|
// no difficulty retargeting.
|
2022-11-28 18:54:47 +01:00
|
|
|
if c.ChainParams().PoWNoRetargeting {
|
|
|
|
return c.ChainParams().PowLimitBits, nil
|
2023-05-23 15:33:55 +02:00
|
|
|
}
|
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// Genesis block.
|
|
|
|
if lastNode == nil {
|
2022-11-28 18:54:47 +01:00
|
|
|
return c.ChainParams().PowLimitBits, nil
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the previous block's difficulty requirements if this block
|
|
|
|
// is not at a difficulty retarget interval.
|
2022-11-28 18:54:47 +01:00
|
|
|
if (lastNode.Height()+1)%c.BlocksPerRetarget() != 0 {
|
2016-08-10 23:02:23 +02:00
|
|
|
// For networks that support it, allow special reduction of the
|
|
|
|
// required difficulty once too much time has elapsed without
|
|
|
|
// mining a block.
|
2022-11-28 18:54:47 +01:00
|
|
|
if c.ChainParams().ReduceMinDifficulty {
|
2016-08-10 23:02:23 +02:00
|
|
|
// Return minimum difficulty when more than the desired
|
|
|
|
// amount of time has elapsed without mining a block.
|
2022-11-28 18:54:47 +01:00
|
|
|
reductionTime := int64(c.ChainParams().MinDiffReductionTime /
|
2017-01-31 01:32:50 +01:00
|
|
|
time.Second)
|
2022-11-28 18:54:47 +01:00
|
|
|
allowMinTime := lastNode.Timestamp() + reductionTime
|
2017-01-31 01:32:50 +01:00
|
|
|
if newBlockTime.Unix() > allowMinTime {
|
2022-11-28 18:54:47 +01:00
|
|
|
return c.ChainParams().PowLimitBits, nil
|
2013-07-24 23:43:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// The block was mined within the desired timeframe, so
|
|
|
|
// return the difficulty for the last block which did
|
|
|
|
// not have the special minimum difficulty rule applied.
|
2022-11-28 18:54:47 +01:00
|
|
|
return findPrevTestNetDifficulty(lastNode, c), nil
|
2014-05-26 17:27:50 +02:00
|
|
|
}
|
2013-07-24 23:43:39 +02:00
|
|
|
|
|
|
|
// For the main network (or any unrecognized networks), simply
|
2014-05-26 17:27:50 +02:00
|
|
|
// return the previous block's difficulty requirements.
|
2022-11-28 18:54:47 +01:00
|
|
|
return lastNode.Bits(), nil
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the block node at the previous retarget (targetTimespan days
|
|
|
|
// worth of blocks).
|
2022-11-28 18:54:47 +01:00
|
|
|
firstNode := lastNode.RelativeAncestorCtx(c.BlocksPerRetarget() - 1)
|
2013-07-18 16:49:28 +02:00
|
|
|
if firstNode == nil {
|
2015-08-26 06:03:18 +02:00
|
|
|
return 0, AssertError("unable to obtain previous retarget block")
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Limit the amount of adjustment that can occur to the previous
|
|
|
|
// difficulty.
|
2022-11-28 18:54:47 +01:00
|
|
|
actualTimespan := lastNode.Timestamp() - firstNode.Timestamp()
|
2013-07-18 16:49:28 +02:00
|
|
|
adjustedTimespan := actualTimespan
|
2022-11-28 18:54:47 +01:00
|
|
|
if actualTimespan < c.MinRetargetTimespan() {
|
|
|
|
adjustedTimespan = c.MinRetargetTimespan()
|
|
|
|
} else if actualTimespan > c.MaxRetargetTimespan() {
|
|
|
|
adjustedTimespan = c.MaxRetargetTimespan()
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate new target difficulty as:
|
|
|
|
// currentDifficulty * (adjustedTimespan / targetTimespan)
|
|
|
|
// The result uses integer division which means it will be slightly
|
|
|
|
// rounded down. Bitcoind also uses integer division to calculate this
|
|
|
|
// result.
|
2022-11-28 18:54:47 +01:00
|
|
|
oldTarget := CompactToBig(lastNode.Bits())
|
2013-07-18 16:49:28 +02:00
|
|
|
newTarget := new(big.Int).Mul(oldTarget, big.NewInt(adjustedTimespan))
|
2022-11-28 18:54:47 +01:00
|
|
|
targetTimeSpan := int64(c.ChainParams().TargetTimespan / time.Second)
|
2017-01-31 01:32:50 +01:00
|
|
|
newTarget.Div(newTarget, big.NewInt(targetTimeSpan))
|
2013-07-18 16:49:28 +02:00
|
|
|
|
|
|
|
// Limit new value to the proof of work limit.
|
2022-11-28 18:54:47 +01:00
|
|
|
if newTarget.Cmp(c.ChainParams().PowLimit) > 0 {
|
|
|
|
newTarget.Set(c.ChainParams().PowLimit)
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Log new target difficulty and return it. The new target logging is
|
|
|
|
// intentionally converting the bits back to a number instead of using
|
|
|
|
// newTarget since conversion to the compact representation loses
|
|
|
|
// precision.
|
|
|
|
newTargetBits := BigToCompact(newTarget)
|
2022-11-28 18:54:47 +01:00
|
|
|
log.Debugf("Difficulty retarget at block height %d", lastNode.Height()+1)
|
|
|
|
log.Debugf("Old target %08x (%064x)", lastNode.Bits(), oldTarget)
|
2013-07-18 16:49:28 +02:00
|
|
|
log.Debugf("New target %08x (%064x)", newTargetBits, CompactToBig(newTargetBits))
|
|
|
|
log.Debugf("Actual timespan %v, adjusted timespan %v, target timespan %v",
|
2017-01-31 01:32:50 +01:00
|
|
|
time.Duration(actualTimespan)*time.Second,
|
|
|
|
time.Duration(adjustedTimespan)*time.Second,
|
2022-11-28 18:54:47 +01:00
|
|
|
c.ChainParams().TargetTimespan)
|
2013-07-18 16:49:28 +02:00
|
|
|
|
|
|
|
return newTargetBits, nil
|
|
|
|
}
|
2014-03-02 19:17:36 +01:00
|
|
|
|
|
|
|
// CalcNextRequiredDifficulty calculates the required difficulty for the block
|
|
|
|
// after the end of the current best chain based on the difficulty retarget
|
|
|
|
// rules.
|
|
|
|
//
|
2015-08-26 06:03:18 +02:00
|
|
|
// This function is safe for concurrent access.
|
2014-03-02 19:17:36 +01:00
|
|
|
func (b *BlockChain) CalcNextRequiredDifficulty(timestamp time.Time) (uint32, error) {
|
2015-08-26 06:03:18 +02:00
|
|
|
b.chainLock.Lock()
|
2022-11-28 18:54:47 +01:00
|
|
|
difficulty, err := calcNextRequiredDifficulty(b.bestChain.Tip(), timestamp, b)
|
2015-08-26 06:03:18 +02:00
|
|
|
b.chainLock.Unlock()
|
|
|
|
return difficulty, err
|
2014-03-02 19:17:36 +01:00
|
|
|
}
|