aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGregory Maxwell <greg@xiph.org>2012-08-01 11:49:26 -0700
committerGregory Maxwell <greg@xiph.org>2012-08-01 11:49:26 -0700
commit8ce7915aad505fd6e57f599b5b43fc241d8b4cb4 (patch)
tree37b12ebf89209456ecce18f18619408ac9b96edb /src
parentf81e6f779b66e235afd6c5241306d5e70ec41276 (diff)
parent1be064190ed0ca95113cf273082a2d81dc8a4357 (diff)
downloadbitcoin-8ce7915aad505fd6e57f599b5b43fc241d8b4cb4.tar.xz
Merge pull request #1612 from luke-jr/opti_getblkhash
Optimize JSON-RPC getblockhash
Diffstat (limited to 'src')
-rw-r--r--src/bitcoinrpc.cpp5
-rw-r--r--src/main.cpp19
-rw-r--r--src/main.h1
3 files changed, 21 insertions, 4 deletions
diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp
index 675c8462ea..2e3f8a8e5b 100644
--- a/src/bitcoinrpc.cpp
+++ b/src/bitcoinrpc.cpp
@@ -2047,10 +2047,7 @@ Value getblockhash(const Array& params, bool fHelp)
if (nHeight < 0 || nHeight > nBestHeight)
throw runtime_error("Block number out of range.");
- CBlock block;
- CBlockIndex* pblockindex = mapBlockIndex[hashBestChain];
- while (pblockindex->nHeight > nHeight)
- pblockindex = pblockindex->pprev;
+ CBlockIndex* pblockindex = FindBlockByHeight(nHeight);
return pblockindex->phashBlock->GetHex();
}
diff --git a/src/main.cpp b/src/main.cpp
index e7095c7001..23c6d988aa 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -811,6 +811,24 @@ bool GetTransaction(const uint256 &hash, CTransaction &tx, uint256 &hashBlock)
// CBlock and CBlockIndex
//
+static CBlockIndex* pblockindexFBBHLast;
+CBlockIndex* FindBlockByHeight(int nHeight)
+{
+ CBlockIndex *pblockindex;
+ if (nHeight < nBestHeight / 2)
+ pblockindex = pindexGenesisBlock;
+ else
+ pblockindex = pindexBest;
+ if (pblockindexFBBHLast && abs(nHeight - pblockindex->nHeight) > abs(nHeight - pblockindexFBBHLast->nHeight))
+ pblockindex = pblockindexFBBHLast;
+ while (pblockindex->nHeight > nHeight)
+ pblockindex = pblockindex->pprev;
+ while (pblockindex->nHeight < nHeight)
+ pblockindex = pblockindex->pnext;
+ pblockindexFBBHLast = pblockindex;
+ return pblockindex;
+}
+
bool CBlock::ReadFromDisk(const CBlockIndex* pindex, bool fReadTransactions)
{
if (!fReadTransactions)
@@ -1634,6 +1652,7 @@ bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew)
// New best block
hashBestChain = hash;
pindexBest = pindexNew;
+ pblockindexFBBHLast = NULL;
nBestHeight = pindexBest->nHeight;
bnBestChainWork = pindexNew->bnChainWork;
nTimeBestReceived = GetTime();
diff --git a/src/main.h b/src/main.h
index 9b58b713d0..688af076b8 100644
--- a/src/main.h
+++ b/src/main.h
@@ -88,6 +88,7 @@ FILE* OpenBlockFile(unsigned int nFile, unsigned int nBlockPos, const char* pszM
FILE* AppendBlockFile(unsigned int& nFileRet);
bool LoadBlockIndex(bool fAllowNew=true);
void PrintBlockTree();
+CBlockIndex* FindBlockByHeight(int nHeight);
bool ProcessMessages(CNode* pfrom);
bool SendMessages(CNode* pto, bool fSendTrickle);
bool LoadExternalBlockFile(FILE* fileIn);