You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

keypool.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python2
  2. # Copyright (c) 2014-2015 The Bitcoin Core developers
  3. # Distributed under the MIT software license, see the accompanying
  4. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. # Exercise the wallet keypool, and interaction with wallet encryption/locking
  6. # Add python-bitcoinrpc to module search path:
  7. from test_framework.test_framework import BitcoinTestFramework
  8. from test_framework.util import *
  9. def check_array_result(object_array, to_match, expected):
  10. """
  11. Pass in array of JSON objects, a dictionary with key/value pairs
  12. to match against, and another dictionary with expected key/value
  13. pairs.
  14. """
  15. num_matched = 0
  16. for item in object_array:
  17. all_match = True
  18. for key,value in to_match.items():
  19. if item[key] != value:
  20. all_match = False
  21. if not all_match:
  22. continue
  23. for key,value in expected.items():
  24. if item[key] != value:
  25. raise AssertionError("%s : expected %s=%s"%(str(item), str(key), str(value)))
  26. num_matched = num_matched+1
  27. if num_matched == 0:
  28. raise AssertionError("No objects matched %s"%(str(to_match)))
  29. class KeyPoolTest(BitcoinTestFramework):
  30. def run_test(self):
  31. nodes = self.nodes
  32. # Encrypt wallet and wait to terminate
  33. nodes[0].encryptwallet('test')
  34. bitcoind_processes[0].wait()
  35. # Restart node 0
  36. nodes[0] = start_node(0, self.options.tmpdir)
  37. # Keep creating keys
  38. addr = nodes[0].getnewaddress()
  39. try:
  40. addr = nodes[0].getnewaddress()
  41. raise AssertionError('Keypool should be exhausted after one address')
  42. except JSONRPCException,e:
  43. assert(e.error['code']==-12)
  44. # put three new keys in the keypool
  45. nodes[0].walletpassphrase('test', 12000)
  46. nodes[0].keypoolrefill(3)
  47. nodes[0].walletlock()
  48. # drain the keys
  49. addr = set()
  50. addr.add(nodes[0].getrawchangeaddress())
  51. addr.add(nodes[0].getrawchangeaddress())
  52. addr.add(nodes[0].getrawchangeaddress())
  53. addr.add(nodes[0].getrawchangeaddress())
  54. # assert that four unique addresses were returned
  55. assert(len(addr) == 4)
  56. # the next one should fail
  57. try:
  58. addr = nodes[0].getrawchangeaddress()
  59. raise AssertionError('Keypool should be exhausted after three addresses')
  60. except JSONRPCException,e:
  61. assert(e.error['code']==-12)
  62. # refill keypool with three new addresses
  63. nodes[0].walletpassphrase('test', 12000)
  64. nodes[0].keypoolrefill(3)
  65. nodes[0].walletlock()
  66. # drain them by mining
  67. nodes[0].generate(1)
  68. nodes[0].generate(1)
  69. nodes[0].generate(1)
  70. nodes[0].generate(1)
  71. try:
  72. nodes[0].generate(1)
  73. raise AssertionError('Keypool should be exhausted after three addesses')
  74. except JSONRPCException,e:
  75. assert(e.error['code']==-12)
  76. def setup_chain(self):
  77. print("Initializing test directory "+self.options.tmpdir)
  78. initialize_chain(self.options.tmpdir)
  79. def setup_network(self):
  80. self.nodes = start_nodes(1, self.options.tmpdir)
  81. if __name__ == '__main__':
  82. KeyPoolTest().main()