PPCoin: Separate newmint value from stake in RPC 'getinfo' output
[novacoin.git] / src / checkpoints.cpp
index 4419a06..f3bfe5d 100644 (file)
@@ -1,31 +1,35 @@
 // Copyright (c) 2011 The Bitcoin 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.
 
-#include "checkpoints.h"
-#include "uint256.h"
-#include "util.h"
-
 #include <boost/assign/list_of.hpp> // for 'map_list_of()'
+#include <boost/foreach.hpp>
+
+#include "headers.h"
+#include "checkpoints.h"
 
 namespace Checkpoints
 {
-    typedef std::map<int, uint256> MapCheckpoints;
+    typedef std::map<int, uint256> MapCheckpoints;   // hardened checkpoints
 
+    //
+    // What makes a good checkpoint block?
+    // + Is surrounded by blocks with reasonable timestamps
+    //   (no blocks before with a timestamp after, none after with
+    //    timestamp before)
+    // + Contains no strange transactions
+    //
     static MapCheckpoints mapCheckpoints =
         boost::assign::map_list_of
-        ( 11111, uint256("0x0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d"))
-        ( 33333, uint256("0x000000002dd5588a74784eaa7ab0507a18ad16a236e7b1ce69f00d7ddfb5d0a6"))
-        ( 68555, uint256("0x00000000001e1b4903550a0b96e9a9405c8a95f387162e4944e8d9fbe501cd6a"))
-        ( 70567, uint256("0x00000000006a49b14bcf27462068f1264c961f11fa2e0eddd2be0791e1d4124a"))
-        ( 74000, uint256("0x0000000000573993a3c9e41ce34471c079dcf5f52a0e824a81e7f953b8661a20"))
-        (105000, uint256("0x00000000000291ce28027faea320c8d2b054b2e0fe44a773f3eefb151d6bdc97"))
-        (118000, uint256("0x000000000000774a7f8a7a12dc906ddb9e17e75d684f15e00f8767f9e8f36553"))
-        (134444, uint256("0x00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe"))
-        (140700, uint256("0x000000000000033b512028abb90e1626d8b346fd0ed598ac0a3c371138dce2bd"))
-        ;
-
-    bool CheckBlock(int nHeight, const uint256& hash)
+        ( 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
 
@@ -34,10 +38,138 @@ namespace Checkpoints
         return hash == i->second;
     }
 
+    // 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);
+    }
+
     int GetTotalBlocksEstimate()
     {
-        if (fTestNet) return 0; // Testnet has no checkpoints
+        if (fTestNet) return 0;
 
         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;
+
+        int64 nResult;
+        BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, mapCheckpoints)
+        {
+            const uint256& hash = i.second;
+            std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
+            if (t != mapBlockIndex.end())
+                return t->second;
+        }
+        return NULL;
+    }
 }