Skip to content

Commit

Permalink
06 Sync Speed Review - Review LogPrinf(), include if (fDebug)
Browse files Browse the repository at this point in the history
  • Loading branch information
wrapperband committed Aug 13, 2017
1 parent e682797 commit 161ac3d
Showing 1 changed file with 68 additions and 30 deletions.
98 changes: 68 additions & 30 deletions src/checkpointsync.cpp
@@ -1,5 +1,6 @@
// Copyright (c) 2012-2013 PPCoin developers // Copyright (c) 2012-2013 PPCoin developers
// Copyright (c) 2013 Primecoin developers // Copyright (c) 2013 Primecoin developers
// Copyright (c) 2015-2017 The Feathercoin developers
// Distributed under conditional MIT/X11 software license, // Distributed under conditional MIT/X11 software license,
// see the accompanying file COPYING // see the accompanying file COPYING
// //
Expand Down Expand Up @@ -194,8 +195,8 @@ bool AcceptPendingSyncCheckpoint()
hashPendingCheckpoint = 0; hashPendingCheckpoint = 0;
checkpointMessage = checkpointMessagePending; checkpointMessage = checkpointMessagePending;
checkpointMessagePending.SetNull(); checkpointMessagePending.SetNull();

if (fDebug)
LogPrintf("AcceptPendingSyncCheckpoint : sync-checkpoint at %s\n", hashSyncCheckpoint.ToString().c_str()); LogPrintf("AcceptPendingSyncCheckpoint : sync-checkpoint at %s\n", hashSyncCheckpoint.ToString().c_str());
// relay the checkpoint // relay the checkpoint
if (!checkpointMessage.IsNull()) if (!checkpointMessage.IsNull())
{ {
Expand Down Expand Up @@ -265,12 +266,15 @@ bool ResetSyncCheckpoint()
if (mapBlockIndex.count(hash) && !mapBlockIndex[hash]->IsInMainChain()) if (mapBlockIndex.count(hash) && !mapBlockIndex[hash]->IsInMainChain())
{ {
// checkpoint block accepted but not yet in main chain // checkpoint block accepted but not yet in main chain
LogPrintf("ResetSyncCheckpoint: SetBestChain to hardened checkpoint %s\n", hash.ToString().c_str()); if (fDebug)
LogPrintf("ResetSyncCheckpoint: SetBestChain to hardened checkpoint %s\n", hash.ToString().c_str());


CValidationState state; CValidationState state;
if (!SetBestChain(state, mapBlockIndex[hash])) if (!SetBestChain(state, mapBlockIndex[hash]))
{ {
return error("ResetSyncCheckpoint: SetBestChain failed for hardened checkpoint %s", hash.ToString().c_str()); if (fDebug)
LogPrintf("ResetSyncCheckpoint: SetBestChain failed for hardened checkpoint %s", hash.ToString().c_str());
return true;
} }
} }
else if(!mapBlockIndex.count(hash)) else if(!mapBlockIndex.count(hash))
Expand All @@ -283,9 +287,13 @@ bool ResetSyncCheckpoint()
} }


if (!WriteSyncCheckpoint((mapBlockIndex.count(hash) && mapBlockIndex[hash]->IsInMainChain())? hash : Params().HashGenesisBlock())) if (!WriteSyncCheckpoint((mapBlockIndex.count(hash) && mapBlockIndex[hash]->IsInMainChain())? hash : Params().HashGenesisBlock()))
return error("ResetSyncCheckpoint: failed to write sync checkpoint %s", hash.ToString().c_str()); {
if (fDebug)
LogPrintf("ResetSyncCheckpoint: failed to write sync checkpoint %s", hash.ToString().c_str());
return true;
}
if (fDebug) if (fDebug)
LogPrintf("ResetSyncCheckpoint: sync-checkpoint reset to %s\n", hashSyncCheckpoint.ToString().c_str()); LogPrintf("ResetSyncCheckpoint: sync-checkpoint reset to %s\n", hashSyncCheckpoint.ToString().c_str());
return true; return true;
} }


