23 if (pindexPrev == NULL)
24 return nProofOfWorkLimit;
27 return pindexPrev->
nBits;
29 uint32_t nHeight = pindexPrev->
nHeight + 1;
43 LogPrintf(
"Use minimum difficulty for the first N blocks since forking. height=%d\n", nHeight);
54 return pindexPrev->
nBits;
72 LogPrintf(
"The new block(height=%d) will come too late. Use minimum difficulty.\n", pblock->
nHeight);
85 if (target > pow_limit) {
100 return pindexPrev->
nBits;
103 const int height = pindexPrev->
nHeight + 1;
107 const int k = (N+1)/2 * 0.998 * T;
111 arith_uint256 sum_target, sum_last10_target,sum_last5_target;;
112 int sum_time = 0, nWeight = 0;
114 int sum_last10_time=0;
115 int sum_last5_time=0;
118 for (
int i = height - N; i < height; i++) {
128 sum_time += solvetime * nWeight;
135 sum_target += target / (k * N * N);
139 sum_last10_time += solvetime;
140 sum_last10_target += target;
143 sum_last5_time += solvetime;
144 sum_last5_target += target;
150 if (sum_time < N * k / 10) {
151 sum_time = N * k / 10;
159 if( sum_last5_time <= T )
162 avg_last5_target = sum_last5_target/5;
163 if(next_target > avg_last5_target/4) next_target = avg_last5_target/4;
165 else if(sum_last10_time <= 2 * T)
168 avg_last10_target = sum_last10_target/10;
169 if(next_target > avg_last10_target/3) next_target = avg_last10_target/3;
171 else if(sum_last10_time <= 5 * T)
174 avg_last10_target = sum_last10_target/10;
175 if(next_target > avg_last10_target*2/3) next_target = avg_last10_target*2/3;
180 if( next_target > last_target * 13/10 ) next_target = last_target * 13/10;
186 if (next_target > pow_limit) {
187 next_target = pow_limit;
196 assert(pindexPrev !=
nullptr);
206 pindexFirst = pindexFirst->
pprev;
210 if (pindexFirst == NULL)
211 return nProofOfWorkLimit;
222 int64_t nActualTimespan = nLastBlockTime - nFirstBlockTime;
234 bnNew *= nActualTimespan;
236 if (bnNew > bnPowLimit)
247 target *= multiplier;
250 if (target > pow_limit)
265 crypto_generichash_blake2b_state state;
277 crypto_generichash_blake2b_update(&state, (
unsigned char*)&ss[0], ss.
size());
282 LogPrintf(
"EhIsValidSolution is invalid, n=%d k=%d ss.size=%d \npblock=%s ", n,k, ss.
size(), pblock->
ToString() );
283 return error(
"CheckEquihashSolution(): invalid solution");
296 bnTarget.
SetCompact(nBits, &fNegative, &fOverflow);
bool error(const char *fmt, const Args &...args)
CBlockIndex * pprev
pointer to the index of the predecessor of this block
unsigned int EquihashN(uint32_t nHeight=0) const
#define EhInitialiseState(n, k, base_state)
const Consensus::Params & GetConsensus() const
bool bZawyLwmaSolvetimeLimitation
bool fPowAllowMinDifficultyBlocks
assert(len-trim+(2 *lenIndices)<=WIDTH)
CChainParams defines various tweakable parameters of a given instance of the Fabcoin system...
Double ended buffer combining vector and stream-like interfaces.
int64_t DigishieldMaxActualTimespan(uint32_t nheight=0) const
const uint256 & PowLimit(bool postfork) const
arith_uint256 UintToArith256(const uint256 &a)
static const std::string MAIN
BIP70 chain name strings (main, test or regtest)
bool CheckEquihashSolution(const CBlockHeader *pblock, const CChainParams ¶ms)
Check whether the Equihash solution in a block header is valid.
uint32_t GetCompact(bool fNegative=false) const
bool CheckProofOfWork(uint256 hash, unsigned int nBits, bool postfork, const Consensus::Params ¶ms)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
int64_t nPowTargetSpacing
uint32_t ContractHeight
Block height at which Fabcoin Smart Contract hard fork becomes active.
int64_t nDigishieldPowAveragingWindow
unsigned int DigishieldCalculateNextWorkRequired(const CBlockIndex *pindexPrev, arith_uint256 bnAvg, int64_t nLastBlockTime, int64_t nFirstBlockTime, const Consensus::Params ¶ms)
#define EhIsValidSolution(n, k, base_state, soln, ret)
Parameters that influence chain consensus.
256-bit unsigned big integer.
unsigned int LwmaGetNextWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const Consensus::Params ¶ms)
Zawy's LWMA - next generation algorithm.
unsigned int ReduceDifficultyBy(const CBlockIndex *pindexPrev, int64_t multiplier, const Consensus::Params ¶ms)
Reduce the difficulty by a given multiplier.
unsigned int DigishieldGetNextWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const Consensus::Params ¶ms)
Digishield v3 - used in Fabcoin mainnet currently.
The block chain is a tree shaped structure starting with the genesis block at the root...
const CChainParams & Params()
Return the currently selected parameters.
unsigned int GetNextWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const Consensus::Params ¶ms)
N diff(N const &_a, N const &_b)
uint32_t EquihashFABHeight
Block height at which EquihashFAB (184,7) becomes active.
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=nullptr, bool *pfOverflow=nullptr)
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar t...
int64_t DigishieldMinActualTimespan(uint32_t nheight=0) const
int nHeight
height of the entry in the chain. The genesis block has height 0
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
int64_t GetBlockTime() const
int64_t GetMedianTimePast() const
int64_t DigishieldAveragingWindowTimespan(uint32_t nheight=0) const
unsigned int LwmaCalculateNextWorkRequired(const CBlockIndex *pindexPrev, const Consensus::Params ¶ms)
unsigned int EquihashK(uint32_t nHeight=0) const
uint32_t LWMAHeight
Block height at which LWMA becomes active.
int64_t nZawyLwmaAveragingWindow