// Copyright (c) 2011 The Bitcoin developers
+// Copyright (c) 2011 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.
namespace Checkpoints
{
- typedef std::map<int, uint256> MapCheckpoints;
+ typedef std::map<int, uint256> MapCheckpoints; // hardened checkpoints
//
// What makes a good checkpoint block?
( 0, hashGenesisBlock )
; // ppcoin: no checkpoint yet; to be created in future releases
- bool CheckBlock(int nHeight, const uint256& hash)
+ // ppcoin: automatic checkpoint (represented by height of checkpoint)
+ int nAutoCheckpoint = 0;
+
+ bool CheckHardened(int nHeight, const uint256& hash)
{
if (fTestNet) return true; // Testnet has no 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
+ 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 auto checkpoint in the chain
+ 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 since checkpoint
+ 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 std::min(nCheckpointMax, nMinTargetHeight);
+ else
+ return nCheckpointMin;
+ }
+
+ // ppcoin: advance to next automatic checkpoint
+ void AdvanceAutoCheckpoint(int nCheckpoint)
+ {
+ nAutoCheckpoint = std::max(nAutoCheckpoint, nCheckpoint);
+ printf("Checkpoints: auto checkpoint now at height=%d\n", nAutoCheckpoint);
+ }
+
int GetTotalBlocksEstimate()
{
if (fTestNet) return 0;
// Copyright (c) 2011 The Bitcoin developers
+// Copyright (c) 2011 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.
#ifndef BITCOIN_CHECKPOINT_H
#include <map>
#include "util.h"
+// ppcoin: auto checkpoint min at 1 day; max at 1 week
+#define AUTO_CHECKPOINT_MIN_SPAN (60 * 60 * 24)
+#define AUTO_CHECKPOINT_MAX_SPAN (60 * 60 * 24 * 7)
+
class uint256;
class CBlockIndex;
//
namespace Checkpoints
{
+ extern int nAutoCheckpoint;
+
// Returns true if block passes checkpoint checks
- bool CheckBlock(int nHeight, const uint256& hash);
+ bool CheckHardened(int nHeight, const uint256& hash);
+ bool CheckAuto(const CBlockIndex *pindex);
+
+ int GetNextChainCheckpoint(const CBlockIndex *pindex);
+ void AdvanceAutoCheckpoint(int nCheckpoint);
// Return conservative estimate of total number of blocks, 0 if unknown
int GetTotalBlocksEstimate();
#include "headers.h"
#include "db.h"
#include "net.h"
+#include "checkpoints.h"
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
return Write(string("nBestInvalidTrust"), nBestInvalidTrust);
}
+bool CTxDB::ReadAutoCheckpoint(int& nAutoCheckpoint)
+{
+ return Read(string("nAutoCheckpoint"), nAutoCheckpoint);
+}
+
+bool CTxDB::WriteAutoCheckpoint(int nCheckpoint)
+{
+ return Write(string("nAutoCheckpoint"), max(Checkpoints::nAutoCheckpoint, nCheckpoint));
+}
+
CBlockIndex static * InsertBlockIndex(uint256 hash)
{
if (hash == 0)
pindexBest = mapBlockIndex[hashBestChain];
nBestHeight = pindexBest->nHeight;
nBestChainTrust = pindexBest->nChainTrust;
- printf("LoadBlockIndex(): hashBestChain=%s height=%d\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight);
+ printf("LoadBlockIndex(): hashBestChain=%s height=%d trust=%d\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, nBestChainTrust);
+
+ // Load nAutoCheckpoint
+ if (!ReadAutoCheckpoint(Checkpoints::nAutoCheckpoint))
+ return error("CTxDB::LoadBlockIndex() : nAutoCheckpoint not loaded");
+ printf("LoadBlockIndex(): automatic checkpoint at height=%d\n", Checkpoints::nAutoCheckpoint);
// Load nBestInvalidTrust, OK if it doesn't exist
ReadBestInvalidTrust(nBestInvalidTrust);
bool WriteHashBestChain(uint256 hashBestChain);
bool ReadBestInvalidTrust(uint64& nBestInvalidTrust);
bool WriteBestInvalidTrust(uint64 nBestInvalidTrust);
+ bool ReadAutoCheckpoint(int& nAutoCheckpoint);
+ bool WriteAutoCheckpoint(int nCheckpoint);
bool LoadBlockIndex();
};
}
if (!txdb.WriteHashBestChain(pindexNew->GetBlockHash()))
return error("Reorganize() : WriteHashBestChain failed");
+ if (!txdb.WriteAutoCheckpoint(pindexNew->nCheckpoint))
+ return error("Reorganize() : WriteAutoCheckpoint failed");
// Make sure it's successfully written to disk before changing memory structure
if (!txdb.TxnCommit())
if (pindexGenesisBlock == NULL && hash == hashGenesisBlock)
{
txdb.WriteHashBestChain(hash);
+ txdb.WriteAutoCheckpoint(pindexNew->nCheckpoint);
if (!txdb.TxnCommit())
return error("SetBestChain() : TxnCommit failed");
pindexGenesisBlock = pindexNew;
else if (hashPrevBlock == hashBestChain)
{
// Adding to current best branch
- if (!ConnectBlock(txdb, pindexNew) || !txdb.WriteHashBestChain(hash))
+ if (!ConnectBlock(txdb, pindexNew) || !txdb.WriteHashBestChain(hash) || !txdb.WriteAutoCheckpoint(pindexNew->nCheckpoint))
{
txdb.TxnAbort();
InvalidChainFound(pindexNew);
nBestChainTrust = pindexNew->nChainTrust;
nTimeBestReceived = GetTime();
nTransactionsUpdated++;
+ Checkpoints::AdvanceAutoCheckpoint(pindexBest->nCheckpoint);
printf("SetBestChain: new best=%s height=%d trust=%s\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, CBigNum(nBestChainTrust).ToString().c_str());
return true;
if (!pindexNew)
return error("AddToBlockIndex() : new CBlockIndex failed");
map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
+
pindexNew->phashBlock = &((*mi).first);
map<uint256, CBlockIndex*>::iterator miPrev = mapBlockIndex.find(hashPrevBlock);
if (miPrev != mapBlockIndex.end())
{
pindexNew->pprev = (*miPrev).second;
pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
+
+ // ppcoin: compute chain checkpoint
+ pindexNew->nCheckpoint = Checkpoints::GetNextChainCheckpoint(pindexNew->pprev);
+ assert (pindexNew->nCheckpoint >= pindexNew->pprev->nCheckpoint);
}
+
+ // ppcoin: compute chain trust score
uint64 nCoinAge = GetBlockCoinAge();
if (!nCoinAge)
return error("AddToBlockIndex() : invalid or orphaned transaction in block");
if (!tx.IsFinal(nHeight, GetBlockTime()))
return DoS(10, error("AcceptBlock() : contains a non-final transaction"));
- // Check that the block chain matches the known block chain up to a checkpoint
- if (!Checkpoints::CheckBlock(nHeight, hash))
- return DoS(100, error("AcceptBlock() : rejected by checkpoint lockin at %d", nHeight));
+ // Check that the block chain matches the known block chain up to a hardened checkpoint
+ if (!Checkpoints::CheckHardened(nHeight, hash))
+ return DoS(100, error("AcceptBlock() : rejected by hardened checkpoint lockin at %d", nHeight));
+
+ // ppcoin: check that the block satisfies automatic checkpoint
+ if (!Checkpoints::CheckAuto(pindexPrev))
+ return DoS(100, error("AcceptBlock() : rejected by automatic checkpoint at %d", Checkpoints::nAutoCheckpoint));
// Write block to history file
if (!CheckDiskSpace(::GetSerializeSize(*this, SER_DISK)))
unsigned int nBlockPos;
uint64 nChainTrust;// ppcoin: trust score of chain, in the unit of coin-days
int nHeight;
+ int nCheckpoint; // ppcoin: chain auto checkpoint height
// block header
int nVersion;
nBlockPos = 0;
nHeight = 0;
nChainTrust = 0;
+ nCheckpoint = 0;
nVersion = 0;
hashMerkleRoot = 0;
nBlockPos = nBlockPosIn;
nHeight = 0;
nChainTrust = 0;
+ nCheckpoint = 0;
nVersion = block.nVersion;
hashMerkleRoot = block.hashMerkleRoot;
std::string ToString() const
{
- return strprintf("CBlockIndex(nprev=%08x, pnext=%08x, nFile=%d, nBlockPos=%-6d nChainTrust=%"PRI64d" nHeight=%d, merkle=%s, hashBlock=%s)",
- pprev, pnext, nFile, nBlockPos, nChainTrust, nHeight,
+ return strprintf("CBlockIndex(nprev=%08x, pnext=%08x, nFile=%d, nBlockPos=%-6d nChainTrust=%"PRI64d" nHeight=%d, nCheckpoint=%d, merkle=%s, hashBlock=%s)",
+ pprev, pnext, nFile, nBlockPos, nChainTrust, nHeight, nCheckpoint,
hashMerkleRoot.ToString().substr(0,10).c_str(),
GetBlockHash().ToString().substr(0,20).c_str());
}
READWRITE(nBlockPos);
READWRITE(nChainTrust);
READWRITE(nHeight);
+ READWRITE(nCheckpoint);
// block header
READWRITE(this->nVersion);