From: Sunny King Date: Wed, 30 May 2012 20:36:36 +0000 (+0100) Subject: PPCoin: Define synchronized checkpoint X-Git-Tag: v0.4.0-unstable~167 X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=commitdiff_plain;h=5fd6c2c30e1a0dd25e6efe20a55ca1b212e80c98 PPCoin: Define synchronized checkpoint --- diff --git a/src/checkpoints.cpp b/src/checkpoints.cpp index f3bfe5d..89e53ca 100644 --- a/src/checkpoints.cpp +++ b/src/checkpoints.cpp @@ -25,10 +25,6 @@ namespace Checkpoints ( 0, hashGenesisBlock ) ; // ppcoin: no checkpoint yet; to be created in future releases - // ppcoin: automatic checkpoint (represented by height of checkpoint) - int nAutoCheckpoint = 0; - int nBranchPoint = 0; // branch point to alternative branch - bool CheckHardened(int nHeight, const uint256& hash) { if (fTestNet) return true; // Testnet has no checkpoints @@ -38,6 +34,39 @@ namespace Checkpoints return hash == i->second; } + int GetTotalBlocksEstimate() + { + if (fTestNet) return 0; + + return mapCheckpoints.rbegin()->first; + } + + CBlockIndex* GetLastCheckpoint(const std::map& mapBlockIndex) + { + if (fTestNet) return NULL; + + int64 nResult; + BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, mapCheckpoints) + { + const uint256& hash = i.second; + std::map::const_iterator t = mapBlockIndex.find(hash); + if (t != mapBlockIndex.end()) + return t->second; + } + return NULL; + } + + // ppcoin: synchronized checkpoint (centrally broadcasted) + uint256 hashSyncCheckpoint; + + bool AcceptNewSyncCheckpoint(uint256 hashCheckpoint) + { + } + + // ppcoin: automatic checkpoint (represented by height of checkpoint) + int nAutoCheckpoint = 0; + int nBranchPoint = 0; // branch point to alternative branch + // ppcoin: check automatic checkpoint // To pass the check: // - All ancestors (including the block itself) have block index already @@ -125,13 +154,6 @@ namespace Checkpoints printf("Checkpoints: auto checkpoint now at height=%d\n", nAutoCheckpoint); } - int GetTotalBlocksEstimate() - { - if (fTestNet) return 0; - - return mapCheckpoints.rbegin()->first; - } - // ppcoin: reset auto checkpoint bool ResetAutoCheckpoint(int nCheckpoint) { @@ -157,19 +179,4 @@ namespace Checkpoints return true; } - - CBlockIndex* GetLastCheckpoint(const std::map& mapBlockIndex) - { - if (fTestNet) return NULL; - - int64 nResult; - BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, mapCheckpoints) - { - const uint256& hash = i.second; - std::map::const_iterator t = mapBlockIndex.find(hash); - if (t != mapBlockIndex.end()) - return t->second; - } - return NULL; - } } diff --git a/src/checkpoints.h b/src/checkpoints.h index c984798..033b228 100644 --- a/src/checkpoints.h +++ b/src/checkpoints.h @@ -22,23 +22,27 @@ class CBlockIndex; // namespace Checkpoints { - extern int nAutoCheckpoint; - extern int nBranchPoint; - // Returns true if block passes checkpoint checks bool CheckHardened(int nHeight, const uint256& hash); - bool CheckAuto(const CBlockIndex *pindex); - - int GetNextChainCheckpoint(const CBlockIndex *pindex); - int GetNextAutoCheckpoint(int nCheckpoint); - void AdvanceAutoCheckpoint(int nCheckpoint); - bool ResetAutoCheckpoint(int nCheckpoint); // Return conservative estimate of total number of blocks, 0 if unknown int GetTotalBlocksEstimate(); // Returns last CBlockIndex* in mapBlockIndex that is a checkpoint CBlockIndex* GetLastCheckpoint(const std::map& mapBlockIndex); + + // ppcoin: synchronized checkpoint + extern uint256 hashSyncCheckpoint; + + // ppcoin: automatic checkpoint + extern int nAutoCheckpoint; + extern int nBranchPoint; + + bool CheckAuto(const CBlockIndex *pindex); + int GetNextChainCheckpoint(const CBlockIndex *pindex); + int GetNextAutoCheckpoint(int nCheckpoint); + void AdvanceAutoCheckpoint(int nCheckpoint); + bool ResetAutoCheckpoint(int nCheckpoint); } #endif diff --git a/src/db.cpp b/src/db.cpp index d85e4a9..a51c217 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -481,6 +481,16 @@ bool CTxDB::WriteAutoCheckpoint(int nCheckpoint, bool fReset) return Write(string("nAutoCheckpoint"), nCheckpoint); } +bool CTxDB::ReadSyncCheckpoint(uint256& hashCheckpoint) +{ + return Read(string("hashSyncCheckpoint"), hashCheckpoint); +} + +bool CTxDB::WriteSyncCheckpoint(uint256 hashCheckpoint) +{ + return Write(string("hashSyncCheckpoint"), hashCheckpoint); +} + CBlockIndex static * InsertBlockIndex(uint256 hash) { if (hash == 0) @@ -581,11 +591,16 @@ bool CTxDB::LoadBlockIndex() nBestChainTrust = pindexBest->nChainTrust; printf("LoadBlockIndex(): hashBestChain=%s height=%d trust=%d\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, nBestChainTrust); - // Load nAutoCheckpoint + // ppcoin: load nAutoCheckpoint if (!ReadAutoCheckpoint(Checkpoints::nAutoCheckpoint)) return error("CTxDB::LoadBlockIndex() : nAutoCheckpoint not loaded"); printf("LoadBlockIndex(): automatic checkpoint at height=%d\n", Checkpoints::nAutoCheckpoint); + // ppcoin: load hashSyncCheckpoint + if (!ReadSyncCheckpoint(Checkpoints::hashSyncCheckpoint)) + return error("CTxDB::LoadBlockIndex() : hashSyncCheckpoint not loaded"); + printf("LoadBlockIndex(): synchronized checkpoint %s\n", Checkpoints::hashSyncCheckpoint.ToString().c_str()); + // Load nBestInvalidTrust, OK if it doesn't exist ReadBestInvalidTrust(nBestInvalidTrust); diff --git a/src/db.h b/src/db.h index b11ccac..f3b7cac 100644 --- a/src/db.h +++ b/src/db.h @@ -293,6 +293,8 @@ public: bool WriteBestInvalidTrust(uint64 nBestInvalidTrust); bool ReadAutoCheckpoint(int& nAutoCheckpoint); bool WriteAutoCheckpoint(int nCheckpoint, bool fReset=false); + bool ReadSyncCheckpoint(uint256& hashCheckpoint); + bool WriteSyncCheckpoint(uint256 hashCheckpoint); bool LoadBlockIndex(); };