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) 2013 Primecoin developers
// Copyright (c) 2015-2017 The Feathercoin developers
// Distributed under conditional MIT/X11 software license,
// see the accompanying file COPYING
//
Expand Down Expand Up @@ -194,8 +195,8 @@ bool AcceptPendingSyncCheckpoint()
hashPendingCheckpoint = 0;
checkpointMessage = checkpointMessagePending;
checkpointMessagePending.SetNull();

LogPrintf("AcceptPendingSyncCheckpoint : sync-checkpoint at %s\n", hashSyncCheckpoint.ToString().c_str());
if (fDebug)
LogPrintf("AcceptPendingSyncCheckpoint : sync-checkpoint at %s\n", hashSyncCheckpoint.ToString().c_str());
// relay the checkpoint
if (!checkpointMessage.IsNull())
{
Expand Down Expand Up @@ -265,12 +266,15 @@ bool ResetSyncCheckpoint()
if (mapBlockIndex.count(hash) && !mapBlockIndex[hash]->IsInMainChain())
{
// 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;
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))
Expand All @@ -283,9 +287,13 @@ bool ResetSyncCheckpoint()
}

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)
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;
}

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

CBitcoinSecret vchSecret;
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;
bool 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());

if (CSyncCheckpoint::strMasterPrivKey.empty())
return error("SendSyncCheckpoint: Checkpoint master key unavailable.");
{
if (fDebug)
LogPrintf("SendSyncCheckpoint: Checkpoint master key unavailable.");
return true;
}
CBitcoinSecret vchSecret;
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;
bool fCompressed;
CSecret secret = vchSecret.GetSecret(fCompressed);
key.SetSecret(secret, fCompressed); // if key is not correct openssl may crash
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 (fDebug)
LogPrintf("WARNING: SendSyncCheckpoint: Failed to process checkpoint.\n");
LogPrintf("WARNING: SendSyncCheckpoint: Failed to process checkpoint.\n");
return false;
}

Expand Down Expand Up @@ -412,10 +435,17 @@ bool CSyncCheckpoint::CheckSignature()
std::string strMasterPubKey = TestNet()? CSyncCheckpoint::strTestPubKey : CSyncCheckpoint::strMainPubKey;
//CPubKey key(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))
return error("CSyncCheckpoint::CheckSignature() : verify signature failed");

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

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

LOCK(cs_hashSyncCheckpoint);
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
hashPendingCheckpoint = hashCheckpoint;
checkpointMessagePending = *this;

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

// Ask this guy to fill in what we're missing
if (pfrom)
Expand All @@ -449,40 +480,47 @@ bool CSyncCheckpoint::ProcessSyncCheckpoint(CNode* pfrom)
// ask directly as well in case rejected earlier by duplicate
// proof-of-stake because getblocks may not get it this time
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;
}


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

if (!ValidateSyncCheckpoint(hashCheckpoint))
return false;

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 (fDebug)
LogPrintf("CSyncCheckpoint::ProcessSyncCheckpoint 220,IsInMainChain=%d\n",pindexCheckpoint->IsInMainChain());

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

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;
hashPendingCheckpoint = 0;
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;
}
Expand Down

0 comments on commit 161ac3d

Please sign in to comment.