Our pacing code is
if(nActualSpacing < 0)
{
printf(">> %s nActualSpacing = %"PRI64d" corrected to 1.\n", fProofOfStake ? "PoS" : "PoW", nActualSpacing);
nActualSpacing = 1;
}
else if(nActualSpacing > nTargetTimespan)
{
printf(">> %s nActualSpacing = %"PRI64d" corrected to nTargetTimespan (%"PRI64d").\n", fProofOfStake ? "PoS" : "PoW", nActualSpacing, nTargetTimespan);
nActualSpacing = nTargetTimespan;
}
This works well for PoS.
Do you think it would be better to use
if (nActualSpacing<0) nActualSpacing=nTargetSpacing
Yes.
bnNew.SetCompact(pindexPrev->nBits);
....
int64 nInterval = nTargetTimespan / nTargetSpacing;
bnNew *= ((nInterval - 1) * nTargetSpacing + nActualSpacing + nActualSpacing);
bnNew /= ((nInterval + 1) * nTargetSpacing);The bnNew is set to the last block's nbits. Then its value multiplied by
((nInterval - 1) * nTargetSpacing + nActualSpacing + nActualSpacing)/((nInterval + 1) * nTargetSpacing)
If you set nActualSpacing to 1 then it will be multiplied by
((nInterval - 1) * nTargetSpacing + 2)/((nInterval + 1) * nTargetSpacing)=
(nInterval - 1)/(nInterval + 1) + 2/((nInterval + 1) * nTargetSpacing) =
((nTargetTimespan-nTargetSpacing) / (nTargetTimespan+nTargetSpacing)) + 2/((nTargetTimespan + nTargetSpacing) =
((nTargetTimespan-nTargetSpacing+2) / (nTargetTimespan+nTargetSpacing)) =
(1802-nTargetSpacing)/(1800+nTargetSpacing)
If nTargetSpacing>1 then it is smaller than 1 so you will decrease the target and increase the diff. It gives an exploitable bug: If you generates negative time hopping then you can artifically increase the diff and slow down the blockchain.
If it is a PoS block then nTargetSpacing=600 and the multiplier is (1802-600)/(1800+600)=0.5 approx. Now there are only PoS blocks, so every negative time hopping block will double the diff.
If you set nActualSpacing=nTargetSpacing, then:
((nInterval - 1) * nTargetSpacing + nActualSpacing + nActualSpacing)/((nInterval + 1) * nTargetSpacing) =
((nInterval - 1) * nTargetSpacing + nTargetSpacing + nTargetSpacing )/((nInterval + 1) * nTargetSpacing) =
((nInterval + 1) * nTargetSpacing )/((nInterval + 1) * nTargetSpacing) =
1So if there are a negative time hopping attack the target and diff will not change, so there will be no advantage of this kind of attack.