| 3323 | } |
| 3324 | |
| 3325 | UniValue generateBlocks(std::shared_ptr<CReserveScript> coinbaseScript, int nGenerate, uint64_t nMaxTries, bool keepScript) |
| 3326 | { |
| 3327 | static const int nInnerLoopCount = 0x10000; |
| 3328 | int nHeightEnd = 0; |
| 3329 | int nHeight = 0; |
| 3330 | beginning: |
| 3331 | { // Don't keep cs_main locked |
| 3332 | LOCK(cs_main); |
| 3333 | nHeight = chainActive.Height(); |
| 3334 | nHeightEnd = nHeight+nGenerate; |
| 3335 | } |
| 3336 | unsigned int nExtraNonce = 0; |
| 3337 | UniValue blockHashes(UniValue::VARR); |
| 3338 | |
| 3339 | while (nHeight < nHeightEnd && nMaxTries > 0 ) |
| 3340 | { |
| 3341 | // nHeight = chainActive.Height(); |
| 3342 | // nHeightEnd = nHeight+nGenerate; |
| 3343 | std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(Params()).CreateNewBlock(coinbaseScript->reserveScript, true, false, 0, 0, 0, true)); |
| 3344 | if (!pblocktemplate.get()) |
| 3345 | throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block"); |
| 3346 | CBlock *pblock = &pblocktemplate->block; |
| 3347 | { |
| 3348 | LOCK(cs_main); |
| 3349 | IncrementExtraNonce(pblock, chainActive.Tip(), nExtraNonce); |
| 3350 | } |
| 3351 | |
| 3352 | while (nMaxTries > 0 && pblock->nNonce < nInnerLoopCount |
| 3353 | && nHeight == chainActive.Height() |
| 3354 | && !ShutdownRequested() |
| 3355 | && !CheckProofOfWork(pblock->GetHash(nHeight + 1,1), pblock->nBits, Params().GetConsensus())) { |
| 3356 | // |
| 3357 | // std::cout << "chainactiveheight " << chainActive.Height() << std::endl; |
| 3358 | ++pblock->nNonce; |
| 3359 | --nMaxTries; |
| 3360 | if (nHeight != chainActive.Height()) { continue;} |
| 3361 | } |
| 3362 | |
| 3363 | // if (!CheckProofOfWork(pblock->GetHash(nHeight + 1,1), pblock->nBits, Params().GetConsensus())) |
| 3364 | // continue; |
| 3365 | |
| 3366 | if (nMaxTries == 0) { |
| 3367 | break; |
| 3368 | } |
| 3369 | if (pblock->nNonce == nInnerLoopCount) { |
| 3370 | continue; |
| 3371 | } |
| 3372 | CValidationState state; |
| 3373 | if (!ProcessNewBlock(state, Params(), NULL, pblock)) |
| 3374 | throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted"); |
| 3375 | ++nHeight; |
| 3376 | blockHashes.push_back(pblock->GetHash().GetHex()); |
| 3377 | |
| 3378 | //mark script as important because it was used at least for one coinbase output if the script came from the wallet |
| 3379 | if (keepScript) |
| 3380 | { |
| 3381 | coinbaseScript->KeepScript(); |
| 3382 | } |
no test coverage detected