Expand Down Expand Up @@ -325,8 +333,12 @@ bool SetCheckpointPrivKey(std::string strPrivKey)


CBitcoinSecret vchSecret; CBitcoinSecret vchSecret;
if (!vchSecret.SetString(strPrivKey)) if (!vchSecret.SetString(strPrivKey))
return error("SendSyncCheckpoint: Checkpoint master key invalid"); {
//CKey key = vchSecret.GetKey(); // if key is not correct openssl may crash if (fDebug)
LogPrintf("SendSyncCheckpoint: Checkpoint master key invalid");
return true;
}
//CKey key = vchSecret.GetKey(); // if key is not correct openssl may crash
CKey key; CKey key;
bool fCompressed; bool fCompressed;
CSecret secret = vchSecret.GetSecret(fCompressed); CSecret secret = vchSecret.GetSecret(fCompressed);
Expand All @@ -348,22 +360,33 @@ bool SendSyncCheckpoint(uint256 hashCheckpoint)
checkpoint.vchMsg = std::vector<unsigned char>(sMsg.begin(), sMsg.end()); checkpoint.vchMsg = std::vector<unsigned char>(sMsg.begin(), sMsg.end());


if (CSyncCheckpoint::strMasterPrivKey.empty()) if (CSyncCheckpoint::strMasterPrivKey.empty())
return error("SendSyncCheckpoint: Checkpoint master key unavailable."); {
if (fDebug)
LogPrintf("SendSyncCheckpoint: Checkpoint master key unavailable.");
return true;
}
CBitcoinSecret vchSecret; CBitcoinSecret vchSecret;
if (!vchSecret.SetString(CSyncCheckpoint::strMasterPrivKey)) if (!vchSecret.SetString(CSyncCheckpoint::strMasterPrivKey))
return error("SendSyncCheckpoint: Checkpoint master key invalid"); {
if (fDebug)
LogPrintf("SendSyncCheckpoint: Checkpoint master key invalid");
return true;
}
//CKey key = vchSecret.GetKey(); // if key is not correct openssl may crash //CKey key = vchSecret.GetKey(); // if key is not correct openssl may crash
CKey key; CKey key;
bool fCompressed; bool fCompressed;
CSecret secret = vchSecret.GetSecret(fCompressed); CSecret secret = vchSecret.GetSecret(fCompressed);
key.SetSecret(secret, fCompressed); // if key is not correct openssl may crash key.SetSecret(secret, fCompressed); // if key is not correct openssl may crash
if (!key.Sign(Hash(checkpoint.vchMsg.begin(), checkpoint.vchMsg.end()), checkpoint.vchSig)) if (!key.Sign(Hash(checkpoint.vchMsg.begin(), checkpoint.vchMsg.end()), checkpoint.vchSig))
return error("SendSyncCheckpoint: Unable to sign checkpoint, check private key?"); {

if (fDebug)
LogPrintf("SendSyncCheckpoint: Unable to sign checkpoint, check private key?");
return true;
}
if(!checkpoint.ProcessSyncCheckpoint(NULL)) if(!checkpoint.ProcessSyncCheckpoint(NULL))
{ {
if (fDebug) if (fDebug)
LogPrintf("WARNING: SendSyncCheckpoint: Failed to process checkpoint.\n"); LogPrintf("WARNING: SendSyncCheckpoint: Failed to process checkpoint.\n");
return false; return false;
} }


