ts_regtest.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2019 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. ts_regtest.py: Regtest tests for the test.py test suite
  20. """
  21. import os,subprocess
  22. from decimal import Decimal
  23. from mmgen.globalvars import g
  24. from mmgen.opts import opt
  25. from mmgen.util import die,gmsg,write_data_to_file
  26. from mmgen.protocol import CoinProtocol
  27. from mmgen.addr import AddrList
  28. from test.common import *
  29. from test.test_py_d.common import *
  30. rt_pw = 'abc-α'
  31. rt_data = {
  32. 'tx_fee': {'btc':'0.0001','bch':'0.001','ltc':'0.01'},
  33. 'rtFundAmt': {'btc':'500','bch':'500','ltc':'5500'},
  34. 'rtFee': {
  35. 'btc': ('20s','10s','60s','31s','10s','20s'),
  36. 'bch': ('20s','10s','60s','0.0001','10s','20s'),
  37. 'ltc': ('1000s','500s','1500s','0.05','400s','1000s')
  38. },
  39. 'rtBals': {
  40. 'btc': ('499.9999488','399.9998282','399.9998147','399.9996877',
  41. '52.99990000','946.99933647','999.99923647','52.9999',
  42. '946.99933647'),
  43. 'bch': ('499.9999484','399.9999194','399.9998972','399.9997692',
  44. '46.78900000','953.20966920','999.99866920','46.789',
  45. '953.2096692'),
  46. 'ltc': ('5499.99744','5399.994425','5399.993885','5399.987535',
  47. '52.99000000','10946.93753500','10999.92753500','52.99',
  48. '10946.937535'),
  49. },
  50. 'rtBals_gb': {
  51. 'btc': ('116.77629233','283.22339537'),
  52. 'bch': ('116.77637483','283.22339437'),
  53. 'ltc': ('5116.77036263','283.21717237')
  54. },
  55. 'rtBobOp3': {'btc':'S:2','bch':'L:3','ltc':'S:2'},
  56. 'rtAmts': {
  57. 'btc': ('500',),
  58. 'bch': ('500',),
  59. 'ltc': ('5500',)
  60. }
  61. }
  62. from test.test_py_d.ts_base import *
  63. from test.test_py_d.ts_shared import *
  64. class TestSuiteRegtest(TestSuiteBase,TestSuiteShared):
  65. 'transacting and tracking wallet operations via regtest mode'
  66. networks = ('btc','ltc','bch')
  67. passthru_opts = ('coin',)
  68. tmpdir_nums = [17]
  69. cmd_group = (
  70. ('setup', 'regtest (Bob and Alice) mode setup'),
  71. ('walletgen_bob', 'wallet generation (Bob)'),
  72. ('walletgen_alice', 'wallet generation (Alice)'),
  73. ('addrgen_bob', 'address generation (Bob)'),
  74. ('addrgen_alice', 'address generation (Alice)'),
  75. ('addrimport_bob', "importing Bob's addresses"),
  76. ('addrimport_alice', "importing Alice's addresses"),
  77. ('fund_bob', "funding Bob's wallet"),
  78. ('fund_alice', "funding Alice's wallet"),
  79. ('bob_bal1', "Bob's balance"),
  80. ('bob_add_label', "adding a 40-character UTF-8 encoded label"),
  81. ('bob_twview1', "viewing Bob's tracking wallet"),
  82. ('bob_split1', "splitting Bob's funds"),
  83. ('generate', 'mining a block'),
  84. ('bob_bal2', "Bob's balance"),
  85. ('bob_bal2a', "Bob's balance (age_fmt=confs)"),
  86. ('bob_bal2b', "Bob's balance (showempty=1)"),
  87. ('bob_bal2c', "Bob's balance (showempty=1 minconf=2 age_fmt=days)"),
  88. ('bob_bal2d', "Bob's balance (minconf=2)"),
  89. ('bob_bal2e', "Bob's balance (showempty=1 sort=age)"),
  90. ('bob_bal2f', "Bob's balance (showempty=1 sort=age,reverse)"),
  91. ('bob_rbf_send', 'sending funds to Alice (RBF)'),
  92. ('get_mempool1', 'mempool (before RBF bump)'),
  93. ('bob_rbf_bump', 'bumping RBF transaction'),
  94. ('get_mempool2', 'mempool (after RBF bump)'),
  95. ('generate', 'mining a block'),
  96. ('bob_bal3', "Bob's balance"),
  97. ('bob_pre_import', 'sending to non-imported address'),
  98. ('generate', 'mining a block'),
  99. ('bob_import_addr', 'importing non-MMGen address with --rescan'),
  100. ('bob_bal4', "Bob's balance (after import with rescan)"),
  101. ('bob_import_list', 'importing flat address list'),
  102. ('bob_split2', "splitting Bob's funds"),
  103. ('generate', 'mining a block'),
  104. ('bob_bal5', "Bob's balance"),
  105. ('bob_bal5_getbalance', "Bob's balance"),
  106. ('bob_send_non_mmgen', 'sending funds to Alice (from non-MMGen addrs)'),
  107. ('generate', 'mining a block'),
  108. ('alice_bal_rpcfail', 'RPC failure code'),
  109. ('alice_send_estimatefee', 'tx creation with no fee on command line'),
  110. ('generate', 'mining a block'),
  111. ('bob_bal6', "Bob's balance"),
  112. ('bob_alice_bal', "Bob and Alice's balances"),
  113. ('alice_bal2', "Alice's balance"),
  114. ('alice_add_label1', 'adding a label'),
  115. ('alice_chk_label1', 'the label'),
  116. ('alice_add_label2', 'adding a label'),
  117. ('alice_chk_label2', 'the label'),
  118. ('alice_edit_label1', 'editing a label'),
  119. ('alice_chk_label3', 'the label'),
  120. ('alice_remove_label1', 'removing a label'),
  121. ('alice_chk_label4', 'the label'),
  122. ('alice_add_label_coinaddr', 'adding a label using the coin address'),
  123. ('alice_chk_label_coinaddr', 'the label'),
  124. ('alice_add_label_badaddr1', 'adding a label with invalid address'),
  125. ('alice_add_label_badaddr2', 'adding a label with invalid address for this chain'),
  126. ('alice_add_label_badaddr3', 'adding a label with wrong MMGen address'),
  127. ('alice_add_label_badaddr4', 'adding a label with wrong coin address'),
  128. ('stop', 'stopping regtest daemon'),
  129. )
  130. def __init__(self,trunner,cfgs,spawn):
  131. coin = g.coin.lower()
  132. for k in rt_data:
  133. globals()[k] = rt_data[k][coin] if coin in rt_data[k] else None
  134. return TestSuiteBase.__init__(self,trunner,cfgs,spawn)
  135. def _add_comments_to_addr_file(self,addrfile,outfile,use_labels=False):
  136. silence()
  137. gmsg("Adding comments to address file '{}'".format(addrfile))
  138. a = AddrList(addrfile)
  139. for n,idx in enumerate(a.idxs(),1):
  140. if use_labels:
  141. a.set_comment(idx,get_label())
  142. else:
  143. if n % 2: a.set_comment(idx,'Test address {}'.format(n))
  144. a.format(enable_comments=True)
  145. write_data_to_file(outfile,a.fmt_data,quiet=True,ignore_opt_outdir=True)
  146. end_silence()
  147. def setup(self):
  148. os.environ['MMGEN_BOGUS_WALLET_DATA'] = ''
  149. if g.testnet:
  150. die(2,'--testnet option incompatible with regtest test suite')
  151. try: shutil.rmtree(joinpath(self.tr.data_dir,'regtest'))
  152. except: pass
  153. os.environ['MMGEN_TEST_SUITE'] = '' # mnemonic is piped to stdin, so stop being a terminal
  154. t = self.spawn('mmgen-regtest',['-n','setup'])
  155. os.environ['MMGEN_TEST_SUITE'] = '1'
  156. for s in ('Starting setup','Creating','Mined','Creating','Creating','Setup complete'):
  157. t.expect(s)
  158. return t
  159. def walletgen(self,user):
  160. t = self.spawn('mmgen-walletgen',['-q','-r0','-p1','--'+user])
  161. t.passphrase_new('new MMGen wallet',rt_pw)
  162. t.label()
  163. t.expect('move it to the data directory? (Y/n): ','y')
  164. t.written_to_file('MMGen wallet')
  165. return t
  166. def walletgen_bob(self): return self.walletgen('bob')
  167. def walletgen_alice(self): return self.walletgen('alice')
  168. def _user_dir(self,user,coin=None):
  169. return joinpath(self.tr.data_dir,'regtest',coin or g.coin.lower(),user)
  170. def _user_sid(self,user):
  171. return os.path.basename(get_file_with_ext(self._user_dir(user),'mmdat'))[:8]
  172. def addrgen(self,user,wf=None,addr_range='1-5',mmtypes=[]):
  173. from mmgen.addr import MMGenAddrType
  174. for mmtype in mmtypes or g.proto.mmtypes:
  175. t = self.spawn('mmgen-addrgen',
  176. ['--quiet','--'+user,'--type='+mmtype,'--outdir={}'.format(self._user_dir(user))] +
  177. ([wf] if wf else []) +
  178. [addr_range],
  179. extra_desc='({})'.format(MMGenAddrType.mmtypes[mmtype]['name']))
  180. t.passphrase('MMGen wallet',rt_pw)
  181. t.written_to_file('Addresses')
  182. ok_msg()
  183. t.skip_ok = True
  184. return t
  185. def addrgen_bob(self): return self.addrgen('bob')
  186. def addrgen_alice(self): return self.addrgen('alice')
  187. def addrimport(self,user,sid=None,addr_range='1-5',num_addrs=5,mmtypes=[]):
  188. id_strs = { 'legacy':'', 'compressed':'-C', 'segwit':'-S', 'bech32':'-B' }
  189. if not sid: sid = self._user_sid(user)
  190. from mmgen.addr import MMGenAddrType
  191. for mmtype in mmtypes or g.proto.mmtypes:
  192. desc = MMGenAddrType.mmtypes[mmtype]['name']
  193. addrfile = joinpath(self._user_dir(user),
  194. '{}{}{}[{}]{x}.testnet.addrs'.format(
  195. sid,self.altcoin_pfx,id_strs[desc],addr_range,
  196. x='-α' if g.debug_utf8 else ''))
  197. if mmtype == g.proto.mmtypes[0] and user == 'bob':
  198. psave = g.proto
  199. g.proto = CoinProtocol(g.coin,True)
  200. self._add_comments_to_addr_file(addrfile,addrfile,use_labels=True)
  201. g.proto = psave
  202. t = self.spawn( 'mmgen-addrimport',
  203. ['--quiet', '--'+user, '--batch', addrfile],
  204. extra_desc='({})'.format(desc))
  205. if g.debug:
  206. t.expect("Type uppercase 'YES' to confirm: ",'YES\n')
  207. t.expect('Importing')
  208. t.expect('{} addresses imported'.format(num_addrs))
  209. ok_msg()
  210. t.skip_ok = True
  211. return t
  212. def addrimport_bob(self): return self.addrimport('bob')
  213. def addrimport_alice(self): return self.addrimport('alice')
  214. def fund_wallet(self,user,mmtype,amt,sid=None,addr_range='1-5'):
  215. if not sid: sid = self._user_sid(user)
  216. addr = self.get_addr_from_addrlist(user,sid,mmtype,0,addr_range=addr_range)
  217. t = self.spawn('mmgen-regtest', ['send',str(addr),str(amt)])
  218. t.expect('Sending {} {}'.format(amt,g.coin))
  219. t.expect('Mined 1 block')
  220. return t
  221. def fund_bob(self): return self.fund_wallet('bob','C',rtFundAmt)
  222. def fund_alice(self): return self.fund_wallet('alice',('L','S')[g.proto.cap('segwit')],rtFundAmt)
  223. def user_twview(self,user,chk=None,sort='age'):
  224. t = self.spawn('mmgen-tool',['--'+user,'twview','sort='+sort])
  225. if chk: t.expect(chk,regex=True)
  226. t.read()
  227. return t
  228. def bob_twview1(self): return self.user_twview('bob',chk=r'1\).*\b{}\b'.format(rtAmts[0]))
  229. def user_bal(self,user,bal,args=['showempty=1'],skip_check=False,exit_val=0):
  230. t = self.spawn('mmgen-tool',['--'+user,'listaddresses'] + args)
  231. if skip_check:
  232. t.read()
  233. else:
  234. total = t.expect_getend('TOTAL: ')
  235. cmp_or_die('{} {}'.format(bal,g.coin),total)
  236. t.req_exit_val = exit_val
  237. return t
  238. def alice_bal1(self):
  239. return self.user_bal('alice',rtFundAmt)
  240. def alice_bal2(self):
  241. return self.user_bal('alice',rtBals[8])
  242. def bob_bal1(self):
  243. return self.user_bal('bob',rtFundAmt)
  244. def bob_bal2(self):
  245. return self.user_bal('bob',rtBals[0])
  246. def bob_bal2a(self):
  247. return self.user_bal('bob',rtBals[0],args=['showempty=1','age_fmt=confs'])
  248. def bob_bal2b(self):
  249. return self.user_bal('bob',rtBals[0],args=['showempty=1'])
  250. def bob_bal2c(self):
  251. return self.user_bal('bob',rtBals[0],args=['showempty=1','minconf=2','age_fmt=days'],skip_check=True)
  252. def bob_bal2d(self):
  253. return self.user_bal('bob',rtBals[0],args=['minconf=2'],skip_check=True)
  254. def bob_bal2e(self):
  255. return self.user_bal('bob',rtBals[0],args=['showempty=1','sort=age'])
  256. def bob_bal2f(self):
  257. return self.user_bal('bob',rtBals[0],args=['showempty=1','sort=age,reverse'])
  258. def bob_bal3(self):
  259. return self.user_bal('bob',rtBals[1])
  260. def bob_bal4(self):
  261. return self.user_bal('bob',rtBals[2])
  262. def bob_bal5(self):
  263. return self.user_bal('bob',rtBals[3])
  264. def bob_bal6(self):
  265. return self.user_bal('bob',rtBals[7])
  266. def bob_bal5_getbalance(self):
  267. t_ext,t_mmgen = rtBals_gb[0],rtBals_gb[1]
  268. assert Decimal(t_ext) + Decimal(t_mmgen) == Decimal(rtBals[3])
  269. t = self.spawn('mmgen-tool',['--bob','getbalance'])
  270. t.expect(r'\n[0-9A-F]{8}: .* '+t_mmgen,regex=True)
  271. t.expect(r'\nNon-MMGen: .* '+t_ext,regex=True)
  272. t.expect(r'\nTOTAL: .* '+rtBals[3],regex=True)
  273. t.read()
  274. return t
  275. def bob_alice_bal(self):
  276. t = self.spawn('mmgen-regtest',['get_balances'])
  277. t.expect('Switching')
  278. ret = t.expect_getend("Bob's balance:").strip()
  279. cmp_or_die(rtBals[4],ret)
  280. ret = t.expect_getend("Alice's balance:").strip()
  281. cmp_or_die(rtBals[5],ret)
  282. ret = t.expect_getend("Total balance:").strip()
  283. cmp_or_die(rtBals[6],ret)
  284. return t
  285. def user_txdo( self, user, fee, outputs_cl, outputs_list,
  286. extra_args = [],
  287. wf = None,
  288. do_label = False,
  289. bad_locktime = False,
  290. full_tx_view = False,
  291. menu = ['M'],
  292. bogus_send = False):
  293. os.environ['MMGEN_BOGUS_SEND'] = ('','1')[bool(bogus_send)]
  294. t = self.spawn('mmgen-txdo',
  295. ['-d',self.tmpdir,'-B','--'+user] +
  296. (['--tx-fee='+fee] if fee else []) +
  297. extra_args + ([],[wf])[bool(wf)] + outputs_cl)
  298. os.environ['MMGEN_BOGUS_SEND'] = '1'
  299. self.txcreate_ui_common(t,
  300. caller = 'txdo',
  301. menu = menu,
  302. inputs = outputs_list,
  303. file_desc = 'Signed transaction',
  304. interactive_fee = (tx_fee,'')[bool(fee)],
  305. add_comment = ref_tx_label_jp,
  306. view = 't',save=True)
  307. t.passphrase('MMGen wallet',rt_pw)
  308. t.written_to_file('Signed transaction')
  309. self._do_confirm_send(t)
  310. s,exit_val = (('Transaction sent',0),("can't be included",1))[bad_locktime]
  311. t.expect(s)
  312. t.req_exit_val = exit_val
  313. return t
  314. def bob_split1(self):
  315. sid = self._user_sid('bob')
  316. outputs_cl = [sid+':C:1,100', sid+':L:2,200',sid+':'+rtBobOp3]
  317. return self.user_txdo('bob',rtFee[0],outputs_cl,'1',do_label=True,full_tx_view=True)
  318. def get_addr_from_addrlist(self,user,sid,mmtype,idx,addr_range='1-5'):
  319. id_str = { 'L':'', 'S':'-S', 'C':'-C', 'B':'-B' }[mmtype]
  320. ext = '{}{}{}[{}]{x}.testnet.addrs'.format(
  321. sid,self.altcoin_pfx,id_str,addr_range,x='-α' if g.debug_utf8 else '')
  322. addrfile = get_file_with_ext(self._user_dir(user),ext,no_dot=True)
  323. psave = g.proto
  324. g.proto = CoinProtocol(g.coin,True)
  325. if hasattr(g.proto,'bech32_hrp_rt'):
  326. g.proto.bech32_hrp = g.proto.bech32_hrp_rt
  327. silence()
  328. addr = AddrList(addrfile).data[idx].addr
  329. end_silence()
  330. g.proto = psave
  331. return addr
  332. def _create_tx_outputs(self,user,data):
  333. sid = self._user_sid(user)
  334. return [self.get_addr_from_addrlist(user,sid,mmtype,idx-1)+amt_str for mmtype,idx,amt_str in data]
  335. def bob_rbf_send(self):
  336. outputs_cl = self._create_tx_outputs('alice',(('L',1,',60'),('C',1,',40'))) # alice_sid:L:1, alice_sid:C:1
  337. outputs_cl += [self._user_sid('bob')+':'+rtBobOp3]
  338. return self.user_txdo('bob',rtFee[1],outputs_cl,'3',
  339. extra_args=([],['--rbf'])[g.proto.cap('rbf')])
  340. def bob_send_non_mmgen(self):
  341. outputs_cl = self._create_tx_outputs('alice',(
  342. (('L','S')[g.proto.cap('segwit')],2,',10'),
  343. (('L','S')[g.proto.cap('segwit')],3,'')
  344. )) # alice_sid:S:2, alice_sid:S:3
  345. keyfile = joinpath(self.tmpdir,'non-mmgen.keys')
  346. return self.user_txdo('bob',rtFee[3],outputs_cl,'1,4-10',
  347. extra_args=['--keys-from-file='+keyfile,'--vsize-adj=1.02'])
  348. def alice_send_estimatefee(self):
  349. outputs_cl = self._create_tx_outputs('bob',(('L',1,''),)) # bob_sid:L:1
  350. return self.user_txdo('alice',None,outputs_cl,'1') # fee=None
  351. def user_txbump(self,user,txfile,fee,red_op):
  352. if not g.proto.cap('rbf'):
  353. msg('Skipping RBF'); return 'skip'
  354. os.environ['MMGEN_BOGUS_SEND'] = ''
  355. t = self.spawn('mmgen-txbump',
  356. ['-d',self.tmpdir,'--send','--'+user,'--tx-fee='+fee,'--output-to-reduce='+red_op] + [txfile])
  357. os.environ['MMGEN_BOGUS_SEND'] = '1'
  358. t.expect('OK? (Y/n): ','y') # output OK?
  359. t.expect('OK? (Y/n): ','y') # fee OK?
  360. t.do_comment(False,has_label=True)
  361. t.passphrase('MMGen wallet',rt_pw)
  362. t.written_to_file('Signed transaction')
  363. self.txsend_ui_common(t,caller='txdo',bogus_send=False,file_desc='Signed transaction')
  364. t.read()
  365. return t
  366. def bob_rbf_bump(self):
  367. ext = ',{}]{x}.testnet.sigtx'.format(rtFee[1][:-1],x='-α' if g.debug_utf8 else '')
  368. txfile = self.get_file_with_ext(ext,delete=False,no_dot=True)
  369. return self.user_txbump('bob',txfile,rtFee[2],'c')
  370. def generate(self,coin=None,num_blocks=1):
  371. int(num_blocks)
  372. if coin: opt.coin = coin
  373. t = self.spawn('mmgen-regtest',['generate',str(num_blocks)])
  374. t.expect('Mined {} block'.format(num_blocks))
  375. return t
  376. def _get_mempool(self):
  377. disable_debug()
  378. ret = self.spawn('mmgen-regtest',['show_mempool']).read()
  379. restore_debug()
  380. from ast import literal_eval
  381. return literal_eval(ret.split('\n')[0]) # allow for extra output by handler at end
  382. def get_mempool1(self):
  383. mp = self._get_mempool()
  384. if len(mp) != 1:
  385. rdie(2,'Mempool has more or less than one TX!')
  386. self.write_to_tmpfile('rbf_txid',mp[0]+'\n')
  387. return 'ok'
  388. def get_mempool2(self):
  389. if not g.proto.cap('rbf'):
  390. msg('Skipping post-RBF mempool check'); return 'skip'
  391. mp = self._get_mempool()
  392. if len(mp) != 1:
  393. rdie(2,'Mempool has more or less than one TX!')
  394. chk = self.read_from_tmpfile('rbf_txid')
  395. if chk.strip() == mp[0]:
  396. rdie(2,'TX in mempool has not changed! RBF bump failed')
  397. return 'ok'
  398. @staticmethod
  399. def _gen_pairs(n):
  400. disable_debug()
  401. ret = [subprocess.check_output(
  402. ['python3',joinpath('cmds','mmgen-tool'),'--testnet=1'] +
  403. (['--type=compressed'],[])[i==0] +
  404. ['-r0','randpair']
  405. ).decode().split() for i in range(n)]
  406. restore_debug()
  407. return ret
  408. def bob_pre_import(self):
  409. pairs = self._gen_pairs(5)
  410. self.write_to_tmpfile('non-mmgen.keys','\n'.join([a[0] for a in pairs])+'\n')
  411. self.write_to_tmpfile('non-mmgen.addrs','\n'.join([a[1] for a in pairs])+'\n')
  412. return self.user_txdo('bob',rtFee[4],[pairs[0][1]],'3')
  413. def user_import(self,user,args):
  414. t = self.spawn('mmgen-addrimport',['--quiet','--'+user]+args)
  415. if g.debug:
  416. t.expect("Type uppercase 'YES' to confirm: ",'YES\n')
  417. t.expect('Importing')
  418. t.expect('OK')
  419. return t
  420. def bob_import_addr(self):
  421. addr = self.read_from_tmpfile('non-mmgen.addrs').split()[0]
  422. return self.user_import('bob',['--rescan','--address='+addr])
  423. def bob_import_list(self):
  424. addrfile = joinpath(self.tmpdir,'non-mmgen.addrs')
  425. return self.user_import('bob',['--addrlist',addrfile])
  426. def bob_split2(self):
  427. addrs = self.read_from_tmpfile('non-mmgen.addrs').split()
  428. amts = (1.12345678,2.87654321,3.33443344,4.00990099,5.43214321)
  429. outputs1 = list(map('{},{}'.format,addrs,amts))
  430. sid = self._user_sid('bob')
  431. l1,l2 = (':S',':B') if 'B' in g.proto.mmtypes else (':S',':S') if g.proto.cap('segwit') else (':L',':L')
  432. outputs2 = [sid+':C:2,6.333', sid+':L:3,6.667',sid+l1+':4,0.123',sid+l2+':5']
  433. return self.user_txdo('bob',rtFee[5],outputs1+outputs2,'1-2')
  434. def user_add_label(self,user,addr,label):
  435. t = self.spawn('mmgen-tool',['--'+user,'add_label',addr,label])
  436. t.expect('Added label.*in tracking wallet',regex=True)
  437. return t
  438. def user_remove_label(self,user,addr):
  439. t = self.spawn('mmgen-tool',['--'+user,'remove_label',addr])
  440. t.expect('Removed label.*in tracking wallet',regex=True)
  441. return t
  442. def bob_add_label(self):
  443. sid = self._user_sid('bob')
  444. return self.user_add_label('bob',sid+':C:1',utf8_label)
  445. def alice_add_label1(self):
  446. sid = self._user_sid('alice')
  447. return self.user_add_label('alice',sid+':C:1','Original Label - 月へ')
  448. def alice_add_label2(self):
  449. sid = self._user_sid('alice')
  450. return self.user_add_label('alice',sid+':C:1','Replacement Label')
  451. def alice_add_label_coinaddr(self):
  452. mmid = self._user_sid('alice') + (':S:1',':L:1')[g.coin=='BCH']
  453. t = self.spawn('mmgen-tool',['--alice','listaddress',mmid],no_msg=True)
  454. btcaddr = [i for i in t.read().splitlines() if i.lstrip()[0:len(mmid)] == mmid][0].split()[1]
  455. return self.user_add_label('alice',btcaddr,'Label added using coin address')
  456. def alice_chk_label_coinaddr(self):
  457. mmid = self._user_sid('alice') + (':S:1',':L:1')[g.coin=='BCH']
  458. return self.user_chk_label('alice',mmid,'Label added using coin address')
  459. def alice_add_label_badaddr(self,addr,reply):
  460. t = self.spawn('mmgen-tool',['--alice','add_label',addr,'(none)'])
  461. t.expect(reply,regex=True)
  462. return t
  463. def alice_add_label_badaddr1(self):
  464. return self.alice_add_label_badaddr(rt_pw,'Invalid coin address for this chain: ')
  465. def alice_add_label_badaddr2(self):
  466. addr = g.proto.pubhash2addr('00'*20,False) # mainnet zero address
  467. return self.alice_add_label_badaddr(addr,'Invalid coin address for this chain: '+addr)
  468. def alice_add_label_badaddr3(self):
  469. addr = self._user_sid('alice') + ':C:123'
  470. return self.alice_add_label_badaddr(addr,
  471. "MMGen address '{}' not found in tracking wallet".format(addr))
  472. def alice_add_label_badaddr4(self):
  473. addr = CoinProtocol(g.coin,True).pubhash2addr('00'*20,False) # testnet zero address
  474. return self.alice_add_label_badaddr(addr,
  475. "Address '{}' not found in tracking wallet".format(addr))
  476. def alice_bal_rpcfail(self):
  477. addr = self._user_sid('alice') + ':C:2'
  478. os.environ['MMGEN_RPC_FAIL_ON_COMMAND'] = 'listunspent'
  479. t = self.spawn('mmgen-tool',['--alice','getbalance'])
  480. os.environ['MMGEN_RPC_FAIL_ON_COMMAND'] = ''
  481. t.expect('Method not found')
  482. t.read()
  483. t.req_exit_val = 3
  484. return t
  485. def alice_remove_label1(self):
  486. sid = self._user_sid('alice')
  487. mmid = sid + (':S:3',':L:3')[g.coin=='BCH']
  488. return self.user_remove_label('alice',mmid)
  489. def user_chk_label(self,user,addr,label,label_pat=None):
  490. t = self.spawn('mmgen-tool',['--'+user,'listaddresses','all_labels=1'])
  491. t.expect(r'{}\s+\S{{30}}\S+\s+{}\s+'.format(addr,(label_pat or label)),regex=True)
  492. return t
  493. def alice_chk_label1(self):
  494. sid = self._user_sid('alice')
  495. return self.user_chk_label('alice',sid+':C:1','Original Label - 月へ')
  496. def alice_chk_label2(self):
  497. sid = self._user_sid('alice')
  498. return self.user_chk_label('alice',sid+':C:1','Replacement Label')
  499. def alice_edit_label1(self):
  500. return self.user_edit_label('alice','4',utf8_label)
  501. def alice_chk_label3(self):
  502. sid = self._user_sid('alice')
  503. mmid = sid + (':S:3',':L:3')[g.coin=='BCH']
  504. return self.user_chk_label('alice',mmid,utf8_label,label_pat=utf8_label_pat)
  505. def alice_chk_label4(self):
  506. sid = self._user_sid('alice')
  507. mmid = sid + (':S:3',':L:3')[g.coin=='BCH']
  508. return self.user_chk_label('alice',mmid,'-')
  509. def user_edit_label(self,user,output,label):
  510. t = self.spawn('mmgen-txcreate',['-B','--'+user,'-i'])
  511. t.expect(r'add \[l\]abel:.','M',regex=True)
  512. t.expect(r'add \[l\]abel:.','l',regex=True)
  513. t.expect(r"Enter unspent.*return to main menu\):.",output+'\n',regex=True)
  514. t.expect(r"Enter label text.*return to main menu\):.",label+'\n',regex=True)
  515. t.expect(r'\[q\]uit view, .*?:.','q',regex=True)
  516. return t
  517. def stop(self):
  518. if opt.no_daemon_stop:
  519. self.spawn('',msg_only=True)
  520. msg_r('(leaving daemon running by user request)')
  521. return 'ok'
  522. else:
  523. return self.spawn('mmgen-regtest',['stop'])