Skip to content

Commit

Permalink
Merge pull request #6567
Browse files Browse the repository at this point in the history
2016576 Fix crash when mining with empty keypool. (Daniel Kraft)
  • Loading branch information
laanwj committed Aug 19, 2015
2 parents 1e92b27 + 2016576 commit 0f0f323
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
15 changes: 15 additions & 0 deletions qa/rpc-tests/keypool.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ def run_test(nodes, tmpdir):
except JSONRPCException,e:
assert(e.error['code']==-12)

# refill keypool with three new addresses
nodes[0].walletpassphrase('test', 12000)
nodes[0].keypoolrefill(3)
nodes[0].walletlock()

# drain them by mining
nodes[0].generate(1)
nodes[0].generate(1)
nodes[0].generate(1)
nodes[0].generate(1)
try:
nodes[0].generate(1)
raise AssertionError('Keypool should be exhausted after three addesses')
except JSONRPCException,e:
assert(e.error['code']==-12)

def main():
import optparse
Expand Down
6 changes: 4 additions & 2 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,10 @@ void static BitcoinMiner(const CChainParams& chainparams)
GetMainSignals().ScriptForMining(coinbaseScript);

try {
//throw an error if no script was provided
if (!coinbaseScript->reserveScript.size())
// Throw an error if no script was provided. This can happen
// due to some internal error but also if the keypool is empty.
// In the latter case, already the pointer is NULL.
if (!coinbaseScript || coinbaseScript->reserveScript.empty())
throw std::runtime_error("No coinbase script available (mining requires a wallet)");

while (true) {
Expand Down
6 changes: 5 additions & 1 deletion src/rpcmining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,12 @@ UniValue generate(const UniValue& params, bool fHelp)
boost::shared_ptr<CReserveScript> coinbaseScript;
GetMainSignals().ScriptForMining(coinbaseScript);

// If the keypool is exhausted, no script is returned at all. Catch this.
if (!coinbaseScript)
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");

//throw an error if no script was provided
if (!coinbaseScript->reserveScript.size())
if (coinbaseScript->reserveScript.empty())
throw JSONRPCError(RPC_INTERNAL_ERROR, "No coinbase script available (mining requires a wallet)");

{ // Don't keep cs_main locked
Expand Down

0 comments on commit 0f0f323

Please sign in to comment.