Skip to content

Commit 6cb8dcd

Browse files
Merge dashpay#6065: fix: add missing checks for not nullptr in rpc for dash specific code
dc15420 refactor: use EnsureConnman and EnsurePeerman when possible (Konstantin Akimov) dc01f07 fix: add missing checks for not nullptr in rpc for dash specific code (Konstantin Akimov) Pull request description: ## Issue being fixed or feature implemented There're some usages of unique pointers in RPC code that are not guarded by non-nullptr checks ## What was done? Added missing `CHECK_NON_FATAL` and refactored some of them to `EnsureConnman` and `EnsurePeerman` ## How Has This Been Tested? Run unit/functional tests ## Breaking Changes N/A ## Checklist: - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have added or updated relevant unit/integration/functional/e2e tests - [ ] I have made corresponding changes to the documentation - [x] I have assigned this pull request to a milestone ACKs for top commit: PastaPastaPasta: utACK dc15420 Tree-SHA512: 0483d9208f38648cad55804791415b2facfd165514651463f9b48c44d1963be2888471bb6b1f51015bb9ddd160f272b86f42ee67aea66e640c410175412f5f75
2 parents d2af69f + dc15420 commit 6cb8dcd

File tree

9 files changed

+59
-12
lines changed

9 files changed

+59
-12
lines changed

src/rpc/blockchain.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,7 @@ static RPCHelpMan verifychain()
16051605
const int check_depth{request.params[1].isNull() ? DEFAULT_CHECKBLOCKS : request.params[1].get_int()};
16061606

16071607
const NodeContext& node = EnsureAnyNodeContext(request.context);
1608+
CHECK_NONFATAL(node.evodb);
16081609

16091610
ChainstateManager& chainman = EnsureChainman(node);
16101611
LOCK(cs_main);
@@ -2962,7 +2963,8 @@ static RPCHelpMan dumptxoutset()
29622963
FILE* file{fsbridge::fopen(temppath, "wb")};
29632964
CAutoFile afile{file, SER_DISK, CLIENT_VERSION};
29642965
NodeContext& node = EnsureAnyNodeContext(request.context);
2965-
UniValue result = CreateUTXOSnapshot(node, node.chainman->ActiveChainstate(), afile);
2966+
const ChainstateManager& chainman = EnsureChainman(node);
2967+
UniValue result = CreateUTXOSnapshot(node, chainman.ActiveChainstate(), afile);
29662968
fs::rename(temppath, path);
29672969

29682970
result.pushKV("path", path.string());

src/rpc/coinjoin.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <coinjoin/context.h>
88
#include <coinjoin/server.h>
99
#include <rpc/blockchain.h>
10+
#include <rpc/net.h>
1011
#include <rpc/server.h>
1112
#include <rpc/util.h>
1213
#include <util/strencodings.h>
@@ -69,7 +70,8 @@ static RPCHelpMan coinjoin()
6970
}
7071

7172
CTxMemPool& mempool = EnsureMemPool(node);
72-
bool result = cj_clientman->DoAutomaticDenominating(*node.connman, mempool);
73+
CConnman& connman = EnsureConnman(node);
74+
bool result = cj_clientman->DoAutomaticDenominating(connman, mempool);
7375
return "Mixing " + (result ? "started successfully" : ("start failed: " + cj_clientman->GetStatuses().original + ", will retry"));
7476
}
7577

src/rpc/evo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,6 +1697,7 @@ static UniValue protx(const JSONRPCRequest& request)
16971697
} else if (command == "protxinfo") {
16981698
return protx_info(new_request, dmnman, mn_metaman, chainman);
16991699
} else if (command == "protxdiff") {
1700+
CHECK_NONFATAL(node.llmq_ctx);
17001701
return protx_diff(new_request, dmnman, chainman, *node.llmq_ctx);
17011702
} else if (command == "protxlistdiff") {
17021703
return protx_listdiff(new_request, dmnman, chainman);

src/rpc/governance.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <node/context.h>
1717
#include <net.h>
1818
#include <rpc/blockchain.h>
19+
#include <rpc/net.h>
1920
#include <rpc/server.h>
2021
#include <rpc/util.h>
2122
#include <governance/common.h>
@@ -53,6 +54,7 @@ static UniValue gobject_count(const JSONRPCRequest& request)
5354
gobject_count_help(request);
5455

5556
const NodeContext& node = EnsureAnyNodeContext(request.context);
57+
CHECK_NONFATAL(node.govman);
5658
return strMode == "json" ? node.govman->ToJson() : node.govman->ToString();
5759
}
5860

