From: Sunny King Date: Sun, 29 Jul 2012 20:13:33 +0000 (+0100) Subject: PPCoin: Only immediately checkpoint proof-of-work block X-Git-Tag: v0.4.0-unstable~120 X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=commitdiff_plain;h=3f94e09d9ae6fdc0bcb608d6f1f62e1491e4d576 PPCoin: Only immediately checkpoint proof-of-work block Max 4 hours trailing checkpoint from latest block --- diff --git a/src/checkpoints.cpp b/src/checkpoints.cpp index bd4eba7..5bb9df6 100644 --- a/src/checkpoints.cpp +++ b/src/checkpoints.cpp @@ -163,7 +163,7 @@ namespace Checkpoints { txdb.TxnAbort(); hashInvalidCheckpoint = hashPendingCheckpoint; - return error("ProcessSyncCheckpoint: Reorganize failed for sync checkpoint %s", hashPendingCheckpoint.ToString().c_str()); + return error("AcceptPendingSyncCheckpoint: Reorganize failed for sync checkpoint %s", hashPendingCheckpoint.ToString().c_str()); } } txdb.Close(); @@ -185,6 +185,21 @@ namespace Checkpoints return false; } + // Automatically select a suitable sync-checkpoint + uint256 AutoSelectSyncCheckpoint() + { + // Proof-of-work blocks are immediately checkpointed + // to defend against 51% attack which rejects other miners block + + // Select the last proof-of-work block + const CBlockIndex *pindex = GetLastBlockIndex(pindexBest, false); + // Search forward for a block within max span and maturity window + while (pindex->pnext && (pindex->GetBlockTime() + CHECKPOINT_MAX_SPAN <= pindexBest->GetBlockTime() || pindex->nHeight + COINBASE_MATURITY <= pindexBest->nHeight)) + pindex = pindex->pnext; + return pindex->GetBlockHash(); + } + + // Check against synchronized checkpoint bool CheckSync(const uint256& hashBlock, const CBlockIndex* pindexPrev) { if (fTestNet) return true; // Testnet has no checkpoints diff --git a/src/checkpoints.h b/src/checkpoints.h index cc4bebd..be18c79 100644 --- a/src/checkpoints.h +++ b/src/checkpoints.h @@ -10,6 +10,7 @@ #include "util.h" #define STAKE_MIN_AGE (60 * 60 * 24) // minimum age for coin age +#define CHECKPOINT_MAX_SPAN (60 * 60 * 4) // max 4 hours before latest block class uint256; class CBlockIndex; @@ -37,6 +38,7 @@ namespace Checkpoints CBlockIndex* GetLastSyncCheckpoint(); bool WriteSyncCheckpoint(const uint256& hashCheckpoint); bool AcceptPendingSyncCheckpoint(); + uint256 AutoSelectSyncCheckpoint(); bool CheckSync(const uint256& hashBlock, const CBlockIndex* pindexPrev); bool WantedByPendingSyncCheckpoint(uint256 hashBlock); bool ResetSyncCheckpoint(); diff --git a/src/main.cpp b/src/main.cpp index 96b05a2..cb4dd17 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2094,7 +2094,7 @@ bool ProcessBlock(CNode* pfrom, CBlock* pblock) // ppcoin: if responsible for sync-checkpoint send it if (pfrom && !CSyncCheckpoint::strMasterPrivKey.empty()) - Checkpoints::SendSyncCheckpoint(hashBestChain); + Checkpoints::SendSyncCheckpoint(Checkpoints::AutoSelectSyncCheckpoint()); return true; }