New algorithm for recalculating the difficulty of "VALM-Cache" - logical analysis, mathematically variable.
Code...
static const int64 nTargetSpacingWorkMaxPow = 12 * nPowTargetSpacing; // 14400
static const int64 nTargetSpacingWorkMaxPos = 12 * nPosTargetSpacing; // 7200
static const int64 nTargetTimespanPow = nTargetSpacingWorkMaxPow * 6 * 12; // 1036800 matrix
static const int64 nTargetTimespanPos = nTargetSpacingWorkMaxPos * 6 * 12; // 518400 matrix
// ppcoin: find last block index up to pindex
const CBlockIndex* GetLastBlockIndex(const CBlockIndex* pindex, bool fProofOfStake)
{
while (pindex && pindex->pprev && (pindex->IsProofOfStake() != fProofOfStake))
pindex = pindex->pprev;
return pindex;
}
const CBlockIndex* GetLastBlockIndexPow(const CBlockIndex* powpindex, bool fProofOfWork)
{
while (powpindex && powpindex->pprev && (powpindex->IsProofOfWork() == fProofOfWork))
powpindex = powpindex->pprev;
return powpindex;
}
const CBlockIndex* GetLastBlockIndexPos(const CBlockIndex* pospindex, bool fProofOfStake)
{
while (pospindex && pospindex->pprev && (pospindex->IsProofOfStake() != fProofOfStake))
pospindex = pospindex->pprev;
return pospindex;
}
unsigned int GetNextTargetRequiredPos(const CBlockIndex* pospindexLast, bool fProofOfStake)
{
CBigNum bnTargetLimitPos = bnProofOfStakeHardLimit;
if (pospindexLast == NULL)
return bnTargetLimitPos.GetCompact(); // last block
const CBlockIndex* pospindexPrev = GetLastBlockIndexPos(pospindexLast, fProofOfStake);
if (pospindexPrev->pprev == NULL)
return bnInitialHashTarget.GetCompact(); // first block
const CBlockIndex* pospindexPrevPrev = GetLastBlockIndexPos(pospindexPrev->pprev, fProofOfStake);
if (pospindexPrevPrev->pprev == NULL)
return bnInitialHashTarget.GetCompact(); // second block 1
const CBlockIndex* pospindexPrevPrevPrev = GetLastBlockIndexPos(pospindexPrevPrev->pprev, fProofOfStake);
if (pospindexPrevPrevPrev->pprev == NULL)
return bnInitialHashTarget.GetCompact(); // second block 2
const CBlockIndex* pospindexPrevPrevPrevPrev = GetLastBlockIndexPos(pospindexPrevPrevPrev->pprev, fProofOfStake);
if (pospindexPrevPrevPrevPrev->pprev == NULL)
return bnInitialHashTarget.GetCompact(); // second block 3
const CBlockIndex* pospindexPrevPrevPrevPrevPrev =
GetLastBlockIndexPos(pospindexPrevPrevPrevPrev->pprev, fProofOfStake);
if (pospindexPrevPrevPrevPrevPrev->pprev == NULL)
return bnInitialHashTarget.GetCompact(); // second block 4
const CBlockIndex* pospindexPrevPrevPrevPrevPrevPrev =
GetLastBlockIndexPos(pospindexPrevPrevPrevPrevPrev->pprev, fProofOfStake);
if (pospindexPrevPrevPrevPrevPrevPrev->pprev == NULL)
return bnInitialHashTarget.GetCompact(); // second block 5
int64 nLastCoinSearchTime = GetAdjustedTime();
if(pospindexPrev->IsProofOfStake() && pospindexPrevPrev->IsProofOfStake() &&
pospindexPrevPrevPrev->IsProofOfStake() && pospindexPrevPrevPrevPrev->IsProofOfStake())
{
nLastCoinPosSearchInterval = ( nLastCoinSearchTime - PosPindexPrevPrevTime ) -
( nLastCoinSearchTime - PosPindexPrevTime );
nLastCoinPosSearchIntervalPrev = ( nLastCoinSearchTime - PosPindexPrevPrevPrevTime ) -
( nLastCoinSearchTime - PosPindexPrevPrevTime );
nLastCoinPosSearchIntervalPrevPrev = ( nLastCoinSearchTime - PosPindexPrevPrevPrevPrevTime ) -
( nLastCoinSearchTime - PosPindexPrevPrevPrevTime );
}
nUnixCachChainTime = nLastCoinSearchTime - 1 + nNewTimeBlock;
double nPosTargetSpacingTest = 0;
if(pospindexPrev->GetBlockTime() > nPowForceTimestamp && pospindexPrev->GetBlockTime() < nPowForceTimestamp + NTest)
nPosTargetSpacingTest = nPosTargetSpacing / nPosTargetSpacing * 600;
else
nPosTargetSpacingTest = nPosTargetSpacing;
int64 nActualTimeIntervalLongPosVeryFirst = nLastCoinPosSearchInterval;
if(nActualTimeIntervalLongPosVeryFirst < 0)
nActualTimeIntervalLongPosVeryFirst = nPosTargetSpacingTest / 100 * nSpamHashControl;
int64 nActualTimeIntervalLongPosFirst = nLastCoinPosSearchIntervalPrev;
if(nActualTimeIntervalLongPosFirst < 0)
nActualTimeIntervalLongPosFirst = nPosTargetSpacingTest / 100 * nSpamHashControl;
int64 nActualTimeIntervalLongPosSecond = nLastCoinPosSearchIntervalPrevPrev;
if(nActualTimeIntervalLongPosSecond < 0)
nActualTimeIntervalLongPosSecond = nPosTargetSpacingTest / 100 * nSpamHashControl;
double nActualSpacingTotalsPos = ( nActualTimeIntervalLongPosVeryFirst + nActualTimeIntervalLongPosFirst ) / 2;
double nActualTimeIntervalNvar = nActualTimeIntervalLongPosVeryFirst;
// cacheproject retarget
// VALM-Cache /logical analysis - mathematically variable/
int64 nActualSpacingPos = 0;
double nVar = nPosTargetSpacingTest / 3;
int64 nNonAccelerating = 0; // sec +0-
PosPindexPrevTime = pospindexPrev->GetBlockTime();
PosPindexPrevPrevTime = pospindexPrevPrev->GetBlockTime();
PosPindexPrevPrevPrevTime = pospindexPrevPrevPrev->GetBlockTime();
PosPindexPrevPrevPrevPrevTime = pospindexPrevPrevPrevPrev->GetBlockTime();
nLastCoinPosTwoInterval = nActualSpacingTotalsPos;
nActualSpacingPos = ( nActualSpacingTotalsPos + nActualTimeIntervalLongPosSecond ) / 2;
if(nActualTimeIntervalNvar >= nNonAccelerating && nActualTimeIntervalNvar < nPosTargetSpacingTest - nNonAccelerating)
nPosTargetSpacingVar = (( nPosTargetSpacingTest - 1 + nVar ) -
( nActualTimeIntervalNvar * nVar / nPosTargetSpacingTest ));
else if
(nActualTimeIntervalNvar > nPosTargetSpacingTest + nNonAccelerating &&
nActualTimeIntervalNvar <= nPosTargetSpacingTest * 2)
nPosTargetSpacingVar = (( nPosTargetSpacingTest + 1 + nVar ) -
( nActualTimeIntervalNvar * nVar / nPosTargetSpacingTest ));
else if
(nActualTimeIntervalNvar > nPosTargetSpacingTest * 2)
nPosTargetSpacingVar = nPosTargetSpacingTest - nVar + 1;
else
nPosTargetSpacingVar = nPosTargetSpacingTest;
double nSTSp = nPosTargetSpacingTest; // 1200 sec
int64 posUppermin = 0;
double posUppermax = nSTSp - nNonAccelerating; // 1199 sec
double posLowermin = nSTSp + nNonAccelerating; // 1201 sec
int64 posLowermax = nTargetSpacingWorkMaxPos; // 2400 sec
if(nActualTimeIntervalLongPosVeryFirst > posLowermin && nActualSpacingTotalsPos < posUppermax)
nActualTimeIntervalXUXLpos = nActualTimeIntervalLongPosVeryFirst;
else if(nActualTimeIntervalLongPosVeryFirst > posLowermin && nActualSpacingTotalsPos > posLowermin)
nActualTimeIntervalXUXLpos = min((double) nActualTimeIntervalLongPosVeryFirst, (double) nActualSpacingTotalsPos);
else if(nActualTimeIntervalLongPosVeryFirst < posUppermax && nActualSpacingTotalsPos < posUppermax)
nActualTimeIntervalXUXLpos = max((double) nActualTimeIntervalLongPosVeryFirst, (double) nActualSpacingTotalsPos);
else if(nActualSpacingTotalsPos < posUppermax && nActualSpacingTotalsPos > nActualSpacingPos)
nActualTimeIntervalXUXLpos = nActualSpacingTotalsPos;
else if(nActualSpacingTotalsPos > posLowermin && nActualSpacingTotalsPos < nActualSpacingPos)
nActualTimeIntervalXUXLpos = nActualSpacingTotalsPos;
else
nActualTimeIntervalXUXLpos = nActualSpacingPos;
double nNix = nSTSp / 100 * 70;
double nReverseEffectPos = 0;
if(nActualTimeIntervalXUXLpos < nNix)
nReverseEffectPos = nActualTimeIntervalXUXLpos / nNix;
else if(nActualTimeIntervalXUXLpos > nSTSp && nActualTimeIntervalXUXLpos <= nSTSp + ( nSTSp - nNix))
nReverseEffectPos = ( nSTSp / nSTSp ) / 2;
else if(nActualTimeIntervalXUXLpos > nSTSp + ( nSTSp - nNix) && nActualTimeIntervalXUXLpos < posLowermax)
nReverseEffectPos = (( nSTSp + ( nSTSp - nNix )) / nActualTimeIntervalXUXLpos ) / 2;
else
nReverseEffectPos = 1;
posUpperLower = ( nSTSp / 2 ) * nReverseEffectPos; // interval sampling 2:1 variable
if(nActualSpacingTotalsPos < nNix / 1.30 && nActualTimeIntervalLongPosVeryFirst < posUppermax)
posUpperLower = posUpperLower * (( nNix / 1.30 ) / nActualSpacingTotalsPos );
double XUXL = nPosTargetSpacingTest / 100 * 4;
double U = 0;
double L = 0;
double XU = XUXL + ( posUppermax * posUpperLower / nSTSp ); // 100.9166 +%
double XL = XUXL + ( nSTSp * posUpperLower / posLowermin ); // 100.9167 +%
double nBalance = 1.0;
double nN = XUXL - ( XUXL / nBalance );
int64 nTargetTimespanMin = nTargetTimespanPos / XL - 1; // min
int64 nActualTimeIntervalXU = nActualTimeIntervalXUXLpos;
int64 nActualTimeIntervalXL = nActualTimeIntervalXUXLpos;
if(nActualTimeIntervalXU >= posUppermin && nActualTimeIntervalXU < posUppermax)
U = nN + (( XU - ( nActualTimeIntervalXU * posUpperLower / nSTSp )) / nBalance );
else U = 1;
if(nActualTimeIntervalXL > posLowermin && nActualTimeIntervalXL < posLowermax)
L = XL - ( nSTSp * posUpperLower / nActualTimeIntervalXL );
else if(nActualTimeIntervalXL >= posLowermax)
L = XL / 2;
else L = 1;
int64 nTargetTimespanControlu = nTargetTimespanPos / U; // min
int64 nTargetTimespanControll = nTargetTimespanPos / L; // min
if(nTargetTimespanControlu >= nTargetTimespanMin)
XUpperPos = U;
else if(nTargetTimespanControlu < nTargetTimespanMin)
XUpperPos = XU;
else
XUpperPos = 1;
if(nTargetTimespanControll >= nTargetTimespanMin)
XLowerPos = L;
else if(nTargetTimespanControll < nTargetTimespanMin)
XLowerPos = XL;
else
XLowerPos = 1;
CBigNum bnNewPos;
bnNewPos.SetCompact(pospindexPrev->nBits);
double nTargetTimespanBn = nTargetTimespanPos / max( XUpperPos, XLowerPos );
double nInterval = nTargetTimespanBn / nPosTargetSpacingTest;
if(pospindexPrev->GetBlockTime() > nPowForceTimestamp)
{
if(pospindexPrev->GetBlockTime() > nPowForceTimestamp && pospindexPrev->IsProofOfStake())
bnNewPos *= (( (int64) nInterval - 1) * (int64) nPosTargetSpacingVar + (int64) nActualTimeIntervalXUXLpos +
(int64) nActualTimeIntervalXUXLpos);
bnNewPos /= (( (int64) nInterval + 1) * (int64) nPosTargetSpacingVar);
if(bnNewPos > bnTargetLimitPos)
bnNewPos = bnTargetLimitPos;
//if(bnNewPos < bnTargetLimitPos + bnTargetLimitPos)
//bnNewPos = bnTargetLimitPos;
}
return bnNewPos.GetCompact();
}
https://github.com/DeffM/CACHeCoin/blob/534d970af686005a46ac7b412835fa9aca4ab885/src/main.cpp#L1149