]>
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 | ||
802ea76b SB |
19 | #ifdef ENABLE_RUST |
20 | #include "librustzcash.h" | |
21 | #endif // ENABLE_RUST | |
ae0bb3d3 | 22 | uint32_t komodo_chainactive_timestamp(); |
802ea76b | 23 | |
d698ef69 | 24 | unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params) |
df852d2b | 25 | { |
fd311996 | 26 | unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact(); |
df852d2b | 27 | // Genesis block |
e583c8f8 | 28 | if (pindexLast == NULL ) |
df852d2b | 29 | return nProofOfWorkLimit; |
30 | ||
f2c48e15 JG |
31 | // Find the first block in the averaging interval |
32 | const CBlockIndex* pindexFirst = pindexLast; | |
7b173bd8 | 33 | arith_uint256 bnTot {0}; |
f2c48e15 | 34 | for (int i = 0; pindexFirst && i < params.nPowAveragingWindow; i++) { |
7b173bd8 JG |
35 | arith_uint256 bnTmp; |
36 | bnTmp.SetCompact(pindexFirst->nBits); | |
37 | bnTot += bnTmp; | |
f2c48e15 JG |
38 | pindexFirst = pindexFirst->pprev; |
39 | } | |
40 | ||
41 | // Check we have enough blocks | |
42 | if (pindexFirst == NULL) | |
43 | return nProofOfWorkLimit; | |
df852d2b | 44 | |
7b173bd8 JG |
45 | arith_uint256 bnAvg {bnTot / params.nPowAveragingWindow}; |
46 | ||
29842505 | 47 | return CalculateNextWorkRequired(bnAvg, pindexLast->GetMedianTimePast(), pindexFirst->GetMedianTimePast(), params); |
34e5015c RN |
48 | } |
49 | ||
29842505 JG |
50 | unsigned int CalculateNextWorkRequired(arith_uint256 bnAvg, |
51 | int64_t nLastBlockTime, int64_t nFirstBlockTime, | |
52 | const Consensus::Params& params) | |
34e5015c | 53 | { |
df852d2b | 54 | // Limit adjustment step |
f2c48e15 | 55 | // Use medians to prevent time-warp attacks |
e99731b4 | 56 | int64_t nActualTimespan = nLastBlockTime - nFirstBlockTime; |
f2c48e15 JG |
57 | LogPrint("pow", " nActualTimespan = %d before dampening\n", nActualTimespan); |
58 | nActualTimespan = params.AveragingWindowTimespan() + (nActualTimespan - params.AveragingWindowTimespan())/4; | |
59 | LogPrint("pow", " nActualTimespan = %d before bounds\n", nActualTimespan); | |
60 | ||
61 | if (nActualTimespan < params.MinActualTimespan()) | |
62 | nActualTimespan = params.MinActualTimespan(); | |
63 | if (nActualTimespan > params.MaxActualTimespan()) | |
64 | nActualTimespan = params.MaxActualTimespan(); | |
df852d2b | 65 | |
66 | // Retarget | |
fd311996 | 67 | const arith_uint256 bnPowLimit = UintToArith256(params.powLimit); |
29842505 | 68 | arith_uint256 bnNew {bnAvg}; |
f2c48e15 | 69 | bnNew /= params.AveragingWindowTimespan(); |
aa86873a | 70 | bnNew *= nActualTimespan; |
df852d2b | 71 | |
fd311996 CF |
72 | if (bnNew > bnPowLimit) |
73 | bnNew = bnPowLimit; | |
df852d2b | 74 | |
75 | /// debug print | |
f2c48e15 JG |
76 | LogPrint("pow", "GetNextWorkRequired RETARGET\n"); |
77 | LogPrint("pow", "params.AveragingWindowTimespan() = %d nActualTimespan = %d\n", params.AveragingWindowTimespan(), nActualTimespan); | |
29842505 | 78 | LogPrint("pow", "Current average: %08x %s\n", bnAvg.GetCompact(), bnAvg.ToString()); |
f2c48e15 | 79 | LogPrint("pow", "After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString()); |
df852d2b | 80 | |
81 | return bnNew.GetCompact(); | |
82 | } | |
83 | ||
fdda3c50 JG |
84 | bool CheckEquihashSolution(const CBlockHeader *pblock, const CChainParams& params) |
85 | { | |
e9574728 JG |
86 | unsigned int n = params.EquihashN(); |
87 | unsigned int k = params.EquihashK(); | |
fdda3c50 JG |
88 | |
89 | // Hash state | |
90 | crypto_generichash_blake2b_state state; | |
e9574728 | 91 | EhInitialiseState(n, k, state); |
fdda3c50 JG |
92 | |
93 | // I = the block header minus nonce and solution. | |
94 | CEquihashInput I{*pblock}; | |
95 | // I||V | |
96 | CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); | |
97 | ss << I; | |
98 | ss << pblock->nNonce; | |
99 | ||
100 | // H(I||V||... | |
101 | crypto_generichash_blake2b_update(&state, (unsigned char*)&ss[0], ss.size()); | |
102 | ||
802ea76b | 103 | #ifdef ENABLE_RUST |
6a0c7cea SB |
104 | // Ensure that our Rust interactions are working in production builds. This is |
105 | // temporary and should be removed. | |
106 | { | |
107 | assert(librustzcash_xor(0x0f0f0f0f0f0f0f0f, 0x1111111111111111) == 0x1e1e1e1e1e1e1e1e); | |
108 | } | |
802ea76b | 109 | #endif // ENABLE_RUST |
6a0c7cea | 110 | |
e9574728 JG |
111 | bool isValid; |
112 | EhIsValidSolution(n, k, state, pblock->nSolution, isValid); | |
113 | if (!isValid) | |
fdda3c50 JG |
114 | return error("CheckEquihashSolution(): invalid solution"); |
115 | ||
116 | return true; | |
117 | } | |
118 | ||
8683bd8d | 119 | int32_t komodo_chosennotary(int32_t *notaryidp,int32_t height,uint8_t *pubkey33,uint32_t timestamp); |
a4ea4a3f | 120 | int32_t komodo_is_special(uint8_t pubkeys[66][33],int32_t mids[66],uint32_t blocktimes[66],int32_t height,uint8_t pubkey33[33],uint32_t blocktime); |
c75c18fc | 121 | int32_t komodo_currentheight(); |
3617ec15 | 122 | CBlockIndex *komodo_chainactive(int32_t height); |
37955303 | 123 | void komodo_index2pubkey33(uint8_t *pubkey33,CBlockIndex *pindex,int32_t height); |
c75c18fc | 124 | extern int32_t KOMODO_CHOSEN_ONE; |
6d649e09 | 125 | extern uint64_t ASSETCHAINS_STAKED; |
9edf27ec | 126 | extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN]; |
6c96293f | 127 | #define KOMODO_ELECTION_GAP 2000 |
c54cb7d5 | 128 | |
a4ea4a3f | 129 | int32_t komodo_eligiblenotary(uint8_t pubkeys[66][33],int32_t *mids,uint32_t blocktimes[66],int32_t *nonzpkeysp,int32_t height); |
54f7311d | 130 | int32_t KOMODO_LOADINGBLOCKS = 1; |
29e60e48 | 131 | |
2a080a8a | 132 | extern std::string NOTARY_PUBKEY; |
133 | ||
a4ea4a3f | 134 | bool CheckProofOfWork(int32_t height,uint8_t *pubkey33,uint256 hash,unsigned int nBits,const Consensus::Params& params,uint32_t blocktime) |
df852d2b | 135 | { |
d8be8b2e | 136 | extern int32_t KOMODO_REWIND; |
a4ea4a3f | 137 | bool fNegative,fOverflow; uint8_t origpubkey33[33]; int32_t i,nonzpkeys=0,nonz=0,special=0,special2=0,notaryid=-1,flag = 0, mids[66]; uint32_t tiptime,blocktimes[66]; |
c54cb7d5 | 138 | arith_uint256 bnTarget; uint8_t pubkeys[66][33]; |
bf30e02d | 139 | //for (i=31; i>=0; i--) |
140 | // fprintf(stderr,"%02x",((uint8_t *)&hash)[i]); | |
141 | //fprintf(stderr," checkpow\n"); | |
54f7311d | 142 | memcpy(origpubkey33,pubkey33,33); |
a4ea4a3f | 143 | memset(blocktimes,0,sizeof(blocktimes)); |
144 | tiptime = komodo_chainactive_timestamp(); | |
df852d2b | 145 | bnTarget.SetCompact(nBits, &fNegative, &fOverflow); |
0d2cefb0 | 146 | if ( height == 0 ) |
c54cb7d5 | 147 | { |
c75c18fc | 148 | height = komodo_currentheight() + 1; |
c54cb7d5 | 149 | //fprintf(stderr,"set height to %d\n",height); |
150 | } | |
151 | if ( height > 34000 && ASSETCHAINS_SYMBOL[0] == 0 ) // 0 -> non-special notary | |
9997caa0 | 152 | { |
a4ea4a3f | 153 | special = komodo_chosennotary(¬aryid,height,pubkey33,tiptime); |
9997caa0 | 154 | for (i=0; i<33; i++) |
681589a5 | 155 | { |
156 | if ( pubkey33[i] != 0 ) | |
157 | nonz++; | |
681589a5 | 158 | } |
681589a5 | 159 | if ( nonz == 0 ) |
c54cb7d5 | 160 | { |
161 | //fprintf(stderr,"ht.%d null pubkey checkproof return\n",height); | |
f9e18307 | 162 | return(true); // will come back via different path with pubkey set |
c54cb7d5 | 163 | } |
a4ea4a3f | 164 | flag = komodo_eligiblenotary(pubkeys,mids,blocktimes,&nonzpkeys,height); |
165 | special2 = komodo_is_special(pubkeys,mids,blocktimes,height,pubkey33,blocktime); | |
8c0ffb40 | 166 | if ( notaryid >= 0 ) |
167 | { | |
8fa95955 | 168 | if ( height > 10000 && height < 80000 && (special != 0 || special2 > 0) ) |
8c0ffb40 | 169 | flag = 1; |
170 | else if ( height >= 80000 && height < 108000 && special2 > 0 ) | |
171 | flag = 1; | |
172 | else if ( height >= 108000 && special2 > 0 ) | |
173 | flag = ((height % KOMODO_ELECTION_GAP) > 64 || (height % KOMODO_ELECTION_GAP) == 0); | |
c54cb7d5 | 174 | else if ( height == 790833 ) |
175 | flag = 1; | |
54f7311d | 176 | else if ( special2 < 0 ) |
177 | { | |
178 | if ( height > 792000 ) | |
179 | flag = 0; | |
180 | else fprintf(stderr,"ht.%d notaryid.%d special.%d flag.%d special2.%d\n",height,notaryid,special,flag,special2); | |
181 | } | |
a4ea4a3f | 182 | if ( (flag != 0 || special2 > 0) && special2 != -2 ) |
93684bd0 | 183 | { |
184 | //fprintf(stderr,"EASY MINING ht.%d\n",height); | |
185 | bnTarget.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow); | |
186 | } | |
fb9fcbf8 | 187 | } |
9997caa0 | 188 | } |
fd311996 | 189 | if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit)) |
5262fde0 | 190 | return error("CheckProofOfWork(): nBits below minimum work"); |
df852d2b | 191 | // Check proof of work matches claimed amount |
4a4e912b | 192 | if ( UintToArith256(hash) > bnTarget ) |
f796b1fb | 193 | { |
54f7311d | 194 | if ( KOMODO_LOADINGBLOCKS != 0 ) |
195 | return true; | |
56aa66a0 | 196 | if ( ASSETCHAINS_SYMBOL[0] != 0 || height > 792000 ) |
a4ea4a3f | 197 | { |
6cbd97e3 | 198 | //if ( 0 && height > 792000 ) |
a4ea4a3f | 199 | { |
200 | for (i=31; i>=0; i--) | |
80afe35b | 201 | fprintf(stderr,"%02x",((uint8_t *)&hash)[i]); |
202 | fprintf(stderr," hash vs "); | |
a4ea4a3f | 203 | for (i=31; i>=0; i--) |
80afe35b | 204 | fprintf(stderr,"%02x",((uint8_t *)&bnTarget)[i]); |
bce6dbb3 | 205 | fprintf(stderr," ht.%d special.%d special2.%d flag.%d notaryid.%d mod.%d error\n",height,special,special2,flag,notaryid,(height % 35)); |
a4ea4a3f | 206 | for (i=0; i<33; i++) |
80afe35b | 207 | fprintf(stderr,"%02x",pubkey33[i]); |
208 | fprintf(stderr," <- pubkey\n"); | |
a4ea4a3f | 209 | for (i=0; i<33; i++) |
80afe35b | 210 | fprintf(stderr,"%02x",origpubkey33[i]); |
211 | fprintf(stderr," <- origpubkey\n"); | |
a4ea4a3f | 212 | } |
54f7311d | 213 | return false; |
a4ea4a3f | 214 | } |
f796b1fb | 215 | } |
a4ea4a3f | 216 | /*for (i=31; i>=0; i--) |
217 | fprintf(stderr,"%02x",((uint8_t *)&hash)[i]); | |
218 | fprintf(stderr," hash vs "); | |
219 | for (i=31; i>=0; i--) | |
220 | fprintf(stderr,"%02x",((uint8_t *)&bnTarget)[i]); | |
221 | fprintf(stderr," height.%d notaryid.%d PoW valid\n",height,notaryid);*/ | |
df852d2b | 222 | return true; |
223 | } | |
224 | ||
29977716 | 225 | |
734f85c4 | 226 | arith_uint256 GetBlockProof(const CBlockIndex& block) |
b343c1a1 | 227 | { |
734f85c4 | 228 | arith_uint256 bnTarget; |
b343c1a1 | 229 | bool fNegative; |
230 | bool fOverflow; | |
092b58d1 | 231 | bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow); |
b343c1a1 | 232 | if (fNegative || fOverflow || bnTarget == 0) |
233 | return 0; | |
234 | // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256 | |
734f85c4 | 235 | // as it's too large for a arith_uint256. However, as 2**256 is at least as large |
b343c1a1 | 236 | // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1, |
237 | // or ~bnTarget / (nTarget+1) + 1. | |
238 | return (~bnTarget / (bnTarget + 1)) + 1; | |
df852d2b | 239 | } |
f7303f97 PW |
240 | |
241 | int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params) | |
242 | { | |
243 | arith_uint256 r; | |
244 | int sign = 1; | |
245 | if (to.nChainWork > from.nChainWork) { | |
246 | r = to.nChainWork - from.nChainWork; | |
247 | } else { | |
248 | r = from.nChainWork - to.nChainWork; | |
249 | sign = -1; | |
250 | } | |
251 | r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip); | |
252 | if (r.bits() > 63) { | |
253 | return sign * std::numeric_limits<int64_t>::max(); | |
254 | } | |
255 | return sign * r.GetLow64(); | |
256 | } |