PPCoin: Remove automatic checkpoint code
authorScott Nadal <scott.nadal@gmail.com>
Tue, 26 Jun 2012 14:37:49 +0000 (15:37 +0100)
committerScott Nadal <scott.nadal@gmail.com>
Tue, 26 Jun 2012 14:37:49 +0000 (15:37 +0100)
src/bitcoinrpc.cpp
src/checkpoints.cpp
src/checkpoints.h
src/db.cpp
src/db.h
src/main.cpp
src/main.h

index dbbe67f..30edbf4 100644 (file)
@@ -1802,51 +1802,6 @@ Value getmemorypool(const Array& params, bool fHelp)
 }
 
 
-// ppcoin: reset auto checkpoint
-Value resetcheckpoint(const Array& params, bool fHelp)
-{
-    if (fHelp || params.size() < 1 || params.size() > 1)
-        throw runtime_error(
-            "resetcheckpoint <checkpointheight>\n"
-            "Reset automatic checkpoint to specified height.\n"
-            "<checkpointheight> is the height of the new checkpoint block.\n");
-
-    int nCheckpoint = params[0].get_int();
-    if (nCheckpoint <= 0 || nCheckpoint >= nBestHeight)
-        throw runtime_error(
-            "invalid checkpoint height.\n"
-        );
-    if (nCheckpoint >= Checkpoints::nAutoCheckpoint)
-        throw runtime_error(
-            "new checkpoint must be earlier than current auto checkpoint.\n"
-        );
-    if (!Checkpoints::ResetAutoCheckpoint(nCheckpoint))
-        throw runtime_error(
-            "internal error - reset checkpoint failed.\n"
-        );
-
-    return Value::null;
-}
-
-
-// ppcoin: get branch point of alternative branch
-Value getbranchpoint(const Array& params, bool fHelp)
-{
-    if (fHelp || params.size() != 0)
-        throw runtime_error(
-            "getbranchpoint\n"
-            "Returns height of branch point of alternative branch.\n");
-
-    Object result;
-    if (Checkpoints::nBranchPoint > 0)
-        result.push_back(Pair("branchpoint", Checkpoints::nBranchPoint));
-    else
-        result.push_back(Pair("branchpoint", "none"));
-    result.push_back(Pair("checkpoint", Checkpoints::nAutoCheckpoint));
-    return result;
-}
-
-
 // ppcoin: reserve balance from being staked for network protection
 Value reservebalance(const Array& params, bool fHelp)
 {
@@ -2106,8 +2061,6 @@ pair<string, rpcfn_type> pCallTable[] =
     make_pair("settxfee",               &settxfee),
     make_pair("getmemorypool",          &getmemorypool),
     make_pair("listsinceblock",        &listsinceblock),
-    make_pair("resetcheckpoint",        &resetcheckpoint),
-    make_pair("getbranchpoint",         &getbranchpoint),
     make_pair("reservebalance",         &reservebalance),
     make_pair("checkwallet",            &checkwallet),
     make_pair("repairwallet",           &repairwallet),
@@ -2140,8 +2093,6 @@ string pAllowInSafeMode[] =
     "validateaddress",
     "getwork",
     "getmemorypool",
-    "resetcheckpoint",
-    "getbranchpoint",
 };
 set<string> setAllowInSafeMode(pAllowInSafeMode, pAllowInSafeMode + sizeof(pAllowInSafeMode)/sizeof(pAllowInSafeMode[0]));
 
@@ -2753,7 +2704,6 @@ int CommandLineRPC(int argc, char *argv[])
             params[1] = v.get_obj();
         }
         if (strMethod == "sendmany"                && n > 2) ConvertTo<boost::int64_t>(params[2]);
-        if (strMethod == "resetcheckpoint"         && n > 0) ConvertTo<boost::int64_t>(params[0]);
         if (strMethod == "reservebalance"          && n > 0) ConvertTo<bool>(params[0]);
         if (strMethod == "reservebalance"          && n > 1) ConvertTo<double>(params[1]);
 
index d84c29d..204741c 100644 (file)
@@ -222,123 +222,6 @@ namespace Checkpoints
         }
         return false;
     }
