X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fcheckpoints.cpp;h=7de5883edda573cc173484379d3cee648a296351;hb=bf2ce95cb5b1da5d894e5302a7c4de0894c1cce2;hp=bd4eba7d87fc1811da34090bf3ca5b3deac8fd58;hpb=44f601723c3a1c68de6e613a897e9f8418eb874a;p=novacoin.git diff --git a/src/checkpoints.cpp b/src/checkpoints.cpp index bd4eba7..7de5883 100644 --- a/src/checkpoints.cpp +++ b/src/checkpoints.cpp @@ -163,7 +163,7 @@ namespace Checkpoints { txdb.TxnAbort(); hashInvalidCheckpoint = hashPendingCheckpoint; - return error("ProcessSyncCheckpoint: Reorganize failed for sync checkpoint %s", hashPendingCheckpoint.ToString().c_str()); + return error("AcceptPendingSyncCheckpoint: Reorganize failed for sync checkpoint %s", hashPendingCheckpoint.ToString().c_str()); } } txdb.Close(); @@ -185,6 +185,21 @@ namespace Checkpoints return false; } + // Automatically select a suitable sync-checkpoint + uint256 AutoSelectSyncCheckpoint() + { + // Proof-of-work blocks are immediately checkpointed + // to defend against 51% attack which rejects other miners block + + // Select the last proof-of-work block + const CBlockIndex *pindex = GetLastBlockIndex(pindexBest, false); + // Search forward for a block within max span and maturity window + while (pindex->pnext && (pindex->GetBlockTime() + CHECKPOINT_MAX_SPAN <= pindexBest->GetBlockTime() || pindex->nHeight + COINBASE_MATURITY <= pindexBest->nHeight)) + pindex = pindex->pnext; + return pindex->GetBlockHash(); + } + + // Check against synchronized checkpoint bool CheckSync(const uint256& hashBlock, const CBlockIndex* pindexPrev) { if (fTestNet) return true; // Testnet has no checkpoints @@ -273,6 +288,26 @@ namespace Checkpoints pfrom->AskFor(CInv(MSG_BLOCK, hashPendingCheckpoint)); } + bool SetCheckpointPrivKey(std::string strPrivKey) + { + // Test signing a sync-checkpoint with genesis block + CSyncCheckpoint checkpoint; + checkpoint.hashCheckpoint = hashGenesisBlock; + CDataStream sMsg(SER_NETWORK, PROTOCOL_VERSION); + sMsg << (CUnsignedSyncCheckpoint)checkpoint; + checkpoint.vchMsg = std::vector(sMsg.begin(), sMsg.end()); + + std::vector vchPrivKey = ParseHex(strPrivKey); + CKey key; + key.SetPrivKey(CPrivKey(vchPrivKey.begin(), vchPrivKey.end())); // if key is not correct openssl may crash + if (!key.Sign(Hash(checkpoint.vchMsg.begin(), checkpoint.vchMsg.end()), checkpoint.vchSig)) + return false; + + // Test signing successful, proceed + CSyncCheckpoint::strMasterPrivKey = strPrivKey; + return true; + } + bool SendSyncCheckpoint(uint256 hashCheckpoint) { CSyncCheckpoint checkpoint;