Skip to content

Commit 87ecfb0

Browse files
committed
Merge pull request #5711
5ebe095 Trim RPC command table (Wladimir J. van der Laan) 4401b2d Removed main.h dependency from rpcserver.cpp (Eric Lombrozo)
2 parents fcf646c + 5ebe095 commit 87ecfb0

File tree

11 files changed

+308
-125
lines changed

11 files changed

+308
-125
lines changed

src/init.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,26 @@ bool static Bind(const CService &addr, unsigned int flags) {
234234
return true;
235235
}
236236

237+
void OnRPCStopped()
238+
{
239+
cvBlockChange.notify_all();
240+
LogPrint("rpc", "RPC stopped.\n");
241+
}
242+
243+
void OnRPCPreCommand(const CRPCCommand& cmd)
244+
{
245+
#ifdef ENABLE_WALLET
246+
if (cmd.reqWallet && !pwalletMain)
247+
throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found (disabled)");
248+
#endif
249+
250+
// Observe safe mode
251+
string strWarning = GetWarnings("rpc");
252+
if (strWarning != "" && !GetBoolArg("-disablesafemode", false) &&
253+
!cmd.okSafeMode)
254+
throw JSONRPCError(RPC_FORBIDDEN_BY_SAFE_MODE, string("Safe mode: ") + strWarning);
255+
}
256+
237257
std::string HelpMessage(HelpMessageMode mode)
238258
{
239259
// When adding new options to the categories, please keep and ensure alphabetical ordering.
@@ -802,6 +822,8 @@ bool AppInit2(boost::thread_group& threadGroup)
802822
if (fServer)
803823
{
804824
uiInterface.InitMessage.connect(SetRPCWarmupStatus);
825+
RPCServer::OnStopped(&OnRPCStopped);
826+
RPCServer::OnPreCommand(&OnRPCPreCommand);
805827
StartRPCThreads();
806828
}
807829

src/rpcblockchain.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Value getblockcount(const Array& params, bool fHelp)
105105
+ HelpExampleRpc("getblockcount", "")
106106
);
107107

108+
LOCK(cs_main);
108109
return chainActive.Height();
109110
}
110111

@@ -121,6 +122,7 @@ Value getbestblockhash(const Array& params, bool fHelp)
121122
+ HelpExampleRpc("getbestblockhash", "")
122123
);
123124

125+
LOCK(cs_main);
124126
return chainActive.Tip()->GetBlockHash().GetHex();
125127
}
126128

@@ -137,6 +139,7 @@ Value getdifficulty(const Array& params, bool fHelp)
137139
+ HelpExampleRpc("getdifficulty", "")
138140
);
139141

142+
LOCK(cs_main);
140143
return GetDifficulty();
141144
}
142145

@@ -173,6 +176,8 @@ Value getrawmempool(const Array& params, bool fHelp)
173176
+ HelpExampleRpc("getrawmempool", "true")
174177
);
175178

179+
LOCK(cs_main);
180+
176181
bool fVerbose = false;
177182
if (params.size() > 0)
178183
fVerbose = params[0].get_bool();
@@ -233,6 +238,8 @@ Value getblockhash(const Array& params, bool fHelp)
233238
+ HelpExampleRpc("getblockhash", "1000")
234239
);
235240

241+
LOCK(cs_main);
242+
236243
int nHeight = params[0].get_int();
237244
if (nHeight < 0 || nHeight > chainActive.Height())
238245
throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range");
@@ -277,6 +284,8 @@ Value getblock(const Array& params, bool fHelp)
277284
+ HelpExampleRpc("getblock", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"")
278285
);
279286

287+
LOCK(cs_main);
288+
280289
std::string strHash = params[0].get_str();
281290
uint256 hash(uint256S(strHash));
282291

@@ -326,6 +335,8 @@ Value gettxoutsetinfo(const Array& params, bool fHelp)
326335
+ HelpExampleRpc("gettxoutsetinfo", "")
327336
);
328337