-
-    // 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
-    //   - The immediate ancestor in main chain must not have height less than
-    //     checkpoint height
-    bool CheckAuto(const CBlockIndex *pindex)
-    {
-        while (pindex)
-        {
-            if (pindex->IsInMainChain())
-            {
-                if (pindex->nHeight >= nAutoCheckpoint)
-                    return true;
-                else
-                {
-                    nBranchPoint = pindex->nHeight;
-                    return error("Checkpoints: new block on alternative branch at height=%d before auto checkpoint at height=%d", pindex->nHeight, nAutoCheckpoint);
-                }
-            }
-            else
-                pindex = pindex->pprev;
-        }
-        return error("Checkpoints: failed to find any ancestor on main chain for the new block - internal error");
-    }
-
-    // ppcoin: get next chain checkpoint
-    int GetNextChainCheckpoint(const CBlockIndex *pindexLast)
-    {
-        CBigNum bnTarget;
-        CBigNum bnTargetMax = 0;  // max target of all blocks since checkpoint
-        CBigNum bnTargetMin = 0;  // min target of all candidate checkpoints
-        int nMinTargetHeight = 0; // min target height of candidate checkpoints
-        int nCheckpointMin = 0;   // minimum candidate checkpoint
-        int nCheckpointMax = 0;   // maximum candidate checkpoint
-        int nDepth = pindexLast->nHeight - pindexLast->nCheckpoint;
-        const CBlockIndex *pindex = pindexLast;
-        while (nDepth >= 0 && pindex)
-        {
-            bnTarget.SetCompact(pindex->nBits);
-            if (bnTarget > bnTargetMax)
-                bnTargetMax = bnTarget;
-            if (nCheckpointMax > 0 && bnTarget < bnTargetMin)
-            {
-                bnTargetMin = bnTarget;
-                nMinTargetHeight = pindex->nHeight;
-            }
-            if (nCheckpointMax == 0 && pindexLast->GetBlockTime() - pindex->GetBlockTime() > AUTO_CHECKPOINT_MIN_SPAN)
-            {
-                nCheckpointMax = pindex->nHeight;
-                bnTargetMin.SetCompact(pindex->nBits);
-                nMinTargetHeight = pindex->nHeight;
-            }
-            if (pindexLast->GetBlockTime() - pindex->GetBlockTime() < AUTO_CHECKPOINT_MAX_SPAN)
-                nCheckpointMin = pindex->nHeight;
-            pindex = pindex->pprev;
-            nDepth--;
-        }
-
-        assert (nDepth == -1);  // arrive at chain checkpoint now
-
-        printf("Checkpoints: min=%d max=%d tminheight=%d tmin=0x%08x tmax=0x%08x\n",
-            nCheckpointMin, nCheckpointMax, nMinTargetHeight,
-            bnTargetMin.GetCompact(), bnTargetMax.GetCompact());
-        if (nCheckpointMax == 0) // checkpoint stays if max candidate not found
-            return pindexLast->nCheckpoint;
-
-        if (bnTargetMin * 100 > bnTargetMax * 90)
-            return nCheckpointMax;
-        if (bnTarget * 100 > bnTargetMax * 90)
-            return nMinTargetHeight;
-        else
-            return nCheckpointMin;
-    }
-
-    // ppcoin: get next auto checkpoint from the new chain checkpoint
-    int GetNextAutoCheckpoint(int nCheckpoint)
-    {
-        return (std::max(nAutoCheckpoint, nCheckpoint));
-    }
-
-    // ppcoin: advance to next automatic checkpoint
-    void AdvanceAutoCheckpoint(int nCheckpoint)
-    {
-        nAutoCheckpoint = GetNextAutoCheckpoint(nCheckpoint);
-        printf("Checkpoints: auto checkpoint now at height=%d\n", nAutoCheckpoint);
-    }
-
-    // ppcoin: reset auto checkpoint
-    bool ResetAutoCheckpoint(int nCheckpoint)
-    {
-        if (nCheckpoint <= 0 || nCheckpoint > nBestHeight)
-            return error("ResetAutoCheckpoint() : new checkpoint invalid");
-        if (nCheckpoint >= nAutoCheckpoint)
-            return error("ResetAutoCheckpoint() : new checkpoint not earlier than current auto checkpoint");
-        CTxDB txdb;
-        txdb.TxnBegin();
-        if (!txdb.WriteAutoCheckpoint(nCheckpoint, true))
-            return error("ResetAutoCheckpoint() : database write failed");
-        if (!txdb.TxnCommit())
-            return error("ResetAutoCheckpoint() : database commit failed");
-        nAutoCheckpoint = nCheckpoint;
-        nBranchPoint = 0;  // clear branch point
-
-        // clear ban list to accept alternative branches
-        CRITICAL_BLOCK(cs_vNodes)
-        {
-            BOOST_FOREACH(CNode* pnode, vNodes)
-                pnode->ClearBanned();
-        }
-
-        return true;
-    }
 }
 
 // ppcoin: sync-checkpoint master key
index a52f720..9d5a166 100644 (file)
 
 class uint256;
 class CBlockIndex;
