Skip to content

Commit ff5c6ef

Browse files
authored
Merge pull request #2490 from jamescowens/fix_blockfinder
rpc, util: Remove caching from BlockFinder
2 parents a881378 + 576e041 commit ff5c6ef

File tree

11 files changed

+24
-86
lines changed

11 files changed

+24
-86
lines changed

src/gridcoin/contract/contract.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -418,16 +418,14 @@ Contract GRC::MakeLegacyContract(
418418

419419
void GRC::ReplayContracts(CBlockIndex* pindex_end, CBlockIndex* pindex_start)
420420
{
421-
static BlockFinder blockFinder;
422-
423421
CBlockIndex*& pindex = pindex_start;
424422

425423
// If there is no pindex_start (i.e. default value of nullptr), then set standard lookback. A Non-standard lookback
426424
// where there is a specific pindex_start argument supplied, is only used in the GRC InitializeContracts call for
427425
// when the beacon database in LevelDB has not already been populated.
428426
if (!pindex)
429427
{
430-
pindex = blockFinder.FindByMinTime(pindexBest->nTime - Beacon::MAX_AGE);
428+
pindex = GRC::BlockFinder::FindByMinTime(pindexBest->nTime - Beacon::MAX_AGE);
431429
}
432430

433431
if (pindex->nHeight < (fTestNet ? 1 : 164618)) {

src/gridcoin/gridcoin.cpp

+2-4
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

@@ -169,9 +169,7 @@ void InitializeContracts(CBlockIndex* pindexBest)
169169
LogPrintf("Gridcoin: replaying contracts...");
170170
uiInterface.InitMessage(_("Replaying contracts..."));
171171

172-
static BlockFinder blockFinder;
173-
174-
CBlockIndex* pindex_start = blockFinder.FindByMinTime(pindexBest->nTime - Beacon::MAX_AGE);
172+
CBlockIndex* pindex_start = GRC::BlockFinder::FindByMinTime(pindexBest->nTime - Beacon::MAX_AGE);
175173

176174
const int& V11_height = Params().GetConsensus().BlockV11Height;
177175
const int& lookback_window_low_height = pindex_start->nHeight;

src/gridcoin/scraper/scraper.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -571,11 +571,9 @@ const CBlockIndex* GetBeaconConsensusHeight() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
571571
{
572572
AssertLockHeld(cs_main);
573573

574-
static BlockFinder block_finder;
575-
576574
// Use 4 times the BLOCK_GRANULARITY which moves the consensus block every hour.
577575
// TODO: Make the mod a function of SCRAPER_CMANIFEST_RETENTION_TIME in scraper.h.
578-
return block_finder.FindByHeight(
576+
return BlockFinder::FindByHeight(
579577
(nBestHeight - CONSENSUS_LOOKBACK)
580578
- (nBestHeight - CONSENSUS_LOOKBACK) % (BLOCK_GRANULARITY * 4));
581579
}

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/gridcoin/voting/registry.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,8 @@ CBlockIndex* PollReference::GetEndingBlockIndexPtr() const
337337
{
338338
// Has poll ended?
339339
if (Expired(GetAdjustedTime())) {
340-
GRC::BlockFinder blockfinder;
341-
342340
// Find and return the last block that contains valid votes for the poll.
343-
return blockfinder.FindByMinTime(Expiration());
341+
return GRC::BlockFinder::FindByMinTime(Expiration());
344342
}
345343

346344
return nullptr;

src/rpc/blockchain.cpp

+4-8
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);
@@ -660,9 +657,8 @@ UniValue getblockbymintime(const UniValue& params, bool fHelp)
660657
LOCK(cs_main);
661658

662659
CBlock block;
663-
static GRC::BlockFinder block_finder;
664660

665-
CBlockIndex* pblockindex = block_finder.FindByMinTime(nTimestamp);
661+
CBlockIndex* pblockindex = GRC::BlockFinder::FindByMinTime(nTimestamp);
666662
ReadBlockFromDisk(block, pblockindex, Params().GetConsensus());
667663

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

src/rpc/dataacq.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
using namespace GRC;
2626
using namespace std;
2727

28-
extern GRC::BlockFinder RPCBlockFinder;
29-
3028
// Brod
3129
static bool compare_second(const pair<std::string, int64_t> &p1, const pair<std::string, int64_t> &p2)
3230
{

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/test/gridcoin/block_finder_tests.cpp

+9-14
Original file line numberDiff line numberDiff line change
@@ -46,46 +46,41 @@ BOOST_AUTO_TEST_SUITE(block_finder_tests);
4646
BOOST_AUTO_TEST_CASE(FindBlockInNormalChainShouldWork)
4747
{
4848
BlockChain<100> chain;
49-
GRC::BlockFinder finder;
5049
for(auto& block : chain.blocks)
51-
BOOST_CHECK_EQUAL(&block, finder.FindByHeight(block.nHeight));
50+
BOOST_CHECK_EQUAL(&block, GRC::BlockFinder::FindByHeight(block.nHeight));
5251
}
5352

5453
BOOST_AUTO_TEST_CASE(FindBlockAboveHighestHeightShouldReturnHighestBlock)
5554
{
5655
BlockChain<100> chain;
57-
GRC::BlockFinder finder;
5856
CBlockIndex& last = chain.blocks.back();
59-
BOOST_CHECK_EQUAL(&last, finder.FindByHeight(101));
57+
BOOST_CHECK_EQUAL(&last, GRC::BlockFinder::FindByHeight(101));
6058
}
6159

6260
BOOST_AUTO_TEST_CASE(FindBlockByHeightShouldWorkOnChainsWithJustOneBlock)
6361
{
6462
BlockChain<1> chain;
65-
GRC::BlockFinder finder;
66-
BOOST_CHECK_EQUAL(&chain.blocks.front(), finder.FindByHeight(0));
67-
BOOST_CHECK_EQUAL(&chain.blocks.front(), finder.FindByHeight(1));
68-
BOOST_CHECK_EQUAL(&chain.blocks.front(), finder.FindByHeight(-1));
63+
BOOST_CHECK_EQUAL(&chain.blocks.front(), GRC::BlockFinder::FindByHeight(0));
64+
BOOST_CHECK_EQUAL(&chain.blocks.front(), GRC::BlockFinder::FindByHeight(1));
65+
BOOST_CHECK_EQUAL(&chain.blocks.front(), GRC::BlockFinder::FindByHeight(-1));
6966
}
7067

7168
BOOST_AUTO_TEST_CASE(FindBlockByTimeShouldReturnNextYoungestBlock)
7269
{
7370
// Chain with block times 0, 10, 20, 30, 40 etc.
7471
BlockChain<10> chain;
75-
GRC::BlockFinder finder;
7672

7773
// Finding the block older than time 10 should return block #2
7874
// which has time 20.
79-
BOOST_CHECK_EQUAL(&chain.blocks[2], finder.FindByMinTime(11));
80-
BOOST_CHECK_EQUAL(&chain.blocks[1], finder.FindByMinTime(10));
81-
BOOST_CHECK_EQUAL(&chain.blocks[1], finder.FindByMinTime(9));
75+
BOOST_CHECK_EQUAL(&chain.blocks[2], GRC::BlockFinder::FindByMinTime(11));
76+
BOOST_CHECK_EQUAL(&chain.blocks[1], GRC::BlockFinder::FindByMinTime(10));
77+
BOOST_CHECK_EQUAL(&chain.blocks[1], GRC::BlockFinder::FindByMinTime(9));
8278
}
8379

8480
BOOST_AUTO_TEST_CASE(FindBlockByTimeShouldReturnLastBlockIfOlderThanTime)
8581
{
8682
BlockChain<10> chain;
87-
GRC::BlockFinder finder;
88-
BOOST_CHECK_EQUAL(&chain.blocks.back(), finder.FindByMinTime(999999));
83+
BOOST_CHECK_EQUAL(&chain.blocks.back(), GRC::BlockFinder::FindByMinTime(999999));
8984
}
9085

9186
BOOST_AUTO_TEST_SUITE_END()

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(nBestHeight);
28642864
std::map<CKeyID, CBlockIndex*> mapKeyFirstBlock;
28652865
std::set<CKeyID> setKeys;
28662866
GetKeys(setKeys);

0 commit comments

Comments
 (0)