PPCoin: Automatically select a checkpoint for 'sendcheckpoint'
[novacoin.git] / src / checkpoints.cpp
index 4419a06..097dd87 100644 (file)
@@ -1,31 +1,31 @@
 // 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
+
+    bool CheckHardened(int nHeight, const uint256& hash)
     {
         if (fTestNet) return true; // Testnet has no checkpoints
 
@@ -36,8 +36,230 @@ namespace Checkpoints
 
     int GetTotalBlocksEstimate()
     {
-        if (fTestNet) return 0; // Testnet has no checkpoints
+        if (fTestNet) return 0;
 
         return mapCheckpoints.rbegin()->first;
     }
+
+    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;
+    }
+
+    // ppcoin: synchronized checkpoint (centrally broadcasted)
+    uint256 hashSyncCheckpoint;
+    CSyncCheckpoint checkpointMessage;
+    CSyncCheckpoint checkpointMessagePending;
+    CCriticalSection cs_hashSyncCheckpoint;
+
+    // ppcoin: only descendant of current sync-checkpoint is allowed
+    bool ValidateSyncCheckpoint(uint256 hashCheckpoint)
+    {
+        if (!mapBlockIndex.count(hashSyncCheckpoint))
+            return error("ValidateSyncCheckpoint: block index missing for current sync-checkpoint %s", hashSyncCheckpoint.ToString().c_str());
+        if (!mapBlockIndex.count(hashCheckpoint))
+            return error("ValidateSyncCheckpoint: block index missing for received sync-checkpoint %s", hashCheckpoint.ToString().c_str());
+
+        CBlockIndex* pindexSyncCheckpoint = mapBlockIndex[hashSyncCheckpoint];
+        CBlockIndex* pindexCheckpointRecv = mapBlockIndex[hashCheckpoint];
+        if (pindexCheckpointRecv->nHeight <= pindexSyncCheckpoint->nHeight)
+            return false;  // this is an older checkpoint, ignore
+
+        CBlockIndex* pindex = pindexCheckpointRecv;
+        while (pindex->nHeight > pindexSyncCheckpoint->nHeight)
+            pindex = pindex->pprev;
+        if (pindex->GetBlockHash() != hashSyncCheckpoint)
+            return error("ValidateSyncCheckpoint: new sync-checkpoint %s is not a descendant of current sync-checkpoint %s", hashCheckpoint.ToString().c_str(), hashSyncCheckpoint.ToString().c_str());
+        return true;
+    }
+
+    bool AcceptPendingSyncCheckpoint(uint256 hashAcceptedBlock)
+    {
+        if (!mapBlockIndex.count(hashAcceptedBlock))
+            return false;
+
+        CRITICAL_BLOCK(cs_hashSyncCheckpoint)
+            if ((!checkpointMessagePending.IsNull()) && checkpointMessagePending.hashCheckpoint == hashAcceptedBlock)
+            {
+                if (!ValidateSyncCheckpoint(checkpointMessagePending.hashCheckpoint))
+                {
+                    checkpointMessagePending.SetNull();
+                    return false;
+                }
+                hashSyncCheckpoint = checkpointMessagePending.hashCheckpoint;
+                checkpointMessage = checkpointMessagePending;
+                checkpointMessagePending.SetNull();
+                printf("AcceptPendingSyncCheckpoint : sync-checkpoint at %s\n", hashSyncCheckpoint.ToString().c_str());
+                // relay the checkpoint
+                BOOST_FOREACH(CNode* pnode, vNodes)
+                    checkpointMessage.RelayTo(pnode);
+                return true;
+            }
+
+        return false;
+    }
+
+    uint256 AutoSelectSyncCheckpoint()
+    {
+        // select block roughly 8 hours ago
+        CBlockIndex *pindex = mapBlockIndex[hashSyncCheckpoint];
+        while (pindex->pnext && pindex->pnext->GetBlockTime() + AUTO_CHECKPOINT_MIN_SPAN <= GetAdjustedTime())
+            pindex = pindex->pnext;
+        return pindex->GetBlockHash();
+    }
+
+    // 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: 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;
+            printf("ProcessSyncCheckpoint : pending for sync-checkpoint %s\n", hashCheckpoint.ToString().c_str());
+            // 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();
+        printf("ProcessSyncCheckpoint : sync-checkpoint at %s\n", Checkpoints::hashSyncCheckpoint.ToString().c_str());
+    }
+    return true;
 }