1616
1717unsigned int static DUAL_KGW3 (const CBlockIndex* pindexLast, const Consensus::Params& params, const CBlockHeader *pblock)
1818{
19- // current difficulty formula, ERC3 - DUAL_KGW3, written by Christian Knoepke - apfelbaum@email.de
20- // BitSend and Eropecoin Developer
2119 const CBlockIndex *BlockLastSolved = pindexLast;
2220 const CBlockIndex *BlockReading = pindexLast;
2321 bool kgwdebug=false ;
@@ -150,7 +148,56 @@ unsigned int static DUAL_KGW3(const CBlockIndex* pindexLast, const Consensus::Pa
150148unsigned int GetNextWorkRequired (const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
151149{
152150 // Initial DK3
153- return DUAL_KGW3 (pindexLast, params, pblock);
151+ int Diff_Fork1 = 951000 ;
152+ assert (pindexLast != nullptr );
153+ unsigned int nProofOfWorkLimit = UintToArith256 (params.powLimit ).GetCompact ();
154+ if (pindexLast->nHeight +1 <= Diff_Fork1)
155+ {
156+ return DUAL_KGW3 (pindexLast, params, pblock);
157+ }
158+ // // Update 0.17.9 BSD
159+ // Genesis block
160+ if (pindexLast == NULL )
161+ return nProofOfWorkLimit;
162+
163+ // Only change once per difficulty adjustment interval
164+ if ((pindexLast->nHeight +1 ) % params.DifficultyAdjustmentInterval () != 0 )
165+ {
166+ if (params.fPowAllowMinDifficultyBlocks )
167+ {
168+ // Special difficulty rule for testnet:
169+ // If the new block's timestamp is more than 2* 10 minutes
170+ // then allow mining of a min-difficulty block.
171+ if (pblock->GetBlockTime () > pindexLast->GetBlockTime () + params.nPowTargetSpacing *2 )
172+ return nProofOfWorkLimit;
173+ else
174+ {
175+ // Return the last non-special-min-difficulty-rules-block
176+ const CBlockIndex* pindex = pindexLast;
177+ while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval () != 0 && pindex->nBits == nProofOfWorkLimit)
178+ pindex = pindex->pprev ;
179+ return pindex->nBits ;
180+ }
181+ }
182+ // LogPrintf("difficulty adjustment interval %d \n",(pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval());
183+ return pindexLast->nBits ;
184+ }
185+
186+ // Go back by what we want to be 14 days worth of blocks
187+ // Litecoin: This fixes an issue where a 51% attack can change difficulty at will.
188+ // Go back the full period unless it's the first retarget after genesis. Code courtesy of Art Forz
189+ int blockstogoback = params.DifficultyAdjustmentInterval ()-1 ;
190+ if ((pindexLast->nHeight +1 ) != params.DifficultyAdjustmentInterval ())
191+ blockstogoback = params.DifficultyAdjustmentInterval ();
192+
193+ // Go back by what we want to be 14 days worth of blocks
194+ const CBlockIndex* pindexFirst = pindexLast;
195+ for (int i = 0 ; pindexFirst && i < blockstogoback; i++)
196+ pindexFirst = pindexFirst->pprev ;
197+
198+ assert (pindexFirst);
199+ return CalculateNextWorkRequired (pindexLast, pindexFirst->GetBlockTime (), params);
200+ // // Update 0.17.9 BSD END
154201
155202}
156203
@@ -161,10 +208,10 @@ unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nF
161208
162209 // Limit adjustment step
163210 int64_t nActualTimespan = pindexLast->GetBlockTime () - nFirstBlockTime;
164- if (nActualTimespan < params.nPowTargetTimespan /4 )
165- nActualTimespan = params.nPowTargetTimespan /4 ;
166- if (nActualTimespan > params.nPowTargetTimespan *4 )
167- nActualTimespan = params.nPowTargetTimespan *4 ;
211+ if (nActualTimespan < params.nPowTargetTimespan /1.2 )
212+ nActualTimespan = params.nPowTargetTimespan /1.2 ;
213+ if (nActualTimespan > params.nPowTargetTimespan *1.2 )
214+ nActualTimespan = params.nPowTargetTimespan *1.2 ;
168215
169216 // Retarget
170217 const arith_uint256 bnPowLimit = UintToArith256 (params.powLimit );
0 commit comments