64 #include <boost/foreach.hpp>
80 const std::string
CSyncCheckpoint::strMainPubKey =
"04b9ff17f2865bc685456429defb94fcb1ffc44a98703f0d62513fa47e7b76e4c6ab10b59e2c068d45f6a04b47bcee8b0fa50b33bfa53ff279a02863dbf1da6128";
81 const std::string
CSyncCheckpoint::strTestPubKey =
"04db371d2b99df9e7962b2ce2369ada9821261e7f943a57a7ff3494d7d6c87939d3632fcaa2878bd5e45cfea0ea435fd6ee049a28ccbd0998ea3aadeb7abeed065";
97 LOCK(cs_hashSyncCheckpoint);
99 error(
"GetSyncCheckpoint: block index missing for current sync-checkpoint %s", hashSyncCheckpoint.
ToString().c_str());
109 return error(
"ValidateSyncCheckpoint: block index missing for current sync-checkpoint %s", hashSyncCheckpoint.
ToString().c_str());
111 return error(
"ValidateSyncCheckpoint: block index missing for received sync-checkpoint %s", hashCheckpoint.
ToString().c_str());
116 if (pindexCheckpointRecv->
nHeight <= pindexSyncCheckpoint->
nHeight)
123 if (!(pindex = pindex->
pprev))
124 return error(
"ValidateSyncCheckpoint: pprev1 null - block index structure failure");
127 hashInvalidCheckpoint = hashCheckpoint;
128 return error(
"ValidateSyncCheckpoint: new sync-checkpoint %s is conflicting with current sync-checkpoint %s", hashCheckpoint.
ToString().c_str(), hashSyncCheckpoint.
ToString().c_str());
138 if (!(pindex = pindex->
pprev))
139 return error(
"ValidateSyncCheckpoint: pprev2 null - block index structure failure");
142 hashInvalidCheckpoint = hashCheckpoint;
143 return error(
"ValidateSyncCheckpoint: new sync-checkpoint %s is not a descendant of current sync-checkpoint %s", hashCheckpoint.
ToString().c_str(), hashSyncCheckpoint.
ToString().c_str());
152 return error(
"WriteSyncCheckpoint(): failed to write to txdb sync checkpoint %s", hashCheckpoint.
ToString().c_str());
155 return error(
"WriteSyncCheckpoint(): failed to commit to txdb sync checkpoint %s", hashCheckpoint.
ToString().c_str());
157 hashSyncCheckpoint = hashCheckpoint;
163 return (
GetBoolArg(
"-checkpointenforce",
true) ||
mapArgs.count(
"-checkpointkey"));
168 LOCK(cs_hashSyncCheckpoint);
169 if (hashPendingCheckpoint != 0 &&
mapBlockIndex.count(hashPendingCheckpoint))
173 hashPendingCheckpoint = 0;
174 checkpointMessagePending.SetNull();
185 return error(
"AcceptPendingSyncCheckpoint: SetBestChain failed for sync checkpoint %s", hashPendingCheckpoint.
ToString().c_str());
190 return error(
"AcceptPendingSyncCheckpoint(): failed to write sync checkpoint %s", hashPendingCheckpoint.
ToString().c_str());
191 hashPendingCheckpoint = 0;
193 checkpointMessagePending.SetNull();
194 printf(
"AcceptPendingSyncCheckpoint : sync-checkpoint at %s\n", hashSyncCheckpoint.
ToString().c_str());
196 if (!checkpointMessage.
IsNull())
199 checkpointMessage.
RelayTo(pnode);
212 pindex = pindex->
pprev;
219 int nHeight = pindexPrev->
nHeight + 1;
221 LOCK(cs_hashSyncCheckpoint);
226 if (nHeight > pindexSync->nHeight)
230 while (pindex->
nHeight > pindexSync->nHeight)
231 if (!(pindex = pindex->
pprev))
232 return error(
"CheckSyncCheckpoint: pprev null - block index structure failure");
236 if (nHeight == pindexSync->nHeight && hashBlock != hashSyncCheckpoint)
238 if (nHeight < pindexSync->nHeight && !
mapBlockIndex.count(hashBlock))
245 LOCK(cs_hashSyncCheckpoint);
246 if (hashPendingCheckpoint == 0)
248 if (hashBlock == hashPendingCheckpoint)
259 LOCK(cs_hashSyncCheckpoint);
264 printf(
"ResetSyncCheckpoint: SetBestChain to hardened checkpoint %s\n", hash.
ToString().c_str());
268 return error(
"ResetSyncCheckpoint: SetBestChain failed for hardened checkpoint %s", hash.ToString().c_str());
274 hashPendingCheckpoint =
hash;
275 checkpointMessagePending.SetNull();
276 printf(
"ResetSyncCheckpoint: pending for sync-checkpoint %s\n", hashPendingCheckpoint.
ToString().c_str());
280 return error(
"ResetSyncCheckpoint: failed to write sync checkpoint %s", hash.
ToString().c_str());
281 printf(
"ResetSyncCheckpoint: sync-checkpoint reset to %s\n", hashSyncCheckpoint.
ToString().c_str());
287 LOCK(cs_hashSyncCheckpoint);
288 if (pfrom && hashPendingCheckpoint != 0 && (!
mapBlockIndex.count(hashPendingCheckpoint)) && (!
mapOrphanBlocks.count(hashPendingCheckpoint)))
295 std::string strPubKey =
"";
301 return error(
"CheckCheckpointPubKey() : failed to write new checkpoint master key to db");
303 return error(
"CheckCheckpointPubKey() : failed to commit new checkpoint master key to db");
305 return error(
"CheckCheckpointPubKey() : failed to reset sync-checkpoint");
317 checkpoint.
vchMsg = std::vector<unsigned char>(sMsg.
begin(), sMsg.
end());
320 if (!vchSecret.SetString(strPrivKey))
321 return error(
"SendSyncCheckpoint: Checkpoint master key invalid");
322 CKey key = vchSecret.GetKey();
337 checkpoint.
vchMsg = std::vector<unsigned char>(sMsg.
begin(), sMsg.
end());
340 return error(
"SendSyncCheckpoint: Checkpoint master key unavailable.");
343 return error(
"SendSyncCheckpoint: Checkpoint master key invalid");
346 return error(
"SendSyncCheckpoint: Unable to sign checkpoint, check private key?");
350 printf(
"WARNING: SendSyncCheckpoint: Failed to process checkpoint.\n");
366 LOCK(cs_hashSyncCheckpoint);
370 return (
nBestHeight >= pindexSync->nHeight + COINBASE_MATURITY);
376 LOCK(cs_hashSyncCheckpoint);
397 if (!key.
Verify(
Hash(vchMsg.begin(), vchMsg.end()), vchSig))
398 return error(
"CSyncCheckpoint::CheckSignature() : verify signature failed");
409 if (!CheckSignature())
412 LOCK(cs_hashSyncCheckpoint);
416 hashPendingCheckpoint = hashCheckpoint;
417 checkpointMessagePending = *
this;
418 printf(
"ProcessSyncCheckpoint: pending for sync-checkpoint %s\n", hashCheckpoint.ToString().c_str());
440 hashInvalidCheckpoint = hashCheckpoint;
441 return error(
"ProcessSyncCheckpoint: SetBestChain failed for sync checkpoint %s", hashCheckpoint.ToString().c_str());
446 return error(
"ProcessSyncCheckpoint(): failed to write sync checkpoint %s", hashCheckpoint.ToString().c_str());
447 checkpointMessage = *
this;
448 hashPendingCheckpoint = 0;
449 checkpointMessagePending.SetNull();
450 printf(
"ProcessSyncCheckpoint: sync-checkpoint at %s\n", hashCheckpoint.ToString().c_str());
459 if (fHelp || params.size() != 0)
462 "Show info of synchronized checkpoint.\n");
467 result.push_back(
Pair(
"synccheckpoint", hashSyncCheckpoint.
ToString().c_str()));
471 result.push_back(
Pair(
"height", pindexCheckpoint->
nHeight));
475 if (
mapArgs.count(
"-checkpointkey"))
476 result.push_back(
Pair(
"checkpointmaster",
true));
483 if (fHelp || params.size() != 1)
485 "sendcheckpoint <blockhash>\n"
486 "Send a synchronized checkpoint.\n");
489 throw runtime_error(
"Not a checkpointmaster node, first set checkpointkey in configuration and restart client. ");
491 std::string strHash = params[0].
get_str();
495 throw runtime_error(
"Failed to send checkpoint, check log. ");
500 result.push_back(
Pair(
"synccheckpoint", hashSyncCheckpoint.
ToString().c_str()));
504 result.push_back(
Pair(
"height", pindexCheckpoint->
nHeight));
508 if (
mapArgs.count(
"-checkpointkey"))
509 result.push_back(
Pair(
"checkpointmaster",
true));
516 if (fHelp || params.size() != 1)
518 "enforcecheckpoint <enforce>\n"
519 "<enforce> is true or false to enable or disable enforcement of broadcasted checkpoints by developer.");
521 bool fEnforceCheckpoint = params[0].
get_bool();
522 if (
mapArgs.count(
"-checkpointkey") && !fEnforceCheckpoint)
524 "checkpoint master node must enforce synchronized checkpoints.");
525 if (fEnforceCheckpoint)
527 mapArgs[
"-checkpointenforce"] = (fEnforceCheckpoint ?
"1" :
"0");
bool error(const char *format,...)
Value sendcheckpoint(const Array ¶ms, bool fHelp)
std::vector< unsigned char > vchSig
const_iterator begin() const
bool ReadCheckpointPubKey(std::string &strPubKey)
uint256 WantedByOrphan(const CBlock *pblockOrphan)
bool WriteSyncCheckpoint(uint256 hashCheckpoint)
int64 GetBlockTime() const
void AskFor(const CInv &inv)
bool RelayTo(CNode *pnode) const
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig) const
bool ProcessSyncCheckpoint(CNode *pfrom)
bool ResetSyncCheckpoint()
Double ended buffer combining vector and stream-like interfaces.
Value enforcecheckpoint(const Array ¶ms, bool fHelp)
map< uint256, CBlock * > mapOrphanBlocks
Value getcheckpoint(const Array ¶ms, bool fHelp)
uint256 hashGenesisBlock("0x12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2")
Config::Object_type Object
CSyncCheckpoint checkpointMessage
bool CheckCheckpointPubKey()
uint256 hashPendingCheckpoint
static std::string strMasterPrivKey
bool SetString(const char *pszSecret)
bool WriteSyncCheckpoint(const uint256 &hashCheckpoint)
std::vector< unsigned char > vchMsg
bool CheckSyncCheckpoint(const uint256 &hashBlock, const CBlockIndex *pindexPrev)
CCriticalSection cs_hashSyncCheckpoint
uint256 hashInvalidCheckpoint
CBlockTreeDB * pblocktree
Global variable that points to the active block tree (protected by cs_main)
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
void AskForPendingSyncCheckpoint(CNode *pfrom)
bool SetBestChain(CValidationState &state, CBlockIndex *pindexNew)
Connect/disconnect blocks until pindexNew is the new tip of the active block chain.
static const std::string strTestPubKey
bool WantedByPendingSyncCheckpoint(uint256 hashBlock)
A base58-encoded secret key.
An encapsulated public key.
uint256 AutoSelectSyncCheckpoint()
static const Value_impl null
bool IsInMainChain() const
uint256 hashSyncCheckpoint
bool ValidateSyncCheckpoint(uint256 hashCheckpoint)
std::string strCheckpointWarning
uint256 Hash(const T1 pbegin, const T1 pend)
const String_type & get_str() const
uint256 GetLatestHardenedCheckpoint()
bool IsMatureSyncCheckpoint()
bool IsSyncCheckpointEnforced()
Capture information about block/transaction validation.
static const std::string strMainPubKey
bool SendSyncCheckpoint(uint256 hashCheckpoint)
The block chain is a tree shaped structure starting with the genesis block at the root...
std::string ToString() const
CSyncCheckpoint checkpointMessagePending
bool AcceptPendingSyncCheckpoint()
bool IsSyncCheckpointTooOld(unsigned int nSeconds)
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
An encapsulated private key.
Information about a peer.
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
bool WriteCheckpointPubKey(const std::string &strPubKey)
CBlockIndex * GetLastSyncCheckpoint()
void PushGetBlocks(CBlockIndex *pindexBegin, uint256 hashEnd)
map< uint256, CBlockIndex * > mapBlockIndex
vector< unsigned char > ParseHex(const char *psz)
CCriticalSection cs_vNodes
map< string, string > mapArgs
uint256 GetBlockHash() const
bool SetCheckpointPrivKey(std::string strPrivKey)
const_iterator end() const