aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
authorMike Hearn <hearn@google.com>2013-05-07 13:59:29 +0200
committerMike Hearn <hearn@google.com>2013-06-19 15:39:45 +0200
commit70e7fba06da36218688a4cae4a5d12332c714247 (patch)
treefe11b5b63d2ff11d9c0d2eab54ee4e806c70caab /src/main.cpp
parent25dbb928600b2c65bc20ce026c5f9cf1fb457d60 (diff)
Move implementation of some CBlockLocator methods
Move out of main.h to improve compile times and add documentation for what the methods do.
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp90
1 files changed, 88 insertions, 2 deletions
diff --git a/src/main.cpp b/src/main.cpp
index f0c08d273f..8d53e3be43 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -158,8 +158,6 @@ void static ResendWalletTransactions()
pwallet->ResendWalletTransactions();
}
-
-
//////////////////////////////////////////////////////////////////////////////
//
// Registration of network node signals.
@@ -177,7 +175,95 @@ void UnregisterNodeSignals(CNodeSignals& nodeSignals)
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;
+}
//////////////////////////////////////////////////////////////////////////////
//