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.

walletbackup.py 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 backup features.
  6. Test case is:
  7. 4 nodes. 1 2 and 3 send transactions between each other,
  8. fourth node is a miner.
  9. 1 2 3 each mine a block to start, then
  10. Miner creates 100 blocks so 1 2 3 each have 50 mature
  11. coins to spend.
  12. Then 5 iterations of 1/2/3 sending coins amongst
  13. themselves to get transactions in the wallets,
  14. and the miner mining one block.
  15. Wallets are backed up using dumpwallet/backupwallet.
  16. Then 5 more iterations of transactions and mining a block.
  17. Miner then generates 101 more blocks, so any
  18. transaction fees paid mature.
  19. Sanity check:
  20. Sum(1,2,3,4 balances) == 114*50
  21. 1/2/3 are shutdown, and their wallets erased.
  22. Then restore using wallet.dat backup. And
  23. confirm 1/2/3/4 balances are same as before.
  24. Shutdown again, restore using importwallet,
  25. and confirm again balances are correct.
  26. """
  27. from test_framework.test_framework import BitcoinTestFramework
  28. from test_framework.util import *
  29. from random import randint
  30. class WalletBackupTest(BitcoinTestFramework):
  31. def __init__(self):
  32. super().__init__()
  33. self.setup_clean_chain = True
  34. self.num_nodes = 4
  35. # nodes 1, 2,3 are spenders, let's give them a keypool=100
  36. self.extra_args = [["-keypool=100"], ["-keypool=100"], ["-keypool=100"], []]
  37. # This mirrors how the network was setup in the bash test
  38. def setup_network(self, split=False):
  39. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, self.extra_args)
  40. connect_nodes(self.nodes[0], 3)
  41. connect_nodes(self.nodes[1], 3)
  42. connect_nodes(self.nodes[2], 3)
  43. connect_nodes(self.nodes[2], 0)
  44. self.is_network_split=False
  45. self.sync_all()
  46. def one_send(self, from_node, to_address):
  47. if (randint(1,2) == 1):
  48. amount = Decimal(randint(1,10)) / Decimal(10)
  49. self.nodes[from_node].sendtoaddress(to_address, amount)
  50. def do_one_round(self):
  51. a0 = self.nodes[0].getnewaddress()
  52. a1 = self.nodes[1].getnewaddress()
  53. a2 = self.nodes[2].getnewaddress()
  54. self.one_send(0, a1)
  55. self.one_send(0, a2)
  56. self.one_send(1, a0)
  57. self.one_send(1, a2)
  58. self.one_send(2, a0)
  59. self.one_send(2, a1)
  60. # Have the miner (node3) mine a block.
  61. # Must sync mempools before mining.
  62. sync_mempools(self.nodes)
  63. self.nodes[3].generate(1)
  64. sync_blocks(self.nodes)
  65. # As above, this mirrors the original bash test.
  66. def start_three(self):
  67. self.nodes[0] = start_node(0, self.options.tmpdir)
  68. self.nodes[1] = start_node(1, self.options.tmpdir)
  69. self.nodes[2] = start_node(2, self.options.tmpdir)
  70. connect_nodes(self.nodes[0], 3)
  71. connect_nodes(self.nodes[1], 3)
  72. connect_nodes(self.nodes[2], 3)
  73. connect_nodes(self.nodes[2], 0)
  74. def stop_three(self):
  75. stop_node(self.nodes[0], 0)
  76. stop_node(self.nodes[1], 1)
  77. stop_node(self.nodes[2], 2)
  78. def erase_three(self):
  79. os.remove(self.options.tmpdir + "/node0/regtest/wallet.dat")
  80. os.remove(self.options.tmpdir + "/node1/regtest/wallet.dat")
  81. os.remove(self.options.tmpdir + "/node2/regtest/wallet.dat")
  82. def run_test(self):
  83. self.log.info("Generating initial blockchain")
  84. self.nodes[0].generate(1)
  85. sync_blocks(self.nodes)
  86. self.nodes[1].generate(1)
  87. sync_blocks(self.nodes)
  88. self.nodes[2].generate(1)
  89. sync_blocks(self.nodes)
  90. self.nodes[3].generate(100)
  91. sync_blocks(self.nodes)
  92. assert_equal(self.nodes[0].getbalance(), 50)
  93. assert_equal(self.nodes[1].getbalance(), 50)
  94. assert_equal(self.nodes[2].getbalance(), 50)
  95. assert_equal(self.nodes[3].getbalance(), 0)
  96. self.log.info("Creating transactions")
  97. # Five rounds of sending each other transactions.
  98. for i in range(5):
  99. self.do_one_round()
  100. self.log.info("Backing up")
  101. tmpdir = self.options.tmpdir
  102. self.nodes[0].backupwallet(tmpdir + "/node0/wallet.bak")
  103. self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.dump")
  104. self.nodes[1].backupwallet(tmpdir + "/node1/wallet.bak")
  105. self.nodes[1].dumpwallet(tmpdir + "/node1/wallet.dump")
  106. self.nodes[2].backupwallet(tmpdir + "/node2/wallet.bak")
  107. self.nodes[2].dumpwallet(tmpdir + "/node2/wallet.dump")
  108. self.log.info("More transactions")
  109. for i in range(5):
  110. self.do_one_round()
  111. # Generate 101 more blocks, so any fees paid mature
  112. self.nodes[3].generate(101)
  113. self.sync_all()
  114. balance0 = self.nodes[0].getbalance()
  115. balance1 = self.nodes[1].getbalance()
  116. balance2 = self.nodes[2].getbalance()
  117. balance3 = self.nodes[3].getbalance()
  118. total = balance0 + balance1 + balance2 + balance3
  119. # At this point, there are 214 blocks (103 for setup, then 10 rounds, then 101.)
  120. # 114 are mature, so the sum of all wallets should be 114 * 50 = 5700.
  121. assert_equal(total, 5700)
  122. ##
  123. # Test restoring spender wallets from backups
  124. ##
  125. self.log.info("Restoring using wallet.dat")
  126. self.stop_three()
  127. self.erase_three()
  128. # Start node2 with no chain
  129. shutil.rmtree(self.options.tmpdir + "/node2/regtest/blocks")
  130. shutil.rmtree(self.options.tmpdir + "/node2/regtest/chainstate")
  131. # Restore wallets from backup
  132. shutil.copyfile(tmpdir + "/node0/wallet.bak", tmpdir + "/node0/regtest/wallet.dat")
  133. shutil.copyfile(tmpdir + "/node1/wallet.bak", tmpdir + "/node1/regtest/wallet.dat")
  134. shutil.copyfile(tmpdir + "/node2/wallet.bak", tmpdir + "/node2/regtest/wallet.dat")
  135. self.log.info("Re-starting nodes")
  136. self.start_three()
  137. sync_blocks(self.nodes)
  138. assert_equal(self.nodes[0].getbalance(), balance0)
  139. assert_equal(self.nodes[1].getbalance(), balance1)
  140. assert_equal(self.nodes[2].getbalance(), balance2)
  141. self.log.info("Restoring using dumped wallet")
  142. self.stop_three()
  143. self.erase_three()
  144. #start node2 with no chain
  145. shutil.rmtree(self.options.tmpdir + "/node2/regtest/blocks")
  146. shutil.rmtree(self.options.tmpdir + "/node2/regtest/chainstate")
  147. self.start_three()
  148. assert_equal(self.nodes[0].getbalance(), 0)
  149. assert_equal(self.nodes[1].getbalance(), 0)
  150. assert_equal(self.nodes[2].getbalance(), 0)
  151. self.nodes[0].importwallet(tmpdir + "/node0/wallet.dump")
  152. self.nodes[1].importwallet(tmpdir + "/node1/wallet.dump")
  153. self.nodes[2].importwallet(tmpdir + "/node2/wallet.dump")
  154. sync_blocks(self.nodes)
  155. assert_equal(self.nodes[0].getbalance(), balance0)
  156. assert_equal(self.nodes[1].getbalance(), balance1)
  157. assert_equal(self.nodes[2].getbalance(), balance2)
  158. if __name__ == '__main__':
  159. WalletBackupTest().main()