Expand Down Expand Up @@ -412,10 +435,17 @@ bool CSyncCheckpoint::CheckSignature()
std::string strMasterPubKey = TestNet()? CSyncCheckpoint::strTestPubKey : CSyncCheckpoint::strMainPubKey; std::string strMasterPubKey = TestNet()? CSyncCheckpoint::strTestPubKey : CSyncCheckpoint::strMainPubKey;
//CPubKey key(ParseHex(strMasterPubKey)); //CPubKey key(ParseHex(strMasterPubKey));
if (!key.SetPubKey(ParseHex(strMasterPubKey))) if (!key.SetPubKey(ParseHex(strMasterPubKey)))
return error("CSyncCheckpoint::CheckSignature() : SetPubKey failed"); {
if (fDebug)
LogPrintf("CSyncCheckpoint::CheckSignature() : SetPubKey failed");
return true;
}
if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig)) if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
return error("CSyncCheckpoint::CheckSignature() : verify signature failed"); {

if (fDebug)
LogPrintf("CSyncCheckpoint::CheckSignature() : verify signature failed");
return true;
}
// Now unserialize the data // Now unserialize the data
CDataStream sMsg(vchMsg, SER_NETWORK, PROTOCOL_VERSION); CDataStream sMsg(vchMsg, SER_NETWORK, PROTOCOL_VERSION);
sMsg >> *(CUnsignedSyncCheckpoint*)this; sMsg >> *(CUnsignedSyncCheckpoint*)this;
Expand All @@ -427,19 +457,20 @@ bool CSyncCheckpoint::ProcessSyncCheckpoint(CNode* pfrom)
{ {
if (!CheckSignature()) if (!CheckSignature())
return false; return false;

if (fDebug)
LogPrintf("CSyncCheckpoint::ProcessSyncCheckpoint 100,hashCheckpoint=%s\n",hashCheckpoint.ToString().c_str()); LogPrintf("CSyncCheckpoint::ProcessSyncCheckpoint 100,hashCheckpoint=%s\n",hashCheckpoint.ToString().c_str());


LOCK(cs_hashSyncCheckpoint); LOCK(cs_hashSyncCheckpoint);
if (!mapBlockIndex.count(hashCheckpoint)) //count=0 if (!mapBlockIndex.count(hashCheckpoint)) //count=0
{ {
LogPrintf("CSyncCheckpoint::ProcessSyncCheckpoint 110,mapBlockIndex.count=%d \n",mapBlockIndex.count(hashCheckpoint)); if (fDebug)
LogPrintf("CSyncCheckpoint::ProcessSyncCheckpoint 110,mapBlockIndex.count=%d \n",mapBlockIndex.count(hashCheckpoint));


// We haven't received the checkpoint chain, keep the checkpoint as pending // We haven't received the checkpoint chain, keep the checkpoint as pending
hashPendingCheckpoint = hashCheckpoint; hashPendingCheckpoint = hashCheckpoint;
checkpointMessagePending = *this; checkpointMessagePending = *this;

if (fDebug)
LogPrintf("CSyncCheckpoint::ProcessSyncCheckpoint 130: pending for sync-checkpoint %s\n", hashCheckpoint.ToString().c_str()); LogPrintf("CSyncCheckpoint::ProcessSyncCheckpoint 130: pending for sync-checkpoint %s\n", hashCheckpoint.ToString().c_str());


// Ask this guy to fill in what we're missing // Ask this guy to fill in what we're missing
if (pfrom) if (pfrom)
Expand All @@ -449,40 +480,47 @@ bool CSyncCheckpoint::ProcessSyncCheckpoint(CNode* pfrom)
// ask directly as well in case rejected earlier by duplicate // ask directly as well in case rejected earlier by duplicate
// proof-of-stake because getblocks may not get it this time // proof-of-stake because getblocks may not get it this time
pfrom->AskFor(CInv(MSG_BLOCK, mapOrphanBlocks.count(hashCheckpoint)? WantedByOrphan(mapOrphanBlocks[hashCheckpoint]) : hashCheckpoint)); pfrom->AskFor(CInv(MSG_BLOCK, mapOrphanBlocks.count(hashCheckpoint)? WantedByOrphan(mapOrphanBlocks[hashCheckpoint]) : hashCheckpoint));
LogPrintf("CSyncCheckpoint::ProcessSyncCheckpoint 140 ,AskFor.\n"); if (fDebug)
LogPrintf("CSyncCheckpoint::ProcessSyncCheckpoint 140 ,AskFor.\n");
} }
return false; return false;
} }



