Skip to content

Commit

Permalink
[RPC] Implement generatetoaddress RPC command
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed May 11, 2021
1 parent 5937c18 commit d6df233
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ static const CRPCConvertParam vRPCConvertParams[] = {
{ "setgenerate", 0 },
{ "setgenerate", 1 },
{ "generate", 0 },
{ "generatetoaddress", 0 },
{ "getnetworkhashps", 0 },
{ "getnetworkhashps", 1 },
{ "delegatestake", 1 },
Expand Down
44 changes: 44 additions & 0 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// file COPYING or https://www.opensource.org/licenses/mit-license.php.

#include "amount.h"
#include "base58.h"
#include "blockassembler.h"
#include "chainparams.h"
#include "core_io.h"
Expand Down Expand Up @@ -146,6 +147,48 @@ UniValue generate(const JSONRPCRequest& request)
return blockHashes;
}

UniValue generatetoaddress(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() < 2 || request.params.size() > 3)
throw std::runtime_error(
"generatetoaddress numblocks \"address\"\n"
"\nMine blocks immediately to a specified address (before the RPC call returns)\n"
"\nArguments:\n"
"1. numblocks (numeric, required) How many blocks are generated immediately.\n"
"2. \"address\" (string, required) The address to send the newly generated bitcoin to.\n"
"\nResult\n"
"[ blockhashes ] (array) hashes of blocks generated\n"
"\nExamples:\n"
"\nGenerate 11 blocks to myaddress\n"
+ HelpExampleCli("generatetoaddress", "11 \"myaddress\"")
);

int nGenerate = request.params[0].get_int();
CTxDestination dest(DecodeDestination(request.params[1].get_str()));
if (!IsValidDestination(dest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Error: Invalid address");
}
CScript coinbaseScript = GetScriptForDestination(dest);

const Consensus::Params& consensus = Params().GetConsensus();
int nHeightEnd = 0;
int nHeight = 0;

{ // Don't keep cs_main locked
LOCK(cs_main);
nHeight = chainActive.Height();
nHeightEnd = nHeight + nGenerate;
}

bool fPoS = consensus.NetworkUpgradeActive(nHeight + 1, Consensus::UPGRADE_POS);
return generateBlocks(consensus,
fPoS,
nGenerate,
nHeight,
nHeightEnd,
&coinbaseScript);
}

#endif // ENABLE_WALLET

#ifdef ENABLE_MINING_RPC
Expand Down Expand Up @@ -834,6 +877,7 @@ static const CRPCCommand commands[] =
/* Not shown in help */
#ifdef ENABLE_WALLET
{ "hidden", "generate", &generate, true },
{ "hidden", "generatetoaddress", &generatetoaddress, true },
#endif
{ "hidden", "submitblock", &submitblock, true },
#ifdef ENABLE_MINING_RPC
Expand Down

0 comments on commit d6df233

Please sign in to comment.