ct_automount.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2024 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen-wallet
  9. # https://gitlab.com/mmgen/mmgen-wallet
  10. """
  11. test.cmdtest_py_d.ct_automount: autosigning with automount tests for the cmdtest.py test suite
  12. """
  13. import os, time
  14. from pathlib import Path
  15. from .ct_autosign import CmdTestAutosignThreaded
  16. from .ct_regtest import CmdTestRegtestBDBWallet, rt_pw
  17. from .common import get_file_with_ext
  18. from ..include.common import cfg
  19. class CmdTestAutosignAutomount(CmdTestAutosignThreaded, CmdTestRegtestBDBWallet):
  20. 'automounted transacting operations via regtest mode'
  21. networks = ('btc', 'bch', 'ltc')
  22. tmpdir_nums = [49]
  23. extra_spawn_args = []
  24. rtFundAmt = None # pylint
  25. rt_data = {
  26. 'rtFundAmt': {'btc':'500', 'bch':'500', 'ltc':'5500'},
  27. }
  28. cmd_group = (
  29. ('setup', 'regtest mode setup'),
  30. ('walletgen_alice', 'wallet generation (Alice)'),
  31. ('addrgen_alice', 'address generation (Alice)'),
  32. ('addrimport_alice', 'importing Alice’s addresses'),
  33. ('fund_alice', 'funding Alice’s wallet'),
  34. ('generate', 'mining a block'),
  35. ('alice_bal1', 'checking Alice’s balance'),
  36. ('alice_txcreate1', 'creating a transaction'),
  37. ('alice_txcreate_bad_have_unsigned', 'creating the transaction again (error)'),
  38. ('copy_wallet', 'copying Alice’s wallet'),
  39. ('alice_run_autosign_setup', 'running ‘autosign setup’ (with default wallet)'),
  40. ('autosign_start_thread', 'starting autosign wait loop'),
  41. ('alice_txstatus1', 'getting transaction status (unsigned)'),
  42. ('alice_txstatus2', 'getting transaction status (unsent)'),
  43. ('alice_txcreate_bad_have_unsent', 'creating the transaction again (error)'),
  44. ('alice_txsend1', 'sending a transaction, editing comment'),
  45. ('alice_txstatus3', 'getting transaction status (in mempool)'),
  46. ('alice_txsend_bad_no_unsent', 'sending the transaction again (error)'),
  47. ('generate', 'mining a block'),
  48. ('alice_txstatus4', 'getting transaction status (one confirmation)'),
  49. ('alice_txcreate2', 'creating a transaction'),
  50. ('alice_txsend_abort1', 'aborting the transaction (raw only)'),
  51. ('alice_txsend_abort2', 'aborting the transaction again (error)'),
  52. ('alice_txcreate3', 'creating a transaction'),
  53. ('alice_txsend_abort3', 'aborting the transaction (user exit)'),
  54. ('alice_txsend_abort4', 'aborting the transaction (raw + signed)'),
  55. ('alice_txsend_abort5', 'aborting the transaction again (error)'),
  56. ('generate', 'mining a block'),
  57. ('alice_txcreate4', 'creating a transaction'),
  58. ('alice_txbump1', 'bumping the unsigned transaction (error)'),
  59. ('alice_txbump2', 'bumping the unsent transaction (error)'),
  60. ('alice_txsend2', 'sending the transaction'),
  61. ('alice_txbump3', 'bumping the transaction'),
  62. ('alice_txsend3', 'sending the bumped transaction'),
  63. ('autosign_kill_thread', 'stopping autosign wait loop'),
  64. ('stop', 'stopping regtest daemon'),
  65. ('txview', 'viewing transactions'),
  66. )
  67. def __init__(self, trunner, cfgs, spawn):
  68. self.coins = [cfg.coin.lower()]
  69. CmdTestAutosignThreaded.__init__(self, trunner, cfgs, spawn)
  70. CmdTestRegtestBDBWallet.__init__(self, trunner, cfgs, spawn)
  71. if trunner == None:
  72. return
  73. self.opts.append('--alice')
  74. def _alice_txcreate(self, chg_addr, opts=[], exit_val=0, expect=None):
  75. self.insert_device_online()
  76. sid = self._user_sid('alice')
  77. t = self.spawn(
  78. 'mmgen-txcreate',
  79. opts
  80. + ['--alice', '--autosign']
  81. + [f'{self.burn_addr},1.23456', f'{sid}:{chg_addr}'],
  82. exit_val = exit_val or None)
  83. if exit_val:
  84. t.expect(expect)
  85. t.read()
  86. self.remove_device_online()
  87. return t
  88. t = self.txcreate_ui_common(
  89. t,
  90. inputs = '1',
  91. interactive_fee = '32s',
  92. file_desc = 'Unsigned automount transaction')
  93. if expect:
  94. t.expect(expect)
  95. t.read()
  96. self.remove_device_online()
  97. return t
  98. def alice_txcreate1(self):
  99. return self._alice_txcreate(chg_addr='C:5')
  100. def alice_txcreate2(self):
  101. return self._alice_txcreate(chg_addr='L:5')
  102. alice_txcreate3 = alice_txcreate2
  103. def alice_txcreate4(self):
  104. if cfg.coin == 'BCH':
  105. return 'skip'
  106. return self._alice_txcreate(chg_addr='L:4')
  107. def _alice_txsend_abort(self, err=False, send_resp='y', expect=None, shred_expect=[]):
  108. self.insert_device_online()
  109. t = self.spawn(
  110. 'mmgen-txsend',
  111. ['--quiet', '--abort'],
  112. exit_val = 2 if err else 1 if send_resp == 'n' else None)
  113. if err:
  114. t.expect(expect)
  115. else:
  116. t.expect('(y/N): ', send_resp)
  117. if expect:
  118. t.expect(expect)
  119. for pat in shred_expect:
  120. t.expect(pat, regex=True)
  121. self.remove_device_online()
  122. return t
  123. def alice_txsend_abort1(self):
  124. return self._alice_txsend_abort(shred_expect=['Shredding .*arawtx'])
  125. def alice_txsend_abort2(self):
  126. return self._alice_txsend_abort(err=True, expect='No unsent transactions')
  127. def alice_txsend_abort3(self):
  128. return self._alice_txsend_abort(send_resp='n', expect='Exiting at user request')
  129. def alice_txsend_abort4(self):
  130. self._wait_signed('transaction')
  131. return self._alice_txsend_abort(shred_expect=[r'Shredding .*arawtx', r'Shredding .*asigtx'])
  132. alice_txsend_abort5 = alice_txsend_abort2
  133. def alice_txcreate_bad_have_unsigned(self):
  134. return self._alice_txcreate(chg_addr='C:5', exit_val=2, expect='already present')
  135. def alice_txcreate_bad_have_unsent(self):
  136. return self._alice_txcreate(chg_addr='C:5', exit_val=2, expect='unsent transaction')
  137. def copy_wallet(self):
  138. self.spawn('', msg_only=True)
  139. if cfg.coin == 'BTC':
  140. return 'skip_msg'
  141. src = Path(self.tr.data_dir, 'regtest', cfg.coin.lower(), 'alice')
  142. dest = Path(self.tr.data_dir, 'regtest', 'btc', 'alice')
  143. dest.mkdir(parents=True, exist_ok=True)
  144. wf = Path(get_file_with_ext(src, 'mmdat')).absolute()
  145. link_path = dest / wf.name
  146. if not link_path.exists():
  147. link_path.symlink_to(wf)
  148. return 'ok'
  149. def alice_run_autosign_setup(self):
  150. self.insert_device()
  151. t = self.run_setup(mn_type='default', use_dfl_wallet=True, passwd=rt_pw)
  152. t.read()
  153. self.remove_device()
  154. return t
  155. def alice_txsend1(self):
  156. return self._alice_txsend('This one’s worth a comment', no_wait=True)
  157. def alice_txsend2(self):
  158. if cfg.coin == 'BCH':
  159. return 'skip'
  160. return self._alice_txsend()
  161. def alice_txsend3(self):
  162. if cfg.coin == 'BCH':
  163. return 'skip'
  164. return self._alice_txsend()
  165. def _alice_txstatus(self, expect, exit_val=None):
  166. self.insert_device_online()
  167. t = self.spawn(
  168. 'mmgen-txsend',
  169. ['--alice', '--autosign', '--status', '--verbose'],
  170. exit_val = exit_val)
  171. t.expect(expect)
  172. self.remove_device_online()
  173. return t
  174. def alice_txstatus1(self):
  175. return self._alice_txstatus('unsigned', 1)
  176. def alice_txstatus2(self):
  177. self._wait_signed('transaction')
  178. return self._alice_txstatus('unsent', 1)
  179. def alice_txstatus3(self):
  180. return self._alice_txstatus('in mempool')
  181. def alice_txstatus4(self):
  182. return self._alice_txstatus('1 confirmation', 0)
  183. def _alice_txsend(self, comment=None, no_wait=False):
  184. if not no_wait:
  185. self._wait_signed('transaction')
  186. self.insert_device_online()
  187. t = self.spawn('mmgen-txsend', ['--alice', '--quiet', '--autosign'])
  188. t.view_tx('t')
  189. t.do_comment(comment)
  190. self._do_confirm_send(t, quiet=True)
  191. t.written_to_file('Sent automount transaction')
  192. t.read()
  193. self.remove_device_online()
  194. return t
  195. def alice_txsend_bad_no_unsent(self):
  196. self.insert_device_online()
  197. t = self.spawn('mmgen-txsend', ['--quiet', '--autosign'], exit_val=2)
  198. t.expect('No unsent transactions')
  199. t.read()
  200. self.remove_device_online()
  201. return t
  202. def _alice_txbump(self, bad_tx_desc=None):
  203. if cfg.coin == 'BCH':
  204. return 'skip'
  205. self.insert_device_online()
  206. t = self.spawn(
  207. 'mmgen-txbump',
  208. ['--alice', '--autosign'],
  209. exit_val = 1 if bad_tx_desc else None)
  210. if bad_tx_desc:
  211. t.expect('Only sent transactions')
  212. t.expect(bad_tx_desc)
  213. else:
  214. t.expect(f'to deduct the fee from .* change output\): ', '\n', regex=True)
  215. t.expect(r'(Y/n): ', 'y') # output OK?
  216. t.expect('transaction fee: ', '200s\n')
  217. t.expect(r'(Y/n): ', 'y') # fee OK?
  218. t.expect(r'(y/N): ', '\n') # add comment?
  219. t.expect(r'(y/N): ', 'y') # save?
  220. t.read()
  221. self.remove_device_online()
  222. return t
  223. def alice_txbump1(self):
  224. return self._alice_txbump(bad_tx_desc='unsigned transaction')
  225. def alice_txbump2(self):
  226. self._wait_signed('transaction')
  227. return self._alice_txbump(bad_tx_desc='unsent transaction')
  228. def alice_txbump3(self):
  229. return self._alice_txbump()