From: Sunny King Date: Sun, 10 Jun 2012 23:55:01 +0000 (+0100) Subject: PPCoin: Move CSyncCheckpoint class outside of Checkpoints namespace X-Git-Tag: v0.4.0-unstable~157 X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=commitdiff_plain;h=c5a1b2f61667ee11bae5183170535482c7b476d9 PPCoin: Move CSyncCheckpoint class outside of Checkpoints namespace --- diff --git a/src/checkpoints.cpp b/src/checkpoints.cpp index aeb0be7..8ea198c 100644 --- a/src/checkpoints.cpp +++ b/src/checkpoints.cpp @@ -83,31 +83,6 @@ namespace Checkpoints return true; } - bool CSyncCheckpoint::ProcessSyncCheckpoint(CNode* pfrom) - { - if (!CheckSignature()) - return false; - - CRITICAL_BLOCK(cs_hashSyncCheckpoint) - { - if (!mapBlockIndex.count(hashCheckpoint)) - { - // We haven't accepted this block, keep the checkpoint as pending - checkpointMessagePending = *this; - // Ask this guy to fill in what we're missing - if (pfrom) - pfrom->PushGetBlocks(pindexBest, hashCheckpoint); - return false; - } - if (!ValidateSyncCheckpoint(hashCheckpoint)) - return false; - hashSyncCheckpoint = this->hashCheckpoint; - checkpointMessage = *this; - checkpointMessagePending.SetNull(); - } - return true; - } - bool AcceptPendingSyncCheckpoint(uint256 hashAcceptedBlock) { if (!mapBlockIndex.count(hashAcceptedBlock)) @@ -247,3 +222,29 @@ namespace Checkpoints return true; } } + +// ppcoin: process synchronized checkpoint +bool CSyncCheckpoint::ProcessSyncCheckpoint(CNode* pfrom) +{ + if (!CheckSignature()) + return false; + + CRITICAL_BLOCK(Checkpoints::cs_hashSyncCheckpoint) + { + if (!mapBlockIndex.count(hashCheckpoint)) + { + // We haven't accepted this block, keep the checkpoint as pending + Checkpoints::checkpointMessagePending = *this; + // Ask this guy to fill in what we're missing + if (pfrom) + pfrom->PushGetBlocks(pindexBest, hashCheckpoint); + return false; + } + if (!Checkpoints::ValidateSyncCheckpoint(hashCheckpoint)) + return false; + Checkpoints::hashSyncCheckpoint = this->hashCheckpoint; + Checkpoints::checkpointMessage = *this; + Checkpoints::checkpointMessagePending.SetNull(); + } + return true; +} diff --git a/src/checkpoints.h b/src/checkpoints.h index 92c14b7..d193a2a 100644 --- a/src/checkpoints.h +++ b/src/checkpoints.h @@ -16,120 +16,120 @@ class uint256; class CBlockIndex; -// -// Block-chain checkpoints are compiled-in sanity checks. -// They are updated every release or three. -// -namespace Checkpoints +// ppcoin: synchronized checkpoint +class CUnsignedSyncCheckpoint { - // Returns true if block passes checkpoint checks - bool CheckHardened(int nHeight, const uint256& hash); +public: + int nVersion; + uint256 hashCheckpoint; // checkpoint block + + IMPLEMENT_SERIALIZE + ( + READWRITE(this->nVersion); + nVersion = this->nVersion; + READWRITE(hashCheckpoint); + ) + + void SetNull() + { + nVersion = 1; + hashCheckpoint = 0; + } - // Return conservative estimate of total number of blocks, 0 if unknown - int GetTotalBlocksEstimate(); + std::string ToString() const + { + return strprintf( + "CSyncCheckpoint(\n" + " nVersion = %d\n" + " hashCheckpoint = %s\n" + ")\n", + nVersion, + hashCheckpoint.ToString().c_str()); + } + + void print() const + { + printf("%s", ToString().c_str()); + } +}; - // Returns last CBlockIndex* in mapBlockIndex that is a checkpoint - CBlockIndex* GetLastCheckpoint(const std::map& mapBlockIndex); +class CSyncCheckpoint : public CUnsignedSyncCheckpoint +{ +public: + std::vector vchMsg; + std::vector vchSig; - // ppcoin: synchronized checkpoint - class CUnsignedSyncCheckpoint + CSyncCheckpoint() { - public: - int nVersion; - uint256 hashCheckpoint; // checkpoint block - - IMPLEMENT_SERIALIZE - ( - READWRITE(this->nVersion); - nVersion = this->nVersion; - READWRITE(hashCheckpoint); - ) - - void SetNull() - { - nVersion = 1; - hashCheckpoint = 0; - } + SetNull(); + } - std::string ToString() const - { - return strprintf( - "CSyncCheckpoint(\n" - " nVersion = %d\n" - " hashCheckpoint = %s\n" - ")\n", - nVersion, - hashCheckpoint.ToString().c_str()); - } + IMPLEMENT_SERIALIZE + ( + READWRITE(vchMsg); + READWRITE(vchSig); + ) - void print() const - { - printf("%s", ToString().c_str()); - } - }; - - class CSyncCheckpoint : public CUnsignedSyncCheckpoint + void SetNull() { - public: - std::vector vchMsg; - std::vector vchSig; + CUnsignedSyncCheckpoint::SetNull(); + vchMsg.clear(); + vchSig.clear(); + } - CSyncCheckpoint() - { - SetNull(); - } + bool IsNull() const + { + return (hashCheckpoint == 0); + } - IMPLEMENT_SERIALIZE - ( - READWRITE(vchMsg); - READWRITE(vchSig); - ) + uint256 GetHash() const + { + return SerializeHash(*this); + } - void SetNull() + bool RelayTo(CNode* pnode) const + { + // returns true if wasn't already sent + if (pnode->hashCheckpointKnown != hashCheckpoint) { - CUnsignedSyncCheckpoint::SetNull(); - vchMsg.clear(); - vchSig.clear(); + pnode->hashCheckpointKnown = hashCheckpoint; + pnode->PushMessage("checkpoint", *this); + return true; } + return false; + } - bool IsNull() const - { - return (hashCheckpoint == 0); - } + bool CheckSignature() + { + CKey key; + if (!key.SetPubKey(ParseHex("0487ca85b6ae9d311f996c7616d20d0c88a5b4f07d25e78f419019f35cce6522acf978b2d99f0e7a58db1f120439e5c1889266927854aa57c93956c2569188a539"))) + return error("CSyncCheckpoint::CheckSignature() : SetPubKey failed"); + if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig)) + return error("CSyncCheckpoint::CheckSignature() : verify signature failed"); - uint256 GetHash() const - { - return SerializeHash(*this); - } + // Now unserialize the data + CDataStream sMsg(vchMsg); + sMsg >> *(CUnsignedSyncCheckpoint*)this; + return true; + } - bool RelayTo(CNode* pnode) const - { - // returns true if wasn't already sent - if (pnode->hashCheckpointKnown != hashCheckpoint) - { - pnode->hashCheckpointKnown = hashCheckpoint; - pnode->PushMessage("checkpoint", *this); - return true; - } - return false; - } + bool ProcessSyncCheckpoint(CNode* pfrom); +}; - bool CheckSignature() - { - CKey key; - if (!key.SetPubKey(ParseHex("0487ca85b6ae9d311f996c7616d20d0c88a5b4f07d25e78f419019f35cce6522acf978b2d99f0e7a58db1f120439e5c1889266927854aa57c93956c2569188a539"))) - return error("CSyncCheckpoint::CheckSignature() : SetPubKey failed"); - if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig)) - return error("CSyncCheckpoint::CheckSignature() : verify signature failed"); - - // Now unserialize the data - CDataStream sMsg(vchMsg); - sMsg >> *(CUnsignedSyncCheckpoint*)this; - return true; - } +// +// Block-chain checkpoints are compiled-in sanity checks. +// They are updated every release or three. +// +namespace Checkpoints +{ + // Returns true if block passes checkpoint checks + bool CheckHardened(int nHeight, const uint256& hash); + + // Return conservative estimate of total number of blocks, 0 if unknown + int GetTotalBlocksEstimate(); - bool ProcessSyncCheckpoint(CNode* pfrom); - }; + // Returns last CBlockIndex* in mapBlockIndex that is a checkpoint + CBlockIndex* GetLastCheckpoint(const std::map& mapBlockIndex); extern uint256 hashSyncCheckpoint; extern CSyncCheckpoint checkpointMessage; diff --git a/src/main.cpp b/src/main.cpp index d6c10a4..f1b2694 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2588,7 +2588,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) else if (strCommand == "checkpoint") { - Checkpoints::CSyncCheckpoint checkpoint; + CSyncCheckpoint checkpoint; vRecv >> checkpoint; if (checkpoint.ProcessSyncCheckpoint(pfrom))