338+
LOCK(cs_main);
339+
329340
Object ret;
330341

331342
CCoinsStats stats;
@@ -380,6 +391,8 @@ Value gettxout(const Array& params, bool fHelp)
380391
+ HelpExampleRpc("gettxout", "\"txid\", 1")
381392
);
382393

394+
LOCK(cs_main);
395+
383396
Object ret;
384397

385398
std::string strHash = params[0].get_str();
@@ -436,6 +449,8 @@ Value verifychain(const Array& params, bool fHelp)
436449
+ HelpExampleRpc("verifychain", "")
437450
);
438451

452+
LOCK(cs_main);
453+
439454
int nCheckLevel = GetArg("-checklevel", 3);
440455
int nCheckDepth = GetArg("-checkblocks", 288);
441456
if (params.size() > 0)
@@ -467,6 +482,8 @@ Value getblockchaininfo(const Array& params, bool fHelp)
467482
+ HelpExampleRpc("getblockchaininfo", "")
468483
);
469484

485+
LOCK(cs_main);
486+
470487
Object obj;
471488
obj.push_back(Pair("chain", Params().NetworkIDString()));
472489
obj.push_back(Pair("blocks", (int)chainActive.Height()));
@@ -526,6 +543,8 @@ Value getchaintips(const Array& params, bool fHelp)
526543
+ HelpExampleRpc("getchaintips", "")
527544
);
528545

546+
LOCK(cs_main);
547+
529548
/* Build up a list of chain tips. We start with the list of all
530549
known blocks, and successively remove blocks that appear as pprev
531550
of another block. */

src/rpcdump.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ Value importprivkey(const Array& params, bool fHelp)
9191
+ HelpExampleRpc("importprivkey", "\"mykey\", \"testing\", false")
9292
);
9393

94+
LOCK2(cs_main, pwalletMain->cs_wallet);
95+
9496
EnsureWalletIsUnlocked();
9597

9698
string strSecret = params[0].get_str();
@@ -158,6 +160,8 @@ Value importaddress(const Array& params, bool fHelp)
158160
+ HelpExampleRpc("importaddress", "\"myaddress\", \"testing\", false")
159161
);
160162

163+
LOCK2(cs_main, pwalletMain->cs_wallet);
164+
161165
CScript script;
162166

163167
CBitcoinAddress address(params[0].get_str());
@@ -223,6 +227,8 @@ Value importwallet(const Array& params, bool fHelp)
223227
+ HelpExampleRpc("importwallet", "\"test\"")
224228
);
225229

230+
LOCK2(cs_main, pwalletMain->cs_wallet);
231+
226232
EnsureWalletIsUnlocked();
227233

228234
ifstream file;
@@ -322,6 +328,8 @@ Value dumpprivkey(const Array& params, bool fHelp)
322328
+ HelpExampleRpc("dumpprivkey", "\"myaddress\"")
323329
);
324330

331+
LOCK2(cs_main, pwalletMain->cs_wallet);
332+
325333
EnsureWalletIsUnlocked();
326334

327335
string strAddress = params[0].get_str();
@@ -351,6 +359,8 @@ Value dumpwallet(const Array& params, bool fHelp)
351359
+ HelpExampleRpc("dumpwallet", "\"test\"")
352360
);
353361

362+
LOCK2(cs_main, pwalletMain->cs_wallet);
363+
354364
EnsureWalletIsUnlocked();
355365

356366
ofstream file;

src/rpcmining.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Value getnetworkhashps(const Array& params, bool fHelp)
8888
+ HelpExampleRpc("getnetworkhashps", "")
8989
);
9090

91+
LOCK(cs_main);
9192
return GetNetworkHashPS(params.size() > 0 ? params[0].get_int() : 120, params.size() > 1 ? params[1].get_int() : -1);
9293
}
9394

@@ -107,6 +108,7 @@ Value getgenerate(const Array& params, bool fHelp)
107108
+ HelpExampleRpc("getgenerate", "")
108109
);
109110

