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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2014-2016 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. """Test the wallet keypool and interaction with wallet encryption/locking."""
  6. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import *
  8. class KeyPoolTest(BitcoinTestFramework):
  9. def set_test_params(self):
  10. self.num_nodes = 1
  11. def run_test(self):
  12. nodes = self.nodes
  13. addr_before_encrypting = nodes[0].getnewaddress()
  14. addr_before_encrypting_data = nodes[0].validateaddress(addr_before_encrypting)
  15. wallet_info_old = nodes[0].getwalletinfo()
  16. assert(addr_before_encrypting_data['hdmasterkeyid'] == wallet_info_old['hdmasterkeyid'])
  17. # Encrypt wallet and wait to terminate
  18. nodes[0].node_encrypt_wallet('test')
  19. # Restart node 0
  20. self.start_node(0)
  21. # Keep creating keys
  22. addr = nodes[0].getnewaddress()
  23. addr_data = nodes[0].validateaddress(addr)
  24. wallet_info = nodes[0].getwalletinfo()
  25. assert(addr_before_encrypting_data['hdmasterkeyid'] != wallet_info['hdmasterkeyid'])
  26. assert(addr_data['hdmasterkeyid'] == wallet_info['hdmasterkeyid'])
  27. assert_raises_rpc_error(-12, "Error: Keypool ran out, please call keypoolrefill first", nodes[0].getnewaddress)
  28. # put six (plus 2) new keys in the keypool (100% external-, +100% internal-keys, 1 in min)
  29. nodes[0].walletpassphrase('test', 12000)
  30. nodes[0].keypoolrefill(6)
  31. nodes[0].walletlock()
  32. wi = nodes[0].getwalletinfo()
  33. assert_equal(wi['keypoolsize_hd_internal'], 6)
  34. assert_equal(wi['keypoolsize'], 6)
  35. # drain the internal keys
  36. nodes[0].getrawchangeaddress()
  37. nodes[0].getrawchangeaddress()
  38. nodes[0].getrawchangeaddress()
  39. nodes[0].getrawchangeaddress()
  40. nodes[0].getrawchangeaddress()
  41. nodes[0].getrawchangeaddress()
  42. addr = set()
  43. # the next one should fail
  44. assert_raises_rpc_error(-12, "Keypool ran out", nodes[0].getrawchangeaddress)
  45. # drain the external keys
  46. addr.add(nodes[0].getnewaddress())
  47. addr.add(nodes[0].getnewaddress())
  48. addr.add(nodes[0].getnewaddress())
  49. addr.add(nodes[0].getnewaddress())
  50. addr.add(nodes[0].getnewaddress())
  51. addr.add(nodes[0].getnewaddress())
  52. assert(len(addr) == 6)
  53. # the next one should fail
  54. assert_raises_rpc_error(-12, "Error: Keypool ran out, please call keypoolrefill first", nodes[0].getnewaddress)
  55. # refill keypool with three new addresses
  56. nodes[0].walletpassphrase('test', 1)
  57. nodes[0].keypoolrefill(3)
  58. # test walletpassphrase timeout
  59. time.sleep(1.1)
  60. assert_equal(nodes[0].getwalletinfo()["unlocked_until"], 0)
  61. # drain them by mining
  62. nodes[0].generate(1)
  63. nodes[0].generate(1)
  64. nodes[0].generate(1)
  65. assert_raises_rpc_error(-12, "Keypool ran out", nodes[0].generate, 1)
  66. nodes[0].walletpassphrase('test', 100)
  67. nodes[0].keypoolrefill(100)
  68. wi = nodes[0].getwalletinfo()
  69. assert_equal(wi['keypoolsize_hd_internal'], 100)
  70. assert_equal(wi['keypoolsize'], 100)
  71. if __name__ == '__main__':
  72. KeyPoolTest().main()