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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 random import randint
  28. import shutil
  29. from test_framework.test_framework import BitcoinTestFramework
  30. from test_framework.util import *
  31. class WalletBackupTest(BitcoinTestFramework):
  32. def set_test_params(self):
  33. self.num_nodes = 4
  34. self.setup_clean_chain = True
  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. def setup_network(self, split=False):
  38. self.setup_nodes()
  39. connect_nodes(self.nodes[0], 3)
  40. connect_nodes(self.nodes[1], 3)
  41. connect_nodes(self.nodes[2], 3)
  42. connect_nodes(self.nodes[2], 0)
  43. self.sync_all()
  44. def one_send(self, from_node, to_address):
  45. if (randint(1,2) == 1):
  46. amount = Decimal(randint(1,10)) / Decimal(10)
  47. self.nodes[from_node].sendtoaddress(to_address, amount)
  48. def do_one_round(self):
  49. a0 = self.nodes[0].getnewaddress()
  50. a1 = self.nodes[1].getnewaddress()
  51. a2 = self.nodes[2].getnewaddress()
  52. self.one_send(0, a1)
  53. self.one_send(0, a2)
  54. self.one_send(1, a0)
  55. self.one_send(1, a2)
  56. self.one_send(2, a0)
  57. self.one_send(2, a1)
  58. # Have the miner (node3) mine a block.
  59. # Must sync mempools before mining.
  60. sync_mempools(self.nodes)
  61. self.nodes[3].generate(1)
  62. sync_blocks(self.nodes)
  63. # As above, this mirrors the original bash test.
  64. def start_three(self):
  65. self.start_node(0)
  66. self.start_node(1)
  67. self.start_node(2)
  68. connect_nodes(self.nodes[0], 3)
  69. connect_nodes(self.nodes[1], 3)
  70. connect_nodes(self.nodes[2], 3)
  71. connect_nodes(self.nodes[2], 0)
  72. def stop_three(self):
  73. self.stop_node(0)
  74. self.stop_node(1)
  75. self.stop_node(2)
  76. def erase_three(self):
  77. os.remove(self.options.tmpdir + "/node0/regtest/wallet.dat")
  78. os.remove(self.options.tmpdir + "/node1/regtest/wallet.dat")
  79. os.remove(self.options.tmpdir + "/node2/regtest/wallet.dat")
  80. def run_test(self):
  81. self.log.info("Generating initial blockchain")
  82. self.nodes[0].generate(1)
  83. sync_blocks(self.nodes)
  84. self.nodes[1].generate(1)
  85. sync_blocks(self.nodes)
  86. self.nodes[2].generate(1)
  87. sync_blocks(self.nodes)
  88. self.nodes[3].generate(100)
  89. sync_blocks(self.nodes)
  90. assert_equal(self.nodes[0].getbalance(), 50)
  91. assert_equal(self.nodes[1].getbalance(), 50)
  92. assert_equal(self.nodes[2].getbalance(), 50)
  93. assert_equal(self.nodes[3].getbalance(), 0)
  94. self.log.info("Creating transactions")
  95. # Five rounds of sending each other transactions.
  96. for i in range(5):
  97. self.do_one_round()
  98. self.log.info("Backing up")
  99. tmpdir = self.options.tmpdir
  100. self.nodes[0].backupwallet(tmpdir + "/node0/wallet.bak")
  101. self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.dump")
  102. self.nodes[1].backupwallet(tmpdir + "/node1/wallet.bak")
  103. self.nodes[1].dumpwallet(tmpdir + "/node1/wallet.dump")
  104. self.nodes[2].backupwallet(tmpdir + "/node2/wallet.bak")
  105. self.nodes[2].dumpwallet(tmpdir + "/node2/wallet.dump")
  106. self.log.info("More transactions")
  107. for i in range(5):
  108. self.do_one_round()
  109. # Generate 101 more blocks, so any fees paid mature
  110. self.nodes[3].generate(101)
  111. self.sync_all()
  112. balance0 = self.nodes[0].getbalance()
  113. balance1 = self.nodes[1].getbalance()
  114. balance2 = self.nodes[2].getbalance()
  115. balance3 = self.nodes[3].getbalance()
  116. total = balance0 + balance1 + balance2 + balance3
  117. # At this point, there are 214 blocks (103 for setup, then 10 rounds, then 101.)
  118. # 114 are mature, so the sum of all wallets should be 114 * 50 = 5700.
  119. assert_equal(total, 5700)
  120. ##
  121. # Test restoring spender wallets from backups
  122. ##
  123. self.log.info("Restoring using wallet.dat")
  124. self.stop_three()
  125. self.erase_three()
  126. # Start node2 with no chain
  127. shutil.rmtree(self.options.tmpdir + "/node2/regtest/blocks")
  128. shutil.rmtree(self.options.tmpdir + "/node2/regtest/chainstate")
  129. # Restore wallets from backup
  130. shutil.copyfile(tmpdir + "/node0/wallet.bak", tmpdir + "/node0/regtest/wallet.dat")
  131. shutil.copyfile(tmpdir + "/node1/wallet.bak", tmpdir + "/node1/regtest/wallet.dat")
  132. shutil.copyfile(tmpdir + "/node2/wallet.bak", tmpdir + "/node2/regtest/wallet.dat")
  133. self.log.info("Re-starting nodes")
  134. self.start_three()
  135. sync_blocks(self.nodes)
  136. assert_equal(self.nodes[0].getbalance(), balance0)
  137. assert_equal(self.nodes[1].getbalance(), balance1)
  138. assert_equal(self.nodes[2].getbalance(), balance2)
  139. self.log.info("Restoring using dumped wallet")
  140. self.stop_three()
  141. self.erase_three()
  142. #start node2 with no chain
  143. shutil.rmtree(self.options.tmpdir + "/node2/regtest/blocks")
  144. shutil.rmtree(self.options.tmpdir + "/node2/regtest/chainstate")
  145. self.start_three()
  146. assert_equal(self.nodes[0].getbalance(), 0)
  147. assert_equal(self.nodes[1].getbalance(), 0)
  148. assert_equal(self.nodes[2].getbalance(), 0)
  149. self.nodes[0].importwallet(tmpdir + "/node0/wallet.dump")
  150. self.nodes[1].importwallet(tmpdir + "/node1/wallet.dump")
  151. self.nodes[2].importwallet(tmpdir + "/node2/wallet.dump")
  152. sync_blocks(self.nodes)
  153. assert_equal(self.nodes[0].getbalance(), balance0)
  154. assert_equal(self.nodes[1].getbalance(), balance1)
  155. assert_equal(self.nodes[2].getbalance(), balance2)
  156. # Backup to source wallet file must fail
  157. sourcePaths = [
  158. tmpdir + "/node0/regtest/wallet.dat",
  159. tmpdir + "/node0/./regtest/wallet.dat",
  160. tmpdir + "/node0/regtest/",
  161. tmpdir + "/node0/regtest"]
  162. for sourcePath in sourcePaths:
  163. assert_raises_rpc_error(-4, "backup failed", self.nodes[0].backupwallet, sourcePath)
  164. if __name__ == '__main__':
  165. WalletBackupTest().main()