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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 run_test(self):
  10. nodes = self.nodes
  11. addr_before_encrypting = nodes[0].getnewaddress()
  12. addr_before_encrypting_data = nodes[0].validateaddress(addr_before_encrypting)
  13. wallet_info_old = nodes[0].getwalletinfo()
  14. assert(addr_before_encrypting_data['hdmasterkeyid'] == wallet_info_old['hdmasterkeyid'])
  15. # Encrypt wallet and wait to terminate
  16. nodes[0].encryptwallet('test')
  17. bitcoind_processes[0].wait()
  18. # Restart node 0
  19. nodes[0] = start_node(0, self.options.tmpdir)
  20. # Keep creating keys
  21. addr = nodes[0].getnewaddress()
  22. addr_data = nodes[0].validateaddress(addr)
  23. wallet_info = nodes[0].getwalletinfo()
  24. assert(addr_before_encrypting_data['hdmasterkeyid'] != wallet_info['hdmasterkeyid'])
  25. assert(addr_data['hdmasterkeyid'] == wallet_info['hdmasterkeyid'])
  26. assert_raises_jsonrpc(-12, "Error: Keypool ran out, please call keypoolrefill first", nodes[0].getnewaddress)
  27. # put three new keys in the keypool
  28. nodes[0].walletpassphrase('test', 12000)
  29. nodes[0].keypoolrefill(3)
  30. nodes[0].walletlock()
  31. # drain the keys
  32. addr = set()
  33. addr.add(nodes[0].getrawchangeaddress())
  34. addr.add(nodes[0].getrawchangeaddress())
  35. addr.add(nodes[0].getrawchangeaddress())
  36. addr.add(nodes[0].getrawchangeaddress())
  37. # assert that four unique addresses were returned
  38. assert(len(addr) == 4)
  39. # the next one should fail
  40. assert_raises_jsonrpc(-12, "Keypool ran out", nodes[0].getrawchangeaddress)
  41. # refill keypool with three new addresses
  42. nodes[0].walletpassphrase('test', 1)
  43. nodes[0].keypoolrefill(3)
  44. # test walletpassphrase timeout
  45. time.sleep(1.1)
  46. assert_equal(nodes[0].getwalletinfo()["unlocked_until"], 0)
  47. # drain them by mining
  48. nodes[0].generate(1)
  49. nodes[0].generate(1)
  50. nodes[0].generate(1)
  51. nodes[0].generate(1)
  52. assert_raises_jsonrpc(-12, "Keypool ran out", nodes[0].generate, 1)
  53. def __init__(self):
  54. super().__init__()
  55. self.setup_clean_chain = False
  56. self.num_nodes = 1
  57. def setup_network(self):
  58. self.nodes = self.setup_nodes()
  59. if __name__ == '__main__':
  60. KeyPoolTest().main()