ct_automount.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2025 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, gr_uc
  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. ('alice_txbump4', 'bumping the transaction (new outputs, fee too low)'),
  59. ('alice_txbump_abort1', 'aborting the transaction'),
  60. ('alice_txbump5', 'bumping the transaction (new outputs)'),
  61. ('alice_txsend5', 'sending the bumped transaction'),
  62. ('alice_txstatus5', 'getting transaction status (in mempool)'),
  63. ('generate', 'mining a block'),
  64. ('alice_bal2', 'checking Alice’s balance'),
  65. ('wait_loop_kill', 'stopping autosign wait loop'),
  66. ('stop', 'stopping regtest daemon'),
  67. ('txview', 'viewing transactions'),
  68. )
  69. def __init__(self, trunner, cfgs, spawn):
  70. self.coins = [cfg.coin.lower()]
  71. CmdTestAutosignThreaded.__init__(self, trunner, cfgs, spawn)
  72. CmdTestRegtestBDBWallet.__init__(self, trunner, cfgs, spawn)
  73. if trunner is None:
  74. return
  75. self.opts.append('--alice')
  76. def _alice_txcreate(self, chg_addr, opts=[], exit_val=0, expect_str=None, data_arg=None, need_rbf=False):
  77. if need_rbf and not self.proto.cap('rbf'):
  78. return 'skip'
  79. def do_return():
  80. if expect_str:
  81. t.expect(expect_str)
  82. t.read()
  83. self.remove_device_online()
  84. return t
  85. self.insert_device_online()
  86. sid = self._user_sid('alice')
  87. t = self.spawn(
  88. 'mmgen-txcreate',
  89. opts
  90. + ['--alice', '--autosign']
  91. + ([data_arg] if data_arg else [])
  92. + [f'{self.burn_addr},1.23456', f'{sid}:{chg_addr}'],
  93. exit_val = exit_val or None)
  94. if exit_val:
  95. return do_return()
  96. t = self.txcreate_ui_common(
  97. t,
  98. inputs = '1',
  99. interactive_fee = '32s',
  100. file_desc = 'Unsigned automount transaction')
  101. return do_return()
  102. def alice_txcreate1(self):
  103. return self._alice_txcreate(
  104. chg_addr = 'C:5',
  105. data_arg = 'data:'+gr_uc[:24])
  106. def alice_txcreate2(self):
  107. return self._alice_txcreate(chg_addr='L:5')
  108. alice_txcreate3 = alice_txcreate2
  109. def alice_txcreate4(self):
  110. return self._alice_txcreate(chg_addr='L:4', need_rbf=True)
  111. def _alice_txsend_abort(self, err=False, send_resp='y', expect=None, shred_expect=[]):
  112. self.insert_device_online()
  113. t = self.spawn(
  114. 'mmgen-txsend',
  115. ['--quiet', '--abort'],
  116. exit_val = 2 if err else 1 if send_resp == 'n' else None)
  117. if err:
  118. t.expect(expect)
  119. else:
  120. t.expect('(y/N): ', send_resp)
  121. if expect:
  122. t.expect(expect)
  123. for pat in shred_expect:
  124. t.expect(pat, regex=True)
  125. t.read()
  126. self.remove_device_online()
  127. return t
  128. def alice_txsend_abort1(self):
  129. return self._alice_txsend_abort(shred_expect=['Shredding .*arawtx'])
  130. def alice_txsend_abort2(self):
  131. return self._alice_txsend_abort(err=True, expect='No unsent transactions')
  132. def alice_txsend_abort3(self):
  133. return self._alice_txsend_abort(send_resp='n', expect='Exiting at user request')
  134. def alice_txsend_abort4(self):
  135. self._wait_signed('transaction')
  136. return self._alice_txsend_abort(shred_expect=[r'Shredding .*arawtx', r'Shredding .*asigtx'])
  137. alice_txsend_abort5 = alice_txsend_abort2
  138. def alice_txcreate_bad_have_unsigned(self):
  139. return self._alice_txcreate(chg_addr='C:5', exit_val=2, expect_str='already present')
  140. def alice_txcreate_bad_have_unsent(self):
  141. return self._alice_txcreate(chg_addr='C:5', exit_val=2, expect_str='unsent transaction')
  142. def alice_run_autosign_setup(self):
  143. return self.run_setup(mn_type='default', use_dfl_wallet=True, passwd=rt_pw)
  144. def alice_txsend1(self):
  145. return self._alice_txsend('This one’s worth a comment', no_wait=True)
  146. def alice_txsend2(self):
  147. return self._alice_txsend(need_rbf=True)
  148. def alice_txsend3(self):
  149. return self._alice_txsend(need_rbf=True)
  150. def alice_txsend5(self):
  151. return self._alice_txsend(need_rbf=True)
  152. def _alice_txstatus(self, expect, exit_val=None, need_rbf=False):
  153. if need_rbf and not self.proto.cap('rbf'):
  154. return 'skip'
  155. self.insert_device_online()
  156. t = self.spawn(
  157. 'mmgen-txsend',
  158. ['--alice', '--autosign', '--status', '--verbose'],
  159. exit_val = exit_val)
  160. t.expect(expect)
  161. t.read()
  162. self.remove_device_online()
  163. return t
  164. def alice_txstatus1(self):
  165. return self._alice_txstatus('unsigned', 1)
  166. def alice_txstatus2(self):
  167. self._wait_signed('transaction')
  168. return self._alice_txstatus('unsent', 1)
  169. def alice_txstatus3(self):
  170. return self._alice_txstatus('in mempool')
  171. def alice_txstatus4(self):
  172. return self._alice_txstatus('1 confirmation', 0)
  173. def alice_txstatus5(self):
  174. return self._alice_txstatus('in mempool', need_rbf=True)
  175. def _alice_txsend(self, comment=None, no_wait=False, need_rbf=False):
  176. if need_rbf and not self.proto.cap('rbf'):
  177. return 'skip'
  178. if not no_wait:
  179. self._wait_signed('transaction')
  180. self.insert_device_online()
  181. t = self.spawn('mmgen-txsend', ['--alice', '--quiet', '--autosign'])
  182. t.view_tx('t')
  183. t.do_comment(comment)
  184. self._do_confirm_send(t, quiet=True)
  185. t.written_to_file('Sent automount transaction')
  186. t.read()
  187. self.remove_device_online()
  188. return t
  189. def alice_txsend_bad_no_unsent(self):
  190. self.insert_device_online()
  191. t = self.spawn('mmgen-txsend', ['--quiet', '--autosign'], exit_val=2)
  192. t.expect('No unsent transactions')
  193. t.read()
  194. self.remove_device_online()
  195. return t
  196. def _alice_txbump(self, fee_opt=None, output_args=[], bad_tx_expect=None, low_fee_fix=None):
  197. if not self.proto.cap('rbf'):
  198. return 'skip'
  199. self.insert_device_online()
  200. t = self.spawn(
  201. 'mmgen-txbump',
  202. ['--alice', '--autosign']
  203. + ([fee_opt] if fee_opt else [])
  204. + output_args,
  205. exit_val = 1 if bad_tx_expect else None)
  206. if bad_tx_expect:
  207. time.sleep(0.5)
  208. t.expect('Only sent transactions')
  209. t.expect(bad_tx_expect)
  210. else:
  211. if not output_args:
  212. t.expect(r'to deduct the fee from .* change output\): ', '\n', regex=True)
  213. t.expect(r'(Y/n): ', 'y') # output OK?
  214. if low_fee_fix or not fee_opt:
  215. if low_fee_fix:
  216. t.expect('Please choose a higher fee')
  217. t.expect('transaction fee: ', (low_fee_fix or '200s') + '\n')
  218. if output_args:
  219. t.expect(r'(Y/n): ', 'y')
  220. t.expect(r'(Y/n): ', 'y') # fee OK?
  221. t.expect(r'(y/N): ', '\n') # add comment?
  222. t.expect(r'(y/N): ', 'y') # save?
  223. t.read()
  224. self.remove_device_online()
  225. return t
  226. def alice_txbump1(self):
  227. return self._alice_txbump(bad_tx_expect='unsigned transaction')
  228. def alice_txbump2(self):
  229. self._wait_signed('transaction')
  230. return self._alice_txbump(bad_tx_expect='unsent transaction')
  231. def alice_txbump3(self):
  232. return self._alice_txbump()
  233. def alice_txbump4(self):
  234. sid = self._user_sid('alice')
  235. return self._alice_txbump(
  236. fee_opt = '--fee=3s',
  237. output_args = [f'{self.burn_addr},7.654321', f'{sid}:C:1'],
  238. low_fee_fix = '300s')
  239. def alice_txbump_abort1(self):
  240. if not self.proto.cap('rbf'):
  241. return 'skip'
  242. return self._alice_txsend_abort(shred_expect=['Shredding .*arawtx'])
  243. def alice_txbump5(self):
  244. sid = self._user_sid('alice')
  245. return self._alice_txbump(
  246. fee_opt = '--fee=400s',
  247. output_args = ['data:message for posterity', f'{self.burn_addr},7.654321', f'{sid}:C:1'])
  248. def alice_bal2(self):
  249. bals = {
  250. 'btc': '491.11002204',
  251. 'ltc': '5491.11002204',
  252. 'bch': '498.7653392',
  253. }
  254. return self.user_bal('alice', bals.get(self.coin, None))