Skip to content

Commit

Permalink
Miner: generate RPC, fix coinbase script key not marked as used
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed Feb 21, 2021
1 parent 296c956 commit b9249c5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
double dHashesPerSec = 0.0;
int64_t nHPSTimerStart = 0;

std::unique_ptr<CBlockTemplate> CreateNewBlockWithKey(CReserveKey& reservekey, CWallet* pwallet)
std::unique_ptr<CBlockTemplate> CreateNewBlockWithKey(CReserveKey* reservekey, CWallet* pwallet)
{
CPubKey pubkey;
if (!reservekey.GetReservedKey(pubkey))
if (!reservekey->GetReservedKey(pubkey))
return nullptr;

const int nHeightNext = chainActive.Tip()->nHeight + 1;
Expand Down Expand Up @@ -169,7 +169,7 @@ void BitcoinMiner(CWallet* pwallet, bool fProofOfStake)

std::unique_ptr<CBlockTemplate> pblocktemplate((fProofOfStake ?
BlockAssembler(Params(), DEFAULT_PRINTPRIORITY).CreateNewBlock(CScript(), pwallet, true, &availableCoins) :
CreateNewBlockWithKey(*opReservekey, pwallet)));
CreateNewBlockWithKey(opReservekey.get_ptr(), pwallet)));
if (!pblocktemplate) continue;
std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>(pblocktemplate->block);

Expand Down
2 changes: 1 addition & 1 deletion src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static const bool DEFAULT_PRINTPRIORITY = false;
/** Run the miner threads */
void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads);
/** Generate a new block, without valid proof-of-work */
std::unique_ptr<CBlockTemplate> CreateNewBlockWithKey(CReserveKey& reservekey, CWallet* pwallet);
std::unique_ptr<CBlockTemplate> CreateNewBlockWithKey(CReserveKey* reservekey, CWallet* pwallet);

void BitcoinMiner(CWallet* pwallet, bool fProofOfStake);
void ThreadStakeMinter();
Expand Down
13 changes: 11 additions & 2 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ UniValue generate(const JSONRPCRequest& request)

const Consensus::Params& consensus = Params().GetConsensus();
bool fPoS = consensus.NetworkUpgradeActive(nHeight + 1, Consensus::UPGRADE_POS);
std::unique_ptr<CReserveKey> reservekey;

if (fPoS) {
// If we are in PoS, wallet must be unlocked.
EnsureWalletIsUnlocked();
} else {
// Coinbase key
reservekey = MakeUnique<CReserveKey>(pwalletMain);
}

UniValue blockHashes(UniValue::VARR);
CReserveKey reservekey(pwalletMain);
unsigned int nExtraNonce = 0;

while (nHeight < nHeightEnd && !ShutdownRequested()) {
Expand All @@ -81,7 +84,7 @@ UniValue generate(const JSONRPCRequest& request)

std::unique_ptr<CBlockTemplate> pblocktemplate((fPoS ?
BlockAssembler(Params(), DEFAULT_PRINTPRIORITY).CreateNewBlock(CScript(), pwalletMain, true, &availableCoins) :
CreateNewBlockWithKey(reservekey, pwalletMain)));
CreateNewBlockWithKey(reservekey.get(), pwalletMain)));
if (!pblocktemplate.get()) break;
std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>(pblocktemplate->block);

Expand Down Expand Up @@ -114,6 +117,12 @@ UniValue generate(const JSONRPCRequest& request)
if (nGenerated == 0 || (!fPoS && nGenerated < nGenerate))
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new blocks");

// mark key as used, only for PoW coinbases
if (reservekey) {
// Remove key from key pool
reservekey->KeepKey();
}

return blockHashes;
}
#endif // ENABLE_WALLET
Expand Down

0 comments on commit b9249c5

Please sign in to comment.