From dffda08fddf00889dd710ab704e9e99929be325a Mon Sep 17 00:00:00 2001 From: Scott Nadal Date: Fri, 15 Jun 2012 16:32:28 +0100 Subject: [PATCH] PPCoin: Save sync-checkpoint to db and enforce sync-checkpoint --- src/checkpoints.cpp | 35 ++++++++++++++++++++++++++++++----- src/checkpoints.h | 3 ++- src/main.cpp | 6 +++++- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/checkpoints.cpp b/src/checkpoints.cpp index 097dd87..d986b07 100644 --- a/src/checkpoints.cpp +++ b/src/checkpoints.cpp @@ -83,19 +83,22 @@ namespace Checkpoints return true; } - bool AcceptPendingSyncCheckpoint(uint256 hashAcceptedBlock) + bool AcceptPendingSyncCheckpoint() { - if (!mapBlockIndex.count(hashAcceptedBlock)) - return false; - CRITICAL_BLOCK(cs_hashSyncCheckpoint) - if ((!checkpointMessagePending.IsNull()) && checkpointMessagePending.hashCheckpoint == hashAcceptedBlock) + if ((!checkpointMessagePending.IsNull()) && mapBlockIndex.count(checkpointMessagePending.hashCheckpoint)) { if (!ValidateSyncCheckpoint(checkpointMessagePending.hashCheckpoint)) { checkpointMessagePending.SetNull(); return false; } + + CTxDB txdb; + if (!txdb.WriteSyncCheckpoint(checkpointMessagePending.hashCheckpoint)) + return error("AcceptPendingSyncCheckpoint() : failed to write to db sync checkpoint %s\n", checkpointMessagePending.hashCheckpoint.ToString().c_str()); + txdb.Close(); + hashSyncCheckpoint = checkpointMessagePending.hashCheckpoint; checkpointMessage = checkpointMessagePending; checkpointMessagePending.SetNull(); @@ -118,6 +121,22 @@ namespace Checkpoints return pindex->GetBlockHash(); } + // Check against synchronized checkpoint + bool CheckSync(int nHeight, const uint256& hashBlock) + { + if (fTestNet) return true; // Testnet has no checkpoints + + CRITICAL_BLOCK(cs_hashSyncCheckpoint) + { + CBlockIndex* pindexSync = mapBlockIndex[hashSyncCheckpoint]; + if (nHeight == pindexSync->nHeight && hashBlock != hashSyncCheckpoint) + return false; // same height with sync-checkpoint + if (nHeight < pindexSync->nHeight && !mapBlockIndex.count(hashBlock)) + return false; // lower height than sync-checkpoint + } + return true; + } + // ppcoin: automatic checkpoint (represented by height of checkpoint) int nAutoCheckpoint = 0; int nBranchPoint = 0; // branch point to alternative branch @@ -256,6 +275,12 @@ bool CSyncCheckpoint::ProcessSyncCheckpoint(CNode* pfrom) } if (!Checkpoints::ValidateSyncCheckpoint(hashCheckpoint)) return false; + + CTxDB txdb; + if (!txdb.WriteSyncCheckpoint(this->hashCheckpoint)) + return error("ProcessSyncCheckpoint() : failed to write to db sync checkpoint %s\n", this->hashCheckpoint.ToString().c_str()); + txdb.Close(); + Checkpoints::hashSyncCheckpoint = this->hashCheckpoint; Checkpoints::checkpointMessage = *this; Checkpoints::checkpointMessagePending.SetNull(); diff --git a/src/checkpoints.h b/src/checkpoints.h index e411c12..db3fe19 100644 --- a/src/checkpoints.h +++ b/src/checkpoints.h @@ -135,8 +135,9 @@ namespace Checkpoints extern CSyncCheckpoint checkpointMessage; extern CCriticalSection cs_hashSyncCheckpoint; - bool AcceptPendingSyncCheckpoint(uint256 hashAcceptedBlock); + bool AcceptPendingSyncCheckpoint(); uint256 AutoSelectSyncCheckpoint(); + bool CheckSync(int nHeight, const uint256& hashBlock); // ppcoin: automatic checkpoint extern int nAutoCheckpoint; diff --git a/src/main.cpp b/src/main.cpp index a8b65a8..4de69bf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1578,6 +1578,10 @@ bool CBlock::AcceptBlock() if (!Checkpoints::CheckAuto(pindexPrev)) return DoS(100, error("AcceptBlock() : rejected by automatic checkpoint at %d", Checkpoints::nAutoCheckpoint)); + // ppcoin: check that the block satisfies synchronized checkpoint + if (!Checkpoints::CheckSync(nHeight, hash)) + return DoS(100, error("AcceptBlock() : rejected by synchronized checkpoint")); + // Write block to history file if (!CheckDiskSpace(::GetSerializeSize(*this, SER_DISK))) return error("AcceptBlock() : out of disk space"); @@ -1596,7 +1600,7 @@ bool CBlock::AcceptBlock() pnode->PushInventory(CInv(MSG_BLOCK, hash)); // ppcoin: check pending sync-checkpoint - Checkpoints::AcceptPendingSyncCheckpoint(hash); + Checkpoints::AcceptPendingSyncCheckpoint(); return true; } -- 1.7.1