Skip to content

Commit dd2dc40

Browse files
committed
[RPC, Wallet] Move RPC dispatch table registration to wallet/ code
Allow extending the rpc dispatch table by appending commands when server is not running.
1 parent e1060c5 commit dd2dc40

File tree

8 files changed

+106
-93
lines changed

8 files changed

+106
-93
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ BITCOIN_CORE_H = \
164164
version.h \
165165
wallet/crypter.h \
166166
wallet/db.h \
167+
wallet/rpcwallet.h \
167168
wallet/wallet.h \
168169
wallet/wallet_ismine.h \
169170
wallet/walletdb.h \

src/init.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,8 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
926926

927927
#ifdef ENABLE_WALLET
928928
bool fDisableWallet = GetBoolArg("-disablewallet", false);
929+
if (!fDisableWallet)
930+
walletRegisterRPCCommands();
929931
#endif
930932

931933
nConnectTimeout = GetArg("-timeout", DEFAULT_CONNECT_TIMEOUT);

src/rpcserver.cpp

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,6 @@ static const CRPCCommand vRPCCommands[] =
309309
{ "rawtransactions", "getrawtransaction", &getrawtransaction, true },
310310
{ "rawtransactions", "sendrawtransaction", &sendrawtransaction, false },
311311
{ "rawtransactions", "signrawtransaction", &signrawtransaction, false }, /* uses wallet if enabled */
312-
#ifdef ENABLE_WALLET
313-
{ "rawtransactions", "fundrawtransaction", &fundrawtransaction, false },
314-
#endif
315312

316313
/* Utility functions */
317314
{ "util", "createmultisig", &createmultisig, true },
@@ -326,54 +323,6 @@ static const CRPCCommand vRPCCommands[] =
326323
{ "hidden", "invalidateblock", &invalidateblock, true },
327324
{ "hidden", "reconsiderblock", &reconsiderblock, true },
328325
{ "hidden", "setmocktime", &setmocktime, true },
329-
#ifdef ENABLE_WALLET
330-
{ "hidden", "resendwallettransactions", &resendwallettransactions, true},
331-
#endif
332-
333-
#ifdef ENABLE_WALLET
334-
/* Wallet */
335-
{ "wallet", "addmultisigaddress", &addmultisigaddress, true },
336-
{ "wallet", "backupwallet", &backupwallet, true },
337-
{ "wallet", "dumpprivkey", &dumpprivkey, true },
338-
{ "wallet", "dumpwallet", &dumpwallet, true },
339-
{ "wallet", "encryptwallet", &encryptwallet, true },
340-
{ "wallet", "getaccountaddress", &getaccountaddress, true },
341-
{ "wallet", "getaccount", &getaccount, true },
342-
{ "wallet", "getaddressesbyaccount", &getaddressesbyaccount, true },
343-
{ "wallet", "getbalance", &getbalance, false },
344-
{ "wallet", "getnewaddress", &getnewaddress, true },
345-
{ "wallet", "getrawchangeaddress", &getrawchangeaddress, true },
346-
{ "wallet", "getreceivedbyaccount", &getreceivedbyaccount, false },
347-
{ "wallet", "getreceivedbyaddress", &getreceivedbyaddress, false },
348-
{ "wallet", "gettransaction", &gettransaction, false },
349-
{ "wallet", "abandontransaction", &abandontransaction, false },
350-
{ "wallet", "getunconfirmedbalance", &getunconfirmedbalance, false },
351-
{ "wallet", "getwalletinfo", &getwalletinfo, false },
352-
{ "wallet", "importprivkey", &importprivkey, true },
353-
{ "wallet", "importwallet", &importwallet, true },
354-
{ "wallet", "importaddress", &importaddress, true },
355-
{ "wallet", "importpubkey", &importpubkey, true },
356-
{ "wallet", "keypoolrefill", &keypoolrefill, true },
357-
{ "wallet", "listaccounts", &listaccounts, false },
358-
{ "wallet", "listaddressgroupings", &listaddressgroupings, false },
359-
{ "wallet", "listlockunspent", &listlockunspent, false },
360-
{ "wallet", "listreceivedbyaccount", &listreceivedbyaccount, false },
361-
{ "wallet", "listreceivedbyaddress", &listreceivedbyaddress, false },
362-
{ "wallet", "listsinceblock", &listsinceblock, false },
363-
{ "wallet", "listtransactions", &listtransactions, false },
364-
{ "wallet", "listunspent", &listunspent, false },
365-
{ "wallet", "lockunspent", &lockunspent, true },
366-
{ "wallet", "move", &movecmd, false },
367-
{ "wallet", "sendfrom", &sendfrom, false },
368-
{ "wallet", "sendmany", &sendmany, false },
369-
{ "wallet", "sendtoaddress", &sendtoaddress, false },
370-
{ "wallet", "setaccount", &setaccount, true },
371-
{ "wallet", "settxfee", &settxfee, true },
372-
{ "wallet", "signmessage", &signmessage, true },
373-
{ "wallet", "walletlock", &walletlock, true },
374-
{ "wallet", "walletpassphrasechange", &walletpassphrasechange, true },
375-
{ "wallet", "walletpassphrase", &walletpassphrase, true },
376-
#endif // ENABLE_WALLET
377326
};
378327

