Skip to content

Commit

Permalink
Fix crash with empty keypool for getauxblock.
Browse files Browse the repository at this point in the history
Fix namecoin#25.  This implements the
upstream fix done in Bitcoin's bitcoin/bitcoin#6567
also for getauxblock.
  • Loading branch information
domob1812 committed Aug 20, 2015
1 parent 737dde9 commit eb6be23
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
37 changes: 37 additions & 0 deletions qa/rpc-tests/keypool.py
Expand Up @@ -16,6 +16,7 @@
import traceback

from test_framework.util import *
from test_framework import auxpow


def check_array_result(object_array, to_match, expected):
Expand Down Expand Up @@ -89,6 +90,42 @@ def run_test(nodes, tmpdir):
except JSONRPCException,e:
assert(e.error['code']==-12)

# test draining with getauxblock
test_auxpow(nodes)

def test_auxpow(nodes):
"""
Test behaviour of getauxpow. Calling getauxpow should reserve
a key from the pool, but it should be released again if the
created block is not actually used. On the other hand, if the
auxpow is submitted and turned into a block, the keypool should
be drained.
"""

nodes[0].walletpassphrase('test', 12000)
nodes[0].keypoolrefill(1)
nodes[0].walletlock()
assert_equal (nodes[0].getwalletinfo()['keypoolsize'], 2)

nodes[0].getauxblock()
assert_equal (nodes[0].getwalletinfo()['keypoolsize'], 2)
nodes[0].generate(1)
assert_equal (nodes[0].getwalletinfo()['keypoolsize'], 1)
auxblock = nodes[0].getauxblock()
assert_equal (nodes[0].getwalletinfo()['keypoolsize'], 1)

target = auxpow.reverseHex(auxblock['_target'])
solved = auxpow.computeAuxpow(auxblock['hash'], target, True)
res = nodes[0].getauxblock(auxblock['hash'], solved)
assert res
assert_equal(nodes[0].getwalletinfo()['keypoolsize'], 0)

try:
nodes[0].getauxblock()
raise AssertionError('Keypool should be exhausted by getauxblock')
except JSONRPCException,e:
assert(e.error['code']==-12)

def main():
import optparse

Expand Down
4 changes: 4 additions & 0 deletions src/rpcmining.cpp
Expand Up @@ -762,6 +762,10 @@ UniValue getauxblock(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())
throw JSONRPCError(RPC_INTERNAL_ERROR, "No coinbase script available (mining requires a wallet)");
Expand Down

0 comments on commit eb6be23

Please sign in to comment.