| 114 | } |
| 115 | |
| 116 | UniValue generate(const UniValue& params, bool fHelp) |
| 117 | { |
| 118 | if (fHelp || params.size() < 1 || params.size() > 1) |
| 119 | throw runtime_error( |
| 120 | "generate numblocks\n" |
| 121 | "\nMine blocks immediately (before the RPC call returns)\n" |
| 122 | "\nNote: this function can only be used on the regtest network\n" |
| 123 | "1. numblocks (numeric) How many blocks are generated immediately.\n" |
| 124 | "\nResult\n" |
| 125 | "[ blockhashes ] (array) hashes of blocks generated\n" |
| 126 | "\nExamples:\n" |
| 127 | "\nGenerate 11 blocks\n" |
| 128 | + HelpExampleCli("generate", "11") |
| 129 | ); |
| 130 | |
| 131 | if (pwalletMain == NULL) |
| 132 | throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found (disabled)"); |
| 133 | if (!Params().MineBlocksOnDemand()) |
| 134 | throw JSONRPCError(RPC_METHOD_NOT_FOUND, "This method can only be used on regtest"); |
| 135 | |
| 136 | int nHeightStart = 0; |
| 137 | int nHeightEnd = 0; |
| 138 | int nHeight = 0; |
| 139 | int nGenerate = params[0].get_int(); |
| 140 | CReserveKey reservekey(pwalletMain); |
| 141 | |
| 142 | { // Don't keep cs_main locked |
| 143 | LOCK(cs_main); |
| 144 | nHeightStart = chainActive.Height(); |
| 145 | nHeight = nHeightStart; |
| 146 | nHeightEnd = nHeightStart+nGenerate; |
| 147 | } |
| 148 | unsigned int nExtraNonce = 0; |
| 149 | UniValue blockHashes(UniValue::VARR); |
| 150 | while (nHeight < nHeightEnd) |
| 151 | { |
| 152 | unique_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey)); |
| 153 | if (!pblocktemplate.get()) |
| 154 | throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet keypool empty"); |
| 155 | CBlock *pblock = &pblocktemplate->block; |
| 156 | { |
| 157 | LOCK(cs_main); |
| 158 | uint64_t nMaxBlockSize = GetNextMaxBlockSize(chainActive.Tip(), Params().GetConsensus()); |
| 159 | IncrementExtraNonce(pblock, chainActive.Tip(), nExtraNonce, nMaxBlockSize); |
| 160 | } |
| 161 | while (!CheckProofOfWork(pblock->GetHash(), pblock->nBits, Params().GetConsensus())) { |
| 162 | // Yes, there is a chance every nonce could fail to satisfy the -regtest |
| 163 | // target -- 1 in 2^(2^32). That ain't gonna happen. |
| 164 | ++pblock->nNonce; |
| 165 | } |
| 166 | CValidationState state; |
| 167 | if (!ProcessNewBlock(state, BlockSource{}, pblock, true, NULL)) |
| 168 | throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted"); |
| 169 | ++nHeight; |
| 170 | blockHashes.push_back(pblock->GetHash().GetHex()); |
| 171 | } |
| 172 | reservekey.KeepKey(); |
| 173 | return blockHashes; |
nothing calls this directly
no test coverage detected