Skip to content

Commit bfa00dd

Browse files
committed
Remove caching from BlockFinder
1 parent a881378 commit bfa00dd

File tree

6 files changed

+10
-56
lines changed

6 files changed

+10
-56
lines changed

src/gridcoin/gridcoin.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ bool InitializeResearchRewardAccounting(CBlockIndex* pindexBest)
120120

121121
const int64_t start_height = Params().GetConsensus().ResearchAgeHeight;
122122

123-
if (!Tally::Initialize(BlockFinder().FindByHeight(start_height))) {
123+
if (!Tally::Initialize(BlockFinder::FindByHeight(start_height))) {
124124
return error("Failed to initialize tally.");
125125
}
126126

src/gridcoin/support/block_finder.cpp

-23
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@
55
#include "main.h"
66
#include "gridcoin/support/block_finder.h"
77

8-
#include <cstdlib>
9-
108
using namespace GRC;
119

12-
BlockFinder::BlockFinder()
13-
: cache(nullptr)
14-
{}
15-
1610
CBlockIndex* BlockFinder::FindByHeight(int height)
1711
{
1812
// If the height is at the bottom half of the chain, start searching from
@@ -23,11 +17,6 @@ CBlockIndex* BlockFinder::FindByHeight(int height)
2317

2418
if(index != nullptr)
2519
{
26-
// Use the cache if it's closer to the target than the current
27-
// start block.
28-
if (cache && abs(height - index->nHeight) > std::abs(height - cache->nHeight))
29-
index = cache;
30-
3120
// Traverse towards the tail.
3221
while (index && index->pprev && index->nHeight > height)
3322
index = index->pprev;
@@ -37,7 +26,6 @@ CBlockIndex* BlockFinder::FindByHeight(int height)
3726
index = index->pnext;
3827
}
3928

40-
cache = index;
4129
return index;
4230
}
4331

@@ -52,11 +40,6 @@ CBlockIndex* BlockFinder::FindByMinTime(int64_t time)
5240

5341
if(index != nullptr)
5442
{
55-
// If we have a cache that's closer to target than our current index,
56-
// use it.
57-
if(cache && abs(time - index->nTime) > abs(time - int64_t(cache->nTime)))
58-
index = cache;
59-
6043
// Move back until the previous block is no longer younger than "time".
6144
while(index && index->pprev && index->pprev->nTime > time)
6245
index = index->pprev;
@@ -66,11 +49,5 @@ CBlockIndex* BlockFinder::FindByMinTime(int64_t time)
6649
index = index->pnext;
6750
}
6851

69-
cache = index;
7052
return index;
7153
}
72-
73-
void BlockFinder::Reset()
74-
{
75-
cache = nullptr;
76-
}

src/gridcoin/support/block_finder.h

+3-19
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,17 @@ namespace GRC {
1414
class BlockFinder
1515
{
1616
public:
17-
//!
18-
//! \brief Constructor.
19-
//!
20-
BlockFinder();
21-
2217
//!
2318
//! \brief Find a block with a specific height.
2419
//!
2520
//! Traverses the chain from head or tail, depending on what's closest to
26-
//! find the block that matches \p height. This is a caching operation
21+
//! find the block that matches \p height.
2722
//!
2823
//! \param nHeight Block height to find.
2924
//! \return The block with the height closest to \p nHeight if found, otherwise
3025
//! \a nullptr is returned.
3126
//!
32-
CBlockIndex* FindByHeight(int height);
27+
static CBlockIndex* FindByHeight(int height);
3328

3429
//!
3530
//! \brief Find block by time.
@@ -42,18 +37,7 @@ class BlockFinder
4237
//! \return The youngest block which is not older than \p time, or the
4338
//! head of the chain if it is older than \p time.
4439
//!
45-
CBlockIndex* FindByMinTime(int64_t time);
46-
47-
//!
48-
//! \brief Reset finder cache.
49-
//!
50-
//! Clears the block finder cache. This should be used when blocks are removed
51-
//! from the chain to avoid accessing deleted memory.
52-
//!
53-
void Reset();
54-
55-
private:
56-
CBlockIndex* cache;
40+
static CBlockIndex* FindByMinTime(int64_t time);
5741
};
5842
} // namespace GRC
5943

