ct_automount.py 8.4 KB

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