Skip to content

Commit 60dc05a

Browse files
committed
rpc/mining: Use existing NodeContext
Also pass in appropriate object to: - GetNetworkHashPS - [gG]enerateBlock{,s} Also: - Misc style/constness changes
1 parent d485e81 commit 60dc05a

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

src/rpc/mining.cpp

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@
4444
* or from the last difficulty change if 'lookup' is nonpositive.
4545
* If 'height' is nonnegative, compute the estimate at the time when a given block was found.
4646
*/
47-
static UniValue GetNetworkHashPS(int lookup, int height) {
48-
CBlockIndex *pb = ::ChainActive().Tip();
47+
static UniValue GetNetworkHashPS(int lookup, int height, const CChain& active_chain) {
48+
const CBlockIndex* pb = active_chain.Tip();
4949

50-
if (height >= 0 && height < ::ChainActive().Height())
51-
pb = ::ChainActive()[height];
50+
if (height >= 0 && height < active_chain.Height()) {
51+
pb = active_chain[height];
52+
}
5253

5354
if (pb == nullptr || !pb->nHeight)
5455
return 0;
@@ -61,7 +62,7 @@ static UniValue GetNetworkHashPS(int lookup, int height) {
6162
if (lookup > pb->nHeight)
6263
lookup = pb->nHeight;
6364

64-
CBlockIndex *pb0 = pb;
65+
const CBlockIndex* pb0 = pb;
6566
int64_t minTime = pb0->GetBlockTime();
6667
int64_t maxTime = minTime;
6768
for (int i = 0; i < lookup; i++) {
@@ -100,7 +101,8 @@ static RPCHelpMan getnetworkhashps()
100101
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
101102
{
102103
LOCK(cs_main);
103-
return GetNetworkHashPS(!request.params[0].isNull() ? request.params[0].get_int() : 120, !request.params[1].isNull() ? request.params[1].get_int() : -1);
104+
const CChain& active_chain = EnsureChainman(request.context).ActiveChain();
105+
return GetNetworkHashPS(!request.params[0].isNull() ? request.params[0].get_int() : 120, !request.params[1].isNull() ? request.params[1].get_int() : -1, active_chain);
104106
},
105107
};
106108
}
@@ -111,7 +113,8 @@ static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t&
111113

112114
{
113115
LOCK(cs_main);
114-
IncrementExtraNonce(&block, ::ChainActive().Tip(), extra_nonce);
116+
CHECK_NONFATAL(std::addressof(::ChainActive()) == std::addressof(chainman.ActiveChain()));
117+
IncrementExtraNonce(&block, chainman.ActiveChain().Tip(), extra_nonce);
115118
}
116119

117120
CChainParams chainparams(Params());
@@ -143,7 +146,8 @@ static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& me
143146

144147
{ // Don't keep cs_main locked
145148
LOCK(cs_main);
146-
nHeight = ::ChainActive().Height();
149+
CHECK_NONFATAL(std::addressof(::ChainActive()) == std::addressof(chainman.ActiveChain()));
150+
nHeight = chainman.ActiveChain().Height();
147151
nHeightEnd = nHeight+nGenerate;
148152
}
149153
unsigned int nExtraNonce = 0;
@@ -354,11 +358,12 @@ static RPCHelpMan generateblock()
354358
CChainParams chainparams(Params());
355359
CBlock block;
356360

361+
ChainstateManager& chainman = EnsureChainman(request.context);
357362
{
358363
LOCK(cs_main);
359364

360365
CTxMemPool empty_mempool;
361-
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler(::ChainstateActive(), empty_mempool, chainparams).CreateNewBlock(coinbase_script));
366+
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler(chainman.ActiveChainstate(), empty_mempool, chainparams).CreateNewBlock(coinbase_script));
362367
if (!blocktemplate) {
363368
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
364369
}
@@ -369,14 +374,14 @@ static RPCHelpMan generateblock()
369374

370375
// Add transactions
371376
block.vtx.insert(block.vtx.end(), txs.begin(), txs.end());
372-
CBlockIndex* prev_block = WITH_LOCK(::cs_main, return g_chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock));
377+
CBlockIndex* prev_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock));
373378
RegenerateCommitments(block, prev_block);
374379