111+
LOCK(cs_main);
110112
return GetBoolArg("-gen", false);
111113
}
112114

@@ -200,7 +202,6 @@ Value setgenerate(const Array& params, bool fHelp)
200202

201203
return Value::null;
202204
}
203-
204205
#endif
205206

206207

@@ -228,6 +229,9 @@ Value getmininginfo(const Array& params, bool fHelp)
228229
+ HelpExampleRpc("getmininginfo", "")
229230
);
230231

232+
233+
LOCK(cs_main);
234+
231235
Object obj;
232236
obj.push_back(Pair("blocks", (int)chainActive.Height()));
233237
obj.push_back(Pair("currentblocksize", (uint64_t)nLastBlockSize));
@@ -268,8 +272,9 @@ Value prioritisetransaction(const Array& params, bool fHelp)
268272
+ HelpExampleRpc("prioritisetransaction", "\"txid\", 0.0, 10000")
269273
);
270274

271-
uint256 hash = ParseHashStr(params[0].get_str(), "txid");
275+
LOCK(cs_main);
272276

277+
uint256 hash = ParseHashStr(params[0].get_str(), "txid");
273278
CAmount nAmount = params[2].get_int64();
274279

275280
mempool.PrioritiseTransaction(hash, params[0].get_str(), params[1].get_real(), nAmount);
@@ -358,6 +363,8 @@ Value getblocktemplate(const Array& params, bool fHelp)
358363
+ HelpExampleRpc("getblocktemplate", "")
359364
);
360365

366+
LOCK(cs_main);
367+
361368
std::string strMode = "template";
362369
Value lpval = Value::null;
363370
if (params.size() > 0)

src/rpcmisc.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ Value getinfo(const Array& params, bool fHelp)
6969
+ HelpExampleRpc("getinfo", "")
7070
);
7171

72+
#ifdef ENABLE_WALLET
73+
LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL);
74+
#else
75+
LOCK(cs_main);
76+
#endif
77+
7278
proxyType proxy;
7379
GetProxy(NET_IPV4, proxy);
7480

@@ -172,6 +178,12 @@ Value validateaddress(const Array& params, bool fHelp)
172178
+ HelpExampleRpc("validateaddress", "\"1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc\"")
173179
);
174180

181+
#ifdef ENABLE_WALLET
182+
LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL);
183+
#else
184+
LOCK(cs_main);
185+
#endif
186+
175187
CBitcoinAddress address(params[0].get_str());
176188
bool isValid = address.IsValid();
177189

@@ -329,6 +341,8 @@ Value verifymessage(const Array& params, bool fHelp)
329341
+ HelpExampleRpc("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XZ\", \"signature\", \"my message\"")
330342
);
331343

344+
LOCK(cs_main);
345+
332346
string strAddress = params[0].get_str();
333347
string strSign = params[1].get_str();
334348
string strMessage = params[2].get_str();
@@ -372,6 +386,8 @@ Value setmocktime(const Array& params, bool fHelp)
372386
if (!Params().MineBlocksOnDemand())
373387
throw runtime_error("setmocktime for regression testing (-regtest mode) only");
374388

389+
LOCK(cs_main);
390+
375391
RPCTypeCheck(params, boost::assign::list_of(int_type));
376392
SetMockTime(params[0].get_int64());
377393

src/rpcnet.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ Value getconnectioncount(const Array& params, bool fHelp)
3434
+ HelpExampleRpc("getconnectioncount", "")
3535
);
3636

37-
LOCK(cs_vNodes);
37+
LOCK2(cs_main, cs_vNodes);
38+
3839
return (int)vNodes.size();
3940
}
4041

@@ -52,7 +53,8 @@ Value ping(const Array& params, bool fHelp)
5253
);
5354

5455
// Request that each node send a ping during next message processing pass
55-
LOCK(cs_vNodes);
56+
LOCK2(cs_main, cs_vNodes);
57+
5658
BOOST_FOREACH(CNode* pNode, vNodes) {
5759
pNode->fPingQueued = true;
5860
}
@@ -113,6 +115,8 @@ Value getpeerinfo(const Array& params, bool fHelp)
113115
+ HelpExampleRpc("getpeerinfo", "")
114116
);
115117

