ts_regtest.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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_twview', "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_add_label1', 'adding a label'),
  109. ('alice_chk_label1', 'the label'),
  110. ('alice_add_label2', 'adding a label'),
  111. ('alice_chk_label2', 'the label'),
  112. ('alice_edit_label1', 'editing a label'),
  113. ('alice_chk_label3', 'the label'),
  114. ('alice_remove_label1', 'removing a label'),
  115. ('alice_chk_label4', 'the label'),
  116. ('alice_add_label_coinaddr', 'adding a label using the coin address'),
  117. ('alice_chk_label_coinaddr', 'the label'),
  118. ('alice_add_label_badaddr1', 'adding a label with invalid address'),
  119. ('alice_add_label_badaddr2', 'adding a label with invalid address for this chain'),
  120. ('alice_add_label_badaddr3', 'adding a label with wrong MMGen address'),
  121. ('alice_add_label_badaddr4', 'adding a label with wrong coin address'),
  122. ('alice_bal_rpcfail', 'RPC failure code'),
  123. ('alice_send_estimatefee', 'tx creation with no fee on command line'),
  124. ('generate', 'mining a block'),
  125. ('bob_bal6', "Bob's balance"),
  126. ('bob_alice_bal', "Bob and Alice's balances"),
  127. ('alice_bal2', "Alice's balance"),
  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'):
  173. from mmgen.addr import MMGenAddrType
  174. for mmtype in g.proto.mmtypes:
  175. t = self.spawn('mmgen-addrgen',
  176. ['--quiet','--'+user,'--type='+mmtype,'--outdir={}'.format(self._user_dir(user))] +
  177. ([],[wf])[bool(wf)] + [addr_range],
  178. extra_desc='({})'.format(MMGenAddrType.mmtypes[mmtype]['name']))
  179. t.passphrase('MMGen wallet',rt_pw)
  180. t.written_to_file('Addresses')
  181. ok_msg()
  182. t.skip_ok = True
  183. return t
  184. def addrgen_bob(self): return self.addrgen('bob')
  185. def addrgen_alice(self): return self.addrgen('alice')
  186. def addrimport(self,user,sid=None,addr_range='1-5',num_addrs=5):
  187. id_strs = { 'legacy':'', 'compressed':'-C', 'segwit':'-S', 'bech32':'-B' }
  188. if not sid: sid = self._user_sid(user)
  189. from mmgen.addr import MMGenAddrType
  190. for mmtype in g.proto.mmtypes:
  191. desc = MMGenAddrType.mmtypes[mmtype]['name']
  192. addrfile = joinpath(self._user_dir(user),
  193. '{}{}{}[{}]{x}.testnet.addrs'.format(
  194. sid,self.altcoin_pfx,id_strs[desc],addr_range,
  195. x='-α' if g.debug_utf8 else ''))
  196. if mmtype == g.proto.mmtypes[0] and user == 'bob':
  197. psave = g.proto
  198. g.proto = CoinProtocol(g.coin,True)
  199. self._add_comments_to_addr_file(addrfile,addrfile,use_labels=True)
  200. g.proto = psave
  201. t = self.spawn( 'mmgen-addrimport',
  202. ['--quiet', '--'+user, '--batch', addrfile],
  203. extra_desc='({})'.format(desc))
  204. if g.debug:
  205. t.expect("Type uppercase 'YES' to confirm: ",'YES\n')
  206. t.expect('Importing')
  207. t.expect('{} addresses imported'.format(num_addrs))
  208. ok_msg()
  209. t.skip_ok = True
  210. return t
  211. def addrimport_bob(self): return self.addrimport('bob')
  212. def addrimport_alice(self): return self.addrimport('alice')
  213. def fund_wallet(self,user,mmtype,amt,sid=None,addr_range='1-5'):
  214. if not sid: sid = self._user_sid(user)
  215. addr = self.get_addr_from_addrlist(user,sid,mmtype,0,addr_range=addr_range)
  216. t = self.spawn('mmgen-regtest', ['send',str(addr),str(amt)])
  217. t.expect('Sending {} {}'.format(amt,g.coin))
  218. t.expect('Mined 1 block')
  219. return t
  220. def fund_bob(self): return self.fund_wallet('bob','C',rtFundAmt)
  221. def fund_alice(self): return self.fund_wallet('alice',('L','S')[g.proto.cap('segwit')],rtFundAmt)
  222. def user_twview(self,user):
  223. t = self.spawn('mmgen-tool',['--'+user,'twview'])
  224. t.expect(r'1\).*\b{}\b'.format(rtAmts[0]),regex=True)
  225. t.read()
  226. return t
  227. def bob_twview(self):
  228. return self.user_twview('bob')
  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. os.environ['MMGEN_BOGUS_SEND'] = ''
  292. t = self.spawn('mmgen-txdo',
  293. ['-d',self.tmpdir,'-B','--'+user] +
  294. (['--tx-fee='+fee] if fee else []) +
  295. extra_args + ([],[wf])[bool(wf)] + outputs_cl)
  296. os.environ['MMGEN_BOGUS_SEND'] = '1'
  297. self.txcreate_ui_common(t,
  298. caller = 'txdo',
  299. menu = ['M'],
  300. inputs = outputs_list,
  301. file_desc = 'Signed transaction',
  302. interactive_fee = (tx_fee,'')[bool(fee)],
  303. add_comment = ref_tx_label_jp,
  304. view = 't',save=True)
  305. t.passphrase('MMGen wallet',rt_pw)
  306. t.written_to_file('Signed transaction')
  307. self._do_confirm_send(t)
  308. s,exit_val = (('Transaction sent',0),("can't be included",1))[bad_locktime]
  309. t.expect(s)
  310. t.req_exit_val = exit_val
  311. return t
  312. def bob_split1(self):
  313. sid = self._user_sid('bob')
  314. outputs_cl = [sid+':C:1,100', sid+':L:2,200',sid+':'+rtBobOp3]
  315. return self.user_txdo('bob',rtFee[0],outputs_cl,'1',do_label=True,full_tx_view=True)
  316. def get_addr_from_addrlist(self,user,sid,mmtype,idx,addr_range='1-5'):
  317. id_str = { 'L':'', 'S':'-S', 'C':'-C', 'B':'-B' }[mmtype]
  318. ext = '{}{}{}[{}]{x}.testnet.addrs'.format(
  319. sid,self.altcoin_pfx,id_str,addr_range,x='-α' if g.debug_utf8 else '')
  320. addrfile = get_file_with_ext(self._user_dir(user),ext,no_dot=True)
  321. psave = g.proto
  322. g.proto = CoinProtocol(g.coin,True)
  323. if hasattr(g.proto,'bech32_hrp_rt'):
  324. g.proto.bech32_hrp = g.proto.bech32_hrp_rt
  325. silence()
  326. addr = AddrList(addrfile).data[idx].addr
  327. end_silence()
  328. g.proto = psave
  329. return addr
  330. def _create_tx_outputs(self,user,data):
  331. sid = self._user_sid(user)
  332. return [self.get_addr_from_addrlist(user,sid,mmtype,idx-1)+amt_str for mmtype,idx,amt_str in data]
  333. def bob_rbf_send(self):
  334. outputs_cl = self._create_tx_outputs('alice',(('L',1,',60'),('C',1,',40'))) # alice_sid:L:1, alice_sid:C:1
  335. outputs_cl += [self._user_sid('bob')+':'+rtBobOp3]
  336. return self.user_txdo('bob',rtFee[1],outputs_cl,'3',
  337. extra_args=([],['--rbf'])[g.proto.cap('rbf')])
  338. def bob_send_non_mmgen(self):
  339. outputs_cl = self._create_tx_outputs('alice',(
  340. (('L','S')[g.proto.cap('segwit')],2,',10'),
  341. (('L','S')[g.proto.cap('segwit')],3,'')
  342. )) # alice_sid:S:2, alice_sid:S:3
  343. keyfile = joinpath(self.tmpdir,'non-mmgen.keys')
  344. return self.user_txdo('bob',rtFee[3],outputs_cl,'1,4-10',
  345. extra_args=['--keys-from-file='+keyfile,'--vsize-adj=1.02'])
  346. def alice_send_estimatefee(self):
  347. outputs_cl = self._create_tx_outputs('bob',(('L',1,''),)) # bob_sid:L:1
  348. return self.user_txdo('alice',None,outputs_cl,'1') # fee=None
  349. def user_txbump(self,user,txfile,fee,red_op):
  350. if not g.proto.cap('rbf'):
  351. msg('Skipping RBF'); return 'skip'
  352. os.environ['MMGEN_BOGUS_SEND'] = ''
  353. t = self.spawn('mmgen-txbump',
  354. ['-d',self.tmpdir,'--send','--'+user,'--tx-fee='+fee,'--output-to-reduce='+red_op] + [txfile])
  355. os.environ['MMGEN_BOGUS_SEND'] = '1'
  356. t.expect('OK? (Y/n): ','y') # output OK?
  357. t.expect('OK? (Y/n): ','y') # fee OK?
  358. t.do_comment(False,has_label=True)
  359. t.passphrase('MMGen wallet',rt_pw)
  360. t.written_to_file('Signed transaction')
  361. self.txsend_ui_common(t,caller='txdo',bogus_send=False,file_desc='Signed transaction')
  362. t.read()
  363. return t
  364. def bob_rbf_bump(self):
  365. ext = ',{}]{x}.testnet.sigtx'.format(rtFee[1][:-1],x='-α' if g.debug_utf8 else '')
  366. txfile = self.get_file_with_ext(ext,delete=False,no_dot=True)
  367. return self.user_txbump('bob',txfile,rtFee[2],'c')
  368. def generate(self,coin=None,num_blocks=1):
  369. int(num_blocks)
  370. if coin: opt.coin = coin
  371. t = self.spawn('mmgen-regtest',['generate',str(num_blocks)])
  372. t.expect('Mined {} block'.format(num_blocks))
  373. return t
  374. def _get_mempool(self):
  375. disable_debug()
  376. ret = self.spawn('mmgen-regtest',['show_mempool']).read()
  377. restore_debug()
  378. from ast import literal_eval
  379. return literal_eval(ret.split('\n')[0]) # allow for extra output by handler at end
  380. def get_mempool1(self):
  381. mp = self._get_mempool()
  382. if len(mp) != 1:
  383. rdie(2,'Mempool has more or less than one TX!')
  384. self.write_to_tmpfile('rbf_txid',mp[0]+'\n')
  385. return 'ok'
  386. def get_mempool2(self):
  387. if not g.proto.cap('rbf'):
  388. msg('Skipping post-RBF mempool check'); return 'skip'
  389. mp = self._get_mempool()
  390. if len(mp) != 1:
  391. rdie(2,'Mempool has more or less than one TX!')
  392. chk = self.read_from_tmpfile('rbf_txid')
  393. if chk.strip() == mp[0]:
  394. rdie(2,'TX in mempool has not changed! RBF bump failed')
  395. return 'ok'
  396. @staticmethod
  397. def _gen_pairs(n):
  398. disable_debug()
  399. ret = [subprocess.check_output(
  400. ['python3',joinpath('cmds','mmgen-tool'),'--testnet=1'] +
  401. (['--type=compressed'],[])[i==0] +
  402. ['-r0','randpair']
  403. ).decode().split() for i in range(n)]
  404. restore_debug()
  405. return ret
  406. def bob_pre_import(self):
  407. pairs = self._gen_pairs(5)
  408. self.write_to_tmpfile('non-mmgen.keys','\n'.join([a[0] for a in pairs])+'\n')
  409. self.write_to_tmpfile('non-mmgen.addrs','\n'.join([a[1] for a in pairs])+'\n')
  410. return self.user_txdo('bob',rtFee[4],[pairs[0][1]],'3')
  411. def user_import(self,user,args):
  412. t = self.spawn('mmgen-addrimport',['--quiet','--'+user]+args)
  413. if g.debug:
  414. t.expect("Type uppercase 'YES' to confirm: ",'YES\n')
  415. t.expect('Importing')
  416. t.expect('OK')
  417. return t
  418. def bob_import_addr(self):
  419. addr = self.read_from_tmpfile('non-mmgen.addrs').split()[0]
  420. return self.user_import('bob',['--rescan','--address='+addr])
  421. def bob_import_list(self):
  422. addrfile = joinpath(self.tmpdir,'non-mmgen.addrs')
  423. return self.user_import('bob',['--addrlist',addrfile])
  424. def bob_split2(self):
  425. addrs = self.read_from_tmpfile('non-mmgen.addrs').split()
  426. amts = (1.12345678,2.87654321,3.33443344,4.00990099,5.43214321)
  427. outputs1 = list(map('{},{}'.format,addrs,amts))
  428. sid = self._user_sid('bob')
  429. l1,l2 = (':S',':B') if 'B' in g.proto.mmtypes else (':S',':S') if g.proto.cap('segwit') else (':L',':L')
  430. outputs2 = [sid+':C:2,6.333', sid+':L:3,6.667',sid+l1+':4,0.123',sid+l2+':5']
  431. return self.user_txdo('bob',rtFee[5],outputs1+outputs2,'1-2')
  432. def user_add_label(self,user,addr,label):
  433. t = self.spawn('mmgen-tool',['--'+user,'add_label',addr,label])
  434. t.expect('Added label.*in tracking wallet',regex=True)
  435. return t
  436. def user_remove_label(self,user,addr):
  437. t = self.spawn('mmgen-tool',['--'+user,'remove_label',addr])
  438. t.expect('Removed label.*in tracking wallet',regex=True)
  439. return t
  440. def bob_add_label(self):
  441. sid = self._user_sid('bob')
  442. return self.user_add_label('bob',sid+':C:1',utf8_label)
  443. def alice_add_label1(self):
  444. sid = self._user_sid('alice')
  445. return self.user_add_label('alice',sid+':C:1','Original Label - 月へ')
  446. def alice_add_label2(self):
  447. sid = self._user_sid('alice')
  448. return self.user_add_label('alice',sid+':C:1','Replacement Label')
  449. def alice_add_label_coinaddr(self):
  450. mmaddr = self._user_sid('alice') + ':C:2'
  451. t = self.spawn('mmgen-tool',['--alice','listaddress',mmaddr],no_msg=True)
  452. btcaddr = [i for i in t.read().splitlines() if i.lstrip()[0:len(mmaddr)] == mmaddr][0].split()[1]
  453. return self.user_add_label('alice',btcaddr,'Label added using coin address')
  454. def alice_chk_label_coinaddr(self):
  455. sid = self._user_sid('alice')
  456. return self.user_chk_label('alice',sid+':C:2','Label added using coin address')
  457. def alice_add_label_badaddr(self,addr,reply):
  458. t = self.spawn('mmgen-tool',['--alice','add_label',addr,'(none)'])
  459. t.expect(reply,regex=True)
  460. return t
  461. def alice_add_label_badaddr1(self):
  462. return self.alice_add_label_badaddr(rt_pw,'Invalid coin address for this chain: ')
  463. def alice_add_label_badaddr2(self):
  464. addr = g.proto.pubhash2addr('00'*20,False) # mainnet zero address
  465. return self.alice_add_label_badaddr(addr,'Invalid coin address for this chain: '+addr)
  466. def alice_add_label_badaddr3(self):
  467. addr = self._user_sid('alice') + ':C:123'
  468. return self.alice_add_label_badaddr(addr,
  469. "MMGen address '{}' not found in tracking wallet".format(addr))
  470. def alice_add_label_badaddr4(self):
  471. addr = CoinProtocol(g.coin,True).pubhash2addr('00'*20,False) # testnet zero address
  472. return self.alice_add_label_badaddr(addr,
  473. "Address '{}' not found in tracking wallet".format(addr))
  474. def alice_bal_rpcfail(self):
  475. addr = self._user_sid('alice') + ':C:2'
  476. os.environ['MMGEN_RPC_FAIL_ON_COMMAND'] = 'listunspent'
  477. t = self.spawn('mmgen-tool',['--alice','getbalance'])
  478. os.environ['MMGEN_RPC_FAIL_ON_COMMAND'] = ''
  479. t.expect('Method not found')
  480. t.read()
  481. t.req_exit_val = 3
  482. return t
  483. def alice_remove_label1(self):
  484. sid = self._user_sid('alice')
  485. return self.user_remove_label('alice',sid+':C:1')
  486. def user_chk_label(self,user,addr,label,label_pat=None):
  487. t = self.spawn('mmgen-tool',['--'+user,'listaddresses','all_labels=1'])
  488. t.expect(r'{}\s+\S{{30}}\S+\s+{}\s+'.format(addr,(label_pat or label)),regex=True)
  489. return t
  490. def alice_chk_label1(self):
  491. sid = self._user_sid('alice')
  492. return self.user_chk_label('alice',sid+':C:1','Original Label - 月へ')
  493. def alice_chk_label2(self):
  494. sid = self._user_sid('alice')
  495. return self.user_chk_label('alice',sid+':C:1','Replacement Label')
  496. def alice_edit_label1(self):
  497. return self.user_edit_label('alice','1',utf8_label)
  498. def alice_chk_label3(self):
  499. sid = self._user_sid('alice')
  500. return self.user_chk_label('alice',sid+':C:1',utf8_label,label_pat=utf8_label_pat)
  501. def alice_chk_label4(self):
  502. sid = self._user_sid('alice')
  503. return self.user_chk_label('alice',sid+':C:1','-')
  504. def user_edit_label(self,user,output,label):
  505. t = self.spawn('mmgen-txcreate',['-B','--'+user,'-i'])
  506. t.expect(r'add \[l\]abel:.','M',regex=True)
  507. t.expect(r'add \[l\]abel:.','l',regex=True)
  508. t.expect(r"Enter unspent.*return to main menu\):.",output+'\n',regex=True)
  509. t.expect(r"Enter label text.*return to main menu\):.",label+'\n',regex=True)
  510. t.expect(r'\[q\]uit view, .*?:.','q',regex=True)
  511. return t
  512. def stop(self):
  513. if opt.no_daemon_stop:
  514. self.spawn('',msg_only=True)
  515. msg_r('(leaving daemon running by user request)')
  516. return 'ok'
  517. else:
  518. return self.spawn('mmgen-regtest',['stop'])