375380
{
376381
LOCK(cs_main);
377382

378383
BlockValidationState state;
379-
if (!TestBlockValidity(state, chainparams, ::ChainstateActive(), block, g_chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), false, false)) {
384+
if (!TestBlockValidity(state, chainparams, chainman.ActiveChainstate(), block, chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), false, false)) {
380385
throw JSONRPCError(RPC_VERIFY_ERROR, strprintf("TestBlockValidity failed: %s", state.ToString()));
381386
}
382387
}
@@ -385,7 +390,7 @@ static RPCHelpMan generateblock()
385390
uint64_t max_tries{DEFAULT_MAX_TRIES};
386391
unsigned int extra_nonce{0};
387392

388-
if (!GenerateBlock(EnsureChainman(request.context), block, max_tries, extra_nonce, block_hash) || block_hash.IsNull()) {
393+
if (!GenerateBlock(chainman, block, max_tries, extra_nonce, block_hash) || block_hash.IsNull()) {
389394
throw JSONRPCError(RPC_MISC_ERROR, "Failed to make block.");
390395
}
391396

@@ -421,12 +426,13 @@ static RPCHelpMan getmininginfo()
421426
{
422427
LOCK(cs_main);
423428
const CTxMemPool& mempool = EnsureMemPool(request.context);
429+
const CChain& active_chain = EnsureChainman(request.context).ActiveChain();
424430

425431
UniValue obj(UniValue::VOBJ);
426-
obj.pushKV("blocks", (int)::ChainActive().Height());
432+
obj.pushKV("blocks", (int)active_chain.Height());
427433
if (BlockAssembler::m_last_block_weight) obj.pushKV("currentblockweight", *BlockAssembler::m_last_block_weight);
428434
if (BlockAssembler::m_last_block_num_txs) obj.pushKV("currentblocktx", *BlockAssembler::m_last_block_num_txs);
429-
obj.pushKV("difficulty", (double)GetDifficulty(::ChainActive().Tip()));
435+
obj.pushKV("difficulty", (double)GetDifficulty(active_chain.Tip()));
430436
obj.pushKV("networkhashps", getnetworkhashps().HandleRequest(request));
431437
obj.pushKV("pooledtx", (uint64_t)mempool.size());
432438
obj.pushKV("chain", Params().NetworkIDString());
@@ -590,6 +596,7 @@ static RPCHelpMan getblocktemplate()
590596
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
591597
{
592598
LOCK(cs_main);
599+
ChainstateManager& chainman = EnsureChainman(request.context);
593600

594601
std::string strMode = "template";
595602
UniValue lpval = NullUniValue;
@@ -620,7 +627,7 @@ static RPCHelpMan getblocktemplate()
620627
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
621628

622629
uint256 hash = block.GetHash();
623-
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
630+
const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
624631
if (pindex) {
625632
if (pindex->IsValid(BLOCK_VALID_SCRIPTS))
626633
return "duplicate";
@@ -629,12 +636,12 @@ static RPCHelpMan getblocktemplate()
629636
return "duplicate-inconclusive";
630637
}
631638

632-
CBlockIndex* const pindexPrev = ::ChainActive().Tip();
639+
CBlockIndex* const pindexPrev = chainman.ActiveChain().Tip();
633640
// TestBlockValidity only supports blocks built on the current Tip
634641
if (block.hashPrevBlock != pindexPrev->GetBlockHash())
635642
return "inconclusive-not-best-prevblk";
636643
BlockValidationState state;
637-
TestBlockValidity(state, Params(), ::ChainstateActive(), block, pindexPrev, false, true);
644+
TestBlockValidity(state, Params(), chainman.ActiveChainstate(), block, pindexPrev, false, true);
638645
return BIP22ValidationResult(state);
639646
}
640647

@@ -665,7 +672,7 @@ static RPCHelpMan getblocktemplate()
665672
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, PACKAGE_NAME " is not connected!");
666673
}
667674

668-
if (::ChainstateActive().IsInitialBlockDownload()) {
675+
if (chainman.ActiveChainstate().IsInitialBlockDownload()) {
669676
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, PACKAGE_NAME " is in initial sync and waiting for blocks...");
670677
}
671678
}
@@ -691,7 +698,7 @@ static RPCHelpMan getblocktemplate()
691698
else
692699
{
693700
// NOTE: Spec does not specify behaviour for non-string longpollid, but this makes testing easier
694-
hashWatchedChain = ::ChainActive().Tip()->GetBlockHash();
701+
hashWatchedChain = chainman.ActiveChain().Tip()->GetBlockHash();
695702
nTransactionsUpdatedLastLP = nTransactionsUpdatedLast;
696703
}
697704

