]>
Commit | Line | Data |
---|---|---|
df852d2b | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
78253fcb | 3 | // Distributed under the MIT software license, see the accompanying |
df852d2b | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | ||
6 | #include "pow.h" | |
7 | ||
734f85c4 | 8 | #include "arith_uint256.h" |
22c4272b | 9 | #include "chain.h" |
fdda3c50 JG |
10 | #include "chainparams.h" |
11 | #include "crypto/equihash.h" | |
d2270111 | 12 | #include "primitives/block.h" |
fdda3c50 | 13 | #include "streams.h" |
df852d2b | 14 | #include "uint256.h" |
ad49c256 | 15 | #include "util.h" |
df852d2b | 16 | |
fdda3c50 JG |
17 | #include "sodium.h" |
18 | ||
d698ef69 | 19 | unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params) |
df852d2b | 20 | { |
fd311996 | 21 | unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact(); |
df852d2b | 22 | |
23 | // Genesis block | |
24 | if (pindexLast == NULL) | |
25 | return nProofOfWorkLimit; | |
26 | ||
f2c48e15 JG |
27 | // Find the first block in the averaging interval |
28 | const CBlockIndex* pindexFirst = pindexLast; | |
7b173bd8 | 29 | arith_uint256 bnTot {0}; |
f2c48e15 | 30 | for (int i = 0; pindexFirst && i < params.nPowAveragingWindow; i++) { |
7b173bd8 JG |
31 | arith_uint256 bnTmp; |
32 | bnTmp.SetCompact(pindexFirst->nBits); | |
33 | bnTot += bnTmp; | |
f2c48e15 JG |
34 | pindexFirst = pindexFirst->pprev; |
35 | } | |
36 | ||
37 | // Check we have enough blocks | |
38 | if (pindexFirst == NULL) | |
39 | return nProofOfWorkLimit; | |
df852d2b | 40 | |
7b173bd8 JG |
41 | arith_uint256 bnAvg {bnTot / params.nPowAveragingWindow}; |
42 | ||
29842505 | 43 | return CalculateNextWorkRequired(bnAvg, pindexLast->GetMedianTimePast(), pindexFirst->GetMedianTimePast(), params); |
34e5015c RN |
44 | } |
45 | ||
29842505 JG |
46 | unsigned int CalculateNextWorkRequired(arith_uint256 bnAvg, |
47 | int64_t nLastBlockTime, int64_t nFirstBlockTime, | |
48 | const Consensus::Params& params) | |
34e5015c | 49 | { |
df852d2b | 50 | // Limit adjustment step |
f2c48e15 | 51 | // Use medians to prevent time-warp attacks |
e99731b4 | 52 | int64_t nActualTimespan = nLastBlockTime - nFirstBlockTime; |
f2c48e15 JG |
53 | LogPrint("pow", " nActualTimespan = %d before dampening\n", nActualTimespan); |
54 | nActualTimespan = params.AveragingWindowTimespan() + (nActualTimespan - params.AveragingWindowTimespan())/4; | |
55 | LogPrint("pow", " nActualTimespan = %d before bounds\n", nActualTimespan); | |
56 | ||
57 | if (nActualTimespan < params.MinActualTimespan()) | |
58 | nActualTimespan = params.MinActualTimespan(); | |
59 | if (nActualTimespan > params.MaxActualTimespan()) | |
60 | nActualTimespan = params.MaxActualTimespan(); | |
df852d2b | 61 | |
62 | // Retarget | |
fd311996 | 63 | const arith_uint256 bnPowLimit = UintToArith256(params.powLimit); |
29842505 | 64 | arith_uint256 bnNew {bnAvg}; |
f2c48e15 | 65 | bnNew /= params.AveragingWindowTimespan(); |
aa86873a | 66 | bnNew *= nActualTimespan; |
df852d2b | 67 | |
fd311996 CF |
68 | if (bnNew > bnPowLimit) |
69 | bnNew = bnPowLimit; | |
df852d2b | 70 | |
71 | /// debug print | |
f2c48e15 JG |
72 | LogPrint("pow", "GetNextWorkRequired RETARGET\n"); |
73 | LogPrint("pow", "params.AveragingWindowTimespan() = %d nActualTimespan = %d\n", params.AveragingWindowTimespan(), nActualTimespan); | |
29842505 | 74 | LogPrint("pow", "Current average: %08x %s\n", bnAvg.GetCompact(), bnAvg.ToString()); |
f2c48e15 | 75 | LogPrint("pow", "After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString()); |
df852d2b | 76 | |
77 | return bnNew.GetCompact(); | |
78 | } | |
79 | ||
fdda3c50 JG |
80 | bool CheckEquihashSolution(const CBlockHeader *pblock, const CChainParams& params) |
81 | { | |
e9574728 JG |
82 | unsigned int n = params.EquihashN(); |
83 | unsigned int k = params.EquihashK(); | |
fdda3c50 JG |
84 | |
85 | // Hash state | |
86 | crypto_generichash_blake2b_state state; | |
e9574728 | 87 | EhInitialiseState(n, k, state); |
fdda3c50 JG |
88 | |
89 | // I = the block header minus nonce and solution. | |
90 | CEquihashInput I{*pblock}; | |
91 | // I||V | |
92 | CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); | |
93 | ss << I; | |
94 | ss << pblock->nNonce; | |
95 | ||
96 | // H(I||V||... | |
97 | crypto_generichash_blake2b_update(&state, (unsigned char*)&ss[0], ss.size()); | |
98 | ||
e9574728 JG |
99 | bool isValid; |
100 | EhIsValidSolution(n, k, state, pblock->nSolution, isValid); | |
101 | if (!isValid) | |
fdda3c50 JG |
102 | return error("CheckEquihashSolution(): invalid solution"); |
103 | ||
104 | return true; | |
105 | } | |
106 | ||
d698ef69 | 107 | bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params) |
df852d2b | 108 | { |
109 | bool fNegative; | |
110 | bool fOverflow; | |
734f85c4 | 111 | arith_uint256 bnTarget; |
f0fd00cb | 112 | |
df852d2b | 113 | bnTarget.SetCompact(nBits, &fNegative, &fOverflow); |
114 | ||
115 | // Check range | |
fd311996 | 116 | if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit)) |
5262fde0 | 117 | return error("CheckProofOfWork(): nBits below minimum work"); |
df852d2b | 118 | |
119 | // Check proof of work matches claimed amount | |
734f85c4 | 120 | if (UintToArith256(hash) > bnTarget) |
5262fde0 | 121 | return error("CheckProofOfWork(): hash doesn't match nBits"); |
df852d2b | 122 | |
123 | return true; | |
124 | } | |
125 | ||
734f85c4 | 126 | arith_uint256 GetBlockProof(const CBlockIndex& block) |
b343c1a1 | 127 | { |
734f85c4 | 128 | arith_uint256 bnTarget; |
b343c1a1 | 129 | bool fNegative; |
130 | bool fOverflow; | |
092b58d1 | 131 | bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow); |
b343c1a1 | 132 | if (fNegative || fOverflow || bnTarget == 0) |
133 | return 0; | |
134 | // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256 | |
734f85c4 | 135 | // as it's too large for a arith_uint256. However, as 2**256 is at least as large |
b343c1a1 | 136 | // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1, |
137 | // or ~bnTarget / (nTarget+1) + 1. | |
138 | return (~bnTarget / (bnTarget + 1)) + 1; | |
df852d2b | 139 | } |
f7303f97 PW |
140 | |
141 | int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params) | |
142 | { | |
143 | arith_uint256 r; | |
144 | int sign = 1; | |
145 | if (to.nChainWork > from.nChainWork) { | |
146 | r = to.nChainWork - from.nChainWork; | |
147 | } else { | |
148 | r = from.nChainWork - to.nChainWork; | |
149 | sign = -1; | |
150 | } | |
151 | r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip); | |
152 | if (r.bits() > 63) { | |
153 | return sign * std::numeric_limits<int64_t>::max(); | |
154 | } | |
155 | return sign * r.GetLow64(); | |
156 | } |