X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fcheckpoints.cpp;h=aeb0be77b361c847586a0c083c3198b78f451216;hb=42535b58bd40e14ea489dd595474c57ca73e52ea;hp=ac86c95b284a56ee933ecfedb0d57b78844c42f3;hpb=2595f99dd9b110b2efcb187da825477f6867a548;p=novacoin.git diff --git a/src/checkpoints.cpp b/src/checkpoints.cpp index ac86c95..aeb0be7 100644 --- a/src/checkpoints.cpp +++ b/src/checkpoints.cpp @@ -62,6 +62,27 @@ namespace Checkpoints CSyncCheckpoint checkpointMessagePending; CCriticalSection cs_hashSyncCheckpoint; + // ppcoin: only descendant of current sync-checkpoint is allowed + bool ValidateSyncCheckpoint(uint256 hashCheckpoint) + { + if (!mapBlockIndex.count(hashSyncCheckpoint)) + return error("ValidateSyncCheckpoint: block index missing for current sync-checkpoint %s", hashSyncCheckpoint.ToString().c_str()); + if (!mapBlockIndex.count(hashCheckpoint)) + return error("ValidateSyncCheckpoint: block index missing for received sync-checkpoint %s", hashCheckpoint.ToString().c_str()); + + CBlockIndex* pindexSyncCheckpoint = mapBlockIndex[hashSyncCheckpoint]; + CBlockIndex* pindexCheckpointRecv = mapBlockIndex[hashCheckpoint]; + if (pindexCheckpointRecv->nHeight <= pindexSyncCheckpoint->nHeight) + return false; // this is an older checkpoint, ignore + + CBlockIndex* pindex = pindexCheckpointRecv; + while (pindex->nHeight > pindexSyncCheckpoint->nHeight) + pindex = pindex->pprev; + if (pindex->GetBlockHash() != hashSyncCheckpoint) + return error("ValidateSyncCheckpoint: new sync-checkpoint %s is not a descendant of current sync-checkpoint %s", hashCheckpoint.ToString().c_str(), hashSyncCheckpoint.ToString().c_str()); + return true; + } + bool CSyncCheckpoint::ProcessSyncCheckpoint(CNode* pfrom) { if (!CheckSignature()) @@ -78,26 +99,37 @@ namespace Checkpoints pfrom->PushGetBlocks(pindexBest, hashCheckpoint); return false; } - - if (!mapBlockIndex.count(hashSyncCheckpoint)) - return error("ProcessSyncCheckpoint: block index missing for synchronized checkpoint %s", hashSyncCheckpoint.ToString().c_str()); - - CBlockIndex* pindexSyncCheckpoint = mapBlockIndex[hashSyncCheckpoint]; - CBlockIndex* pindexCheckpointPending = mapBlockIndex[hashCheckpoint]; - if (pindexCheckpointPending->nHeight <= pindexSyncCheckpoint->nHeight) - return false; // this is an older checkpoint, ignore - - CBlockIndex* pindex = pindexCheckpointPending; - while (pindex->nHeight > pindexSyncCheckpoint->nHeight) - pindex = pindex->pprev; - if (pindex->GetBlockHash() != hashSyncCheckpoint) - return error("ProcessSyncCheckpoint: new sync-checkpoint %s is not a descendant of current sync-checkpoint %s", hashCheckpoint.ToString().c_str(), hashSyncCheckpoint.ToString().c_str()); + if (!ValidateSyncCheckpoint(hashCheckpoint)) + return false; hashSyncCheckpoint = this->hashCheckpoint; checkpointMessage = *this; + checkpointMessagePending.SetNull(); } return true; } + bool AcceptPendingSyncCheckpoint(uint256 hashAcceptedBlock) + { + if (!mapBlockIndex.count(hashAcceptedBlock)) + return false; + + CRITICAL_BLOCK(cs_hashSyncCheckpoint) + if ((!checkpointMessagePending.IsNull()) && checkpointMessagePending.hashCheckpoint == hashAcceptedBlock) + { + if (!ValidateSyncCheckpoint(checkpointMessagePending.hashCheckpoint)) + { + checkpointMessagePending.SetNull(); + return false; + } + hashSyncCheckpoint = checkpointMessagePending.hashCheckpoint; + checkpointMessage = checkpointMessagePending; + checkpointMessagePending.SetNull(); + return true; + } + + return false; + } + // ppcoin: automatic checkpoint (represented by height of checkpoint) int nAutoCheckpoint = 0; int nBranchPoint = 0; // branch point to alternative branch