if (fDebug)
LogPrintf("CSyncCheckpoint::ProcessSyncCheckpoint 201 hashCheckpoint=%s ,nHeight=%d\n",hashCheckpoint.ToString().c_str(),mapBlockIndex[hashCheckpoint]->nHeight); LogPrintf("CSyncCheckpoint::ProcessSyncCheckpoint 201 hashCheckpoint=%s ,nHeight=%d\n",hashCheckpoint.ToString().c_str(),mapBlockIndex[hashCheckpoint]->nHeight);


if (!ValidateSyncCheckpoint(hashCheckpoint)) if (!ValidateSyncCheckpoint(hashCheckpoint))
return false; return false;


CBlockIndex* pindexCheckpoint = mapBlockIndex[hashCheckpoint]; CBlockIndex* pindexCheckpoint = mapBlockIndex[hashCheckpoint];
LogPrintf("CSyncCheckpoint::ProcessSyncCheckpoint 210,pindexCheckpoint=%s\n",pindexCheckpoint->ToString()); if (fDebug)
LogPrintf("CSyncCheckpoint::ProcessSyncCheckpoint 210,pindexCheckpoint=%s\n",pindexCheckpoint->ToString());


if (IsSyncCheckpointEnforced() && !pindexCheckpoint->IsInMainChain()) if (IsSyncCheckpointEnforced() && !pindexCheckpoint->IsInMainChain())
{ {
if (fDebug)
LogPrintf("CSyncCheckpoint::ProcessSyncCheckpoint 220,IsInMainChain=%d\n",pindexCheckpoint->IsInMainChain()); LogPrintf("CSyncCheckpoint::ProcessSyncCheckpoint 220,IsInMainChain=%d\n",pindexCheckpoint->IsInMainChain());


// checkpoint chain received but not yet main chain // checkpoint chain received but not yet main chain
CValidationState state; CValidationState state;
if (!SetBestChain(state, pindexCheckpoint)) if (!SetBestChain(state, pindexCheckpoint))
{ {
hashInvalidCheckpoint = hashCheckpoint; hashInvalidCheckpoint = hashCheckpoint;
LogPrintf("ProcessSyncCheckpoint SetBestChain Invalid,hashInvalidCheckpoint=%s.\n",hashInvalidCheckpoint.ToString().c_str()); if (fDebug)
return error("ProcessSyncCheckpoint: SetBestChain failed for sync checkpoint %s", hashCheckpoint.ToString().c_str()); LogPrintf("ProcessSyncCheckpoint SetBestChain Invalid,hashInvalidCheckpoint=%s.\n",hashInvalidCheckpoint.ToString().c_str());
return true;
} }
} }


if (!WriteSyncCheckpoint(hashCheckpoint)) if (!WriteSyncCheckpoint(hashCheckpoint))
return error("ProcessSyncCheckpoint(): failed to write sync checkpoint %s", hashCheckpoint.ToString().c_str()); if (fDebug)
LogPrintf("ProcessSyncCheckpoint(): failed to write sync checkpoint %s", hashCheckpoint.ToString().c_str());
return true ;
checkpointMessage = *this; checkpointMessage = *this;
hashPendingCheckpoint = 0; hashPendingCheckpoint = 0;
checkpointMessagePending.SetNull(); checkpointMessagePending.SetNull();
LogPrintf("ProcessSyncCheckpoint: sync-checkpoint at %s\n", hashCheckpoint.ToString().c_str()); if (fDebug)
LogPrintf("ProcessSyncCheckpoint: sync-checkpoint at %s\n", hashCheckpoint.ToString().c_str());


return true; return true;
} }
Expand Down

0 comments on commit 161ac3d

Please sign in to comment.