ct_automount.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based 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_d.ct_automount: autosigning with automount tests for the cmdtest.py test suite
  12. """
  13. import 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. rtFundAmt = None # pylint
  24. rt_data = {
  25. 'rtFundAmt': {'btc':'500', 'bch':'500', 'ltc':'5500'},
  26. }
  27. cmd_group = (
  28. ('setup', 'regtest mode setup'),
  29. ('walletgen_alice', 'wallet generation (Alice)'),
  30. ('addrgen_alice', 'address generation (Alice)'),
  31. ('addrimport_alice', 'importing Alice’s addresses'),
  32. ('fund_alice', 'funding Alice’s wallet'),
  33. ('generate', 'mining a block'),
  34. ('alice_bal1', 'checking Alice’s balance'),
  35. ('alice_txcreate1', 'creating a transaction'),
  36. ('alice_txcreate_bad_have_unsigned', 'creating the transaction again (error)'),
  37. ('alice_run_autosign_setup', 'running ‘autosign setup’ (with default wallet)'),
  38. ('wait_loop_start', 'starting autosign wait loop'),
  39. ('alice_txstatus1', 'getting transaction status (unsigned)'),
  40. ('alice_txstatus2', 'getting transaction status (unsent)'),
  41. ('alice_txcreate_bad_have_unsent', 'creating the transaction again (error)'),
  42. ('alice_txsend1', 'sending a transaction, editing comment'),
  43. ('alice_txstatus3', 'getting transaction status (in mempool)'),
  44. ('alice_txsend_bad_no_unsent', 'sending the transaction again (error)'),
  45. ('generate', 'mining a block'),
  46. ('alice_txstatus4', 'getting transaction status (one confirmation)'),
  47. ('alice_txcreate2', 'creating a transaction'),
  48. ('alice_txsend_abort1', 'aborting the transaction (raw only)'),
  49. ('alice_txsend_abort2', 'aborting the transaction again (error)'),
  50. ('alice_txcreate3', 'creating a transaction'),
  51. ('alice_txsend_abort3', 'aborting the transaction (user exit)'),
  52. ('alice_txsend_abort4', 'aborting the transaction (raw + signed)'),
  53. ('alice_txsend_abort5', 'aborting the transaction again (error)'),
  54. ('generate', 'mining a block'),
  55. ('alice_txcreate4', 'creating a transaction'),
  56. ('alice_txbump1', 'bumping the unsigned transaction (error)'),
  57. ('alice_txbump2', 'bumping the unsent transaction (error)'),
  58. ('alice_txsend2', 'sending the transaction'),
  59. ('alice_txbump3', 'bumping the transaction'),
  60. ('alice_txsend3', 'sending the bumped transaction'),
  61. ('wait_loop_kill', 'stopping autosign wait loop'),
  62. ('stop', 'stopping regtest daemon'),
  63. ('txview', 'viewing transactions'),
  64. )
  65. def __init__(self, trunner, cfgs, spawn):
  66. self.coins = [cfg.coin.lower()]
  67. CmdTestAutosignThreaded.__init__(self, trunner, cfgs, spawn)
  68. CmdTestRegtestBDBWallet.__init__(self, trunner, cfgs, spawn)
  69. if trunner is None:
  70. return
  71. self.opts.append('--alice')
  72. def _alice_txcreate(self, chg_addr, opts=[], exit_val=0, expect_str=None):
  73. def do_return():
  74. if expect_str:
  75. t.expect(expect_str)
  76. t.read()
  77. self.remove_device_online()
  78. return t
  79. self.insert_device_online()
  80. sid = self._user_sid('alice')
  81. t = self.spawn(
  82. 'mmgen-txcreate',
  83. opts
  84. + ['--alice', '--autosign']
  85. + [f'{self.burn_addr},1.23456', f'{sid}:{chg_addr}'],
  86. exit_val = exit_val or None)
  87. if exit_val:
  88. return do_return()
  89. t = self.txcreate_ui_common(
  90. t,
  91. inputs = '1',
  92. interactive_fee = '32s',
  93. file_desc = 'Unsigned automount transaction')
  94. return do_return()
  95. def alice_txcreate1(self):
  96. return self._alice_txcreate(chg_addr='C:5')
  97. def alice_txcreate2(self):
  98. return self._alice_txcreate(chg_addr='L:5')
  99. alice_txcreate3 = alice_txcreate2
  100. def alice_txcreate4(self):
  101. if cfg.coin == 'BCH':
  102. return 'skip'
  103. return self._alice_txcreate(chg_addr='L:4')
  104. def _alice_txsend_abort(self, err=False, send_resp='y', expect=None, shred_expect=[]):
  105. self.insert_device_online()
  106. t = self.spawn(
  107. 'mmgen-txsend',
  108. ['--quiet', '--abort'],
  109. exit_val = 2 if err else 1 if send_resp == 'n' else None)
  110. if err:
  111. t.expect(expect)
  112. else:
  113. t.expect('(y/N): ', send_resp)
  114. if expect:
  115. t.expect(expect)
  116. for pat in shred_expect:
  117. t.expect(pat, regex=True)
  118. t.read()
  119. self.remove_device_online()
  120. return t
  121. def alice_txsend_abort1(self):
  122. return self._alice_txsend_abort(shred_expect=['Shredding .*arawtx'])
  123. def alice_txsend_abort2(self):
  124. return self._alice_txsend_abort(err=True, expect='No unsent transactions')
  125. def alice_txsend_abort3(self):
  126. return self._alice_txsend_abort(send_resp='n', expect='Exiting at user request')
  127. def alice_txsend_abort4(self):
  128. self._wait_signed('transaction')
  129. return self._alice_txsend_abort(shred_expect=[r'Shredding .*arawtx', r'Shredding .*asigtx'])
  130. alice_txsend_abort5 = alice_txsend_abort2
  131. def alice_txcreate_bad_have_unsigned(self):
  132. return self._alice_txcreate(chg_addr='C:5', exit_val=2, expect_str='already present')
  133. def alice_txcreate_bad_have_unsent(self):
  134. return self._alice_txcreate(chg_addr='C:5', exit_val=2, expect_str='unsent transaction')
  135. def alice_run_autosign_setup(self):
  136. return self.run_setup(mn_type='default', use_dfl_wallet=True, passwd=rt_pw)
  137. def alice_txsend1(self):
  138. return self._alice_txsend('This one’s worth a comment', no_wait=True)
  139. def alice_txsend2(self):
  140. if cfg.coin == 'BCH':
  141. return 'skip'
  142. return self._alice_txsend()
  143. def alice_txsend3(self):
  144. if cfg.coin == 'BCH':
  145. return 'skip'
  146. return self._alice_txsend()
  147. def _alice_txstatus(self, expect, exit_val=None):
  148. self.insert_device_online()
  149. t = self.spawn(
  150. 'mmgen-txsend',
  151. ['--alice', '--autosign', '--status', '--verbose'],
  152. exit_val = exit_val)
  153. t.expect(expect)
  154. t.read()
  155. self.remove_device_online()
  156. return t
  157. def alice_txstatus1(self):
  158. return self._alice_txstatus('unsigned', 1)
  159. def alice_txstatus2(self):
  160. self._wait_signed('transaction')
  161. return self._alice_txstatus('unsent', 1)
  162. def alice_txstatus3(self):
  163. return self._alice_txstatus('in mempool')
  164. def alice_txstatus4(self):
  165. return self._alice_txstatus('1 confirmation', 0)
  166. def _alice_txsend(self, comment=None, no_wait=False):
  167. if not no_wait:
  168. self._wait_signed('transaction')
  169. self.insert_device_online()
  170. t = self.spawn('mmgen-txsend', ['--alice', '--quiet', '--autosign'])
  171. t.view_tx('t')
  172. t.do_comment(comment)
  173. self._do_confirm_send(t, quiet=True)
  174. t.written_to_file('Sent automount transaction')
  175. t.read()
  176. self.remove_device_online()
  177. return t
  178. def alice_txsend_bad_no_unsent(self):
  179. self.insert_device_online()
  180. t = self.spawn('mmgen-txsend', ['--quiet', '--autosign'], exit_val=2)
  181. t.expect('No unsent transactions')
  182. t.read()
  183. self.remove_device_online()
  184. return t
  185. def _alice_txbump(self, bad_tx_desc=None):
  186. if cfg.coin == 'BCH':
  187. return 'skip'
  188. self.insert_device_online()
  189. t = self.spawn(
  190. 'mmgen-txbump',
  191. ['--alice', '--autosign'],
  192. exit_val = 1 if bad_tx_desc else None)
  193. if bad_tx_desc:
  194. time.sleep(0.5)
  195. t.expect('Only sent transactions')
  196. t.expect(bad_tx_desc)
  197. else:
  198. t.expect(r'to deduct the fee from .* change output\): ', '\n', regex=True)
  199. t.expect(r'(Y/n): ', 'y') # output OK?
  200. t.expect('transaction fee: ', '200s\n')
  201. t.expect(r'(Y/n): ', 'y') # fee OK?
  202. t.expect(r'(y/N): ', '\n') # add comment?
  203. t.expect(r'(y/N): ', 'y') # save?
  204. t.read()
  205. self.remove_device_online()
  206. return t
  207. def alice_txbump1(self):
  208. return self._alice_txbump(bad_tx_desc='unsigned transaction')
  209. def alice_txbump2(self):
  210. self._wait_signed('transaction')
  211. return self._alice_txbump(bad_tx_desc='unsent transaction')
  212. def alice_txbump3(self):
  213. return self._alice_txbump()