PPCoin: RPC commands 'getbranchpoint' and 'resetcheckpoint'
authorSunny King <p2pcoin@gmail.com>
Fri, 17 Feb 2012 21:23:43 +0000 (21:23 +0000)
committerSunny King <p2pcoin@gmail.com>
Fri, 17 Feb 2012 21:23:43 +0000 (21:23 +0000)
src/bitcoinrpc.cpp
src/checkpoints.cpp
src/checkpoints.h
src/db.cpp
src/db.h

index 18dccf5..5a697d9 100644 (file)
@@ -8,6 +8,7 @@
 #include "db.h"
 #include "net.h"
 #include "init.h"
+#include "checkpoints.h"
 #undef printf
 #include <boost/asio.hpp>
 #include <boost/iostreams/concepts.hpp>
@@ -1790,7 +1791,49 @@ 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;
+}
 
 
 
@@ -1845,6 +1888,8 @@ 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),
 };
 map<string, rpcfn_type> mapCallTable(pCallTable, pCallTable + sizeof(pCallTable)/sizeof(pCallTable[0]));
 
@@ -2477,6 +2522,7 @@ 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]);
 
         // Execute
         Object reply = CallRPC(strMethod, params);
index 69024e2..f3bfe5d 100644 (file)
@@ -1,5 +1,5 @@
 // Copyright (c) 2011 The Bitcoin developers
-// Copyright (c) 2011 The PPCoin developers
+// Copyright (c) 2011-2012 The PPCoin developers
 // Distributed under the MIT/X11 software license, see the accompanying
 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
 
@@ -27,6 +27,7 @@ namespace Checkpoints
 
     // 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)
     {
@@ -51,7 +52,10 @@ namespace Checkpoints
                 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;
@@ -128,6 +132,32 @@ namespace Checkpoints
         return mapCheckpoints.rbegin()->first;
     }
 
+    // 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;
+    }
+
     CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
     {
         if (fTestNet) return NULL;
index 241039d..f9037a2 100644 (file)
@@ -1,5 +1,5 @@
 // Copyright (c) 2011 The Bitcoin developers
-// Copyright (c) 2011 The PPCoin developers
+// Copyright (c) 2011-2012 The PPCoin developers
 // Distributed under the MIT/X11 software license, see the accompanying
 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
 #ifndef BITCOIN_CHECKPOINT_H
@@ -23,6 +23,7 @@ class CBlockIndex;
 namespace Checkpoints
 {
     extern int nAutoCheckpoint;
+    extern int nBranchPoint;
 
     // Returns true if block passes checkpoint checks
     bool CheckHardened(int nHeight, const uint256& hash);
@@ -31,6 +32,7 @@ namespace Checkpoints
     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();
index 40cc35e..c260fb3 100644 (file)
@@ -1,6 +1,6 @@
 // Copyright (c) 2009-2010 Satoshi Nakamoto
 // Copyright (c) 2011 The Bitcoin developers
-// Copyright (c) 2011 The PPCoin developers
+// Copyright (c) 2011-2012 The PPCoin developers
 // Distributed under the MIT/X11 software license, see the accompanying
 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
 
@@ -475,9 +475,10 @@ bool CTxDB::ReadAutoCheckpoint(int& nAutoCheckpoint)
     return Read(string("nAutoCheckpoint"), nAutoCheckpoint);
 }
 
-bool CTxDB::WriteAutoCheckpoint(int nCheckpoint)
+bool CTxDB::WriteAutoCheckpoint(int nCheckpoint, bool fReset)
 {
-    return Write(string("nAutoCheckpoint"), max(Checkpoints::nAutoCheckpoint, nCheckpoint));
+    nCheckpoint = fReset? nCheckpoint : max(Checkpoints::nAutoCheckpoint, nCheckpoint);
+    return Write(string("nAutoCheckpoint"), nCheckpoint);
 }
 
 CBlockIndex static * InsertBlockIndex(uint256 hash)
index 06e8dbd..b11ccac 100644 (file)
--- a/src/db.h
+++ b/src/db.h
@@ -1,6 +1,6 @@
 // Copyright (c) 2009-2010 Satoshi Nakamoto
 // Copyright (c) 2011 The Bitcoin developers
-// Copyright (c) 2011 The PPCoin developers
+// Copyright (c) 2011-2012 The PPCoin developers
 // Distributed under the MIT/X11 software license, see the accompanying
 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
 #ifndef BITCOIN_DB_H
@@ -292,7 +292,7 @@ public:
     bool ReadBestInvalidTrust(uint64& nBestInvalidTrust);
     bool WriteBestInvalidTrust(uint64 nBestInvalidTrust);
     bool ReadAutoCheckpoint(int& nAutoCheckpoint);
-    bool WriteAutoCheckpoint(int nCheckpoint);
+    bool WriteAutoCheckpoint(int nCheckpoint, bool fReset=false);
     bool LoadBlockIndex();
 };