118+
LOCK(cs_main);
119+
116120
vector<CNodeStats> vstats;
117121
CopyNodeStats(vstats);
118122

@@ -411,6 +415,8 @@ Value getnetworkinfo(const Array& params, bool fHelp)
411415
+ HelpExampleRpc("getnetworkinfo", "")
412416
);
413417

418+
LOCK(cs_main);
419+
414420
Object obj;
415421
obj.push_back(Pair("version", CLIENT_VERSION));
416422
obj.push_back(Pair("subversion",

src/rpcrawtransaction.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ Value getrawtransaction(const Array& params, bool fHelp)
169169
+ HelpExampleRpc("getrawtransaction", "\"mytxid\", 1")
170170
);
171171

172+
LOCK(cs_main);
173+
172174
uint256 hash = ParseHashV(params[0], "parameter 1");
173175

174176
bool fVerbose = false;
@@ -256,6 +258,7 @@ Value listunspent(const Array& params, bool fHelp)
256258
Array results;
257259
vector<COutput> vecOutputs;
258260
assert(pwalletMain != NULL);
261+
LOCK2(cs_main, pwalletMain->cs_wallet);
259262
pwalletMain->AvailableCoins(vecOutputs, false);
260263
BOOST_FOREACH(const COutput& out, vecOutputs) {
261264
if (out.nDepth < nMinDepth || out.nDepth > nMaxDepth)
@@ -334,6 +337,7 @@ Value createrawtransaction(const Array& params, bool fHelp)
334337
+ HelpExampleRpc("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\", \"{\\\"address\\\":0.01}\"")
335338
);
336339

340+
LOCK(cs_main);
337341
RPCTypeCheck(params, boost::assign::list_of(array_type)(obj_type));
338342

339343
Array inputs = params[0].get_array();
@@ -428,6 +432,7 @@ Value decoderawtransaction(const Array& params, bool fHelp)
428432
+ HelpExampleRpc("decoderawtransaction", "\"hexstring\"")
429433
);
430434

435+
LOCK(cs_main);
431436
RPCTypeCheck(params, boost::assign::list_of(str_type));
432437

433438
CTransaction tx;
@@ -466,6 +471,7 @@ Value decodescript(const Array& params, bool fHelp)
466471
+ HelpExampleRpc("decodescript", "\"hexstring\"")
467472
);
468473

474+
LOCK(cs_main);
469475
RPCTypeCheck(params, boost::assign::list_of(str_type));
470476

471477
Object r;
@@ -532,6 +538,11 @@ Value signrawtransaction(const Array& params, bool fHelp)
532538
+ HelpExampleRpc("signrawtransaction", "\"myhex\"")
533539
);
534540

541+
#ifdef ENABLE_WALLET
542+
LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL);
543+
#else
544+
LOCK(cs_main);
545+
#endif
535546
RPCTypeCheck(params, boost::assign::list_of(str_type)(array_type)(array_type)(str_type), true);
536547

537548
vector<unsigned char> txData(ParseHexV(params[0], "argument 1"));
@@ -591,7 +602,7 @@ Value signrawtransaction(const Array& params, bool fHelp)
591602
}
592603
}
593604
#ifdef ENABLE_WALLET
594-
else
605+
else if (pwalletMain)
595606
EnsureWalletIsUnlocked();
596607
#endif
597608

@@ -722,6 +733,7 @@ Value sendrawtransaction(const Array& params, bool fHelp)
722733
+ HelpExampleRpc("sendrawtransaction", "\"signedhex\"")
723734
);
724735

736+
LOCK(cs_main);
725737
RPCTypeCheck(params, boost::assign::list_of(str_type)(bool_type));
726738

727739
// parse hex string from parameter

0 commit comments

Comments
 (0)