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-topup.py 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2017 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 HD Wallet keypool restore function.
  6. Two nodes. Node1 is under test. Node0 is providing transactions and generating blocks.
  7. - Start node1, shutdown and backup wallet.
  8. - Generate 110 keys (enough to drain the keypool). Store key 90 (in the initial keypool) and key 110 (beyond the initial keypool). Send funds to key 90 and key 110.
  9. - Stop node1, clear the datadir, move wallet file back into the datadir and restart node1.
  10. - connect node1 to node0. Verify that they sync and node1 receives its funds."""
  11. import shutil
  12. from test_framework.test_framework import BitcoinTestFramework
  13. from test_framework.util import (
  14. assert_equal,
  15. connect_nodes_bi,
  16. sync_blocks,
  17. )
  18. class KeypoolRestoreTest(BitcoinTestFramework):
  19. def set_test_params(self):
  20. self.setup_clean_chain = True
  21. self.num_nodes = 2
  22. self.extra_args = [['-usehd=0'], ['-usehd=1', '-keypool=100', '-keypoolmin=20']]
  23. def run_test(self):
  24. self.tmpdir = self.options.tmpdir
  25. self.nodes[0].generate(101)
  26. self.log.info("Make backup of wallet")
  27. self.stop_node(1)
  28. shutil.copyfile(self.tmpdir + "/node1/regtest/wallet.dat", self.tmpdir + "/wallet.bak")
  29. self.start_node(1, self.extra_args[1])
  30. connect_nodes_bi(self.nodes, 0, 1)
  31. self.log.info("Generate keys for wallet")
  32. for _ in range(90):
  33. addr_oldpool = self.nodes[1].getnewaddress()
  34. for _ in range(20):
  35. addr_extpool = self.nodes[1].getnewaddress()
  36. self.log.info("Send funds to wallet")
  37. self.nodes[0].sendtoaddress(addr_oldpool, 10)
  38. self.nodes[0].generate(1)
  39. self.nodes[0].sendtoaddress(addr_extpool, 5)
  40. self.nodes[0].generate(1)
  41. sync_blocks(self.nodes)
  42. self.log.info("Restart node with wallet backup")
  43. self.stop_node(1)
  44. shutil.copyfile(self.tmpdir + "/wallet.bak", self.tmpdir + "/node1/regtest/wallet.dat")
  45. self.log.info("Verify keypool is restored and balance is correct")
  46. self.start_node(1, self.extra_args[1])
  47. connect_nodes_bi(self.nodes, 0, 1)
  48. self.sync_all()
  49. assert_equal(self.nodes[1].getbalance(), 15)
  50. assert_equal(self.nodes[1].listtransactions()[0]['category'], "receive")
  51. # Check that we have marked all keys up to the used keypool key as used
  52. assert_equal(self.nodes[1].validateaddress(self.nodes[1].getnewaddress())['hdkeypath'], "m/0'/0'/111'")
  53. if __name__ == '__main__':
  54. KeypoolRestoreTest().main()