Skip to content

Commit d81bec7

Browse files
committed
Merge #10683: rpc: Move the generate RPC call to rpcwallet
2a96283 rpc: Update `generate` for developer notes (Wladimir J. van der Laan) df7e2f0 rpc: Move the `generate` RPC call to rpcwallet (Wladimir J. van der Laan) Tree-SHA512: ec658d6178f8435dc54b9d9c6dd59f873055a8ae0c3f177c02049d77b93107dd5fc17a1ff56d50f051810d52fdf306846eaba2ef4fc8d2a6cfa831f57a1045c4
2 parents dd07f47 + 2a96283 commit d81bec7

File tree

7 files changed

+66
-44
lines changed

7 files changed

+66
-44
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ BITCOIN_CORE_H = \
127127
reverselock.h \
128128
rpc/blockchain.h \
129129
rpc/client.h \
130+
rpc/mining.h \
130131
rpc/protocol.h \
131132
rpc/server.h \
132133
rpc/register.h \

src/rpc/mining.cpp

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "policy/fees.h"
1919
#include "pow.h"
2020
#include "rpc/blockchain.h"
21+
#include "rpc/mining.h"
2122
#include "rpc/server.h"
2223
#include "txmempool.h"
2324
#include "util.h"
@@ -141,42 +142,6 @@ UniValue generateBlocks(std::shared_ptr<CReserveScript> coinbaseScript, int nGen
141142
return blockHashes;
142143
}
143144

144-
UniValue generate(const JSONRPCRequest& request)
145-
{
146-
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
147-
throw std::runtime_error(
148-
"generate nblocks ( maxtries )\n"
149-
"\nMine up to nblocks blocks immediately (before the RPC call returns)\n"
150-
"\nArguments:\n"
151-
"1. nblocks (numeric, required) How many blocks are generated immediately.\n"
152-
"2. maxtries (numeric, optional) How many iterations to try (default = 1000000).\n"
153-
"\nResult:\n"
154-
"[ blockhashes ] (array) hashes of blocks generated\n"
155-
"\nExamples:\n"
156-
"\nGenerate 11 blocks\n"
157-
+ HelpExampleCli("generate", "11")
158-
);
159-
160-
int nGenerate = request.params[0].get_int();
161-
uint64_t nMaxTries = 1000000;
162-
if (request.params.size() > 1) {
163-
nMaxTries = request.params[1].get_int();
164-
}
165-
166-
std::shared_ptr<CReserveScript> coinbaseScript;
167-
GetMainSignals().ScriptForMining(coinbaseScript);
168-
169-
// If the keypool is exhausted, no script is returned at all. Catch this.
170-
if (!coinbaseScript)
171-
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
172-
173-
//throw an error if no script was provided
174-
if (coinbaseScript->reserveScript.empty())
175-
throw JSONRPCError(RPC_INTERNAL_ERROR, "No coinbase script available (mining requires a wallet)");
176-
177-
return generateBlocks(coinbaseScript, nGenerate, nMaxTries, true);
178-
}
179-
180145
UniValue generatetoaddress(const JSONRPCRequest& request)
181146
{
182147
if (request.fHelp || request.params.size() < 2 || request.params.size() > 3)
@@ -962,7 +927,6 @@ static const CRPCCommand commands[] =
962927
{ "mining", "getblocktemplate", &getblocktemplate, true, {"template_request"} },
963928
{ "mining", "submitblock", &submitblock, true, {"hexdata","dummy"} },
964929

965-
{ "generating", "generate", &generate, true, {"nblocks","maxtries"} },
966930
{ "generating", "generatetoaddress", &generatetoaddress, true, {"nblocks","address","maxtries"} },
967931

968932
{ "util", "estimatefee", &estimatefee, true, {"nblocks"} },

src/rpc/mining.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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_MINING_H
6+
#define BITCOIN_RPC_MINING_H
7+
8+
#include "script/script.h"
9+
10+
#include <univalue.h>
11+
12+
/** Generate blocks (mine) */
13+
UniValue generateBlocks(std::shared_ptr<CReserveScript> coinbaseScript, int nGenerate, uint64_t nMaxTries, bool keepScript);
14+
15+
#endif

src/validationinterface.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ void RegisterValidationInterface(CValidationInterface* pwalletIn) {
2121
g_signals.Inventory.connect(boost::bind(&CValidationInterface::Inventory, pwalletIn, _1));
2222
g_signals.Broadcast.connect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1, _2));
2323
g_signals.BlockChecked.connect(boost::bind(&CValidationInterface::BlockChecked, pwalletIn, _1, _2));
24-
g_signals.ScriptForMining.connect(boost::bind(&CValidationInterface::GetScriptForMining, pwalletIn, _1));
2524
g_signals.NewPoWValidBlock.connect(boost::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, _1, _2));
2625
}
2726

2827
void UnregisterValidationInterface(CValidationInterface* pwalletIn) {
29-
g_signals.ScriptForMining.disconnect(boost::bind(&CValidationInterface::GetScriptForMining, pwalletIn, _1));
3028
g_signals.BlockChecked.disconnect(boost::bind(&CValidationInterface::BlockChecked, pwalletIn, _1, _2));
3129
g_signals.Broadcast.disconnect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1, _2));
3230
g_signals.Inventory.disconnect(boost::bind(&CValidationInterface::Inventory, pwalletIn, _1));
@@ -39,7 +37,6 @@ void UnregisterValidationInterface(CValidationInterface* pwalletIn) {
3937
}
4038