+class CSyncCheckpoint;
+
+//
+// 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();
+
+    // Returns last CBlockIndex* in mapBlockIndex that is a checkpoint
+    CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex);
+
+    extern uint256 hashSyncCheckpoint;
+    extern CSyncCheckpoint checkpointMessage;
+    extern uint256 hashInvalidCheckpoint;
+    extern CCriticalSection cs_hashSyncCheckpoint;
+
+    CBlockIndex* GetLastSyncCheckpoint();
+    bool AcceptPendingSyncCheckpoint();
+    uint256 AutoSelectSyncCheckpoint();
+    bool CheckSync(const uint256& hashBlock, const CBlockIndex* pindexPrev);
+    bool WantedByPendingSyncCheckpoint(uint256 hashBlock);
+}
 
 // ppcoin: synchronized checkpoint
 class CUnsignedSyncCheckpoint
@@ -105,41 +133,4 @@ public:
     bool ProcessSyncCheckpoint(CNode* pfrom);
 };
 
-//
-// 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();
-
-    // Returns last CBlockIndex* in mapBlockIndex that is a checkpoint
-    CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex);
-
-    extern uint256 hashSyncCheckpoint;
-    extern CSyncCheckpoint checkpointMessage;
-    extern uint256 hashInvalidCheckpoint;
-    extern CCriticalSection cs_hashSyncCheckpoint;
-
-    CBlockIndex* GetLastSyncCheckpoint();
-    bool AcceptPendingSyncCheckpoint();
-    uint256 AutoSelectSyncCheckpoint();
-    bool CheckSync(const uint256& hashBlock, const CBlockIndex* pindexPrev);
-    bool WantedByPendingSyncCheckpoint(uint256 hashBlock);
-
-    // 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
index 89f8b16..e52e6cf 100644 (file)
@@ -470,17 +470,6 @@ bool CTxDB::WriteBestInvalidTrust(uint64 nBestInvalidTrust)
     return Write(string("nBestInvalidTrust"), nBestInvalidTrust);
 }
 
