Skip to content

Commit daa4e94

Browse files
POSv Fix #2
1 parent cd44dbd commit daa4e94

4 files changed

Lines changed: 160 additions & 161 deletions

File tree

src/checkpoints.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace Checkpoints
4848
( 80000, uint256("0x0def72391fd1db25297478048a8b1b5feca86061d614146ea8e875d27be1f41f"))
4949
(120000, uint256("0xa6d147731bb021c5365ba44264e7faffd47aaf806861278a4227deac33f78207"))
5050
(258805, uint256("0x74133722e84132005691a21a8092f0c590da7ab5744f3bdf8113089cc6d55051"))
51-
(458580, uint256("0x51fe53e2091ee1f2e8244cf500027a1900e05cd01427a5258dfac8c3d759e7fe"))
51+
//(458580, uint256("0x51fe53e2091ee1f2e8244cf500027a1900e05cd01427a5258dfac8c3d759e7fe"))
5252
(564890, uint256("0x1230d31d9b93651e02c877776e01158496fbac59dd3d898d9b86b76a8e6beb83"))
5353
;
5454
static const CCheckpointData data = {

src/main.cpp

Lines changed: 155 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,176 +1315,175 @@ return bnNew.GetCompact();
13151315

13161316
unsigned int static KimotoGravityWell(const CBlockIndex* pindexLast, const CBlockHeader *pblock, uint64 TargetBlocksSpacingSeconds, uint64 PastBlocksMin, uint64 PastBlocksMax) {
13171317
/* current difficulty formula, megacoin - kimoto gravity well */
1318-
const CBlockIndex *BlockLastSolved = pindexLast;
1319-
const CBlockIndex *BlockReading = pindexLast;
1320-
const CBlockHeader *BlockCreating = pblock;
1321-
BlockCreating = BlockCreating;
1322-
uint64 PastBlocksMass = 0;
1323-
int64 PastRateActualSeconds = 0;
1324-
int64 PastRateTargetSeconds = 0;
1325-
double PastRateAdjustmentRatio = double(1);
1326-
CBigNum PastDifficultyAverage;
1327-
CBigNum PastDifficultyAveragePrev;
1328-
double EventHorizonDeviation;
1329-
double EventHorizonDeviationFast;
1330-
double EventHorizonDeviationSlow;
1331-
if (BlockLastSolved == NULL || BlockLastSolved->nHeight == 0 || (uint64)BlockLastSolved->nHeight < PastBlocksMin) {
1332-
return bnProofOfWorkLimit.GetCompact();
1333-
}
1334-
int64 LatestBlockTime = BlockLastSolved->GetBlockTime();
1335-
for (unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) {
1336-
if (PastBlocksMax > 0 && i > PastBlocksMax) {
1337-
break;
1338-
}
1339-
PastBlocksMass++;
1340-
/* Patched with http://pastebin.com/PY6NHGzu by Sygma (manually) */
1341-
if (i == 1) {
1342-
PastDifficultyAverage.SetCompact(BlockReading->nBits);
1343-
} else {
1344-
PastDifficultyAverage = ((CBigNum().SetCompact(BlockReading->nBits) - PastDifficultyAveragePrev) / i) + PastDifficultyAveragePrev;
1345-
}
1346-
PastDifficultyAveragePrev = PastDifficultyAverage;
1347-
if (LatestBlockTime < BlockReading->GetBlockTime()) {
1348-
if (BlockReading->nHeight > 158000){ // HARD Fork block number
1349-
LatestBlockTime = BlockReading->GetBlockTime();
1350-
}
1351-
}
1352-
PastRateActualSeconds = LatestBlockTime - BlockReading->GetBlockTime();
1353-
PastRateTargetSeconds = TargetBlocksSpacingSeconds * PastBlocksMass;
1354-
PastRateAdjustmentRatio = double(1);
1355-
if (BlockReading->nHeight > 158000) { // HARD Fork block number
1356-
if (PastRateActualSeconds < 1) {
1357-
PastRateActualSeconds = 1;
1358-
}
1359-
} else {
1360-
if (PastRateActualSeconds < 0) {
1361-
PastRateActualSeconds = 0;
1362-
}
1363-
}
1364-
if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) {
1365-
PastRateAdjustmentRatio = double(PastRateTargetSeconds) / double(PastRateActualSeconds);
1366-
}
1367-
EventHorizonDeviation = 1 + (0.7084 * pow((double(PastBlocksMass)/double(144)), -1.228));
1368-
EventHorizonDeviationFast = EventHorizonDeviation;
1369-
EventHorizonDeviationSlow = 1 / EventHorizonDeviation;
1370-
if (PastBlocksMass >= PastBlocksMin) {
1371-
if ((PastRateAdjustmentRatio <= EventHorizonDeviationSlow) || (PastRateAdjustmentRatio >= EventHorizonDeviationFast)) {
1372-
assert(BlockReading);
1373-
break;
1374-
}
1375-
}
1376-
if (BlockReading->pprev == NULL) {
1377-
assert(BlockReading);
1378-
break;
1379-
}
1380-
BlockReading = BlockReading->pprev;
1381-
}
1382-
CBigNum bnNew(PastDifficultyAverage);
1383-
if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) {
1384-
bnNew *= PastRateActualSeconds;
1385-
bnNew /= PastRateTargetSeconds;
1386-
}
1387-
if (bnNew > bnProofOfWorkLimit) {
1388-
bnNew = bnProofOfWorkLimit;
1389-
}
1390-
/// debug print
1391-
printf("Difficulty Retarget - Kimoto Gravity Well\n");
1392-
printf("PastRateAdjustmentRatio = %g\n", PastRateAdjustmentRatio);
1393-
printf("Before: %08x %s\n", BlockLastSolved->nBits, CBigNum().SetCompact(BlockLastSolved->nBits).getuint256().ToString().c_str());
1394-
printf("After: %08x %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString().c_str());
1395-
return bnNew.GetCompact();
1318+
const CBlockIndex *BlockLastSolved = pindexLast;
1319+
const CBlockIndex *BlockReading = pindexLast;
1320+
const CBlockHeader *BlockCreating = pblock;
1321+
BlockCreating = BlockCreating;
1322+
uint64 PastBlocksMass = 0;
1323+
int64 PastRateActualSeconds = 0;
1324+
int64 PastRateTargetSeconds = 0;
1325+
double PastRateAdjustmentRatio = double(1);
1326+
CBigNum PastDifficultyAverage;
1327+
CBigNum PastDifficultyAveragePrev;
1328+
double EventHorizonDeviation;
1329+
double EventHorizonDeviationFast;
1330+
double EventHorizonDeviationSlow;
1331+
if (BlockLastSolved == NULL || BlockLastSolved->nHeight == 0 || (uint64)BlockLastSolved->nHeight < PastBlocksMin) {
1332+
return bnProofOfWorkLimit.GetCompact();
1333+
}
1334+
int64 LatestBlockTime = BlockLastSolved->GetBlockTime();
1335+
for (unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) {
1336+
if (PastBlocksMax > 0 && i > PastBlocksMax) {
1337+
break;
1338+
}
1339+
PastBlocksMass++;
1340+
/* Patched with http://pastebin.com/PY6NHGzu by Sygma (manually) */
1341+
if (i == 1) {
1342+
PastDifficultyAverage.SetCompact(BlockReading->nBits);
1343+
} else {
1344+
PastDifficultyAverage = ((CBigNum().SetCompact(BlockReading->nBits) - PastDifficultyAveragePrev) / i) + PastDifficultyAveragePrev;
1345+
}
1346+
PastDifficultyAveragePrev = PastDifficultyAverage;
1347+
if (LatestBlockTime < BlockReading->GetBlockTime()) {
1348+
if (BlockReading->nHeight > 158000){ // HARD Fork block number
1349+
LatestBlockTime = BlockReading->GetBlockTime();
1350+
}
1351+
}
1352+
PastRateActualSeconds = LatestBlockTime - BlockReading->GetBlockTime();
1353+
PastRateTargetSeconds = TargetBlocksSpacingSeconds * PastBlocksMass;
1354+
PastRateAdjustmentRatio = double(1);
1355+
if (BlockReading->nHeight > 158000) { // HARD Fork block number
1356+
if (PastRateActualSeconds < 1) {
1357+
PastRateActualSeconds = 1;
1358+
}
1359+
} else {
1360+
if (PastRateActualSeconds < 0) {
1361+
PastRateActualSeconds = 0;
1362+
}
1363+
}
1364+
if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) {
1365+
PastRateAdjustmentRatio = double(PastRateTargetSeconds) / double(PastRateActualSeconds);
1366+
}
1367+
EventHorizonDeviation = 1 + (0.7084 * pow((double(PastBlocksMass)/double(144)), -1.228));
1368+
EventHorizonDeviationFast = EventHorizonDeviation;
1369+
EventHorizonDeviationSlow = 1 / EventHorizonDeviation;
1370+
if (PastBlocksMass >= PastBlocksMin) {
1371+
if ((PastRateAdjustmentRatio <= EventHorizonDeviationSlow) || (PastRateAdjustmentRatio >= EventHorizonDeviationFast)) {
1372+
assert(BlockReading);
1373+
break;
1374+
}
1375+
}
1376+
if (BlockReading->pprev == NULL) {
1377+
assert(BlockReading);
1378+
break;
1379+
}
1380+
BlockReading = BlockReading->pprev;
1381+
}
1382+
CBigNum bnNew(PastDifficultyAverage);
1383+
if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) {
1384+
bnNew *= PastRateActualSeconds;
1385+
bnNew /= PastRateTargetSeconds;
1386+
}
1387+
if (bnNew > bnProofOfWorkLimit) {
1388+
bnNew = bnProofOfWorkLimit;
1389+
}
1390+
/// debug print
1391+
printf("Difficulty Retarget - Kimoto Gravity Well\n");
1392+
printf("PastRateAdjustmentRatio = %g\n", PastRateAdjustmentRatio);
1393+
printf("Before: %08x %s\n", BlockLastSolved->nBits, CBigNum().SetCompact(BlockLastSolved->nBits).getuint256().ToString().c_str());
1394+
printf("After: %08x %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString().c_str());
1395+
return bnNew.GetCompact();
13961396
}
1397-
unsigned int static GetNextWorkRequired_V2(const CBlockIndex* pindexLast, const CBlockHeader *pblock)
1398-
{
1399-
static const int64 BlocksTargetSpacing = 40; // 2.5 minutes
1400-
unsigned int TimeDaySeconds = 60 * 60 * 24;
1401-
int64 PastSecondsMin = TimeDaySeconds * 0.01;
1402-
int64 PastSecondsMax = TimeDaySeconds * 0.14;
1403-
uint64 PastBlocksMin = PastSecondsMin / BlocksTargetSpacing;
1404-
uint64 PastBlocksMax = PastSecondsMax / BlocksTargetSpacing;
1405-
return KimotoGravityWell(pindexLast, pblock, BlocksTargetSpacing, PastBlocksMin, PastBlocksMax);
1397+
unsigned int static GetNextWorkRequired_V2(const CBlockIndex* pindexLast, const CBlockHeader *pblock) {
1398+
static const int64 BlocksTargetSpacing = 40; // 2.5 minutes
1399+
unsigned int TimeDaySeconds = 60 * 60 * 24;
1400+
int64 PastSecondsMin = TimeDaySeconds * 0.01;
1401+
int64 PastSecondsMax = TimeDaySeconds * 0.14;
1402+
uint64 PastBlocksMin = PastSecondsMin / BlocksTargetSpacing;
1403+
uint64 PastBlocksMax = PastSecondsMax / BlocksTargetSpacing;
1404+
return KimotoGravityWell(pindexLast, pblock, BlocksTargetSpacing, PastBlocksMin, PastBlocksMax);
14061405
}
14071406
unsigned int static GetNextWorkRequired_V3(const CBlockIndex* pindexLast, const CBlockHeader *pblock){
1408-
unsigned int nProofOfWorkLimit = bnProofOfWorkLimit.GetCompact();
1409-
int nHeight = pindexLast->nHeight + 1;
1410-
int64 retargetTimespan = nTargetTimespan;
1411-
int64 retargetSpacing = nTargetSpacing;
1412-
int64 retargetInterval = nInterval;
1413-
retargetInterval = nTargetTimespanNEW / nTargetSpacing;
1414-
retargetTimespan = nTargetTimespanNEW;
1415-
// Genesis block
1416-
if (pindexLast == NULL)
1417-
return nProofOfWorkLimit;
1418-
// Only change once per interval
1419-
if ((pindexLast->nHeight+1) % retargetInterval != 0)
1420-
{
1421-
// Special difficulty rule for testnet:
1422-
if (fTestNet)
1423-
{
1424-
// If the new block's timestamp is more than 2* nTargetSpacing minutes
1425-
// then allow mining of a min-difficulty block.
1426-
if (pblock->nTime > pindexLast->nTime + retargetSpacing*2)
1427-
return nProofOfWorkLimit;
1428-
else
1429-
{
1430-
// Return the last non-special-min-difficulty-rules-block
1431-
const CBlockIndex* pindex = pindexLast;
1432-
while (pindex->pprev && pindex->nHeight % retargetInterval != 0 && pindex->nBits == nProofOfWorkLimit)
1433-
pindex = pindex->pprev;
1434-
return pindex->nBits;
1435-
}
1436-
}
1437-
return pindexLast->nBits;
1438-
}
1439-
// Dogecoin: This fixes an issue where a 51% attack can change difficulty at will.
1440-
// Go back the full period unless it's the first retarget after genesis. Code courtesy of Art Forz
1441-
int blockstogoback = retargetInterval-1;
1442-
if ((pindexLast->nHeight+1) != retargetInterval)
1443-
blockstogoback = retargetInterval;
1444-
// Go back by what we want to be 14 days worth of blocks
1445-
const CBlockIndex* pindexFirst = pindexLast;
1446-
for (int i = 0; pindexFirst && i < blockstogoback; i++)
1447-
pindexFirst = pindexFirst->pprev;
1448-
assert(pindexFirst);
1449-
// Limit adjustment step
1450-
int64 nActualTimespan = pindexLast->GetBlockTime() - pindexFirst->GetBlockTime();
1451-
printf(" nActualTimespan = %"PRI64d" before bounds\n", nActualTimespan);
1452-
CBigNum bnNew;
1453-
bnNew.SetCompact(pindexLast->nBits);
1454-
//DigiShield implementation - thanks to RealSolid & WDC for this code
1455-
// amplitude filter - thanks to daft27 for this code
1456-
nActualTimespan = retargetTimespan + (nActualTimespan - retargetTimespan)/8;
1457-
printf("DIGISHIELD RETARGET\n");
1458-
if (nActualTimespan < (retargetTimespan - (retargetTimespan/4)) ) nActualTimespan = (retargetTimespan - (retargetTimespan/4));
1459-
if (nActualTimespan > (retargetTimespan + (retargetTimespan/2)) ) nActualTimespan = (retargetTimespan + (retargetTimespan/2));
1460-
// Retarget
1461-
bnNew *= nActualTimespan;
1462-
bnNew /= retargetTimespan;
1463-
if (bnNew > bnProofOfWorkLimit)
1464-
bnNew = bnProofOfWorkLimit;
1465-
/// debug print
1466-
printf("GetNextWorkRequired RETARGET\n");
1467-
printf("nTargetTimespan = %"PRI64d" nActualTimespan = %"PRI64d"\n", retargetTimespan, nActualTimespan);
1468-
printf("Before: %08x %s\n", pindexLast->nBits, CBigNum().SetCompact(pindexLast->nBits).getuint256().ToString().c_str());
1469-
printf("After: %08x %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString().c_str());
1470-
return bnNew.GetCompact();
1407+
unsigned int nProofOfWorkLimit = bnProofOfWorkLimit.GetCompact();
1408+
int64 retargetTimespan = nTargetTimespan;
1409+
int64 retargetSpacing = nTargetSpacing;
1410+
int64 retargetInterval = nInterval;
1411+
retargetInterval = nTargetTimespanNEW / nTargetSpacing;
1412+
retargetTimespan = nTargetTimespanNEW;
1413+
// Genesis block
1414+
if (pindexLast == NULL)
1415+
return nProofOfWorkLimit;
1416+
// Only change once per interval
1417+
if ((pindexLast->nHeight+1) % retargetInterval != 0)
1418+
{
1419+
// Special difficulty rule for testnet:
1420+
if (fTestNet)
1421+
{
1422+
// If the new block's timestamp is more than 2* nTargetSpacing minutes
1423+
// then allow mining of a min-difficulty block.
1424+
if (pblock->nTime > pindexLast->nTime + retargetSpacing*2)
1425+
return nProofOfWorkLimit;
1426+
else
1427+
{
1428+
// Return the last non-special-min-difficulty-rules-block
1429+
const CBlockIndex* pindex = pindexLast;
1430+
while (pindex->pprev && pindex->nHeight % retargetInterval != 0 && pindex->nBits == nProofOfWorkLimit)
1431+
pindex = pindex->pprev;
1432+
return pindex->nBits;
1433+
}
1434+
}
1435+
return pindexLast->nBits;
1436+
}
1437+
// Dogecoin: This fixes an issue where a 51% attack can change difficulty at will.
1438+
// Go back the full period unless it's the first retarget after genesis. Code courtesy of Art Forz
1439+
int blockstogoback = retargetInterval-1;
1440+
if ((pindexLast->nHeight+1) != retargetInterval)
1441+
blockstogoback = retargetInterval;
1442+
// Go back by what we want to be 14 days worth of blocks
1443+
const CBlockIndex* pindexFirst = pindexLast;
1444+
for (int i = 0; pindexFirst && i < blockstogoback; i++)
1445+
pindexFirst = pindexFirst->pprev;
1446+
assert(pindexFirst);
1447+
// Limit adjustment step
1448+
int64 nActualTimespan = pindexLast->GetBlockTime() - pindexFirst->GetBlockTime();
1449+
printf(" nActualTimespan = %"PRI64d" before bounds\n", nActualTimespan);
1450+
CBigNum bnNew;
1451+
bnNew.SetCompact(pindexLast->nBits);
1452+
//DigiShield implementation - thanks to RealSolid & WDC for this code
1453+
// amplitude filter - thanks to daft27 for this code
1454+
nActualTimespan = retargetTimespan + (nActualTimespan - retargetTimespan)/8;
1455+
printf("DIGISHIELD RETARGET\n");
1456+
if (nActualTimespan < (retargetTimespan - (retargetTimespan/4)) ) nActualTimespan = (retargetTimespan - (retargetTimespan/4));
1457+
if (nActualTimespan > (retargetTimespan + (retargetTimespan/2)) ) nActualTimespan = (retargetTimespan + (retargetTimespan/2));
1458+
// Retarget
1459+
bnNew *= nActualTimespan;
1460+
bnNew /= retargetTimespan;
1461+
if (bnNew > bnProofOfWorkLimit)
1462+
bnNew = bnProofOfWorkLimit;
1463+
/// debug print
1464+
printf("GetNextWorkRequired RETARGET\n");
1465+
printf("nTargetTimespan = %"PRI64d" nActualTimespan = %"PRI64d"\n", retargetTimespan, nActualTimespan);
1466+
printf("Before: %08x %s\n", pindexLast->nBits, CBigNum().SetCompact(pindexLast->nBits).getuint256().ToString().c_str());
1467+
printf("After: %08x %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString().c_str());
1468+
return bnNew.GetCompact();
14711469
}
14721470

14731471
unsigned int static GetPoSDifficulty(const CBlockIndex* pindexLast, const CBlockHeader *pblock)
14741472
{
1475-
printf("New PoS Difficulty Protocol ACTIVE");
1476-
1473+
if (fDebug)
1474+
printf("New PoS Difficulty Protocol ACTIVE");
1475+
14771476
CBigNum bnTargetLimit = bnProofOfWorkLimit;
1478-
1477+
14791478
const CBlockIndex* pindexPrev = GetLastBlockIndex(pindexLast, true);
14801479
const CBlockIndex* pindexPrevPrev = GetLastBlockIndex(pindexPrev->pprev, true);
1481-
1480+
14821481
// Reset difficulty for PoS switchover
14831482
if (pindexLast->nHeight < LAST_POW_BLOCK + 50)
14841483
return bnTargetLimit.GetCompact();
14851484

14861485
int64 nActualSpacing = pindexPrev->GetBlockTime() - pindexPrevPrev->GetBlockTime();
1487-
1486+
14881487
// Normalize extreme values
14891488
if (nActualSpacing < 1)
14901489
nActualSpacing = 1;
@@ -1508,7 +1507,7 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
15081507
// always mine PoW blocks at the lowest diff on testnet
15091508
if (fTestNet && pindexLast->nHeight < LAST_POW_BLOCK)
15101509
return bnProofOfWorkLimit.GetCompact();
1511-
1510+
15121511
// New mode for PoS
15131512
if (pindexLast->nHeight >= LAST_POW_BLOCK)
15141513
return GetPoSDifficulty(pindexLast, pblock);

src/main.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ class CTransaction
529529
public:
530530
static int64 nMinTxFee;
531531
static int64 nMinRelayTxFee;
532-
static const int CURRENT_VERSION=3;
532+
static const int CURRENT_VERSION=4;
533533
int nVersion;
534534
std::vector<CTxIn> vin;
535535
std::vector<CTxOut> vout;

src/version.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ static const int DATABASE_VERSION = 80000;
3030
// vchBlockSig field added to CBlock 1000000
3131
static const int POW_CLIENT_VERSION = 80701;
3232
static const int POW_PROTOCOL_VERSION = 70002;
33-
static const int POW_TX_VERSION = 1;
33+
static const int POW_TX_VERSION = 3;
3434
static const int POW_BLOCK_VERSION = 2;
3535

3636
// network protocol versioning
3737
//
38-
static const int PROTOCOL_VERSION = 70003;
38+
static const int PROTOCOL_VERSION = 70420;
3939

4040
// intial proto version, to be increased after version/verack negotiation
4141
static const int INIT_PROTO_VERSION = 209;
4242

4343
// disconnect from peers older than this proto version
44-
static const int MIN_PEER_PROTO_VERSION = 70002;
44+
static const int MIN_PEER_PROTO_VERSION = 70420;
4545

4646
// nTime field added to CAddress, starting with this version;
4747
// if possible, avoid requesting addresses nodes older than this

0 commit comments

Comments
 (0)