@@ -309,6 +311,8 @@ static UniValue gobject_submit(const JSONRPCRequest& request)
309311
gobject_submit_help(request);
310312

311313
const NodeContext& node = EnsureAnyNodeContext(request.context);
314+
CHECK_NONFATAL(node.dmnman);
315+
CHECK_NONFATAL(node.govman);
312316

313317
if(!node.mn_sync->IsBlockchainSynced()) {
314318
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Must wait for client to sync with masternode network. Try again in a minute or so.");
@@ -395,11 +399,13 @@ static UniValue gobject_submit(const JSONRPCRequest& request)
395399

396400
LogPrintf("gobject(submit) -- Adding locally created governance object - %s\n", strHash);
397401

402+
PeerManager& peerman = EnsurePeerman(node);
398403
if (fMissingConfirmations) {
404+
CHECK_NONFATAL(node.mn_sync);
399405
node.govman->AddPostponedObject(govobj);
400-
govobj.Relay(*node.peerman, *node.mn_sync);
406+
govobj.Relay(peerman, *node.mn_sync);
401407
} else {
402-
node.govman->AddGovernanceObject(govobj, *node.peerman);
408+
node.govman->AddGovernanceObject(govobj, peerman);
403409
}
404410

405411
return govobj.GetHash().ToString();
@@ -410,6 +416,7 @@ static UniValue VoteWithMasternodes(const JSONRPCRequest& request, const std::ma
410416
vote_outcome_enum_t eVoteOutcome)
411417
{
412418
const NodeContext& node = EnsureAnyNodeContext(request.context);
419+
CHECK_NONFATAL(node.govman);
413420
{
414421
LOCK(node.govman->cs);
415422
const CGovernanceObject *pGovObj = node.govman->FindConstGovernanceObject(hash);
@@ -450,7 +457,9 @@ static UniValue VoteWithMasternodes(const JSONRPCRequest& request, const std::ma
450457
}
451458

452459
CGovernanceException exception;
453-
if (node.govman->ProcessVoteAndRelay(vote, exception, *node.connman, *node.peerman)) {
460+
CConnman& connman = EnsureConnman(node);
461+
PeerManager& peerman = EnsurePeerman(node);
462+
if (node.govman->ProcessVoteAndRelay(vote, exception, connman, peerman)) {
454463
nSuccessful++;
455464
statusObj.pushKV("result", "success");
456465
} else {
@@ -493,6 +502,7 @@ static UniValue gobject_vote_many(const JSONRPCRequest& request)
493502
if (!wallet) return NullUniValue;
494503

495504
const NodeContext& node = EnsureAnyNodeContext(request.context);
505+
CHECK_NONFATAL(node.dmnman);
496506

497507
uint256 hash(ParseHashV(request.params[0], "Object hash"));
498508
std::string strVoteSignal = request.params[1].get_str();
@@ -554,6 +564,7 @@ static UniValue gobject_vote_alias(const JSONRPCRequest& request)
554564
if (!wallet) return NullUniValue;
555565

556566
const NodeContext& node = EnsureAnyNodeContext(request.context);
567+
CHECK_NONFATAL(node.dmnman);
557568

558569
uint256 hash(ParseHashV(request.params[0], "Object hash"));
559570
std::string strVoteSignal = request.params[1].get_str();
@@ -690,6 +701,7 @@ static UniValue gobject_list(const JSONRPCRequest& request)
690701

691702
const NodeContext& node = EnsureAnyNodeContext(request.context);
692703
CHECK_NONFATAL(node.dmnman);
704+
CHECK_NONFATAL(node.govman);
693705

694706
return ListObjects(*node.govman, node.dmnman->GetListAtChainTip(), strCachedSignal, strType, 0);
695707
}
@@ -857,6 +869,7 @@ static UniValue gobject_getcurrentvotes(const JSONRPCRequest& request)
857869

858870
// FIND OBJECT USER IS LOOKING FOR
859871
const NodeContext& node = EnsureAnyNodeContext(request.context);
872+
CHECK_NONFATAL(node.govman);
860873

861874
LOCK(node.govman->cs);
862875
const CGovernanceObject* pGovObj = node.govman->FindConstGovernanceObject(hash);
@@ -995,6 +1008,7 @@ static UniValue voteraw(const JSONRPCRequest& request)
9951008
}
9961009

9971010
const NodeContext& node = EnsureAnyNodeContext(request.context);
1011+
CHECK_NONFATAL(node.govman);
9981012
GovernanceObject govObjType = WITH_LOCK(node.govman->cs, return [&](){
9991013
AssertLockHeld(node.govman->cs);
10001014
const CGovernanceObject *pGovObj = node.govman->FindConstGovernanceObject(hashGovObj);
@@ -1032,8 +1046,11 @@ static UniValue voteraw(const JSONRPCRequest& request)
10321046
throw JSONRPCError(RPC_INTERNAL_ERROR, "Failure to verify vote.");
10331047
}
10341048

1049+
CConnman& connman = EnsureConnman(node);
1050+
PeerManager& peerman = EnsurePeerman(node);
1051+
10351052
CGovernanceException exception;
1036-
if (node.govman->ProcessVoteAndRelay(vote, exception, *node.connman, *node.peerman)) {
1053+
if (node.govman->ProcessVoteAndRelay(vote, exception, connman, peerman)) {
10371054
return "Voted successfully";
10381055
} else {
10391056
throw JSONRPCError(RPC_INTERNAL_ERROR, "Error voting : " + exception.GetMessage());
@@ -1068,6 +1085,7 @@ static UniValue getgovernanceinfo(const JSONRPCRequest& request)
10681085

10691086
const NodeContext& node = EnsureAnyNodeContext(request.context);
10701087
const ChainstateManager& chainman = EnsureAnyChainman(request.context);
1088+
CHECK_NONFATAL(node.dmnman);
10711089

10721090
const auto* pindex = WITH_LOCK(cs_main, return chainman.ActiveChain().Tip());
10731091
int nBlockHeight = pindex->nHeight;

src/rpc/masternode.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <net.h>
1616
#include <netbase.h>
1717
#include <rpc/blockchain.h>
18+
#include <rpc/net.h>
1819
#include <rpc/server.h>
1920
#include <rpc/util.h>
2021
#include <univalue.h>
@@ -47,8 +48,10 @@ static RPCHelpMan masternode_connect()
4748
throw JSONRPCError(RPC_INTERNAL_ERROR, strprintf("Incorrect masternode address %s", strAddress));
4849

4950
const NodeContext& node = EnsureAnyNodeContext(request.context);
50-
node.connman->OpenMasternodeConnection(CAddress(addr, NODE_NETWORK));
51-
if (!node.connman->IsConnected(CAddress(addr, NODE_NETWORK), CConnman::AllNodes))
51+
CConnman& connman = EnsureConnman(node);
52+
53+
connman.OpenMasternodeConnection(CAddress(addr, NODE_NETWORK));
54+
if (!connman.IsConnected(CAddress(addr, NODE_NETWORK), CConnman::AllNodes))
5255
throw JSONRPCError(RPC_INTERNAL_ERROR, strprintf("Couldn't connect to masternode %s", strAddress));
5356

5457
return "successfully connected";
@@ -133,6 +136,7 @@ static RPCHelpMan masternode_winner()
133136
}
134137

135138
const NodeContext& node = EnsureAnyNodeContext(request.context);
139+
CHECK_NONFATAL(node.dmnman);
136140
return GetNextMasternodeForPayment(*node.dmnman, 10);
137141
},
138142
};
@@ -152,6 +156,7 @@ static RPCHelpMan masternode_current()
152156
}
153157

154158
const NodeContext& node = EnsureAnyNodeContext(request.context);
159+
CHECK_NONFATAL(node.dmnman);
155160
return GetNextMasternodeForPayment(*node.dmnman, 1);
156161
},
157162
};
@@ -204,6 +209,7 @@ static RPCHelpMan masternode_status()
204209
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
205210
{
206211
const NodeContext& node = EnsureAnyNodeContext(request.context);
212+
CHECK_NONFATAL(node.dmnman);
207213
if (!node.mn_activeman) {
208214
throw JSONRPCError(RPC_INTERNAL_ERROR, "This node does not run an active masternode.");
209215
}
@@ -302,6 +308,7 @@ static RPCHelpMan masternode_winners()
302308
int nStartHeight = std::max(nChainTipHeight - nCount, 1);
303309

304310
CHECK_NONFATAL(node.dmnman);
311+
CHECK_NONFATAL(node.govman);
305312

306313
const auto tip_mn_list = node.dmnman->GetListAtChainTip();
307314
for (int h = nStartHeight; h <= nChainTipHeight; h++) {
@@ -384,6 +391,8 @@ static RPCHelpMan masternode_payments()
384391
// A temporary vector which is used to sort results properly (there is no "reverse" in/for UniValue)
385392
std::vector<UniValue> vecPayments;
386393

394+
CHECK_NONFATAL(node.chain_helper);
395+
CHECK_NONFATAL(node.dmnman);
387396
while (vecPayments.size() < uint64_t(std::abs(nCount)) && pindex != nullptr) {
388397
CBlock block;
389398
if (!ReadBlockFromDisk(block, pindex, Params().GetConsensus())) {
@@ -407,8 +416,6 @@ static RPCHelpMan masternode_payments()
407416
nBlockFees += nValueIn - tx->GetValueOut();
408417
}
409418

410-
CHECK_NONFATAL(node.chain_helper);
411-
412419
std::vector<CTxOut> voutMasternodePayments, voutDummy;
413420
CMutableTransaction dummyTx;
414421
CAmount blockSubsidy = GetBlockSubsidy(pindex, Params().GetConsensus());
@@ -548,6 +555,7 @@ static RPCHelpMan masternodelist_helper(bool is_composite)
548555

549556
const NodeContext& node = EnsureAnyNodeContext(request.context);
550557
const ChainstateManager& chainman = EnsureChainman(node);
558+
CHECK_NONFATAL(node.dmnman);
551559

552560
UniValue obj(UniValue::VOBJ);
553561

src/rpc/mining.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ static RPCHelpMan generateblock()
393393
block.vtx.insert(block.vtx.end(), txs.begin(), txs.end());
394394

395395
{
396+
CHECK_NONFATAL(node.evodb);
397+
396398
LOCK(cs_main);
397399

398400
BlockValidationState state;
@@ -704,6 +706,7 @@ static RPCHelpMan getblocktemplate()
704706
}
705707

706708
LLMQContext& llmq_ctx = EnsureLLMQContext(node);
709+
CHECK_NONFATAL(node.evodb);
707710

708711
CBlockIndex* const pindexPrev = active_chain.Tip();
709712
// TestBlockValidity only supports blocks built on the current Tip
@@ -733,6 +736,7 @@ static RPCHelpMan getblocktemplate()
733736
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
734737

735738
const CConnman& connman = EnsureConnman(node);
739+
CHECK_NONFATAL(node.sporkman);
736740
if (connman.GetNodeCount(ConnectionDirection::Both) == 0)
737741
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, PACKAGE_NAME " is not connected!");
738742

src/rpc/misc.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ static RPCHelpMan mnsync()
108108
std::string strMode = request.params[0].get_str();
109109

110110
const NodeContext& node = EnsureAnyNodeContext(request.context);
111+
CHECK_NONFATAL(node.mn_sync);
111112
auto& mn_sync = *node.mn_sync;
112113

113114
if(strMode == "status") {
@@ -170,6 +171,7 @@ static RPCHelpMan spork()
170171
// basic mode, show info
171172
std:: string strCommand = request.params[0].get_str();
172173
const NodeContext& node = EnsureAnyNodeContext(request.context);
174+
CHECK_NONFATAL(node.sporkman);
173175
if (strCommand == "show") {
174176
UniValue ret(UniValue::VOBJ);
175177
for (const auto& sporkDef : sporkDefs) {
@@ -215,6 +217,7 @@ static RPCHelpMan sporkupdate()
215217

216218
const NodeContext& node = EnsureAnyNodeContext(request.context);
217219
PeerManager& peerman = EnsurePeerman(node);
220+
CHECK_NONFATAL(node.sporkman);
218221

219222
// SPORK VALUE
220223
int64_t nValue = request.params[1].get_int64();

src/rpc/quorums.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <index/txindex.h>
88
#include <node/context.h>
99
#include <rpc/blockchain.h>
10+
#include <rpc/net.h>
1011
#include <rpc/server.h>
1112
#include <rpc/util.h>
1213
#include <validation.h>
@@ -279,6 +280,9 @@ static RPCHelpMan quorum_dkgstatus()
279280
const NodeContext& node = EnsureAnyNodeContext(request.context);
280281
const ChainstateManager& chainman = EnsureChainman(node);
281282
const LLMQContext& llmq_ctx = EnsureLLMQContext(node);
283+
const CConnman& connman = EnsureConnman(node);
284+
CHECK_NONFATAL(node.dmnman);
285+
CHECK_NONFATAL(node.sporkman);
282286

283287
int detailLevel = 0;
284288
if (!request.params[0].isNull()) {
@@ -322,7 +326,7 @@ static RPCHelpMan quorum_dkgstatus()
322326
auto allConnections = llmq::utils::GetQuorumConnections(llmq_params, *node.dmnman, *node.sporkman, pQuorumBaseBlockIndex, proTxHash, false);
323327
auto outboundConnections = llmq::utils::GetQuorumConnections(llmq_params, *node.dmnman, *node.sporkman, pQuorumBaseBlockIndex, proTxHash, true);
324328
std::map<uint256, CAddress> foundConnections;
325-
node.connman->ForEachNode([&](const CNode* pnode) {
329+
connman.ForEachNode([&](const CNode* pnode) {
326330
auto verifiedProRegTxHash = pnode->GetVerifiedProRegTxHash();
327331
if (!verifiedProRegTxHash.IsNull() && allConnections.count(verifiedProRegTxHash)) {
328332
foundConnections.emplace(verifiedProRegTxHash, pnode->addr);
@@ -381,6 +385,7 @@ static RPCHelpMan quorum_memberof()
381385
const NodeContext& node = EnsureAnyNodeContext(request.context);
382386
const ChainstateManager& chainman = EnsureChainman(node);
383387
const LLMQContext& llmq_ctx = EnsureLLMQContext(node);
388+
CHECK_NONFATAL(node.dmnman);
384389

385390
uint256 protxHash(ParseHashV(request.params[0], "proTxHash"));
386391
int scanQuorumsCount = -1;
@@ -747,6 +752,7 @@ static RPCHelpMan quorum_getdata()
747752
const NodeContext& node = EnsureAnyNodeContext(request.context);
748753
const ChainstateManager& chainman = EnsureChainman(node);
749754
const LLMQContext& llmq_ctx = EnsureLLMQContext(node);
755+
CConnman& connman = EnsureConnman(node);
750756

751757
NodeId nodeId = ParseInt64V(request.params[0], "nodeId");
752758
Consensus::LLMQType llmqType = static_cast<Consensus::LLMQType>(ParseInt32V(request.params[1], "llmqType"));
@@ -768,7 +774,7 @@ static RPCHelpMan quorum_getdata()
768774

769775
const CBlockIndex* pQuorumBaseBlockIndex = WITH_LOCK(cs_main, return chainman.m_blockman.LookupBlockIndex(quorumHash));
770776

771-
return node.connman->ForNode(nodeId, [&](CNode* pNode) {
777+
return connman.ForNode(nodeId, [&](CNode* pNode) {
772778
return llmq_ctx.qman->RequestQuorumData(pNode, llmqType, pQuorumBaseBlockIndex, nDataMask, proTxHash);
773779
});
774780
},
@@ -791,6 +797,7 @@ static RPCHelpMan quorum_rotationinfo()
791797
{
792798
const NodeContext& node = EnsureAnyNodeContext(request.context);
793799
const LLMQContext& llmq_ctx = EnsureLLMQContext(node);
800+
CHECK_NONFATAL(node.dmnman);
794801

795802
llmq::CGetQuorumRotationInfo cmd;
796803
llmq::CQuorumRotationInfo quorumRotationInfoRet;

src/rpc/rawtransaction.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,8 @@ static RPCHelpMan getassetunlockstatuses()
490490
throw JSONRPCError(RPC_INTERNAL_ERROR, "No blocks in chain");
491491
}
492492

493+
CHECK_NONFATAL(node.cpoolman);
494+
493495
std::optional<CCreditPool> poolCL{std::nullopt};
494496
std::optional<CCreditPool> poolOnTip{std::nullopt};
495497
std::optional<int> nSpecificCoreHeight{std::nullopt};

0 commit comments

Comments
 (0)