ct_automount.py 8.4 KB

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