PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/qa/rpc-tests/wallet-dump.py

http://github.com/bitcoin/bitcoin
Python | 104 lines | 70 code | 15 blank | 19 comment | 16 complexity | 364857097eb032aa817ad4579cbd77ea MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, MIT
  1. #!/usr/bin/env python3
  2. # Copyright (c) 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. from test_framework.test_framework import BitcoinTestFramework
  6. from test_framework.util import (start_nodes, start_node, assert_equal, bitcoind_processes)
  7. def read_dump(file_name, addrs, hd_master_addr_old):
  8. """
  9. Read the given dump, count the addrs that match, count change and reserve.
  10. Also check that the old hd_master is inactive
  11. """
  12. with open(file_name) as inputfile:
  13. found_addr = 0
  14. found_addr_chg = 0
  15. found_addr_rsv = 0
  16. hd_master_addr_ret = None
  17. for line in inputfile:
  18. # only read non comment lines
  19. if line[0] != "#" and len(line) > 10:
  20. # split out some data
  21. key_label, comment = line.split("#")
  22. # key = key_label.split(" ")[0]
  23. keytype = key_label.split(" ")[2]
  24. if len(comment) > 1:
  25. addr_keypath = comment.split(" addr=")[1]
  26. addr = addr_keypath.split(" ")[0]
  27. keypath = None
  28. if keytype == "inactivehdmaster=1":
  29. # ensure the old master is still available
  30. assert(hd_master_addr_old == addr)
  31. elif keytype == "hdmaster=1":
  32. # ensure we have generated a new hd master key
  33. assert(hd_master_addr_old != addr)
  34. hd_master_addr_ret = addr
  35. else:
  36. keypath = addr_keypath.rstrip().split("hdkeypath=")[1]
  37. # count key types
  38. for addrObj in addrs:
  39. if addrObj['address'] == addr and addrObj['hdkeypath'] == keypath and keytype == "label=":
  40. found_addr += 1
  41. break
  42. elif keytype == "change=1":
  43. found_addr_chg += 1
  44. break
  45. elif keytype == "reserve=1":
  46. found_addr_rsv += 1
  47. break
  48. return found_addr, found_addr_chg, found_addr_rsv, hd_master_addr_ret
  49. class WalletDumpTest(BitcoinTestFramework):
  50. def __init__(self):
  51. super().__init__()
  52. self.setup_clean_chain = False
  53. self.num_nodes = 1
  54. self.extra_args = [["-keypool=90"]]
  55. def setup_network(self, split=False):
  56. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, self.extra_args)
  57. def run_test (self):
  58. tmpdir = self.options.tmpdir
  59. # generate 20 addresses to compare against the dump
  60. test_addr_count = 20
  61. addrs = []
  62. for i in range(0,test_addr_count):
  63. addr = self.nodes[0].getnewaddress()
  64. vaddr= self.nodes[0].validateaddress(addr) #required to get hd keypath
  65. addrs.append(vaddr)
  66. # Should be a no-op:
  67. self.nodes[0].keypoolrefill()
  68. # dump unencrypted wallet
  69. self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.unencrypted.dump")
  70. found_addr, found_addr_chg, found_addr_rsv, hd_master_addr_unenc = \
  71. read_dump(tmpdir + "/node0/wallet.unencrypted.dump", addrs, None)
  72. assert_equal(found_addr, test_addr_count) # all keys must be in the dump
  73. assert_equal(found_addr_chg, 50) # 50 blocks where mined
  74. assert_equal(found_addr_rsv, 90 + 1) # keypool size (TODO: fix off-by-one)
  75. #encrypt wallet, restart, unlock and dump
  76. self.nodes[0].encryptwallet('test')
  77. bitcoind_processes[0].wait()
  78. self.nodes[0] = start_node(0, self.options.tmpdir, self.extra_args[0])
  79. self.nodes[0].walletpassphrase('test', 10)
  80. # Should be a no-op:
  81. self.nodes[0].keypoolrefill()
  82. self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.encrypted.dump")
  83. found_addr, found_addr_chg, found_addr_rsv, hd_master_addr_enc = \
  84. read_dump(tmpdir + "/node0/wallet.encrypted.dump", addrs, hd_master_addr_unenc)
  85. assert_equal(found_addr, test_addr_count)
  86. assert_equal(found_addr_chg, 90 + 1 + 50) # old reserve keys are marked as change now
  87. assert_equal(found_addr_rsv, 90 + 1) # keypool size (TODO: fix off-by-one)
  88. if __name__ == '__main__':
  89. WalletDumpTest().main ()