mirror of
https://github.com/twisterarmy/twister-core.git
synced 2025-01-11 15:27:57 +00:00
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
This commit is contained in:
parent
4060d64fc9
commit
1be064190e
@ -2023,10 +2023,7 @@ Value getblockhash(const Array& params, bool fHelp)
|
|||||||
if (nHeight < 0 || nHeight > nBestHeight)
|
if (nHeight < 0 || nHeight > nBestHeight)
|
||||||
throw runtime_error("Block number out of range.");
|
throw runtime_error("Block number out of range.");
|
||||||
|
|
||||||
CBlock block;
|
CBlockIndex* pblockindex = FindBlockByHeight(nHeight);
|
||||||
CBlockIndex* pblockindex = mapBlockIndex[hashBestChain];
|
|
||||||
while (pblockindex->nHeight > nHeight)
|
|
||||||
pblockindex = pblockindex->pprev;
|
|
||||||
return pblockindex->phashBlock->GetHex();
|
return pblockindex->phashBlock->GetHex();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
19
src/main.cpp
19
src/main.cpp
@ -802,6 +802,24 @@ bool GetTransaction(const uint256 &hash, CTransaction &tx, uint256 &hashBlock)
|
|||||||
// CBlock and CBlockIndex
|
// 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)
|
bool CBlock::ReadFromDisk(const CBlockIndex* pindex, bool fReadTransactions)
|
||||||
{
|
{
|
||||||
if (!fReadTransactions)
|
if (!fReadTransactions)
|
||||||
@ -1615,6 +1633,7 @@ bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew)
|
|||||||
// New best block
|
// New best block
|
||||||
hashBestChain = hash;
|
hashBestChain = hash;
|
||||||
pindexBest = pindexNew;
|
pindexBest = pindexNew;
|
||||||
|
pblockindexFBBHLast = NULL;
|
||||||
nBestHeight = pindexBest->nHeight;
|
nBestHeight = pindexBest->nHeight;
|
||||||
bnBestChainWork = pindexNew->bnChainWork;
|
bnBestChainWork = pindexNew->bnChainWork;
|
||||||
nTimeBestReceived = GetTime();
|
nTimeBestReceived = GetTime();
|
||||||
|
@ -88,6 +88,7 @@ FILE* OpenBlockFile(unsigned int nFile, unsigned int nBlockPos, const char* pszM
|
|||||||
FILE* AppendBlockFile(unsigned int& nFileRet);
|
FILE* AppendBlockFile(unsigned int& nFileRet);
|
||||||
bool LoadBlockIndex(bool fAllowNew=true);
|
bool LoadBlockIndex(bool fAllowNew=true);
|
||||||
void PrintBlockTree();
|
void PrintBlockTree();
|
||||||
|
CBlockIndex* FindBlockByHeight(int nHeight);
|
||||||
bool ProcessMessages(CNode* pfrom);
|
bool ProcessMessages(CNode* pfrom);
|
||||||
bool SendMessages(CNode* pto, bool fSendTrickle);
|
bool SendMessages(CNode* pto, bool fSendTrickle);
|
||||||
bool LoadExternalBlockFile(FILE* fileIn);
|
bool LoadExternalBlockFile(FILE* fileIn);
|
||||||
|
Loading…
Reference in New Issue
Block a user