diff options
author | Luke Dashjr <luke-jr+git@utopios.org> | 2012-07-19 20:06:20 +0000 |
---|---|---|
committer | Luke Dashjr <luke-jr+git@utopios.org> | 2012-08-01 18:23:30 +0000 |
commit | 1be064190ed0ca95113cf273082a2d81dc8a4357 (patch) | |
tree | 037ef6896b2408ab396e336c890b9e758e64dce7 /src | |
parent | 4060d64fc9de6f11ae69f3961d4f1f0450dd8286 (diff) |
Optimize JSON-RPC getblockhash
- If the height is in the first half, start at the genesis block and go up, rather than at the top
- Cache the last lookup and use it as a reference point if it's close to the next request, to make linear lookups always fast
Diffstat (limited to 'src')
-rw-r--r-- | src/bitcoinrpc.cpp | 5 | ||||
-rw-r--r-- | src/main.cpp | 19 | ||||
-rw-r--r-- | src/main.h | 1 |
3 files changed, 21 insertions, 4 deletions
diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 62b0b497ed..474207bdce 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -2023,10 +2023,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 3052cfb8c4..e56a163199 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -802,6 +802,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) @@ -1615,6 +1633,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 b3cc9ab40e..d7b8cc4613 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); |