PPCoin: Enter safe mode if detected invalid synchronized checkpoint
authorScott Nadal <scott.nadal@gmail.com>
Fri, 22 Jun 2012 21:54:47 +0000 (22:54 +0100)
committerScott Nadal <scott.nadal@gmail.com>
Fri, 22 Jun 2012 21:54:47 +0000 (22:54 +0100)
src/checkpoints.cpp
src/checkpoints.h
src/main.cpp

index 518e66c..1384082 100644 (file)
@@ -60,6 +60,7 @@ namespace Checkpoints
     uint256 hashSyncCheckpoint;
     CSyncCheckpoint checkpointMessage;
     CSyncCheckpoint checkpointMessagePending;
+    uint256 hashInvalidCheckpoint = 0;
     CCriticalSection cs_hashSyncCheckpoint;
 
     // ppcoin: only descendant of current sync-checkpoint is allowed
@@ -72,15 +73,36 @@ namespace Checkpoints
 
         CBlockIndex* pindexSyncCheckpoint = mapBlockIndex[hashSyncCheckpoint];
         CBlockIndex* pindexCheckpointRecv = mapBlockIndex[hashCheckpoint];
+
         if (pindexCheckpointRecv->nHeight <= pindexSyncCheckpoint->nHeight)
-            return false;  // this is an older checkpoint, ignore
+        {
+            // Received an older checkpoint, trace back from current checkpoint
+            // to the same height of the received checkpoint to verify
+            // that current checkpoint should be a descendant block
+            CBlockIndex* pindex = pindexSyncCheckpoint;
+            while (pindex->nHeight > pindexCheckpointRecv->nHeight)
+                if (!(pindex = pindex->pprev))
+                    return error("ValidateSyncCheckpoint: pprev1 null - block index structure failure");
+            if (pindex->GetBlockHash() != hashCheckpoint)
+            {
+                hashInvalidCheckpoint = hashCheckpoint;
+                return error("ValidateSyncCheckpoint: new sync-checkpoint %s is conflicting with current sync-checkpoint %s", hashCheckpoint.ToString().c_str(), hashSyncCheckpoint.ToString().c_str());
+            }
+            return false; // ignore older checkpoint
+        }
 
+        // Received checkpoint should be a descendant block of the current
+        // checkpoint. Trace back to the same height of current checkpoint
+        // to verify.
         CBlockIndex* pindex = pindexCheckpointRecv;
         while (pindex->nHeight > pindexSyncCheckpoint->nHeight)
             if (!(pindex = pindex->pprev))
-                return error("ValidateSyncCheckpoint: pprev null - block index structure failure");
+                return error("ValidateSyncCheckpoint: pprev2 null - block index structure failure");
         if (pindex->GetBlockHash() != hashSyncCheckpoint)
+        {
+            hashInvalidCheckpoint = hashCheckpoint;
             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;
     }
 
@@ -104,6 +126,7 @@ namespace Checkpoints
                     if (!Reorganize(txdb, pindexCheckpoint))
                     {
                         txdb.TxnAbort();
+                        hashInvalidCheckpoint = checkpointMessagePending.hashCheckpoint;
                         return error("ProcessSyncCheckpoint: Reorganize failed for sync checkpoint %s", checkpointMessagePending.hashCheckpoint.ToString().c_str());
                     }
                 }
@@ -321,6 +344,7 @@ bool CSyncCheckpoint::ProcessSyncCheckpoint(CNode* pfrom)
             if (!Reorganize(txdb, pindexCheckpoint))
             {
                 txdb.TxnAbort();
+                Checkpoints::hashInvalidCheckpoint = hashCheckpoint;
                 return error("ProcessSyncCheckpoint: Reorganize failed for sync checkpoint %s", hashCheckpoint.ToString().c_str());
             }
         }
index e90458e..caafb09 100644 (file)
@@ -133,6 +133,7 @@ namespace Checkpoints
 
     extern uint256 hashSyncCheckpoint;
     extern CSyncCheckpoint checkpointMessage;
+    extern uint256 hashInvalidCheckpoint;
     extern CCriticalSection cs_hashSyncCheckpoint;
 
     bool AcceptPendingSyncCheckpoint();
index 4e27d4b..a50b5ab 100644 (file)
@@ -1979,6 +1979,12 @@ string GetWarnings(string strFor)
         strStatusBar = strRPC = "WARNING: Displayed transactions may not be correct!  You may need to upgrade, or other nodes may need to upgrade.";
     }
 
+    if (Checkpoints::hashInvalidCheckpoint != 0)
+    {
+        nPriority = 3000;
+        strStatusBar = strRPC = "WARNING: Invalid checkpoint found!  Displayed transactions may not be correct!  You may need to upgrade, or other nodes may need to upgrade.";
+    }
+
     // Alerts
     CRITICAL_BLOCK(cs_mapAlerts)
     {