]> Git Repo - VerusCoin.git/blame - src/pow.cpp
test
[VerusCoin.git] / src / pow.cpp
CommitLineData
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 19unsigned 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
e583c8f8 24 if (pindexLast == NULL )
df852d2b 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
46unsigned 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
80bool 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
9997caa0 107int32_t komodo_heightnotary(int32_t height,uint8_t *pubkey33);
108
f2dd868d 109bool CheckProofOfWork(int32_t height,uint8_t *pubkey33,uint256 hash, unsigned int nBits, const Consensus::Params& params)
df852d2b 110{
9997caa0 111 bool fNegative,fOverflow; int32_t special;
734f85c4 112 arith_uint256 bnTarget;
f0fd00cb 113
df852d2b 114 bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
f68be922 115 if ( height > 34000 && (special= komodo_heightnotary(height,pubkey33)) != 0 ) // 0 -> non-special notary
9997caa0 116 {
681589a5 117 int32_t i,nonz = 0;
9997caa0 118 for (i=0; i<33; i++)
681589a5 119 {
120 if ( pubkey33[i] != 0 )
121 nonz++;
d28fd4bc 122 //fprintf(stderr,"%02x",pubkey33[i]);
681589a5 123 }
d28fd4bc 124 //fprintf(stderr," height.%d special.%d nonz.%d\n",height,special,nonz);
681589a5 125 if ( nonz == 0 )
f9e18307 126 return(true); // will come back via different path with pubkey set
493a9ec7 127 if ( special > 0 ) // special notary id == (height % numnotaries)
9ebb63d7 128 {
129 if (UintToArith256(hash) <= bnTarget) // accept normal diff
130 return true;
131 bnTarget.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow);
b3101d06 132 } //else bnTarget /= 8;
9997caa0 133 }
df852d2b 134 // Check range
fd311996 135 if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit))
5262fde0 136 return error("CheckProofOfWork(): nBits below minimum work");
df852d2b 137 // Check proof of work matches claimed amount
734f85c4 138 if (UintToArith256(hash) > bnTarget)
9ebb63d7 139 {
5262fde0 140 return error("CheckProofOfWork(): hash doesn't match nBits");
9ebb63d7 141 }
df852d2b 142 return true;
143}
144
734f85c4 145arith_uint256 GetBlockProof(const CBlockIndex& block)
b343c1a1 146{
734f85c4 147 arith_uint256 bnTarget;
b343c1a1 148 bool fNegative;
149 bool fOverflow;
092b58d1 150 bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
b343c1a1 151 if (fNegative || fOverflow || bnTarget == 0)
152 return 0;
153 // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
734f85c4 154 // as it's too large for a arith_uint256. However, as 2**256 is at least as large
b343c1a1 155 // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
156 // or ~bnTarget / (nTarget+1) + 1.
157 return (~bnTarget / (bnTarget + 1)) + 1;
df852d2b 158}
f7303f97
PW
159
160int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
161{
162 arith_uint256 r;
163 int sign = 1;
164 if (to.nChainWork > from.nChainWork) {
165 r = to.nChainWork - from.nChainWork;
166 } else {
167 r = from.nChainWork - to.nChainWork;
168 sign = -1;
169 }
170 r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
171 if (r.bits() > 63) {
172 return sign * std::numeric_limits<int64_t>::max();
173 }
174 return sign * r.GetLow64();
175}
This page took 0.178136 seconds and 4 git commands to generate.