4139
void UnregisterAllValidationInterfaces() {
42-
g_signals.ScriptForMining.disconnect_all_slots();
4340
g_signals.BlockChecked.disconnect_all_slots();
4441
g_signals.Broadcast.disconnect_all_slots();
4542
g_signals.Inventory.disconnect_all_slots();

src/validationinterface.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class CValidationInterface {
4040
virtual void Inventory(const uint256 &hash) {}
4141
virtual void ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman) {}
4242
virtual void BlockChecked(const CBlock&, const CValidationState&) {}
43-
virtual void GetScriptForMining(std::shared_ptr<CReserveScript>&) {};
4443
virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& block) {};
4544
friend void ::RegisterValidationInterface(CValidationInterface*);
4645
friend void ::UnregisterValidationInterface(CValidationInterface*);
@@ -72,8 +71,6 @@ struct CMainSignals {
7271
* callback was generated (not necessarily now)
7372
*/
7473
boost::signals2::signal<void (const CBlock&, const CValidationState&)> BlockChecked;
75-
/** Notifies listeners that a key for mining is required (coinbase) */
76-
boost::signals2::signal<void (std::shared_ptr<CReserveScript>&)> ScriptForMining;
7774
/**
7875
* Notifies listeners that a block which builds directly on our current tip
7976
* has been received and connected to the headers tree, though not validated yet */

src/wallet/rpcwallet.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "policy/fees.h"
1616
#include "policy/policy.h"
1717
#include "policy/rbf.h"
18+
#include "rpc/mining.h"
1819
#include "rpc/server.h"
1920
#include "script/sign.h"
2021
#include "timedata.h"
@@ -2922,6 +2923,51 @@ UniValue bumpfee(const JSONRPCRequest& request)
29222923
return result;
29232924
}
29242925

2926+
UniValue generate(const JSONRPCRequest& request)
2927+
{
2928+
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
2929+
2930+
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2931+
return NullUniValue;
2932+
}
2933+
2934+
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2) {
2935+
throw std::runtime_error(
2936+
"generate nblocks ( maxtries )\n"
2937+
"\nMine up to nblocks blocks immediately (before the RPC call returns) to an address in the wallet.\n"
2938+
"\nArguments:\n"
2939+
"1. nblocks (numeric, required) How many blocks are generated immediately.\n"
2940+
"2. maxtries (numeric, optional) How many iterations to try (default = 1000000).\n"
2941+
"\nResult:\n"
2942+
"[ blockhashes ] (array) hashes of blocks generated\n"
2943+
"\nExamples:\n"
2944+
"\nGenerate 11 blocks\n"
2945+
+ HelpExampleCli("generate", "11")
2946+
);
2947+
}
2948+
2949+
int num_generate = request.params[0].get_int();
2950+
uint64_t max_tries = 1000000;
2951+
if (request.params.size() > 1 && !request.params[1].isNull()) {
2952+
max_tries = request.params[1].get_int();
2953+
}
2954+
2955+
std::shared_ptr<CReserveScript> coinbase_script;
2956+
pwallet->GetScriptForMining(coinbase_script);
2957+
2958+
// If the keypool is exhausted, no script is returned at all. Catch this.
2959+
if (!coinbase_script) {
2960+
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
2961+
}
2962+
2963+
//throw an error if no script was provided
2964+
if (coinbase_script->reserveScript.empty()) {
2965+
throw JSONRPCError(RPC_INTERNAL_ERROR, "No coinbase script available");
2966+
}
2967+
2968+
return generateBlocks(coinbase_script, num_generate, max_tries, true);
2969+
}
2970+
29252971
extern UniValue abortrescan(const JSONRPCRequest& request); // in rpcdump.cpp
29262972
extern UniValue dumpprivkey(const JSONRPCRequest& request); // in rpcdump.cpp
29272973
extern UniValue importprivkey(const JSONRPCRequest& request);
@@ -2985,6 +3031,8 @@ static const CRPCCommand commands[] =
29853031
{ "wallet", "walletpassphrasechange", &walletpassphrasechange, true, {"oldpassphrase","newpassphrase"} },
29863032
{ "wallet", "walletpassphrase", &walletpassphrase, true, {"passphrase","timeout"} },
29873033
{ "wallet", "removeprunedfunds", &removeprunedfunds, true, {"txid"} },
3034+
3035+
{ "generating", "generate", &generate, true, {"nblocks","maxtries"} },
29883036
};
29893037

29903038
void RegisterWalletRPCCommands(CRPCTable &t)

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
10251025
}
10261026
}
10271027

1028-
void GetScriptForMining(std::shared_ptr<CReserveScript> &script) override;
1028+
void GetScriptForMining(std::shared_ptr<CReserveScript> &script);
10291029

10301030
unsigned int GetKeyPoolSize()
10311031
{

0 commit comments

Comments
 (0)