PPCoin: Reset sync-checkpoint when checkpoint master key changes
[novacoin.git] / src / db.cpp
index e52e6cf..2d6293d 100644 (file)
@@ -480,6 +480,16 @@ bool CTxDB::WriteSyncCheckpoint(uint256 hashCheckpoint)
     return Write(string("hashSyncCheckpoint"), hashCheckpoint);
 }
 
+bool CTxDB::ReadCheckpointPubKey(string& strPubKey)
+{
+    return Read(string("strCheckpointPubKey"), strPubKey);
+}
+
+bool CTxDB::WriteCheckpointPubKey(const string& strPubKey)
+{
+    return Write(string("strCheckpointPubKey"), strPubKey);
+}
+
 CBlockIndex static * InsertBlockIndex(uint256 hash)
 {
     if (hash == 0)
@@ -584,6 +594,19 @@ bool CTxDB::LoadBlockIndex()
         return error("CTxDB::LoadBlockIndex() : hashSyncCheckpoint not loaded");
     printf("LoadBlockIndex(): synchronized checkpoint %s\n", Checkpoints::hashSyncCheckpoint.ToString().c_str());
 
+    // ppcoin: if checkpoint master key changed must reset sync-checkpoint
+    string strPubKey = "";
+    if (!ReadCheckpointPubKey(strPubKey) || strPubKey != CSyncCheckpoint::strMasterPubKey)
+    {
+        // write checkpoint master key to db
+        TxnBegin();
+        WriteCheckpointPubKey(CSyncCheckpoint::strMasterPubKey);
+        if (!TxnCommit())
+            return error("CTxDB::LoadBlockIndex() : failed to write new checkpoint master key");
+        if (!Checkpoints::ResetSyncCheckpoint())
+            return error("CTxDB::LoadBlockIndex() : failed to reset sync-checkpoint");
+    }
+
     // Load nBestInvalidTrust, OK if it doesn't exist
     ReadBestInvalidTrust(nBestInvalidTrust);