Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
daa.cpp
Go to the documentation of this file.
1// Copyright (c) 2017-2020 The Bitcoin developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <pow/daa.h>
6
7#include <arith_uint256.h>
8#include <chain.h>
9#include <consensus/params.h>
10
17 const Consensus::Params &params) {
18 assert(pindexLast->nHeight > pindexFirst->nHeight);
19
25 arith_uint256 work = pindexLast->nChainWork - pindexFirst->nChainWork;
26 work *= params.nPowTargetSpacing;
27
28 // In order to avoid difficulty cliffs, we bound the amplitude of the
29 // adjustment we are going to do to a factor in [0.5, 2].
31 int64_t(pindexLast->nTime) - int64_t(pindexFirst->nTime);
32 if (nActualTimespan > 288 * params.nPowTargetSpacing) {
34 } else if (nActualTimespan < 72 * params.nPowTargetSpacing) {
36 }
37
39
45 return (-work) / work;
46}
47
52static const CBlockIndex *GetSuitableBlock(const CBlockIndex *pindex) {
53 assert(pindex->nHeight >= 3);
54
60 const CBlockIndex *blocks[3];
61 blocks[2] = pindex;
62 blocks[1] = pindex->pprev;
63 blocks[0] = blocks[1]->pprev;
64
65 // Sorting network.
66 if (blocks[0]->nTime > blocks[2]->nTime) {
67 std::swap(blocks[0], blocks[2]);
68 }
69
70 if (blocks[0]->nTime > blocks[1]->nTime) {
71 std::swap(blocks[0], blocks[1]);
72 }
73
74 if (blocks[1]->nTime > blocks[2]->nTime) {
75 std::swap(blocks[1], blocks[2]);
76 }
77
78 // We should have our candidate in the middle now.
79 return blocks[1];
80}
81
92 const CBlockHeader *pblock,
93 const Consensus::Params &params) {
94 // This cannot handle the genesis block and early blocks in general.
96
97 // Special difficulty rule for testnet:
98 // If the new block's timestamp is more than 2* 10 minutes then allow
99 // mining of a min-difficulty block.
100 if (params.fPowAllowMinDifficultyBlocks &&
101 (pblock->GetBlockTime() >
102 pindexPrev->GetBlockTime() + 2 * params.nPowTargetSpacing)) {
103 return UintToArith256(params.powLimit).GetCompact();
104 }
105
106 // Compute the difficulty based on the full adjustment interval.
107 const uint32_t nHeight = pindexPrev->nHeight;
109
110 // Get the last suitable block of the difficulty interval.
113
114 // Get the first suitable block of the difficulty interval.
115 uint32_t nHeightFirst = nHeight - 144;
116 const CBlockIndex *pindexFirst =
117 GetSuitableBlock(pindexPrev->GetAncestor(nHeightFirst));
119
120 // Compute the target based on time and work done during the interval.
123
124 const arith_uint256 powLimit = UintToArith256(params.powLimit);
125 if (nextTarget > powLimit) {
126 return powLimit.GetCompact();
127 }
128
129 return nextTarget.GetCompact();
130}
arith_uint256 UintToArith256(const uint256 &a)
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition block.h:23
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition blockindex.h:25
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition blockindex.h:32
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition blockindex.h:38
256-bit unsigned big integer.
uint32_t GetCompact(bool fNegative=false) const
static arith_uint256 ComputeTarget(const CBlockIndex *pindexFirst, const CBlockIndex *pindexLast, const Consensus::Params &params)
Compute a target based on the work done between 2 blocks and the time required to produce that work.
Definition daa.cpp:15
uint32_t GetNextDAAWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const Consensus::Params &params)
Compute the next required proof of work using a weighted average of the estimated hashrate per block.
Definition daa.cpp:91
static const CBlockIndex * GetSuitableBlock(const CBlockIndex *pindex)
To reduce the impact of timestamp manipulation, we select the block we are basing our computation on ...
Definition daa.cpp:52
unsigned int nHeight
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
Parameters that influence chain consensus.
Definition params.h:34
int64_t DifficultyAdjustmentInterval() const
Definition params.h:85
uint256 powLimit
Proof of work parameters.
Definition params.h:76
int64_t nPowTargetSpacing
Definition params.h:80
bool fPowAllowMinDifficultyBlocks
Definition params.h:77
assert(!tx.IsCoinBase())