Skip to content

Commit e0e3cbb

Browse files
committed
Merge #11179: rpc: Push down safe mode checks
ec6902d rpc: Push down safe mode checks (Andrew Chow) Pull request description: This contains most of the changes of #10563 "remove safe mode" by @achow101, but doesn't remove the safe mode yet, but put an `ObserveSafeMode()` check in (all 23) individual calls which used to have okSafeMode=false. This cleans up the ugly "okSafeMode" flag from the dispatch tables, which is not a concern for the RPC server. Extra-author: Wladimir J. van der Laan <[email protected]> Tree-SHA512: eee0f251fe2f38f122e7391e3c4e98d6a1e2757f3b718d6b560ad835ae94f11490865a0aef893e90b5fe298165932c8dd8298224173ac2677a5245cd532bac6e
2 parents 50fae68 + ec6902d commit e0e3cbb

File tree

14 files changed

+189
-150
lines changed

14 files changed

+189
-150
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ BITCOIN_CORE_H = \
130130
rpc/client.h \
131131
rpc/mining.h \
132132
rpc/protocol.h \
133+
rpc/safemode.h \
133134
rpc/server.h \
134135
rpc/register.h \
135136
scheduler.h \
@@ -210,6 +211,7 @@ libbitcoin_server_a_SOURCES = \
210211
rpc/misc.cpp \
211212
rpc/net.cpp \
212213
rpc/rawtransaction.cpp \
214+
rpc/safemode.cpp \
213215
rpc/server.cpp \
214216
script/sigcache.cpp \
215217
script/ismine.cpp \

src/init.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "policy/policy.h"
3131
#include "rpc/server.h"
3232
#include "rpc/register.h"
33+
#include "rpc/safemode.h"
3334
#include "rpc/blockchain.h"
3435
#include "script/standard.h"
3536
#include "script/sigcache.h"
@@ -70,7 +71,6 @@
7071
bool fFeeEstimatesInitialized = false;
7172
static const bool DEFAULT_PROXYRANDOMIZE = true;
7273
static const bool DEFAULT_REST_ENABLE = false;
73-
static const bool DEFAULT_DISABLE_SAFEMODE = true;
7474
static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
7575

7676
std::unique_ptr<CConnman> g_connman;
@@ -318,15 +318,6 @@ void OnRPCStopped()
318318
LogPrint(BCLog::RPC, "RPC stopped.\n");
319319
}
320320