-bool CTxDB::ReadAutoCheckpoint(int& nAutoCheckpoint)
-{
-    return Read(string("nAutoCheckpoint"), nAutoCheckpoint);
-}
-
-bool CTxDB::WriteAutoCheckpoint(int nCheckpoint, bool fReset)
-{
-    nCheckpoint = fReset? nCheckpoint : max(Checkpoints::nAutoCheckpoint, nCheckpoint);
-    return Write(string("nAutoCheckpoint"), nCheckpoint);
-}
-
 bool CTxDB::ReadSyncCheckpoint(uint256& hashCheckpoint)
 {
     return Read(string("hashSyncCheckpoint"), hashCheckpoint);
@@ -550,7 +539,6 @@ bool CTxDB::LoadBlockIndex()
             pindexNew->nBlockPos      = diskindex.nBlockPos;
             pindexNew->nChainTrust    = diskindex.nChainTrust;
             pindexNew->nHeight        = diskindex.nHeight;
-            pindexNew->nCheckpoint    = diskindex.nCheckpoint;
             pindexNew->fProofOfStake  = diskindex.fProofOfStake;
             pindexNew->prevoutStake   = diskindex.prevoutStake;
             pindexNew->nVersion       = diskindex.nVersion;
@@ -591,11 +579,6 @@ bool CTxDB::LoadBlockIndex()
     nBestChainTrust = pindexBest->nChainTrust;
     printf("LoadBlockIndex(): hashBestChain=%s  height=%d  trust=%d\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, nBestChainTrust);
 
-    // 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");
index f3b7cac..a899e51 100644 (file)
--- a/src/db.h
+++ b/src/db.h
@@ -291,8 +291,6 @@ public:
     bool WriteHashBestChain(uint256 hashBestChain);
     bool ReadBestInvalidTrust(uint64& nBestInvalidTrust);
     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();
index fc591be..3598c4a 100644 (file)
@@ -1200,8 +1200,6 @@ bool Reorganize(CTxDB& txdb, CBlockIndex* pindexNew)
     }
     if (!txdb.WriteHashBestChain(pindexNew->GetBlockHash()))
         return error("Reorganize() : WriteHashBestChain failed");
-    if (!txdb.WriteAutoCheckpoint(Checkpoints::GetNextAutoCheckpoint(pindexNew->nCheckpoint)))
-        return error("Reorganize() : WriteAutoCheckpoint failed");
 
     // Make sure it's successfully written to disk before changing memory structure
     if (!txdb.TxnCommit())
@@ -1237,7 +1235,6 @@ bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew)
     if (pindexGenesisBlock == NULL && hash == hashGenesisBlock)
     {
         txdb.WriteHashBestChain(hash);
-        txdb.WriteAutoCheckpoint(Checkpoints::GetNextAutoCheckpoint(pindexNew->nCheckpoint));
         if (!txdb.TxnCommit())
             return error("SetBestChain() : TxnCommit failed");
         pindexGenesisBlock = pindexNew;
@@ -1245,7 +1242,7 @@ bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew)
     else if (hashPrevBlock == hashBestChain)
     {
         // Adding to current best branch
-        if (!ConnectBlock(txdb, pindexNew) || !txdb.WriteHashBestChain(hash) || !txdb.WriteAutoCheckpoint(Checkpoints::GetNextAutoCheckpoint(pindexNew->nCheckpoint)))
+        if (!ConnectBlock(txdb, pindexNew) || !txdb.WriteHashBestChain(hash))
         {
             txdb.TxnAbort();
             InvalidChainFound(pindexNew);
@@ -1286,7 +1283,6 @@ bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew)
     nBestChainTrust = pindexNew->nChainTrust;
     nTimeBestReceived = GetTime();
     nTransactionsUpdated++;
-    Checkpoints::AdvanceAutoCheckpoint(pindexBest->nCheckpoint);
     printf("SetBestChain: new best=%s  height=%d  trust=%s\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, CBigNum(nBestChainTrust).ToString().c_str());
 
     return true;
@@ -1446,10 +1442,6 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
     {
         pindexNew->pprev = (*miPrev).second;
         pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
-
-        // ppcoin: compute chain checkpoint
-        pindexNew->nCheckpoint = Checkpoints::GetNextChainCheckpoint(pindexNew->pprev);
-        assert (pindexNew->nCheckpoint >= pindexNew->pprev->nCheckpoint);
     }
 
     // ppcoin: compute chain trust score
@@ -1583,10 +1575,6 @@ bool CBlock::AcceptBlock()
     if (!Checkpoints::CheckHardened(nHeight, hash))
         return DoS(100, error("AcceptBlock() : rejected by hardened checkpoint lockin at %d", nHeight));
 
-    // ppcoin: check that the block satisfies automatic checkpoint
-    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(hash, pindexPrev))
         return error("AcceptBlock() : rejected by synchronized checkpoint");
index 77d9199..2748269 100644 (file)
@@ -1132,7 +1132,6 @@ public:
     unsigned int nBlockPos;
     uint64 nChainTrust;// ppcoin: trust score of chain, in the unit of coin-days
     int nHeight;
-    int nCheckpoint;    // ppcoin: chain auto checkpoint height
     bool fProofOfStake; // ppcoin: is the block of proof-of-stake type
     COutPoint prevoutStake;
     unsigned int nStakeTime;
@@ -1154,7 +1153,6 @@ public:
         nBlockPos = 0;
         nHeight = 0;
         nChainTrust = 0;
-        nCheckpoint = 0;
         fProofOfStake = true;
         prevoutStake.SetNull();
         nStakeTime = 0;
@@ -1175,7 +1173,6 @@ public:
         nBlockPos = nBlockPosIn;
         nHeight = 0;
         nChainTrust = 0;
-        nCheckpoint = 0;
         fProofOfStake = block.IsProofOfStake();
         if (fProofOfStake)
         {
@@ -1288,8 +1285,8 @@ public:
 
     std::string ToString() const
     {
-        return strprintf("CBlockIndex(nprev=%08x, pnext=%08x, nFile=%d, nBlockPos=%-6d nChainTrust=%"PRI64d" nHeight=%d, nCheckpoint=%d, fProofOfStake=%d prevoutStake=(%s), nStakeTime=%d merkle=%s, hashBlock=%s)",
-            pprev, pnext, nFile, nBlockPos, nChainTrust, nHeight, nCheckpoint,
+        return strprintf("CBlockIndex(nprev=%08x, pnext=%08x, nFile=%d, nBlockPos=%-6d nChainTrust=%"PRI64d" nHeight=%d, fProofOfStake=%d prevoutStake=(%s), nStakeTime=%d merkle=%s, hashBlock=%s)",
+            pprev, pnext, nFile, nBlockPos, nChainTrust, nHeight,
             fProofOfStake, prevoutStake.ToString().c_str(), nStakeTime,
             hashMerkleRoot.ToString().substr(0,10).c_str(),
             GetBlockHash().ToString().substr(0,20).c_str());
@@ -1334,7 +1331,6 @@ public:
         READWRITE(nBlockPos);
         READWRITE(nChainTrust);
         READWRITE(nHeight);
-        READWRITE(nCheckpoint);
         READWRITE(fProofOfStake);
         if (fProofOfStake)
         {