379328
CRPCTable::CRPCTable()
@@ -396,6 +345,20 @@ const CRPCCommand *CRPCTable::operator[](const std::string &name) const
396345
return (*it).second;
397346
}
398347

348+
bool CRPCTable::appendCommand(const std::string& name, const CRPCCommand* pcmd)
349+
{
350+
if (IsRPCRunning())
351+
return false;
352+
353+
// don't allow overwriting for now
354+
map<string, const CRPCCommand*>::const_iterator it = mapCommands.find(name);
355+
if (it != mapCommands.end())
356+
return false;
357+
358+
mapCommands[name] = pcmd;
359+
return true;
360+
}
361+
399362
bool StartRPC()
400363
{
401364
LogPrint("rpc", "Starting RPC\n");
@@ -573,4 +536,4 @@ void RPCRunLater(const std::string& name, boost::function<void(void)> func, int6
573536
deadlineTimers.insert(std::make_pair(name, boost::shared_ptr<RPCTimerBase>(timerInterface->NewTimer(func, nSeconds*1000))));
574537
}
575538

576-
const CRPCTable tableRPC;
539+
CRPCTable tableRPC;

src/rpcserver.h

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,17 @@ class CRPCTable
144144
* @throws an exception (UniValue) when an error happens.
145145
*/
146146
UniValue execute(const std::string &method, const UniValue &params) const;
147+
148+
149+
/**
150+
* Appends a CRPCCommand to the dispatch table.
151+
* Returns false if RPC server is already running (dump concurrency protection).
152+
* Commands cannot be overwritten (returns false).
153+
*/
154+
bool appendCommand(const std::string& name, const CRPCCommand* pcmd);
147155
};
148156

149-
extern const CRPCTable tableRPC;
157+
extern CRPCTable tableRPC;
150158