321-
void OnRPCPreCommand(const CRPCCommand& cmd)
322-
{
323-
// Observe safe mode
324-
std::string strWarning = GetWarnings("rpc");
325-
if (strWarning != "" && !gArgs.GetBoolArg("-disablesafemode", DEFAULT_DISABLE_SAFEMODE) &&
326-
!cmd.okSafeMode)
327-
throw JSONRPCError(RPC_FORBIDDEN_BY_SAFE_MODE, std::string("Safe mode: ") + strWarning);
328-
}
329-
330321
std::string HelpMessage(HelpMessageMode mode)
331322
{
332323
const auto defaultBaseParams = CreateBaseChainParams(CBaseChainParams::MAIN);
@@ -721,7 +712,6 @@ bool AppInitServers(boost::thread_group& threadGroup)
721712
{
722713
RPCServer::OnStarted(&OnRPCStarted);
723714
RPCServer::OnStopped(&OnRPCStopped);
724-
RPCServer::OnPreCommand(&OnRPCPreCommand);
725715
if (!InitHTTPServer())
726716
return false;
727717
if (!StartRPC())

src/qt/test/rpcnestedtests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static UniValue rpcNestedTest_rpc(const JSONRPCRequest& request)
2929

3030
static const CRPCCommand vRPCCommands[] =
3131
{
32-
{ "test", "rpcNestedTest", &rpcNestedTest_rpc, true, {} },
32+
{ "test", "rpcNestedTest", &rpcNestedTest_rpc, {} },
3333
};
3434

3535
void RPCNestedTests::rpcNestedTests()

src/rpc/blockchain.cpp

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,35 +1533,35 @@ UniValue getchaintxstats(const JSONRPCRequest& request)
15331533
}
15341534

15351535
static const CRPCCommand commands[] =
1536-
{ // category name actor (function) okSafe argNames
1537-
// --------------------- ------------------------ ----------------------- ------ ----------
1538-
{ "blockchain", "getblockchaininfo", &getblockchaininfo, true, {} },
1539-
{ "blockchain", "getchaintxstats", &getchaintxstats, true, {"nblocks", "blockhash"} },
1540-
{ "blockchain", "getbestblockhash", &getbestblockhash, true, {} },
1541-
{ "blockchain", "getblockcount", &getblockcount, true, {} },
1542-
{ "blockchain", "getblock", &getblock, true, {"blockhash","verbosity|verbose"} },
1543-
{ "blockchain", "getblockhash", &getblockhash, true, {"height"} },
1544-
{ "blockchain", "getblockheader", &getblockheader, true, {"blockhash","verbose"} },
1545-
{ "blockchain", "getchaintips", &getchaintips, true, {} },
1546-
{ "blockchain", "getdifficulty", &getdifficulty, true, {} },
1547-
{ "blockchain", "getmempoolancestors", &getmempoolancestors, true, {"txid","verbose"} },
1548-
{ "blockchain", "getmempooldescendants", &getmempooldescendants, true, {"txid","verbose"} },
1549-
{ "blockchain", "getmempoolentry", &getmempoolentry, true, {"txid"} },
1550-
{ "blockchain", "getmempoolinfo", &getmempoolinfo, true, {} },
1551-
{ "blockchain", "getrawmempool", &getrawmempool, true, {"verbose"} },
1552-
{ "blockchain", "gettxout", &gettxout, true, {"txid","n","include_mempool"} },
1553-
{ "blockchain", "gettxoutsetinfo", &gettxoutsetinfo, true, {} },
1554-
{ "blockchain", "pruneblockchain", &pruneblockchain, true, {"height"} },
1555-
{ "blockchain", "verifychain", &verifychain, true, {"checklevel","nblocks"} },
1556-
1557-
{ "blockchain", "preciousblock", &preciousblock, true, {"blockhash"} },
1536+
{ // category name actor (function) argNames
1537+
// --------------------- ------------------------ ----------------------- ----------
1538+
{ "blockchain", "getblockchaininfo", &getblockchaininfo, {} },
1539+
{ "blockchain", "getchaintxstats", &getchaintxstats, {"nblocks", "blockhash"} },
1540+
{ "blockchain", "getbestblockhash", &getbestblockhash, {} },
1541+
{ "blockchain", "getblockcount", &getblockcount, {} },
1542+
{ "blockchain", "getblock", &getblock, {"blockhash","verbosity|verbose"} },
1543+
{ "blockchain", "getblockhash", &getblockhash, {"height"} },
1544+
{ "blockchain", "getblockheader", &getblockheader, {"blockhash","verbose"} },
1545+
{ "blockchain", "getchaintips", &getchaintips, {} },
1546+
{ "blockchain", "getdifficulty", &getdifficulty, {} },
1547+
{ "blockchain", "getmempoolancestors", &getmempoolancestors, {"txid","verbose"} },
1548+
{ "blockchain", "getmempooldescendants", &getmempooldescendants, {"txid","verbose"} },
1549+
{ "blockchain", "getmempoolentry", &getmempoolentry, {"txid"} },
1550+
{ "blockchain", "getmempoolinfo", &getmempoolinfo, {} },
1551+
{ "blockchain", "getrawmempool", &getrawmempool, {"verbose"} },
1552+
{ "blockchain", "gettxout", &gettxout, {"txid","n","include_mempool"} },
1553+
{ "blockchain", "gettxoutsetinfo", &gettxoutsetinfo, {} },
1554+
{ "blockchain", "pruneblockchain", &pruneblockchain, {"height"} },
1555+
{ "blockchain", "verifychain", &verifychain, {"checklevel","nblocks"} },
1556+
1557+
{ "blockchain", "preciousblock", &preciousblock, {"blockhash"} },
15581558

15591559
/* Not shown in help */
1560-
{ "hidden", "invalidateblock", &invalidateblock, true, {"blockhash"} },
1561-
{ "hidden", "reconsiderblock", &reconsiderblock, true, {"blockhash"} },
1562-
{ "hidden", "waitfornewblock", &waitfornewblock, true, {"timeout"} },
1563-
{ "hidden", "waitforblock", &waitforblock, true, {"blockhash","timeout"} },
1564-
{ "hidden", "waitforblockheight", &waitforblockheight, true, {"height","timeout"} },
1560+
{ "hidden", "invalidateblock", &invalidateblock, {"blockhash"} },
1561+
{ "hidden", "reconsiderblock", &reconsiderblock, {"blockhash"} },
1562+
{ "hidden", "waitfornewblock", &waitfornewblock, {"timeout"} },
1563+
{ "hidden", "waitforblock", &waitforblock, {"blockhash","timeout"} },
1564+
{ "hidden", "waitforblockheight", &waitforblockheight, {"height","timeout"} },
15651565
};
15661566

15671567
void RegisterBlockchainRPCCommands(CRPCTable &t)

src/rpc/mining.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -967,20 +967,21 @@ UniValue estimaterawfee(const JSONRPCRequest& request)
967967
}
968968

969969
static const CRPCCommand commands[] =
970-
{ // category name actor (function) okSafeMode
970+
{ // category name actor (function) argNames
971971
// --------------------- ------------------------ ----------------------- ----------
972-
{ "mining", "getnetworkhashps", &getnetworkhashps, true, {"nblocks","height"} },
973-
{ "mining", "getmininginfo", &getmininginfo, true, {} },
974-
{ "mining", "prioritisetransaction", &prioritisetransaction, true, {"txid","dummy","fee_delta"} },
975-
{ "mining", "getblocktemplate", &getblocktemplate, true, {"template_request"} },
976-
{ "mining", "submitblock", &submitblock, true, {"hexdata","dummy"} },
972+
{ "mining", "getnetworkhashps", &getnetworkhashps, {"nblocks","height"} },
973+
{ "mining", "getmininginfo", &getmininginfo, {} },
974+
{ "mining", "prioritisetransaction", &prioritisetransaction, {"txid","dummy","fee_delta"} },
975+
{ "mining", "getblocktemplate", &getblocktemplate, {"template_request"} },
976+
{ "mining", "submitblock", &submitblock, {"hexdata","dummy"} },
977977

978-
{ "generating", "generatetoaddress", &generatetoaddress, true, {"nblocks","address","maxtries"} },
979978

980-
{ "util", "estimatefee", &estimatefee, true, {"nblocks"} },
981-
{ "util", "estimatesmartfee", &estimatesmartfee, true, {"conf_target", "estimate_mode"} },
979+
{ "generating", "generatetoaddress", &generatetoaddress, {"nblocks","address","maxtries"} },
982980

983-
{ "hidden", "estimaterawfee", &estimaterawfee, true, {"conf_target", "threshold"} },
981+
{ "util", "estimatefee", &estimatefee, {"nblocks"} },
982+
{ "util", "estimatesmartfee", &estimatesmartfee, {"nblocks", "estimate_mode"} },
983+
984+
{ "hidden", "estimaterawfee", &estimaterawfee, {"conf_target", "threshold"} },
984985
};
985986

986987
void RegisterMiningRPCCommands(CRPCTable &t)

src/rpc/misc.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -649,20 +649,20 @@ UniValue echo(const JSONRPCRequest& request)
649649
}
650650

651651
static const CRPCCommand commands[] =
652-
{ // category name actor (function) okSafeMode
652+
{ // category name actor (function) argNames
653653
// --------------------- ------------------------ ----------------------- ----------
654-
{ "control", "getinfo", &getinfo, true, {} }, /* uses wallet if enabled */
655-
{ "control", "getmemoryinfo", &getmemoryinfo, true, {"mode"} },
656-
{ "util", "validateaddress", &validateaddress, true, {"address"} }, /* uses wallet if enabled */
657-
{ "util", "createmultisig", &createmultisig, true, {"nrequired","keys"} },
658-
{ "util", "verifymessage", &verifymessage, true, {"address","signature","message"} },
659-
{ "util", "signmessagewithprivkey", &signmessagewithprivkey, true, {"privkey","message"} },
654+
{ "control", "getinfo", &getinfo, {} }, /* uses wallet if enabled */
655+
{ "control", "getmemoryinfo", &getmemoryinfo, {"mode"} },
656+
{ "util", "validateaddress", &validateaddress, {"address"} }, /* uses wallet if enabled */
657+
{ "util", "createmultisig", &createmultisig, {"nrequired","keys"} },
658+
{ "util", "verifymessage", &verifymessage, {"address","signature","message"} },
659+
{ "util", "signmessagewithprivkey", &signmessagewithprivkey, {"privkey","message"} },
660660

661661
/* Not shown in help */
662-
{ "hidden", "setmocktime", &setmocktime, true, {"timestamp"}},
663-
{ "hidden", "echo", &echo, true, {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
664-
{ "hidden", "echojson", &echo, true, {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
665-
{ "hidden", "logging", &logging, true, {"include", "exclude"}},
662+
{ "hidden", "setmocktime", &setmocktime, {"timestamp"}},
663+
{ "hidden", "echo", &echo, {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
664+
{ "hidden", "echojson", &echo, {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
665+
{ "hidden", "logging", &logging, {"include", "exclude"}},
666666
};
667667

668668
void RegisterMiscRPCCommands(CRPCTable &t)

src/rpc/net.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -623,20 +623,20 @@ UniValue setnetworkactive(const JSONRPCRequest& request)
623623
}
624624

625625
static const CRPCCommand commands[] =
626-
{ // category name actor (function) okSafeMode
626+
{ // category name actor (function) argNames
627627
// --------------------- ------------------------ ----------------------- ----------
628-
{ "network", "getconnectioncount", &getconnectioncount, true, {} },
629-
{ "network", "ping", &ping, true, {} },
630-
{ "network", "getpeerinfo", &getpeerinfo, true, {} },
631-
{ "network", "addnode", &addnode, true, {"node","command"} },
632-
{ "network", "disconnectnode", &disconnectnode, true, {"address", "nodeid"} },
633-
{ "network", "getaddednodeinfo", &getaddednodeinfo, true, {"node"} },
634-
{ "network", "getnettotals", &getnettotals, true, {} },
635-
{ "network", "getnetworkinfo", &getnetworkinfo, true, {} },
636-
{ "network", "setban", &setban, true, {"subnet", "command", "bantime", "absolute"} },
637-
{ "network", "listbanned", &listbanned, true, {} },
638-
{ "network", "clearbanned", &clearbanned, true, {} },
639-
{ "network", "setnetworkactive", &setnetworkactive, true, {"state"} },
628+
{ "network", "getconnectioncount", &getconnectioncount, {} },
629+
{ "network", "ping", &ping, {} },
630+
{ "network", "getpeerinfo", &getpeerinfo, {} },
631+
{ "network", "addnode", &addnode, {"node","command"} },
632+
{ "network", "disconnectnode", &disconnectnode, {"address", "nodeid"} },
633+
{ "network", "getaddednodeinfo", &getaddednodeinfo, {"node"} },
634+
{ "network", "getnettotals", &getnettotals, {} },
635+
{ "network", "getnetworkinfo", &getnetworkinfo, {} },
636+
{ "network", "setban", &setban, {"subnet", "command", "bantime", "absolute"} },
637+
{ "network", "listbanned", &listbanned, {} },
638+
{ "network", "clearbanned", &clearbanned, {} },
639+
{ "network", "setnetworkactive", &setnetworkactive, {"state"} },
640640
};
641641

642642
void RegisterNetRPCCommands(CRPCTable &t)

src/rpc/rawtransaction.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "policy/policy.h"
1717
#include "policy/rbf.h"
1818
#include "primitives/transaction.h"
19+
#include "rpc/safemode.h"
1920
#include "rpc/server.h"
2021
#include "script/script.h"
2122
#include "script/script_error.h"
@@ -703,6 +704,7 @@ UniValue signrawtransaction(const JSONRPCRequest& request)
703704
+ HelpExampleRpc("signrawtransaction", "\"myhex\"")
704705
);
705706

707+
ObserveSafeMode();
706708
#ifdef ENABLE_WALLET
707709
LOCK2(cs_main, pwallet ? &pwallet->cs_wallet : nullptr);
708710
#else
@@ -908,6 +910,7 @@ UniValue sendrawtransaction(const JSONRPCRequest& request)
908910
+ HelpExampleRpc("sendrawtransaction", "\"signedhex\"")
909911
);
910912

913+
ObserveSafeMode();
911914
LOCK(cs_main);
912915
RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VBOOL});
913916

@@ -959,18 +962,18 @@ UniValue sendrawtransaction(const JSONRPCRequest& request)
959962
}
960963

961964
static const CRPCCommand commands[] =
962-
{ // category name actor (function) okSafeMode
965+
{ // category name actor (function) argNames
963966
// --------------------- ------------------------ ----------------------- ----------
964-
{ "rawtransactions", "getrawtransaction", &getrawtransaction, true, {"txid","verbose"} },
965-
{ "rawtransactions", "createrawtransaction", &createrawtransaction, true, {"inputs","outputs","locktime","replaceable"} },
966-
{ "rawtransactions", "decoderawtransaction", &decoderawtransaction, true, {"hexstring"} },
967-
{ "rawtransactions", "decodescript", &decodescript, true, {"hexstring"} },
968-
{ "rawtransactions", "sendrawtransaction", &sendrawtransaction, false, {"hexstring","allowhighfees"} },
969-
{ "rawtransactions", "combinerawtransaction", &combinerawtransaction, true, {"txs"} },
970-
{ "rawtransactions", "signrawtransaction", &signrawtransaction, false, {"hexstring","prevtxs","privkeys","sighashtype"} }, /* uses wallet if enabled */
971-
972-
{ "blockchain", "gettxoutproof", &gettxoutproof, true, {"txids", "blockhash"} },
973-
{ "blockchain", "verifytxoutproof", &verifytxoutproof, true, {"proof"} },
967+
{ "rawtransactions", "getrawtransaction", &getrawtransaction, {"txid","verbose"} },
968+
{ "rawtransactions", "createrawtransaction", &createrawtransaction, {"inputs","outputs","locktime","replaceable"} },
969+
{ "rawtransactions", "decoderawtransaction", &decoderawtransaction, {"hexstring"} },
970+
{ "rawtransactions", "decodescript", &decodescript, {"hexstring"} },
971+
{ "rawtransactions", "sendrawtransaction", &sendrawtransaction, {"hexstring","allowhighfees"} },
972+
{ "rawtransactions", "combinerawtransaction", &combinerawtransaction, {"txs"} },
973+
{ "rawtransactions", "signrawtransaction", &signrawtransaction, {"hexstring","prevtxs","privkeys","sighashtype"} }, /* uses wallet if enabled */
974+
975+
{ "blockchain", "gettxoutproof", &gettxoutproof, {"txids", "blockhash"} },
976+
{ "blockchain", "verifytxoutproof", &verifytxoutproof, {"proof"} },
974977
};
975978

976979
void RegisterRawTransactionRPCCommands(CRPCTable &t)

src/rpc/safemode.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "safemode.h"
2+
3+
#include "rpc/protocol.h"
4+
#include "util.h"
5+
#include "warnings.h"
6+
7+
void ObserveSafeMode()
8+
{
9+
std::string warning = GetWarnings("rpc");
10+
if (warning != "" && !gArgs.GetBoolArg("-disablesafemode", DEFAULT_DISABLE_SAFEMODE)) {
11+
throw JSONRPCError(RPC_FORBIDDEN_BY_SAFE_MODE, std::string("Safe mode: ") + warning);
12+
}
13+
}
14+

src/rpc/safemode.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) 2017 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_RPC_SAFEMODE_H
6+
#define BITCOIN_RPC_SAFEMODE_H
7+
8+
static const bool DEFAULT_DISABLE_SAFEMODE = true;
9+
10+
void ObserveSafeMode();
11+
12+
#endif // BITCOIN_RPC_SAFEMODE_H

0 commit comments

Comments
 (0)