Move implementation of some CBlockLocator methods

Move out of main.h to improve compile times and add documentation
for what the methods do.
This commit is contained in:
Mike Hearn 2013-05-07 13:59:29 +02:00
parent 25dbb92860
commit 70e7fba06d
2 changed files with 100 additions and 89 deletions

View file

@ -158,8 +158,6 @@ void static ResendWalletTransactions()
pwallet->ResendWalletTransactions(); pwallet->ResendWalletTransactions();
} }
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// //
// Registration of network node signals. // Registration of network node signals.
@ -177,7 +175,95 @@ void UnregisterNodeSignals(CNodeSignals& nodeSignals)
nodeSignals.SendMessages.disconnect(&SendMessages); nodeSignals.SendMessages.disconnect(&SendMessages);
} }
//////////////////////////////////////////////////////////////////////////////
//
// CBlockLocator implementation
//
CBlockLocator::CBlockLocator(uint256 hashBlock)
{
std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
if (mi != mapBlockIndex.end())
Set((*mi).second);
}
void CBlockLocator::Set(const CBlockIndex* pindex)
{
vHave.clear();
int nStep = 1;
while (pindex)
{
vHave.push_back(pindex->GetBlockHash());
// Exponentially larger steps back
for (int i = 0; pindex && i < nStep; i++)
pindex = pindex->pprev;
if (vHave.size() > 10)
nStep *= 2;
}
vHave.push_back(hashGenesisBlock);
}
int CBlockLocator::GetDistanceBack()
{
// Retrace how far back it was in the sender's branch
int nDistance = 0;
int nStep = 1;
BOOST_FOREACH(const uint256& hash, vHave)
{
std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
if (mi != mapBlockIndex.end())
{
CBlockIndex* pindex = (*mi).second;
if (pindex->IsInMainChain())
return nDistance;
}
nDistance += nStep;
if (nDistance > 10)
nStep *= 2;
}
return nDistance;
}
CBlockIndex *CBlockLocator::GetBlockIndex()
{
// Find the first block the caller has in the main chain
BOOST_FOREACH(const uint256& hash, vHave)
{
std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
if (mi != mapBlockIndex.end())
{
CBlockIndex* pindex = (*mi).second;
if (pindex->IsInMainChain())
return pindex;
}
}
return pindexGenesisBlock;
}
uint256 CBlockLocator::GetBlockHash()
{
// Find the first block the caller has in the main chain
BOOST_FOREACH(const uint256& hash, vHave)
{
std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
if (mi != mapBlockIndex.end())
{
CBlockIndex* pindex = (*mi).second;
if (pindex->IsInMainChain())
return hash;
}
}
return hashGenesisBlock;
}
int CBlockLocator::GetHeight()
{
CBlockIndex* pindex = GetBlockIndex();
if (!pindex)
return 0;
return pindex->nHeight;
}
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// //

View file

@ -1202,22 +1202,14 @@ class CBlockLocator
protected: protected:
std::vector<uint256> vHave; std::vector<uint256> vHave;
public: public:
CBlockLocator() {}
CBlockLocator()
{
}
explicit CBlockLocator(const CBlockIndex* pindex) explicit CBlockLocator(const CBlockIndex* pindex)
{ {
Set(pindex); Set(pindex);
} }
explicit CBlockLocator(uint256 hashBlock) explicit CBlockLocator(uint256 hashBlock);
{
std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
if (mi != mapBlockIndex.end())
Set((*mi).second);
}
CBlockLocator(const std::vector<uint256>& vHaveIn) CBlockLocator(const std::vector<uint256>& vHaveIn)
{ {
@ -1241,83 +1233,16 @@ public:
return vHave.empty(); return vHave.empty();
} }
void Set(const CBlockIndex* pindex) /** Given a block initialises the locator to that point in the chain. */
{ void Set(const CBlockIndex* pindex);
vHave.clear(); /** Returns the distance in blocks this locator is from our chain head. */
int nStep = 1; int GetDistanceBack();
while (pindex) /** Returns the first best-chain block the locator contains. */
{ CBlockIndex* GetBlockIndex();
vHave.push_back(pindex->GetBlockHash()); /** Returns the hash of the first best chain block the locator contains. */
uint256 GetBlockHash();
// Exponentially larger steps back /** Returns the height of the first best chain block the locator has. */
for (int i = 0; pindex && i < nStep; i++) int GetHeight();
pindex = pindex->pprev;
if (vHave.size() > 10)
nStep *= 2;
}
vHave.push_back(hashGenesisBlock);
}
int GetDistanceBack()
{
// Retrace how far back it was in the sender's branch
int nDistance = 0;
int nStep = 1;
BOOST_FOREACH(const uint256& hash, vHave)
{
std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
if (mi != mapBlockIndex.end())
{
CBlockIndex* pindex = (*mi).second;
if (pindex->IsInMainChain())
return nDistance;
}
nDistance += nStep;
if (nDistance > 10)
nStep *= 2;
}
return nDistance;
}
CBlockIndex* GetBlockIndex()
{
// Find the first block the caller has in the main chain
BOOST_FOREACH(const uint256& hash, vHave)
{
std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
if (mi != mapBlockIndex.end())
{
CBlockIndex* pindex = (*mi).second;
if (pindex->IsInMainChain())
return pindex;
}
}
return pindexGenesisBlock;
}
uint256 GetBlockHash()
{
// Find the first block the caller has in the main chain
BOOST_FOREACH(const uint256& hash, vHave)
{
std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
if (mi != mapBlockIndex.end())
{
CBlockIndex* pindex = (*mi).second;
if (pindex->IsInMainChain())
return hash;
}
}
return hashGenesisBlock;
}
int GetHeight()
{
CBlockIndex* pindex = GetBlockIndex();
if (!pindex)
return 0;
return pindex->nHeight;
}
}; };