src/rpc/blockchain.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ double CoinToDouble(double surrogate);
3232
extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry);
3333
UniValue ContractToJson(const GRC::Contract& contract);
3434

35-
GRC::BlockFinder RPCBlockFinder;
36-
3735
UniValue MRCToJson(const GRC::MRC& mrc) {
3836
UniValue json(UniValue::VOBJ);
3937

@@ -471,7 +469,7 @@ UniValue showblock(const UniValue& params, bool fHelp)
471469

472470
LOCK(cs_main);
473471

474-
CBlockIndex* pblockindex = RPCBlockFinder.FindByHeight(nHeight);
472+
CBlockIndex* pblockindex = GRC::BlockFinder::FindByHeight(nHeight);
475473

476474
if (pblockindex == nullptr)
477475
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
@@ -587,7 +585,7 @@ UniValue getblockhash(const UniValue& params, bool fHelp)
587585

588586
LOCK(cs_main);
589587

590-
CBlockIndex* RPCpblockindex = RPCBlockFinder.FindByHeight(nHeight);
588+
CBlockIndex* RPCpblockindex = GRC::BlockFinder::FindByHeight(nHeight);
591589

592590
return RPCpblockindex->phashBlock->GetHex();
593591
}
@@ -634,9 +632,8 @@ UniValue getblockbynumber(const UniValue& params, bool fHelp)
634632
LOCK(cs_main);
635633

636634
CBlock block;
637-
static GRC::BlockFinder block_finder;
638635

639-
CBlockIndex* pblockindex = block_finder.FindByHeight(nHeight);
636+
CBlockIndex* pblockindex = GRC::BlockFinder::FindByHeight(nHeight);
640637
ReadBlockFromDisk(block, pblockindex, Params().GetConsensus());
641638

642639
return blockToJSON(block, pblockindex, params.size() > 1 ? params[1].get_bool() : false);

src/rpc/rawtransaction.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -1075,9 +1075,7 @@ UniValue consolidatemsunspent(const UniValue& params, bool fHelp)
10751075
if (nMaxInputs == 0 || nMaxInputs > nEqMaxInputs)
10761076
nMaxInputs = nEqMaxInputs;
10771077

1078-
GRC::BlockFinder blockfinder;
1079-
1080-
CBlockIndex* pblkindex = blockfinder.FindByHeight((nBlockStart - 1));
1078+
CBlockIndex* pblkindex = GRC::BlockFinder::FindByHeight((nBlockStart - 1));
10811079

10821080
if (!pblkindex)
10831081
throw JSONRPCError(RPC_PARSE_ERROR, "Block not found");
@@ -1279,9 +1277,7 @@ UniValue scanforunspent(const UniValue& params, bool fHelp)
12791277
{
12801278
LOCK(cs_main);
12811279

1282-
GRC::BlockFinder blockfinder;
1283-
1284-
CBlockIndex* pblkindex = blockfinder.FindByHeight((nBlockStart - 1));
1280+
CBlockIndex* pblkindex = GRC::BlockFinder::FindByHeight((nBlockStart - 1));
12851281

12861282
if (!pblkindex)
12871283
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");

src/wallet/wallet.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2860,7 +2860,7 @@ void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const EXC
28602860
mapKeyBirth[it->first] = it->second.nCreateTime;
28612861

28622862
// map in which we'll infer heights of other keys
2863-
CBlockIndex *pindexMax = GRC::BlockFinder().FindByHeight(std::max(0, nBestHeight - 144)); // the tip can be reorganised; use a 144-block safety margin
2863+
CBlockIndex *pindexMax = GRC::BlockFinder::FindByHeight(std::max(0, nBestHeight - 144)); // the tip can be reorganised; use a 144-block safety margin
28642864
std::map<CKeyID, CBlockIndex*> mapKeyFirstBlock;
28652865
std::set<CKeyID> setKeys;
28662866
GetKeys(setKeys);

0 commit comments

Comments
 (0)