}
-// ppcoin: reset auto checkpoint
-Value resetcheckpoint(const Array& params, bool fHelp)
-{
- if (fHelp || params.size() < 1 || params.size() > 1)
- throw runtime_error(
- "resetcheckpoint <checkpointheight>\n"
- "Reset automatic checkpoint to specified height.\n"
- "<checkpointheight> is the height of the new checkpoint block.\n");
-
- int nCheckpoint = params[0].get_int();
- if (nCheckpoint <= 0 || nCheckpoint >= nBestHeight)
- throw runtime_error(
- "invalid checkpoint height.\n"
- );
- if (nCheckpoint >= Checkpoints::nAutoCheckpoint)
- throw runtime_error(
- "new checkpoint must be earlier than current auto checkpoint.\n"
- );
- if (!Checkpoints::ResetAutoCheckpoint(nCheckpoint))
- throw runtime_error(
- "internal error - reset checkpoint failed.\n"
- );
-
- return Value::null;
-}
-
-
-// ppcoin: get branch point of alternative branch
-Value getbranchpoint(const Array& params, bool fHelp)
-{
- if (fHelp || params.size() != 0)
- throw runtime_error(
- "getbranchpoint\n"
- "Returns height of branch point of alternative branch.\n");
-
- Object result;
- if (Checkpoints::nBranchPoint > 0)
- result.push_back(Pair("branchpoint", Checkpoints::nBranchPoint));
- else
- result.push_back(Pair("branchpoint", "none"));
- result.push_back(Pair("checkpoint", Checkpoints::nAutoCheckpoint));
- return result;
-}
-
-
// ppcoin: reserve balance from being staked for network protection
Value reservebalance(const Array& params, bool fHelp)
{
make_pair("settxfee", &settxfee),
make_pair("getmemorypool", &getmemorypool),
make_pair("listsinceblock", &listsinceblock),
- make_pair("resetcheckpoint", &resetcheckpoint),
- make_pair("getbranchpoint", &getbranchpoint),
make_pair("reservebalance", &reservebalance),
make_pair("checkwallet", &checkwallet),
make_pair("repairwallet", &repairwallet),
"validateaddress",
"getwork",
"getmemorypool",
- "resetcheckpoint",
- "getbranchpoint",
};
set<string> setAllowInSafeMode(pAllowInSafeMode, pAllowInSafeMode + sizeof(pAllowInSafeMode)/sizeof(pAllowInSafeMode[0]));
params[1] = v.get_obj();
}
if (strMethod == "sendmany" && n > 2) ConvertTo<boost::int64_t>(params[2]);
- if (strMethod == "resetcheckpoint" && n > 0) ConvertTo<boost::int64_t>(params[0]);
if (strMethod == "reservebalance" && n > 0) ConvertTo<bool>(params[0]);
if (strMethod == "reservebalance" && n > 1) ConvertTo<double>(params[1]);
}
return false;
}
-
- // 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: sync-checkpoint master key
class uint256;
class CBlockIndex;
+class CSyncCheckpoint;
+
+//
+// Block-chain checkpoints are compiled-in sanity checks.
+// They are updated every release or three.
+//
+namespace Checkpoints
+{
+ // Returns true if block passes checkpoint checks
+ bool CheckHardened(int nHeight, const uint256& hash);
+
+ // Return conservative estimate of total number of blocks, 0 if unknown
+ int GetTotalBlocksEstimate();
+
+ // Returns last CBlockIndex* in mapBlockIndex that is a checkpoint
+ CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex);
+
+ extern uint256 hashSyncCheckpoint;
+ extern CSyncCheckpoint checkpointMessage;
+ extern uint256 hashInvalidCheckpoint;
+ extern CCriticalSection cs_hashSyncCheckpoint;
+
+ CBlockIndex* GetLastSyncCheckpoint();
+ bool AcceptPendingSyncCheckpoint();
+ uint256 AutoSelectSyncCheckpoint();
+ bool CheckSync(const uint256& hashBlock, const CBlockIndex* pindexPrev);
+ bool WantedByPendingSyncCheckpoint(uint256 hashBlock);
+}
// ppcoin: synchronized checkpoint
class CUnsignedSyncCheckpoint
bool ProcessSyncCheckpoint(CNode* pfrom);
};
-//
-// Block-chain checkpoints are compiled-in sanity checks.
-// They are updated every release or three.
-//
-namespace Checkpoints
-{
- // Returns true if block passes checkpoint checks
- bool CheckHardened(int nHeight, const uint256& hash);
-
- // Return conservative estimate of total number of blocks, 0 if unknown
- int GetTotalBlocksEstimate();
-
- // Returns last CBlockIndex* in mapBlockIndex that is a checkpoint
- CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex);
-
- extern uint256 hashSyncCheckpoint;
- extern CSyncCheckpoint checkpointMessage;
- extern uint256 hashInvalidCheckpoint;
- extern CCriticalSection cs_hashSyncCheckpoint;
-
- CBlockIndex* GetLastSyncCheckpoint();
- bool AcceptPendingSyncCheckpoint();
- uint256 AutoSelectSyncCheckpoint();
- bool CheckSync(const uint256& hashBlock, const CBlockIndex* pindexPrev);
- bool WantedByPendingSyncCheckpoint(uint256 hashBlock);
-
- // ppcoin: automatic checkpoint
- extern int nAutoCheckpoint;
- extern int nBranchPoint;
-
- bool CheckAuto(const CBlockIndex *pindex);
- int GetNextChainCheckpoint(const CBlockIndex *pindex);
- int GetNextAutoCheckpoint(int nCheckpoint);
- void AdvanceAutoCheckpoint(int nCheckpoint);
- bool ResetAutoCheckpoint(int nCheckpoint);
-}
-
#endif
return Write(string("nBestInvalidTrust"), nBestInvalidTrust);
}
-bool CTxDB::ReadAutoCheckpoint(int& nAutoCheckpoint)
-{
- return Read(string("nAutoCheckpoint"), nAutoCheckpoint);
-}
-
-bool CTxDB::WriteAutoCheckpoint(int nCheckpoint, bool fReset)
-{
- nCheckpoint = fReset? nCheckpoint : max(Checkpoints::nAutoCheckpoint, nCheckpoint);
- return Write(string("nAutoCheckpoint"), nCheckpoint);
-}
-
bool CTxDB::ReadSyncCheckpoint(uint256& hashCheckpoint)
{
return Read(string("hashSyncCheckpoint"), hashCheckpoint);
pindexNew->nBlockPos = diskindex.nBlockPos;
pindexNew->nChainTrust = diskindex.nChainTrust;
pindexNew->nHeight = diskindex.nHeight;
- pindexNew->nCheckpoint = diskindex.nCheckpoint;
pindexNew->fProofOfStake = diskindex.fProofOfStake;
pindexNew->prevoutStake = diskindex.prevoutStake;
pindexNew->nVersion = diskindex.nVersion;
nBestChainTrust = pindexBest->nChainTrust;
printf("LoadBlockIndex(): hashBestChain=%s height=%d trust=%d\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, nBestChainTrust);
- // ppcoin: load nAutoCheckpoint
- if (!ReadAutoCheckpoint(Checkpoints::nAutoCheckpoint))
- return error("CTxDB::LoadBlockIndex() : nAutoCheckpoint not loaded");
- printf("LoadBlockIndex(): automatic checkpoint at height=%d\n", Checkpoints::nAutoCheckpoint);
-
// ppcoin: load hashSyncCheckpoint
if (!ReadSyncCheckpoint(Checkpoints::hashSyncCheckpoint))
return error("CTxDB::LoadBlockIndex() : hashSyncCheckpoint not loaded");
bool WriteHashBestChain(uint256 hashBestChain);
bool ReadBestInvalidTrust(uint64& nBestInvalidTrust);
bool WriteBestInvalidTrust(uint64 nBestInvalidTrust);
- bool ReadAutoCheckpoint(int& nAutoCheckpoint);
- bool WriteAutoCheckpoint(int nCheckpoint, bool fReset=false);
bool ReadSyncCheckpoint(uint256& hashCheckpoint);
bool WriteSyncCheckpoint(uint256 hashCheckpoint);
bool LoadBlockIndex();
}
if (!txdb.WriteHashBestChain(pindexNew->GetBlockHash()))
return error("Reorganize() : WriteHashBestChain failed");
- if (!txdb.WriteAutoCheckpoint(Checkpoints::GetNextAutoCheckpoint(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(Checkpoints::GetNextAutoCheckpoint(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) || !txdb.WriteAutoCheckpoint(Checkpoints::GetNextAutoCheckpoint(pindexNew->nCheckpoint)))
+ if (!ConnectBlock(txdb, pindexNew) || !txdb.WriteHashBestChain(hash))
{
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;
{
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
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));
-
// ppcoin: check that the block satisfies synchronized checkpoint
if (!Checkpoints::CheckSync(hash, pindexPrev))
return error("AcceptBlock() : rejected by synchronized checkpoint");
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
bool fProofOfStake; // ppcoin: is the block of proof-of-stake type
COutPoint prevoutStake;
unsigned int nStakeTime;
nBlockPos = 0;
nHeight = 0;
nChainTrust = 0;
- nCheckpoint = 0;
fProofOfStake = true;
prevoutStake.SetNull();
nStakeTime = 0;
nBlockPos = nBlockPosIn;
nHeight = 0;
nChainTrust = 0;
- nCheckpoint = 0;
fProofOfStake = block.IsProofOfStake();
if (fProofOfStake)
{
std::string ToString() const
{
- return strprintf("CBlockIndex(nprev=%08x, pnext=%08x, nFile=%d, nBlockPos=%-6d nChainTrust=%"PRI64d" nHeight=%d, nCheckpoint=%d, fProofOfStake=%d prevoutStake=(%s), nStakeTime=%d merkle=%s, hashBlock=%s)",
- pprev, pnext, nFile, nBlockPos, nChainTrust, nHeight, nCheckpoint,
+ return strprintf("CBlockIndex(nprev=%08x, pnext=%08x, nFile=%d, nBlockPos=%-6d nChainTrust=%"PRI64d" nHeight=%d, fProofOfStake=%d prevoutStake=(%s), nStakeTime=%d merkle=%s, hashBlock=%s)",
+ pprev, pnext, nFile, nBlockPos, nChainTrust, nHeight,
fProofOfStake, prevoutStake.ToString().c_str(), nStakeTime,
hashMerkleRoot.ToString().substr(0,10).c_str(),
GetBlockHash().ToString().substr(0,20).c_str());
READWRITE(nBlockPos);
READWRITE(nChainTrust);
READWRITE(nHeight);
- READWRITE(nCheckpoint);
READWRITE(fProofOfStake);
if (fProofOfStake)
{