@@ -736,20 +743,20 @@ static RPCHelpMan getblocktemplate()
736743
static CBlockIndex* pindexPrev;
737744
static int64_t nStart;
738745
static std::unique_ptr<CBlockTemplate> pblocktemplate;
739-
if (pindexPrev != ::ChainActive().Tip() ||
746+
if (pindexPrev != chainman.ActiveChain().Tip() ||
740747
(mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 5))
741748
{
742749
// Clear pindexPrev so future calls make a new block, despite any failures from here on
743750
pindexPrev = nullptr;
744751

745752
// Store the pindexBest used before CreateNewBlock, to avoid races
746753
nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
747-
CBlockIndex* pindexPrevNew = ::ChainActive().Tip();
754+
CBlockIndex* pindexPrevNew = chainman.ActiveChain().Tip();
748755
nStart = GetTime();
749756

750757
// Create new block
751758
CScript scriptDummy = CScript() << OP_TRUE;
752-
pblocktemplate = BlockAssembler(::ChainstateActive(), mempool, Params()).CreateNewBlock(scriptDummy);
759+
pblocktemplate = BlockAssembler(chainman.ActiveChainstate(), mempool, Params()).CreateNewBlock(scriptDummy);
753760
if (!pblocktemplate)
754761
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
755762

@@ -885,7 +892,7 @@ static RPCHelpMan getblocktemplate()
885892
result.pushKV("transactions", transactions);
886893
result.pushKV("coinbaseaux", aux);
887894
result.pushKV("coinbasevalue", (int64_t)pblock->vtx[0]->vout[0].nValue);
888-
result.pushKV("longpollid", ::ChainActive().Tip()->GetBlockHash().GetHex() + ToString(nTransactionsUpdatedLast));
895+
result.pushKV("longpollid", chainman.ActiveChain().Tip()->GetBlockHash().GetHex() + ToString(nTransactionsUpdatedLast));
889896
result.pushKV("target", hashTarget.GetHex());
890897
result.pushKV("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1);
891898
result.pushKV("mutable", aMutable);
@@ -968,10 +975,11 @@ static RPCHelpMan submitblock()
968975
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block does not start with a coinbase");
969976
}
970977

978+
ChainstateManager& chainman = EnsureChainman(request.context);
971979
uint256 hash = block.GetHash();
972980
{
973981
LOCK(cs_main);
974-
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
982+
const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
975983
if (pindex) {
976984
if (pindex->IsValid(BLOCK_VALID_SCRIPTS)) {
977985
return "duplicate";
@@ -984,7 +992,7 @@ static RPCHelpMan submitblock()
984992

985993
{
986994
LOCK(cs_main);
987-
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock);
995+
const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock);
988996
if (pindex) {
989997
UpdateUncommittedBlockStructures(block, pindex, Params().GetConsensus());
990998
}
@@ -993,7 +1001,7 @@ static RPCHelpMan submitblock()
9931001
bool new_block;
9941002
auto sc = std::make_shared<submitblock_StateCatcher>(block.GetHash());
9951003
RegisterSharedValidationInterface(sc);
996-
bool accepted = EnsureChainman(request.context).ProcessNewBlock(Params(), blockptr, /* fForceProcessing */ true, /* fNewBlock */ &new_block);
1004+
bool accepted = chainman.ProcessNewBlock(Params(), blockptr, /* fForceProcessing */ true, /* fNewBlock */ &new_block);
9971005
UnregisterSharedValidationInterface(sc);
9981006
if (!new_block && accepted) {
9991007
return "duplicate";
@@ -1026,15 +1034,16 @@ static RPCHelpMan submitheader()
10261034
if (!DecodeHexBlockHeader(h, request.params[0].get_str())) {
10271035
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block header decode failed");
10281036
}
1037+
ChainstateManager& chainman = EnsureChainman(request.context);
10291038
{
10301039
LOCK(cs_main);
1031-
if (!g_chainman.m_blockman.LookupBlockIndex(h.hashPrevBlock)) {
1040+
if (!chainman.m_blockman.LookupBlockIndex(h.hashPrevBlock)) {
10321041
throw JSONRPCError(RPC_VERIFY_ERROR, "Must submit previous header (" + h.hashPrevBlock.GetHex() + ") first");
10331042
}
10341043
}
10351044

10361045
BlockValidationState state;
1037-
EnsureChainman(request.context).ProcessNewBlockHeaders({h}, state, Params());
1046+
chainman.ProcessNewBlockHeaders({h}, state, Params());
10381047
if (state.IsValid()) return NullUniValue;
10391048
if (state.IsError()) {
10401049
throw JSONRPCError(RPC_VERIFY_ERROR, state.ToString());

0 commit comments

Comments
 (0)