Skip to content

Commit 6b5ce00

Browse files
committed
[RPC] Don't do extra PoW round for pos blocks in 'generate' RPC
1 parent da7c50e commit 6b5ce00

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/rpc/mining.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -136,47 +136,49 @@ UniValue generate(const UniValue& params, bool fHelp)
136136
if (!Params().MineBlocksOnDemand())
137137
throw JSONRPCError(RPC_METHOD_NOT_FOUND, "This method can only be used on regtest");
138138

139-
int nHeightStart = 0;
139+
const int nGenerate = params[0].get_int();
140140
int nHeightEnd = 0;
141141
int nHeight = 0;
142-
int nGenerate = params[0].get_int();
143142
CReserveKey reservekey(pwalletMain);
144143

145144
{ // Don't keep cs_main locked
146145
LOCK(cs_main);
147-
nHeightStart = chainActive.Height();
148-
nHeight = nHeightStart;
149-
nHeightEnd = nHeightStart+nGenerate;
146+
nHeight = chainActive.Height();
147+
nHeightEnd = nHeight + nGenerate;
150148
}
151149
unsigned int nExtraNonce = 0;
152150
UniValue blockHashes(UniValue::VARR);
153-
bool fPoS = nHeight >= Params().LAST_POW_BLOCK();
154-
while (nHeight < nHeightEnd)
155-
{
156151

152+
bool fPoS = false;
153+
const int last_pow_block = Params().LAST_POW_BLOCK();
154+
while (nHeight < nHeightEnd && !ShutdownRequested())
155+
{
156+
if (!fPoS) fPoS = (nHeight >= last_pow_block);
157157
std::unique_ptr<CBlockTemplate> pblocktemplate(
158158
fPoS ? CreateNewBlock(CScript(), pwalletMain, fPoS) : CreateNewBlockWithKey(reservekey, pwalletMain)
159159
);
160160
if (!pblocktemplate.get())
161161
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
162162
CBlock *pblock = &pblocktemplate->block;
163163

164-
if(!fPoS){
164+
if(!fPoS) {
165165
{
166166
LOCK(cs_main);
167167
IncrementExtraNonce(pblock, chainActive.Tip(), nExtraNonce);
168168
}
169+
while (pblock->nNonce < std::numeric_limits<uint32_t>::max() &&
170+
!CheckProofOfWork(pblock->GetHash(), pblock->nBits)) {
171+
++pblock->nNonce;
172+
}
173+
if (ShutdownRequested()) break;
174+
if (pblock->nNonce == std::numeric_limits<uint32_t>::max()) continue;
169175
}
170-
while (!CheckProofOfWork(pblock->GetHash(), pblock->nBits)) {
171-
// Yes, there is a chance every nonce could fail to satisfy the -regtest
172-
// target -- 1 in 2^(2^32). That ain't gonna happen.
173-
++pblock->nNonce;
174-
}
176+
175177
CValidationState state;
176-
if (!ProcessNewBlock(state, NULL, pblock))
178+
if (!ProcessNewBlock(state, nullptr, pblock))
177179
throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted");
180+
178181
++nHeight;
179-
fPoS = nHeight >= Params().LAST_POW_BLOCK();
180182
blockHashes.push_back(pblock->GetHash().GetHex());
181183
}
182184
return blockHashes;

0 commit comments

Comments
 (0)