151159
/**
152160
* Utilities: convert hex-encoded Values
@@ -178,13 +186,6 @@ extern UniValue setban(const UniValue& params, bool fHelp);
178186
extern UniValue listbanned(const UniValue& params, bool fHelp);
179187
extern UniValue clearbanned(const UniValue& params, bool fHelp);
180188

181-
extern UniValue dumpprivkey(const UniValue& params, bool fHelp); // in rpcdump.cpp
182-
extern UniValue importprivkey(const UniValue& params, bool fHelp);
183-
extern UniValue importaddress(const UniValue& params, bool fHelp);
184-
extern UniValue importpubkey(const UniValue& params, bool fHelp);
185-
extern UniValue dumpwallet(const UniValue& params, bool fHelp);
186-
extern UniValue importwallet(const UniValue& params, bool fHelp);
187-
188189
extern UniValue getgenerate(const UniValue& params, bool fHelp); // in rpcmining.cpp
189190
extern UniValue setgenerate(const UniValue& params, bool fHelp);
190191
extern UniValue generate(const UniValue& params, bool fHelp);
@@ -198,45 +199,13 @@ extern UniValue estimatepriority(const UniValue& params, bool fHelp);
198199
extern UniValue estimatesmartfee(const UniValue& params, bool fHelp);
199200
extern UniValue estimatesmartpriority(const UniValue& params, bool fHelp);
200201

201-
extern UniValue getnewaddress(const UniValue& params, bool fHelp); // in rpcwallet.cpp
202-
extern UniValue getaccountaddress(const UniValue& params, bool fHelp);
203-
extern UniValue getrawchangeaddress(const UniValue& params, bool fHelp);
204-
extern UniValue setaccount(const UniValue& params, bool fHelp);
205-
extern UniValue getaccount(const UniValue& params, bool fHelp);
206-
extern UniValue getaddressesbyaccount(const UniValue& params, bool fHelp);
207-
extern UniValue sendtoaddress(const UniValue& params, bool fHelp);
208-
extern UniValue signmessage(const UniValue& params, bool fHelp);
209202
extern UniValue verifymessage(const UniValue& params, bool fHelp);
210-
extern UniValue getreceivedbyaddress(const UniValue& params, bool fHelp);
211-
extern UniValue getreceivedbyaccount(const UniValue& params, bool fHelp);
212-
extern UniValue getbalance(const UniValue& params, bool fHelp);
213-
extern UniValue getunconfirmedbalance(const UniValue& params, bool fHelp);
214-
extern UniValue movecmd(const UniValue& params, bool fHelp);
215-
extern UniValue sendfrom(const UniValue& params, bool fHelp);
216-
extern UniValue sendmany(const UniValue& params, bool fHelp);
217-
extern UniValue addmultisigaddress(const UniValue& params, bool fHelp);
218203
extern UniValue createmultisig(const UniValue& params, bool fHelp);
219-
extern UniValue listreceivedbyaddress(const UniValue& params, bool fHelp);
220-
extern UniValue listreceivedbyaccount(const UniValue& params, bool fHelp);
221-
extern UniValue listtransactions(const UniValue& params, bool fHelp);
222-
extern UniValue listaddressgroupings(const UniValue& params, bool fHelp);
223-
extern UniValue listaccounts(const UniValue& params, bool fHelp);
224-
extern UniValue listsinceblock(const UniValue& params, bool fHelp);
225-
extern UniValue gettransaction(const UniValue& params, bool fHelp);
226-
extern UniValue abandontransaction(const UniValue& params, bool fHelp);
227-
extern UniValue backupwallet(const UniValue& params, bool fHelp);
228-
extern UniValue keypoolrefill(const UniValue& params, bool fHelp);
229-
extern UniValue walletpassphrase(const UniValue& params, bool fHelp);
230-
extern UniValue walletpassphrasechange(const UniValue& params, bool fHelp);
231-
extern UniValue walletlock(const UniValue& params, bool fHelp);
232-
extern UniValue encryptwallet(const UniValue& params, bool fHelp);
233204
extern UniValue validateaddress(const UniValue& params, bool fHelp);
234205
extern UniValue getinfo(const UniValue& params, bool fHelp);
235-
extern UniValue getwalletinfo(const UniValue& params, bool fHelp);
236206
extern UniValue getblockchaininfo(const UniValue& params, bool fHelp);
237207
extern UniValue getnetworkinfo(const UniValue& params, bool fHelp);
238208
extern UniValue setmocktime(const UniValue& params, bool fHelp);
239-
extern UniValue resendwallettransactions(const UniValue& params, bool fHelp);
240209

241210
extern UniValue getrawtransaction(const UniValue& params, bool fHelp); // in rcprawtransaction.cpp
242211
extern UniValue listunspent(const UniValue& params, bool fHelp);
@@ -245,7 +214,6 @@ extern UniValue listlockunspent(const UniValue& params, bool fHelp);
245214
extern UniValue createrawtransaction(const UniValue& params, bool fHelp);
246215
extern UniValue decoderawtransaction(const UniValue& params, bool fHelp);
247216
extern UniValue decodescript(const UniValue& params, bool fHelp);
248-
extern UniValue fundrawtransaction(const UniValue& params, bool fHelp);
249217
extern UniValue signrawtransaction(const UniValue& params, bool fHelp);
250218
extern UniValue sendrawtransaction(const UniValue& params, bool fHelp);
251219
extern UniValue gettxoutproof(const UniValue& params, bool fHelp);

src/test/test_bitcoin.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
5454
const CChainParams& chainparams = Params();
5555
#ifdef ENABLE_WALLET
5656
bitdb.MakeMock();
57+
walletRegisterRPCCommands();
5758
#endif
5859
ClearDatadirCache();
5960
pathTemp = GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000)));

src/wallet/rpcwallet.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2475,3 +2475,70 @@ UniValue fundrawtransaction(const UniValue& params, bool fHelp)
24752475

24762476
return result;
24772477
}
2478+
2479+
extern UniValue dumpprivkey(const UniValue& params, bool fHelp); // in rpcdump.cpp
2480+
extern UniValue importprivkey(const UniValue& params, bool fHelp);
2481+
extern UniValue importaddress(const UniValue& params, bool fHelp);
2482+
extern UniValue importpubkey(const UniValue& params, bool fHelp);
2483+
extern UniValue dumpwallet(const UniValue& params, bool fHelp);
2484+
extern UniValue importwallet(const UniValue& params, bool fHelp);
2485+
2486+
const CRPCCommand vWalletRPCCommands[] =
2487+
{ // category name actor (function) okSafeMode
2488+
// --------------------- ------------------------ ----------------------- ----------
2489+
{ "rawtransactions", "fundrawtransaction", &fundrawtransaction, false },
2490+
{ "hidden", "resendwallettransactions", &resendwallettransactions, true },
2491+
{ "wallet", "abandontransaction", &abandontransaction, false },
2492+
{ "wallet", "addmultisigaddress", &addmultisigaddress, true },
2493+
{ "wallet", "backupwallet", &backupwallet, true },
2494+
{ "wallet", "dumpprivkey", &dumpprivkey, true },
2495+
{ "wallet", "dumpwallet", &dumpwallet, true },
2496+
{ "wallet", "encryptwallet", &encryptwallet, true },
2497+
{ "wallet", "getaccountaddress", &getaccountaddress, true },
2498+
{ "wallet", "getaccount", &getaccount, true },
2499+
{ "wallet", "getaddressesbyaccount", &getaddressesbyaccount, true },
2500+
{ "wallet", "getbalance", &getbalance, false },
2501+
{ "wallet", "getnewaddress", &getnewaddress, true },
2502+
{ "wallet", "getrawchangeaddress", &getrawchangeaddress, true },
2503+
{ "wallet", "getreceivedbyaccount", &getreceivedbyaccount, false },
2504+
{ "wallet", "getreceivedbyaddress", &getreceivedbyaddress, false },
2505+
{ "wallet", "gettransaction", &gettransaction, false },
2506+
{ "wallet", "getunconfirmedbalance", &getunconfirmedbalance, false },
2507+
{ "wallet", "getwalletinfo", &getwalletinfo, false },
2508+
{ "wallet", "importprivkey", &importprivkey, true },
2509+
{ "wallet", "importwallet", &importwallet, true },
2510+
{ "wallet", "importaddress", &importaddress, true },
2511+
{ "wallet", "importpubkey", &importpubkey, true },
2512+
{ "wallet", "keypoolrefill", &keypoolrefill, true },
2513+
{ "wallet", "listaccounts", &listaccounts, false },
2514+
{ "wallet", "listaddressgroupings", &listaddressgroupings, false },
2515+
{ "wallet", "listlockunspent", &listlockunspent, false },
2516+
{ "wallet", "listreceivedbyaccount", &listreceivedbyaccount, false },
2517+
{ "wallet", "listreceivedbyaddress", &listreceivedbyaddress, false },
2518+
{ "wallet", "listsinceblock", &listsinceblock, false },
2519+
{ "wallet", "listtransactions", &listtransactions, false },
2520+
{ "wallet", "listunspent", &listunspent, false },
2521+
{ "wallet", "lockunspent", &lockunspent, true },
2522+
{ "wallet", "move", &movecmd, false },
2523+
{ "wallet", "sendfrom", &sendfrom, false },
2524+
{ "wallet", "sendmany", &sendmany, false },
2525+
{ "wallet", "sendtoaddress", &sendtoaddress, false },
2526+
{ "wallet", "setaccount", &setaccount, true },
2527+
{ "wallet", "settxfee", &settxfee, true },
2528+
{ "wallet", "signmessage", &signmessage, true },
2529+
{ "wallet", "walletlock", &walletlock, true },
2530+
{ "wallet", "walletpassphrasechange", &walletpassphrasechange, true },
2531+
{ "wallet", "walletpassphrase", &walletpassphrase, true },
2532+
};
2533+
2534+
void walletRegisterRPCCommands()
2535+
{
2536+
unsigned int vcidx;
2537+
for (vcidx = 0; vcidx < ARRAYLEN(vWalletRPCCommands); vcidx++)
2538+
{
2539+
const CRPCCommand *pcmd;
2540+
2541+
pcmd = &vWalletRPCCommands[vcidx];
2542+
tableRPC.appendCommand(pcmd->name, pcmd);
2543+
}
2544+
}

src/wallet/rpcwallet.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) 2016 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_WALLET_RPCWALLET_H
6+
#define BITCOIN_WALLET_RPCWALLET_H
7+
8+
void walletRegisterRPCCommands();
9+
10+
#endif //BITCOIN_WALLET_RPCWALLET_H

src/wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "wallet/crypter.h"
1616
#include "wallet/wallet_ismine.h"
1717
#include "wallet/walletdb.h"
18+
#include "wallet/rpcwallet.h"
1819

1920
#include <algorithm>
2021
#include <map>

0 commit comments

Comments
 (0)