ct_regtest.py 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  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. #
  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. test.cmdtest_d.ct_regtest: Regtest tests for the cmdtest.py test suite
  20. """
  21. import os, json, time, re
  22. from decimal import Decimal
  23. from mmgen.proto.btc.regtest import MMGenRegtest
  24. from mmgen.proto.bch.cashaddr import b32a
  25. from mmgen.proto.btc.common import b58a
  26. from mmgen.color import yellow
  27. from mmgen.util import msg_r, die, gmsg, capfirst, fmt_list
  28. from mmgen.protocol import init_proto
  29. from mmgen.addrlist import AddrList
  30. from mmgen.wallet import Wallet, get_wallet_cls
  31. from ..include.common import (
  32. cfg,
  33. imsg,
  34. omsg,
  35. ok,
  36. stop_test_daemons,
  37. joinpath,
  38. silence,
  39. end_silence,
  40. cmp_or_die,
  41. strip_ansi_escapes,
  42. gr_uc,
  43. getrandhex
  44. )
  45. from .common import (
  46. ok_msg,
  47. get_file_with_ext,
  48. get_comment,
  49. tw_comment_lat_cyr_gr,
  50. tw_comment_zh,
  51. tx_comment_jp,
  52. get_env_without_debug_vars
  53. )
  54. from .ct_base import CmdTestBase
  55. from .ct_shared import CmdTestShared
  56. pat_date = r'\b\d\d-\d\d-\d\d\b'
  57. pat_date_time = r'\b\d\d\d\d-\d\d-\d\d\s+\d\d:\d\d\b'
  58. dfl_wcls = get_wallet_cls('mmgen')
  59. tx_fee = rtFundAmt = rtFee = rtBals = rtBals_gb = rtBobOp3 = rtAmts = {} # pylint
  60. rt_pw = 'abc-α'
  61. rt_data = {
  62. 'tx_fee': {'btc':'0.0001', 'bch':'0.001', 'ltc':'0.01'},
  63. 'rtFundAmt': {'btc':'500', 'bch':'500', 'ltc':'5500'},
  64. 'rtFee': {
  65. 'btc': ('20s', '10s', '60s', '31s', '10s', '20s'),
  66. 'bch': ('20s', '10s', '60s', '0.0001', '10s', '20s'),
  67. 'ltc': ('1000s', '500s', '1500s', '0.05', '400s', '1000s')
  68. },
  69. 'rtBals': {
  70. 'btc': ('499.9999488', '399.9998282', '399.9998147', '399.9996877',
  71. '52.99980410', '946.99933647', '999.99914057', '52.9999',
  72. '946.99933647', '0.4169328', '6.24987417'),
  73. 'bch': ('499.9999484', '399.9999194', '399.9998972', '399.9997692',
  74. '46.78890380', '953.20966920', '999.99857300', '46.789',
  75. '953.2096692', '0.4169328', '39.58187387'),
  76. 'ltc': ('5499.99744', '5399.994425', '5399.993885', '5399.987535',
  77. '52.98520500', '10946.93753500', '10999.92274000', '52.99',
  78. '10946.937535', '0.41364', '6.24846787'),
  79. },
  80. 'rtBals_gb': {
  81. 'btc': {
  82. '0conf0': {
  83. 'mmgen': ('283.22339537', '283.22339537'),
  84. 'nonmm': ('16.77647763', '116.77629233'),
  85. 'total': ('299.999873', '399.9996877'),
  86. },
  87. '0conf1': {
  88. 'mmgen': ('283.22339537', '0'),
  89. 'nonmm': ('16.77647763', '99.9998147'),
  90. 'total': ('299.999873', '99.9998147'),
  91. },
  92. '1conf1': {
  93. 'mmgen': ('0', '283.22339537'),
  94. 'nonmm': ('0', '116.77629233'),
  95. 'total': ('0', '399.9996877'),
  96. },
  97. '1conf2': {
  98. 'mmgen': ('0', '283.22339537', '0'),
  99. 'nonmm': ('0', '16.77647763', '99.9998147'),
  100. 'total': ('0', '299.999873', '99.9998147'),
  101. },
  102. },
  103. 'bch': {
  104. '0conf0': {
  105. 'mmgen': ('283.22339437', '283.22339437'),
  106. 'nonmm': ('16.77647763', '116.77637483'),
  107. 'total': ('299.999872', '399.9997692'),
  108. },
  109. '0conf1': {
  110. 'mmgen': ('283.22339437', '0'),
  111. 'nonmm': ('16.77647763', '99.9998972'),
  112. 'total': ('299.999872', '99.9998972'),
  113. },
  114. '1conf1': {
  115. 'mmgen': ('0', '283.22339437'),
  116. 'nonmm': ('0', '116.77637483'),
  117. 'total': ('0', '399.9997692'),
  118. },
  119. '1conf2': {
  120. 'mmgen': ('0', '283.22339437', '0'),
  121. 'nonmm': ('0', '16.77647763', '99.9998972'),
  122. 'total': ('0', '299.999872', '99.9998972'),
  123. },
  124. },
  125. 'ltc': {
  126. '0conf0': {
  127. 'mmgen': ('283.21717237', '283.21717237'),
  128. 'nonmm': ('16.77647763', '5116.77036263'),
  129. 'total': ('299.99365', '5399.987535'),
  130. },
  131. '0conf1': {
  132. 'mmgen': ('283.21717237', '0'),
  133. 'nonmm': ('16.77647763', '5099.993885'),
  134. 'total': ('299.99365', '5099.993885'),
  135. },
  136. '1conf1': {
  137. 'mmgen': ('0', '283.21717237'),
  138. 'nonmm': ('0', '5116.77036263'),
  139. 'total': ('0', '5399.987535'),
  140. },
  141. '1conf2': {
  142. 'mmgen': ('0', '283.21717237', '0'),
  143. 'nonmm': ('0', '16.77647763', '5099.993885'),
  144. 'total': ('0', '299.99365', '5099.993885'),
  145. },
  146. }
  147. },
  148. 'rtBobOp3': {'btc':'S:2', 'bch':'L:3', 'ltc':'S:2'},
  149. 'rtAmts': {
  150. 'btc': ('500', '500'),
  151. 'bch': ('500', '560'),
  152. 'ltc': ('5500', '5500')
  153. }
  154. }
  155. def make_burn_addr(proto):
  156. from mmgen.tool.coin import tool_cmd
  157. return tool_cmd(
  158. cfg = cfg,
  159. cmdname = 'pubhash2addr',
  160. proto = proto,
  161. mmtype = 'compressed').pubhash2addr('00'*20)
  162. class CmdTestRegtest(CmdTestBase, CmdTestShared):
  163. 'transacting and tracking wallet operations via regtest mode'
  164. networks = ('btc', 'ltc', 'bch')
  165. passthru_opts = ('coin', 'rpc_backend')
  166. tmpdir_nums = [17]
  167. color = True
  168. deterministic = False
  169. test_rbf = False
  170. proto = None # pylint
  171. bdb_wallet = False
  172. cmd_group_in = (
  173. ('setup', 'regtest (Bob and Alice) mode setup'),
  174. ('subgroup.misc', []),
  175. ('subgroup.init_bob', []),
  176. ('subgroup.init_alice', []),
  177. ('subgroup.fund_users', ['init_bob', 'init_alice']),
  178. ('subgroup.msg', ['init_bob']),
  179. ('subgroup.twexport', ['fund_users']),
  180. ('subgroup.rescan', ['fund_users']),
  181. ('subgroup.errors', ['fund_users']),
  182. ('subgroup.main', ['fund_users']),
  183. ('subgroup.twprune', ['main']),
  184. ('subgroup.txhist', ['main']),
  185. ('subgroup.label', ['main']),
  186. ('subgroup.view', ['label']),
  187. ('subgroup._auto_chg_deps', ['twexport', 'label']),
  188. ('subgroup.auto_chg', ['_auto_chg_deps']),
  189. ('stop', 'stopping regtest daemon'),
  190. )
  191. cmd_subgroups = {
  192. 'misc': (
  193. 'miscellaneous commands',
  194. ('daemon_version', 'mmgen-tool daemon_version'),
  195. ('halving_calculator_bob', 'halving calculator (Bob)'),
  196. ),
  197. 'init_bob': (
  198. 'creating Bob’s MMGen wallet and tracking wallet',
  199. ('bob_twview_noaddrs', 'viewing Bob’s unspent outputs (error, no addrs)'),
  200. ('bob_listaddrs_noaddrs', 'viewing Bob’s addresses (error, no addrs)'),
  201. ('walletgen_bob', 'wallet generation (Bob)'),
  202. ('addrgen_bob', 'address generation (Bob)'),
  203. ('addrimport_bob', 'importing Bob’s addresses'),
  204. ('bob_twview_nobal', 'viewing Bob’s unspent outputs (error, no balance)'),
  205. ('bob_listaddrs_nobal', 'viewing Bob’s addresses (OK, no balance)'),
  206. ),
  207. 'init_alice': (
  208. 'creating Alice’s MMGen wallet and tracking wallet',
  209. ('walletgen_alice', 'wallet generation (Alice)'),
  210. ('addrgen_alice', 'address generation (Alice)'),
  211. ('addrimport_alice', 'importing Alice’s addresses'),
  212. ),
  213. 'fund_users': (
  214. 'funding Bob and Alice’s wallets',
  215. ('bob_import_miner_addr', 'importing miner’s coinbase addr into Bob’s wallet'),
  216. ('fund_bob_deterministic', 'funding Bob’s first MMGen address (deterministic method)'),
  217. ('fund_alice_deterministic', 'funding Alice’s first MMGen address (deterministic method)'),
  218. ('bob_recreate_tracking_wallet', 'creation of new tracking wallet (Bob)'),
  219. ('addrimport_bob2', 'reimporting Bob’s addresses'),
  220. ('fund_bob', 'funding Bob’s wallet'),
  221. ('fund_alice', 'funding Alice’s wallet'),
  222. ('generate', 'mining a block'),
  223. ('bob_bal1', 'Bob’s balance'),
  224. ('generate_extra_deterministic', 'generate extra blocks for deterministic run'),
  225. ),
  226. 'msg': (
  227. 'message signing',
  228. ('bob_msgcreate', 'creating a message file for signing'),
  229. ('bob_msgsign', 'signing the message file (default wallet)'),
  230. ('bob_walletconv_words', 'creating an MMGen mnemonic wallet'),
  231. ('bob_subwalletgen_bip39', 'creating a BIP39 mnemonic subwallet'),
  232. ('bob_msgsign_userwallet', 'signing the message file (user-specified wallet)'),
  233. ('bob_msgsign_userwallets', 'signing the message file (user-specified wallets)'),
  234. ('bob_msgverify', 'verifying the message file (all addresses)'),
  235. ('bob_msgverify_raw', 'verifying the raw message file (all addresses)'),
  236. ('bob_msgverify_single', 'verifying the message file (single address)'),
  237. ('bob_msgexport_single', 'exporting the message file (single address)'),
  238. ('bob_msgexport', 'exporting the message file (all addresses)'),
  239. ('bob_msgverify_export', 'verifying the exported JSON data (all addresses)'),
  240. ('bob_msgverify_export_single', 'verifying the exported JSON data (single address)'),
  241. ),
  242. 'twexport': (
  243. 'exporting and importing tracking wallet to JSON',
  244. ('bob_twexport', 'exporting a tracking wallet to JSON'),
  245. ('carol_twimport', 'importing a tracking wallet JSON dump'),
  246. ('carol_delete_wallet', 'unloading and deleting Carol’s tracking wallet'),
  247. ('bob_twexport_noamt', 'exporting a tracking wallet to JSON (include_amts=0)'),
  248. ('carol_twimport_nochksum', 'importing a tracking wallet JSON dump (ignore_checksum=1)'),
  249. ('carol_delete_wallet', 'unloading and deleting Carol’s tracking wallet'),
  250. ('carol_twimport_batch', 'importing a tracking wallet JSON dump (batch=1)'),
  251. ('bob_twexport_pretty', 'exporting a tracking wallet to JSON (pretty=1)'),
  252. ('bob_edit_json_twdump', 'editing a tracking wallet JSON dump'),
  253. ('carol_delete_wallet', 'unloading and deleting Carol’s tracking wallet'),
  254. ('carol_twimport_pretty', 'importing an edited tracking wallet JSON dump (ignore_checksum=1)'),
  255. ('carol_listaddresses', 'viewing Carol’s tracking wallet'),
  256. ('carol_delete_wallet', 'unloading and deleting Carol’s tracking wallet'),
  257. ),
  258. 'rescan': (
  259. 'rescanning address and blockchain',
  260. ('bob_resolve_addr', 'resolving an address in the tracking wallet'),
  261. ('bob_rescan_addr', 'rescanning an address'),
  262. ('bob_rescan_blockchain_all', 'rescanning the blockchain (full rescan)'),
  263. ('bob_rescan_blockchain_gb', 'rescanning the blockchain (Genesis block)'),
  264. ('bob_rescan_blockchain_one', 'rescanning the blockchain (single block)'),
  265. ('bob_rescan_blockchain_ss', 'rescanning the blockchain (range of blocks)'),
  266. ),
  267. 'errors': (
  268. 'various error conditions',
  269. ('bob_bad_locktime1', 'broadcast of transaction with bad locktime (block)'),
  270. ('bob_bad_locktime2', 'broadcast of transaction with bad locktime (integer size)'),
  271. ('bob_bad_locktime3', 'broadcast of transaction with bad locktime (time)'),
  272. ),
  273. 'main': (
  274. 'creating, signing, sending and bumping transactions',
  275. ('bob_add_comment1', 'adding an 80-screen-width label (lat+cyr+gr)'),
  276. ('bob_twview1', 'viewing Bob’s tracking wallet'),
  277. ('bob_split1', 'splitting Bob’s funds'),
  278. ('generate', 'mining a block'),
  279. ('bob_bal2', 'Bob’s balance'),
  280. ('bob_rbf_1output_create', 'creating RBF tx with one output'),
  281. ('bob_rbf_1output_bump', 'bumping RBF tx with one output'),
  282. ('bob_bal2a', 'Bob’s balance (age_fmt=confs)'),
  283. ('bob_bal2b', 'Bob’s balance (showempty=1)'),
  284. ('bob_bal2c', 'Bob’s balance (showempty=1 minconf=2 age_fmt=days)'),
  285. ('bob_bal2d', 'Bob’s balance (minconf=2)'),
  286. ('bob_bal2e', 'Bob’s balance (showempty=1 sort=age)'),
  287. ('bob_bal2f', 'Bob’s balance (showempty=1 sort=age,reverse)'),
  288. ('bob_send_maybe_rbf', 'sending funds to Alice (RBF, if supported)'),
  289. ('get_mempool1', 'mempool (before RBF bump)'),
  290. ('bob_rbf_status1', 'getting status of transaction'),
  291. ('bob_rbf_bump', 'bumping RBF transaction'),
  292. ('get_mempool2', 'mempool (after RBF bump)'),
  293. ('bob_rbf_status2', 'getting status of transaction after replacement'),
  294. ('bob_rbf_status3', 'getting status of replacement transaction (mempool)'),
  295. ('generate', 'mining a block'),
  296. ('bob_rbf_status4', 'getting status of transaction after confirmed (1) replacement'),
  297. ('bob_rbf_status5', 'getting status of replacement transaction (confirmed)'),
  298. ('generate', 'mining a block'),
  299. ('bob_rbf_status6', 'getting status of transaction after confirmed (2) replacement'),
  300. ('bob_bal3', 'Bob’s balance'),
  301. ('bob_pre_import', 'sending to non-imported address'),
  302. ('generate', 'mining a block'),
  303. ('bob_import_addr', 'importing non-MMGen address'),
  304. ('bob_bal4', 'Bob’s balance (after import)'),
  305. ('bob_import_list', 'importing flat address list'),
  306. ('bob_import_list_rescan', 'importing flat address list with --rescan'),
  307. ('bob_import_list_rescan_aio', 'importing flat address list with --rescan (aiohttp backend)'),
  308. ('bob_split2', 'splitting Bob’s funds'),
  309. ('bob_0conf0_getbalance', 'Bob’s balance (unconfirmed, minconf=0)'),
  310. ('bob_0conf1_getbalance', 'Bob’s balance (unconfirmed, minconf=1)'),
  311. ('generate', 'mining a block'),
  312. ('bob_1conf1_getbalance', 'Bob’s balance (confirmed, minconf=1)'),
  313. ('bob_1conf2_getbalance', 'Bob’s balance (confirmed, minconf=2)'),
  314. ('bob_bal5', 'Bob’s balance'),
  315. ('bob_send_non_mmgen', 'sending funds to Alice (from non-MMGen addrs)'),
  316. ('generate', 'mining a block'),
  317. ('alice_send_estimatefee', 'tx creation with no fee on command line'),
  318. ('generate', 'mining a block'),
  319. ('bob_bal6', 'Bob’s balance'),
  320. ('bob_subwallet_addrgen1', 'generating Bob’s addrs from subwallet 29L'),
  321. ('bob_subwallet_addrgen2', 'generating Bob’s addrs from subwallet 127S'),
  322. ('bob_subwallet_addrimport1', 'importing Bob’s addrs from subwallet 29L'),
  323. ('bob_subwallet_addrimport2', 'importing Bob’s addrs from subwallet 127S'),
  324. ('bob_subwallet_fund', 'funding Bob’s subwallet addrs'),
  325. ('generate', 'mining a block'),
  326. ('bob_twview2', 'viewing Bob’s tracking wallet'),
  327. ('bob_twview3', 'viewing Bob’s tracking wallet'),
  328. ('bob_subwallet_txcreate', 'creating a transaction with subwallet inputs'),
  329. ('bob_subwallet_txsign', 'signing a transaction with subwallet inputs'),
  330. ('bob_subwallet_txdo', 'sending from Bob’s subwallet addrs'),
  331. ('generate', 'mining a block'),
  332. ('bob_twview4', 'viewing Bob’s tracking wallet'),
  333. ('bob_alice_bal', 'Bob and Alice’s balances'),
  334. ('bob_nochg_burn', 'zero-change transaction to burn address'),
  335. ('generate', 'mining a block'),
  336. ),
  337. 'twprune': (
  338. 'exporting a pruned tracking wallet to JSON',
  339. ('bob_twprune_noask', 'pruning a tracking wallet'),
  340. ('bob_twprune_skip', 'pruning a tracking wallet (skip pruning)'),
  341. ('bob_twprune_all', 'pruning a tracking wallet (pruning all addrs)'),
  342. ('bob_twprune_skipamt', 'pruning a tracking wallet (skipping addrs with amt)'),
  343. ('bob_twprune_skipused', 'pruning a tracking wallet (skipping used addrs)'),
  344. ('bob_twprune_allamt', 'pruning a tracking wallet (pruning addrs with amt)'),
  345. ('bob_twprune_allused', 'pruning a tracking wallet (pruning used addrs)'),
  346. ('bob_twprune1', 'pruning a tracking wallet (selective prune)'),
  347. ('bob_twprune2', 'pruning a tracking wallet (selective prune)'),
  348. ('bob_twprune3', 'pruning a tracking wallet (selective prune)'),
  349. ('bob_twprune4', 'pruning a tracking wallet (selective prune)'),
  350. ('bob_twprune5', 'pruning a tracking wallet (selective prune)'),
  351. ('bob_twprune6', 'pruning a tracking wallet (selective prune)'),
  352. ),
  353. 'txhist': (
  354. 'viewing transaction history',
  355. ('bob_txhist1', 'viewing Bob’s transaction history (sort=age)'),
  356. ('bob_txhist2', 'viewing Bob’s transaction history (sort=blockheight reverse=1)'),
  357. ('bob_txhist3', 'viewing Bob’s transaction history (sort=blockheight sinceblock=-7)'),
  358. ('bob_txhist4', 'viewing Bob’s transaction history (detail=1)'),
  359. ('bob_txhist5', 'viewing Bob’s transaction history (sinceblock=399 detail=1)'),
  360. ('bob_txhist_interactive', 'viewing Bob’s transaction history (age_fmt=date_time interactive=true)'),
  361. ),
  362. 'label': (
  363. 'adding, removing and editing labels',
  364. ('alice_bal2', 'Alice’s balance'),
  365. ('alice_add_comment1', 'adding a label'),
  366. ('alice_chk_comment1', 'the label'),
  367. ('alice_add_comment2', 'adding a label'),
  368. ('alice_chk_comment2', 'the label'),
  369. ('alice_edit_comment1', 'editing a label (zh)'),
  370. ('alice_edit_comment2', 'editing a label (lat+cyr+gr)'),
  371. ('alice_chk_comment3', 'the label'),
  372. ('alice_remove_comment1', 'removing a label'),
  373. ('alice_chk_comment4', 'the label'),
  374. ('alice_add_comment_coinaddr', 'adding a label using the coin address'),
  375. ('alice_chk_comment_coinaddr', 'the label'),
  376. ('alice_add_comment_badaddr1', 'adding a label with invalid address'),
  377. ('alice_add_comment_badaddr2', 'adding a label with invalid address for this chain'),
  378. ('alice_add_comment_badaddr3', 'adding a label with wrong MMGen address'),
  379. ('alice_add_comment_badaddr4', 'adding a label with wrong coin address'),
  380. ),
  381. 'view': (
  382. 'viewing addresses and unspent outputs',
  383. ('alice_listaddresses_scroll', 'listaddresses (--scroll, interactive=1)'),
  384. ('alice_listaddresses_cashaddr', 'listaddresses (BCH cashaddr)'),
  385. ('alice_listaddresses_empty', 'listaddresses (no data)'),
  386. ('alice_listaddresses_menu', 'listaddresses (menu items)'),
  387. ('alice_listaddresses1', 'listaddresses'),
  388. ('alice_listaddresses_days', 'listaddresses (age_fmt=days)'),
  389. ('alice_listaddresses_date', 'listaddresses (age_fmt=date)'),
  390. ('alice_listaddresses_date_time', 'listaddresses (age_fmt=date_time)'),
  391. ('alice_twview1', 'twview'),
  392. ('alice_twview_days', 'twview (age_fmt=days)'),
  393. ('alice_twview_date', 'twview (age_fmt=date)'),
  394. ('alice_twview_date_time', 'twview (age_fmt=date_time)'),
  395. ('alice_twview_interactive_cashaddr', 'twview (interactive=1, BCH cashaddr)'),
  396. ('alice_txcreate_info', 'txcreate -i'),
  397. ('alice_txcreate_info_term', 'txcreate -i (pexpect_spawn)'),
  398. ('bob_send_to_alice_2addr', 'sending a TX to 2 addresses in Alice’s wallet'),
  399. ('bob_send_to_alice_reuse', 'sending a TX to a used address in Alice’s wallet'),
  400. ('generate', 'mining a block'),
  401. ('alice_twview_grouped', 'twview (testing ‘grouped’ option for TX and address)'),
  402. ),
  403. '_auto_chg_deps': (
  404. 'automatic change address selection dependencies',
  405. ('bob_auto_chg_split', 'splitting Bob’s funds (auto-chg-addr dependency)'),
  406. ('bob_auto_chg_generate', 'mining a block (auto-chg-addr dependency)'),
  407. ),
  408. 'auto_chg': (
  409. 'automatic change address selection',
  410. ('bob_auto_chg1', 'creating an automatic change address transaction (C)'),
  411. ('bob_auto_chg2', 'creating an automatic change address transaction (B)'),
  412. ('bob_auto_chg3', 'creating an automatic change address transaction (S)'),
  413. ('bob_auto_chg4', 'creating an automatic change address transaction (single address)'),
  414. ('bob_auto_chg_addrtype1', 'creating an automatic change address transaction by addrtype (C)'),
  415. ('bob_auto_chg_addrtype2', 'creating an automatic change address transaction by addrtype (B)'),
  416. ('bob_auto_chg_addrtype3', 'creating an automatic change address transaction by addrtype (S)'),
  417. ('bob_auto_chg_addrtype4', 'creating an automatic change address transaction by addrtype (single address)'),
  418. ('bob_add_comment_uua1', 'adding a comment for unused address in tracking wallet (C)'),
  419. ('bob_auto_chg5', 'creating an auto-chg-address TX, skipping unused address with label (C)'),
  420. ('bob_auto_chg_addrtype5', 'creating an auto-chg-address TX by addrtype, skipping unused address '
  421. 'with label (C)'),
  422. ('bob_auto_chg6', 'creating an auto-chg-address TX, using unused address with label (C)'),
  423. ('bob_auto_chg7', 'creating an automatic change address transaction (exclude cmdline output)'),
  424. ('bob_auto_chg_addrtype6', 'creating an auto-chg-address TX by addrtype, using unused address with '
  425. 'label (C)'),
  426. ('bob_remove_comment_uua1', 'removing a comment for unused address in tracking wallet (C)'),
  427. ('bob_auto_chg_bad1', 'error handling for auto change address transaction (bad ID FFFFFFFF:C)'),
  428. ('bob_auto_chg_bad2', 'error handling for auto change address transaction (bad ID 00000000:C)'),
  429. ('bob_auto_chg_bad3', 'error handling for auto change address transaction (no unused addresses)'),
  430. ('bob_auto_chg_bad4', 'error handling for auto change address transaction by addrtype '
  431. '(no unused addresses)'),
  432. ('bob_auto_chg_bad5', 'error handling (more than one chg address listed)'),
  433. ('bob_auto_chg_bad6', 'error handling for auto change address transaction '
  434. '(more than one chg address, mixed)'),
  435. ('bob_auto_chg_bad7', 'error handling for auto change address transaction '
  436. '(more than one chg address requested)'),
  437. ('carol_twimport2', 'recreating Carol’s tracking wallet from JSON dump'),
  438. ('carol_rescan_blockchain', 'rescanning the blockchain (full rescan)'),
  439. ('carol_auto_chg1', 'creating an automatic change address transaction (C)'),
  440. ('carol_auto_chg2', 'creating an automatic change address transaction (B)'),
  441. ('carol_auto_chg3', 'creating an automatic change address transaction (S)'),
  442. ('carol_auto_chg_addrtype1', 'creating an automatic change address transaction by addrtype (C)'),
  443. ('carol_auto_chg_addrtype2', 'creating an automatic change address transaction by addrtype (B)'),
  444. ('carol_auto_chg_addrtype3', 'creating an automatic change address transaction by addrtype (S)'),
  445. ('carol_auto_chg_addrtype4', 'creating an automatic change address transaction by addrtype (C) (exclude cmdline output)'),
  446. ('carol_auto_chg_bad1', 'error handling for auto change address transaction (no unused addresses)'),
  447. ('carol_auto_chg_bad2', 'error handling for auto change address transaction by addrtype '
  448. '(no unused addresses)'),
  449. ('carol_delete_wallet', 'unloading and deleting Carol’s tracking wallet'),
  450. ),
  451. }
  452. def __init__(self, trunner, cfgs, spawn):
  453. CmdTestBase.__init__(self, trunner, cfgs, spawn)
  454. if trunner is None:
  455. return
  456. if self.proto.testnet:
  457. die(2, '--testnet and --regtest options incompatible with regtest test suite')
  458. coin = self.coin
  459. self.proto = init_proto(cfg, coin, network='regtest', need_amt=True)
  460. gldict = globals()
  461. for k in rt_data:
  462. gldict[k] = rt_data[k][coin] if coin in rt_data[k] else None
  463. self.use_bdb_wallet = self.bdb_wallet or coin != 'btc'
  464. self.rt = MMGenRegtest(cfg, coin, bdb_wallet=self.use_bdb_wallet)
  465. if coin == 'btc':
  466. self.test_rbf = True # tests are non-coin-dependent, so run just once for BTC
  467. if cfg.test_suite_deterministic:
  468. self.deterministic = True
  469. self.spawn_env['MMGEN_BOGUS_SEND'] = ''
  470. self.write_to_tmpfile('wallet_password', rt_pw)
  471. self.dfl_mmtype = 'C' if coin == 'bch' else 'B'
  472. self.burn_addr = make_burn_addr(self.proto)
  473. self.user_sids = {}
  474. def _add_comments_to_addr_file(self, addrfile, outfile, use_comments=False):
  475. silence()
  476. gmsg(f'Adding comments to address file {addrfile!r}')
  477. a = AddrList(cfg, self.proto, addrfile)
  478. for n, idx in enumerate(a.idxs(), 1):
  479. if use_comments:
  480. a.set_comment(idx, get_comment())
  481. else:
  482. if n % 2:
  483. a.set_comment(idx, f'Test address {n}')
  484. a.file.format(add_comments=True)
  485. from mmgen.fileutil import write_data_to_file
  486. write_data_to_file(
  487. cfg,
  488. outfile = outfile,
  489. data = a.file.fmt_data,
  490. quiet = True,
  491. ignore_opt_outdir = True)
  492. end_silence()
  493. def setup(self):
  494. stop_test_daemons(self.proto.network_id, force=True, remove_datadir=True)
  495. from shutil import rmtree
  496. try:
  497. rmtree(joinpath(self.tr.data_dir, 'regtest'))
  498. except:
  499. pass
  500. t = self.spawn(
  501. 'mmgen-regtest',
  502. (['--bdb-wallet'] if self.use_bdb_wallet else [])
  503. + ['--setup-no-stop-daemon', 'setup'])
  504. t.expect('Starting')
  505. for _ in range(3): t.expect('Creating')
  506. for _ in range(5): t.expect('Mined')
  507. t.expect('Setup complete')
  508. return t
  509. def daemon_version(self):
  510. t = self.spawn('mmgen-tool', ['--bob', 'daemon_version'])
  511. t.expect('version')
  512. return t
  513. def halving_calculator_bob(self):
  514. t = self.spawn('halving-calculator.py', ['--bob'], cmd_dir='examples')
  515. t.expect('time until halving')
  516. return t
  517. def walletgen(self, user):
  518. t = self.spawn('mmgen-walletgen', ['-q', '-r0', '-p1', f'--{user}'], no_passthru_opts=True)
  519. t.passphrase_new(f'new {dfl_wcls.desc}', rt_pw)
  520. t.label()
  521. t.expect('move it to the data directory? (Y/n): ', 'y')
  522. t.written_to_file(capfirst(dfl_wcls.desc))
  523. return t
  524. def walletgen_bob(self):
  525. return self.walletgen('bob')
  526. def walletgen_alice(self):
  527. return self.walletgen('alice')
  528. def _user_dir(self, user, coin=None):
  529. return joinpath(self.tr.data_dir, 'regtest', user)
  530. def _user_sid(self, user):
  531. if user in self.user_sids:
  532. return self.user_sids[user]
  533. else:
  534. self.user_sids[user] = os.path.basename(get_file_with_ext(self._user_dir(user), 'mmdat'))[:8]
  535. return self.user_sids[user]
  536. def _get_user_subsid(self, user, subseed_idx):
  537. fn = get_file_with_ext(self._user_dir(user), dfl_wcls.ext)
  538. silence()
  539. w = Wallet(cfg, fn=fn, passwd_file=os.path.join(self.tmpdir, 'wallet_password'))
  540. end_silence()
  541. return w.seed.subseed(subseed_idx).sid
  542. def addrgen(
  543. self,
  544. user,
  545. wf = None,
  546. addr_range = '1-5',
  547. subseed_idx = None,
  548. mmtypes = []):
  549. from mmgen.addr import MMGenAddrType
  550. for mmtype in mmtypes or self.proto.mmtypes:
  551. t = self.spawn(
  552. 'mmgen-addrgen',
  553. ['--quiet', f'--{user}', f'--type={mmtype}', f'--outdir={self._user_dir(user)}']
  554. + ([wf] if wf else [])
  555. + ([f'--subwallet={subseed_idx}'] if subseed_idx else [])
  556. + [addr_range],
  557. extra_desc = '({})'.format(MMGenAddrType.mmtypes[mmtype].name))
  558. t.passphrase(dfl_wcls.desc, rt_pw)
  559. t.written_to_file('Addresses')
  560. ok_msg()
  561. t.skip_ok = True
  562. return t
  563. def addrgen_bob(self):
  564. return self.addrgen('bob')
  565. def addrgen_alice(self):
  566. return self.addrgen('alice')
  567. def addrimport(
  568. self,
  569. user,
  570. sid = None,
  571. addr_range = '1-5',
  572. num_addrs = 5,
  573. mmtypes = [],
  574. batch = True,
  575. quiet = True):
  576. id_strs = {'legacy':'', 'compressed':'-C', 'segwit':'-S', 'bech32':'-B'}
  577. if not sid:
  578. sid = self._user_sid(user)
  579. from mmgen.addr import MMGenAddrType
  580. for mmtype in mmtypes or self.proto.mmtypes:
  581. desc = MMGenAddrType.mmtypes[mmtype].name
  582. addrfile = joinpath(self._user_dir(user),
  583. '{}{}{}[{}]{x}.regtest.addrs'.format(
  584. sid, self.altcoin_pfx, id_strs[desc], addr_range,
  585. x='-α' if cfg.debug_utf8 else ''))
  586. if mmtype == self.proto.mmtypes[0] and user == 'bob':
  587. self._add_comments_to_addr_file(addrfile, addrfile, use_comments=True)
  588. t = self.spawn(
  589. 'mmgen-addrimport',
  590. args = (
  591. (['--quiet'] if quiet else []) +
  592. ['--'+user] +
  593. (['--batch'] if batch else []) +
  594. [addrfile]),
  595. extra_desc = f'({desc})')
  596. if cfg.debug:
  597. t.expect("Type uppercase 'YES' to confirm: ", 'YES\n')
  598. t.expect('Importing')
  599. if batch:
  600. t.expect(f'{num_addrs} addresses imported')
  601. else:
  602. t.expect('import completed OK')
  603. t.ok()
  604. t.skip_ok = True
  605. return t
  606. def addrimport_bob(self):
  607. return self.addrimport('bob')
  608. def addrimport_alice(self):
  609. return self.addrimport('alice', batch=False, quiet=False)
  610. async def bob_import_miner_addr(self):
  611. if not self.deterministic:
  612. return 'skip'
  613. return self.spawn(
  614. 'mmgen-addrimport',
  615. ['--bob', '--rescan', '--quiet', f'--address={await self.rt.miner_addr}'])
  616. async def fund_wallet_deterministic(self, addr, utxo_nums, skip_passphrase=False):
  617. """
  618. the deterministic funding method using specific inputs
  619. """
  620. if not self.deterministic:
  621. return 'skip'
  622. self.write_to_tmpfile('miner.key', f'{await self.rt.miner_wif}\n')
  623. keyfile = joinpath(self.tmpdir, 'miner.key')
  624. return self.user_txdo(
  625. 'bob', '40s',
  626. [f'{addr}, {rtFundAmt}', self.burn_addr],
  627. utxo_nums,
  628. extra_args = [f'--keys-from-file={keyfile}'],
  629. skip_passphrase = skip_passphrase)
  630. def fund_bob_deterministic(self):
  631. return self.fund_wallet_deterministic(f'{self._user_sid("bob")}:C:1', '1-11')
  632. def fund_alice_deterministic(self):
  633. sid = self._user_sid('alice')
  634. mmtype = ('L', 'S')[self.proto.cap('segwit')]
  635. addr = self.get_addr_from_addrlist('alice', sid, mmtype, 0, addr_range='1-5')
  636. return self.fund_wallet_deterministic(addr, '1-11', skip_passphrase=True)
  637. def generate_extra_deterministic(self):
  638. if not self.deterministic:
  639. return 'skip'
  640. return self.generate(num_blocks=2) # do this so block count matches non-deterministic run
  641. async def bob_recreate_tracking_wallet(self):
  642. if not self.deterministic:
  643. return 'skip'
  644. self.spawn('', msg_only=True)
  645. await self.rt.stop()
  646. from shutil import rmtree
  647. imsg('Deleting Bob’s old tracking wallet')
  648. rmtree(os.path.join(self.rt.d.datadir, 'regtest', 'wallets', 'bob'), ignore_errors=True)
  649. self.rt.init_daemon()
  650. self.rt.d.start(silent=True)
  651. imsg('Creating Bob’s new tracking wallet')
  652. await self.rt.create_wallet('bob')
  653. await self.rt.stop()
  654. await self.rt.start()
  655. return 'ok'
  656. def addrimport_bob2(self):
  657. if not self.deterministic:
  658. return 'skip'
  659. return self.addrimport('bob')
  660. def fund_wallet(self, user, mmtype, amt, sid=None, addr_range='1-5'):
  661. if self.deterministic:
  662. return 'skip'
  663. if not sid:
  664. sid = self._user_sid(user)
  665. addr = self.get_addr_from_addrlist(user, sid, mmtype, 0, addr_range=addr_range)
  666. t = self.spawn('mmgen-regtest', ['send', str(addr), str(amt)])
  667. t.expect(f'Sending {amt} miner {self.proto.coin}')
  668. t.expect('Mined 1 block')
  669. return t
  670. def fund_bob(self):
  671. return self.fund_wallet('bob', 'C', rtFundAmt)
  672. def fund_alice(self):
  673. return self.fund_wallet('alice', ('L', 'S')[self.proto.cap('segwit')], rtFundAmt)
  674. def user_twview(
  675. self,
  676. user,
  677. chk = None,
  678. expect = None,
  679. cmd = 'twview',
  680. opts = [],
  681. sort = 'age',
  682. exit_val = None):
  683. t = self.spawn('mmgen-tool', [f'--{user}'] + opts + [cmd] + [f'sort={sort}'], exit_val=exit_val)
  684. if chk:
  685. t.expect(r'{}\b.*\D{}\b'.format(*chk), regex=True)
  686. if expect:
  687. t.expect(expect, regex=True)
  688. return t
  689. def bob_twview_noaddrs(self):
  690. return self.user_twview('bob', expect='No spendable', exit_val=1)
  691. def bob_listaddrs_noaddrs(self):
  692. return self.user_twview('bob', cmd='listaddresses', expect='No addresses', exit_val=1)
  693. def bob_twview_nobal(self):
  694. return self.user_twview('bob', expect='No spendable', exit_val=1)
  695. def bob_listaddrs_nobal(self):
  696. return self.user_twview('bob', cmd='listaddresses', expect='TOTAL:')
  697. def bob_twview1(self):
  698. return self.user_twview('bob', chk=('1', rtAmts[0]))
  699. def user_bal(self, user, bal, opts=[], args=['showempty=1'], skip_check=False):
  700. t = self.spawn('mmgen-tool', opts + [f'--{user}', 'listaddresses'] + args)
  701. if not skip_check:
  702. cmp_or_die(f'{bal} {self.proto.coin}', strip_ansi_escapes(t.expect_getend('TOTAL: ')))
  703. return t
  704. def alice_bal1(self):
  705. return self.user_bal('alice', rtFundAmt)
  706. def alice_bal2(self):
  707. return self.user_bal('alice', rtBals[8])
  708. def bob_bal1(self):
  709. return self.user_bal('bob', rtFundAmt, self._cashaddr_opt(0))
  710. def bob_bal2(self):
  711. return self.user_bal('bob', rtBals[0], self._cashaddr_opt(1))
  712. def bob_bal2a(self):
  713. return self.user_bal('bob', rtBals[0], args=['showempty=1', 'age_fmt=confs'])
  714. def bob_bal2b(self):
  715. return self.user_bal('bob', rtBals[0], args=['showempty=1'])
  716. def bob_bal2c(self):
  717. return self.user_bal('bob', rtBals[0], args=['showempty=1', 'minconf=2', 'age_fmt=days'], skip_check=True)
  718. def bob_bal2d(self):
  719. return self.user_bal('bob', rtBals[0], args=['minconf=2'], skip_check=True)
  720. def bob_bal2e(self):
  721. return self.user_bal('bob', rtBals[0], args=['showempty=1', 'sort=amt'])
  722. def bob_bal2f(self):
  723. return self.user_bal('bob', rtBals[0], args=['showempty=0', 'sort=twmmid', 'reverse=1'])
  724. def bob_bal3(self):
  725. return self.user_bal('bob', rtBals[1])
  726. def bob_bal4(self):
  727. return self.user_bal('bob', rtBals[2])
  728. def bob_bal5(self):
  729. return self.user_bal('bob', rtBals[3])
  730. def bob_bal6(self):
  731. return self.user_bal('bob', rtBals[7])
  732. def bob_subwallet_addrgen1(self):
  733. return self.addrgen('bob', subseed_idx='29L', mmtypes=['C']) # 29L: 2FA7BBA8
  734. def bob_subwallet_addrgen2(self):
  735. return self.addrgen('bob', subseed_idx='127S', mmtypes=['C']) # 127S: '09E8E286'
  736. def subwallet_addrimport(self, user, subseed_idx):
  737. sid = self._get_user_subsid(user, subseed_idx)
  738. return self.addrimport(user, sid=sid, mmtypes=['C'])
  739. def bob_subwallet_addrimport1(self):
  740. return self.subwallet_addrimport('bob', '29L')
  741. def bob_subwallet_addrimport2(self):
  742. return self.subwallet_addrimport('bob', '127S')
  743. def bob_subwallet_fund(self):
  744. sid1 = self._get_user_subsid('bob', '29L')
  745. sid2 = self._get_user_subsid('bob', '127S')
  746. chg_addr = self._user_sid('bob') + (':B:1', ':L:1')[self.proto.coin=='BCH']
  747. return self.user_txdo(
  748. user = 'bob',
  749. fee = rtFee[1],
  750. outputs_cl = [sid1+':C:2,0.29', sid2+':C:3,0.127', chg_addr],
  751. outputs_list = ('3', '1')[self.proto.coin=='BCH'],
  752. extra_args = ['--subseeds=127'],
  753. used_chg_addr_resp = (None, 'y')[self.proto.coin=='BCH'])
  754. def bob_twview2(self):
  755. sid1 = self._get_user_subsid('bob', '29L')
  756. return self.user_twview(
  757. 'bob',
  758. opts = self._cashaddr_opt(0),
  759. chk = (f'{sid1}:C:2', '0.29'),
  760. sort = 'twmmid',
  761. expect = rf'[{b58a}]{{8}}' if self.proto.coin == 'BCH' else None)
  762. def bob_twview3(self):
  763. sid2 = self._get_user_subsid('bob', '127S')
  764. return self.user_twview(
  765. 'bob',
  766. opts = self._cashaddr_opt(1),
  767. chk = (f'{sid2}:C:3', '0.127'),
  768. sort = 'amt',
  769. expect = rf'[{b32a}]{{8}}' if self.proto.coin == 'BCH' else None)
  770. def bob_subwallet_txcreate(self):
  771. sid1 = self._get_user_subsid('bob', '29L')
  772. sid2 = self._get_user_subsid('bob', '127S')
  773. outputs_cl = [sid1+':C:5,0.0159', sid2+':C:5']
  774. t = self.spawn('mmgen-txcreate', ['-d', self.tmpdir, '-B', '--bob'] + outputs_cl)
  775. return self.txcreate_ui_common(t,
  776. menu = ['a'],
  777. inputs = ('1,2', '2,3')[self.proto.coin=='BCH'],
  778. interactive_fee = '0.00001')
  779. def bob_subwallet_txsign(self):
  780. fn = get_file_with_ext(self.tmpdir, 'rawtx')
  781. t = self.spawn('mmgen-txsign', ['-d', self.tmpdir, '--bob', '--subseeds=127', fn])
  782. t.view_tx('t')
  783. t.passphrase(dfl_wcls.desc, rt_pw)
  784. t.do_comment(None)
  785. t.expect('(Y/n): ', 'y')
  786. t.written_to_file('Signed transaction')
  787. return t
  788. def bob_subwallet_txdo(self):
  789. outputs_cl = [self._user_sid('bob')+':L:5']
  790. inputs = ('1,2', '2,3')[self.proto.coin=='BCH']
  791. return self.user_txdo(
  792. 'bob',
  793. rtFee[5],
  794. outputs_cl,
  795. inputs,
  796. menu = ['a'],
  797. extra_args = ['--subseeds=127']) # sort: amt
  798. def bob_twview4(self):
  799. sid = self._user_sid('bob')
  800. return self.user_twview('bob', chk=(sid+':L:5', rtBals[9]), sort='twmmid')
  801. def user_txhist(self, user, args, expect, opts=[], expect2=None):
  802. t = self.spawn('mmgen-tool', opts + [f'--{user}', 'txhist'] + args)
  803. text = t.read(strip_color=True)
  804. for s in (expect, expect2):
  805. if s:
  806. m = re.search(s, text, re.DOTALL)
  807. assert m, f'Expected: {s}'
  808. return t
  809. def bob_txhist1(self):
  810. return self.user_txhist('bob',
  811. opts = self._cashaddr_opt(1),
  812. args = ['sort=age'],
  813. expect = fr'\s1\).*\s{rtFundAmt}\s',
  814. expect2 = rf'[{b32a}]{{8}}' if self.proto.coin == 'BCH' else None)
  815. def bob_txhist2(self):
  816. return self.user_txhist('bob',
  817. opts = self._cashaddr_opt(0),
  818. args = ['sort=blockheight', 'reverse=1', 'age_fmt=block'],
  819. expect = fr'\s1\).*:{self.dfl_mmtype}:1\s',
  820. expect2 = rf'[{b58a}]{{8}}' if self.proto.coin == 'BCH' else None)
  821. def bob_txhist3(self):
  822. return self.user_txhist('bob',
  823. args = ['sort=blockheight', 'sinceblock=-7', 'age_fmt=block'],
  824. expect = fr'Displaying transactions since block 399.*\s6\)\s+405\s.*\s{rtBals[9]}\s.*:L:5.*\s7\)'
  825. )
  826. def bob_txhist4(self):
  827. return self.user_txhist('bob',
  828. args = ['sort=blockheight', 'age_fmt=block', 'detail=1'],
  829. expect = fr'Block:.*406.*Value:.*{rtBals[10]}'
  830. )
  831. def bob_txhist5(self):
  832. return self.user_txhist('bob',
  833. args = ['sort=blockheight', 'sinceblock=399', 'age_fmt=block', 'detail=1'],
  834. expect = fr'Displaying transactions since block 399.*\s7\).*Block:.*406.*Value:.*{rtBals[10]}'
  835. )
  836. def bob_txhist_interactive(self):
  837. self.get_file_with_ext('out', delete_all=True)
  838. t = self.spawn('mmgen-tool',
  839. ['--bob', f'--outdir={self.tmpdir}', 'txhist', 'age_fmt=date_time', 'interactive=true'])
  840. if self.proto.coin == 'BCH':
  841. for expect, resp in (
  842. (rf'[{b32a}]{{8}}', 'h'),
  843. (rf'[{b58a}]{{8}}', 'h')
  844. ):
  845. t.expect(expect, regex=True)
  846. t.expect('draw:\b', resp, regex=True)
  847. for resp in ('u', 'i', 't', 'a', 'm', 'T', 'A', 'r', 'r', 'D', 'D', 'D', 'D', 'p', 'P', 'n', 'V'):
  848. t.expect('draw:\b', resp, regex=True)
  849. if t.pexpect_spawn:
  850. t.expect(r'Block:.*394', regex=True)
  851. time.sleep(1)
  852. t.send('q')
  853. time.sleep(0.2)
  854. t.send('n')
  855. t.expect('draw:\b', 'q', regex=True)
  856. else:
  857. txnum, idx = (8, 1) if self.proto.coin == 'BCH' else (9, 3)
  858. t.expect(rf'\s{txnum}\).*Inputs:.*:L:{idx}.*Outputs \(3\):.*:C:2.*\s10\)', 'q', regex=True)
  859. return t
  860. def bob_getbalance(self, bals, confs=1):
  861. for i in range(len(bals['mmgen'])):
  862. assert Decimal(bals['mmgen'][i]) + Decimal(bals['nonmm'][i]) == Decimal(bals['total'][i])
  863. sid = self._user_sid('bob')
  864. t = self.spawn('mmgen-tool', ['--bob', 'getbalance', f'minconf={confs}'])
  865. t.expect('Wallet')
  866. for k, lbl in (
  867. ('mmgen', f'{sid}:'),
  868. ('nonmm', 'Non-MMGen:'),
  869. ('total', f'TOTAL {self.proto.coin}')
  870. ):
  871. ret = strip_ansi_escapes(t.expect_getend(lbl + ' '))
  872. cmp_or_die(
  873. ' '.join(bals[k]),
  874. ' '.join(ret.split()),
  875. desc = k,
  876. )
  877. return t
  878. def bob_0conf0_getbalance(self):
  879. return self.bob_getbalance(rtBals_gb['0conf0'], confs=0)
  880. def bob_0conf1_getbalance(self):
  881. return self.bob_getbalance(rtBals_gb['0conf1'], confs=1)
  882. def bob_1conf1_getbalance(self):
  883. return self.bob_getbalance(rtBals_gb['1conf1'], confs=1)
  884. def bob_1conf2_getbalance(self):
  885. return self.bob_getbalance(rtBals_gb['1conf2'], confs=2)
  886. def bob_nochg_burn(self):
  887. return self.user_txdo('bob',
  888. fee = '0.00009713',
  889. outputs_cl = [self.burn_addr],
  890. outputs_list = '1')
  891. def bob_alice_bal(self):
  892. t = self.spawn('mmgen-regtest', ['balances'])
  893. ret = t.expect_getend("Bob's balance:").strip()
  894. cmp_or_die(rtBals[4], ret)
  895. ret = t.expect_getend("Alice's balance:").strip()
  896. cmp_or_die(rtBals[5], ret)
  897. ret = t.expect_getend('Total balance:').strip()
  898. cmp_or_die(rtBals[6], ret)
  899. return t
  900. def user_txsend_status(
  901. self,
  902. user,
  903. tx_file,
  904. exp1 = '',
  905. exp2 = '',
  906. extra_args = [],
  907. exit_val = None):
  908. t = self.spawn(
  909. 'mmgen-txsend',
  910. ['-d', self.tmpdir, '--'+user, '--status'] + extra_args + [tx_file],
  911. exit_val = exit_val)
  912. if exp1:
  913. t.expect(exp1, regex=True)
  914. if exp2:
  915. t.expect(exp2, regex=True)
  916. if self.deterministic:
  917. imsg(
  918. 'DETERMINISTIC TESTING NOTE:\n '
  919. 'output of mmgen-txsend --status cannot be made deterministic, as it uses '
  920. 'gettransaction’s ‘timereceived’ field')
  921. return t
  922. def user_txdo(
  923. self,
  924. user,
  925. fee,
  926. outputs_cl,
  927. outputs_list,
  928. extra_args = [],
  929. wf = None,
  930. add_comment = tx_comment_jp,
  931. return_early = False,
  932. return_after_send = False,
  933. menu = ['M'],
  934. skip_passphrase = False,
  935. used_chg_addr_resp = None,
  936. exit_val = None):
  937. t = self.spawn(
  938. 'mmgen-txdo',
  939. ['-d', self.tmpdir, '-B', '--'+user]
  940. + (['--fee='+fee] if fee else [])
  941. + extra_args
  942. + ([], [wf])[bool(wf)]
  943. + outputs_cl,
  944. exit_val = exit_val)
  945. self.txcreate_ui_common(
  946. t,
  947. caller = 'txdo',
  948. menu = menu,
  949. inputs = outputs_list,
  950. file_desc = 'Signed transaction',
  951. interactive_fee = (tx_fee, '')[bool(fee)],
  952. add_comment = add_comment,
  953. return_early = return_early,
  954. view = 't',
  955. save = True,
  956. used_chg_addr_resp = used_chg_addr_resp)
  957. if return_early:
  958. return t
  959. if not skip_passphrase:
  960. t.passphrase(dfl_wcls.desc, rt_pw)
  961. t.written_to_file('Signed transaction')
  962. self._do_confirm_send(t)
  963. if return_after_send:
  964. return t
  965. t.expect('Transaction sent')
  966. return t
  967. def bob_split1(self):
  968. sid = self._user_sid('bob')
  969. outputs_cl = [sid+':C:1,100', sid+':L:2,200', sid+':'+rtBobOp3]
  970. return self.user_txdo('bob', rtFee[0], outputs_cl, '1', extra_args=['--locktime=500000001'])
  971. def get_addr_from_addrlist(self, user, sid, mmtype, idx, addr_range='1-5'):
  972. id_str = {'L':'', 'S':'-S', 'C':'-C', 'B':'-B'}[mmtype]
  973. ext = '{}{}{}[{}]{x}.regtest.addrs'.format(
  974. sid, self.altcoin_pfx, id_str, addr_range, x='-α' if cfg.debug_utf8 else '')
  975. addrfile = get_file_with_ext(self._user_dir(user), ext, no_dot=True)
  976. silence()
  977. addr = AddrList(cfg, self.proto, addrfile).data[idx].addr
  978. end_silence()
  979. return addr
  980. def _create_tx_outputs(self, user, data):
  981. sid = self._user_sid(user)
  982. return [self.get_addr_from_addrlist(user, sid, mmtype, idx-1)+amt_str for mmtype, idx, amt_str in data]
  983. def bob_rbf_1output_create(self):
  984. if not self.test_rbf:
  985. return 'skip'
  986. out_addr = self._create_tx_outputs('alice', (('B', 5, ''),))
  987. t = self.spawn('mmgen-txcreate', ['-d', self.tr.trash_dir, '-B', '--bob'] + out_addr)
  988. return self.txcreate_ui_common(t, menu=[], inputs='3', interactive_fee='3s') # out amt: 199.99999343
  989. def bob_rbf_1output_bump(self):
  990. if not self.test_rbf:
  991. return 'skip'
  992. ext = '9343,3]{x}.regtest.rawtx'.format(x='-α' if cfg.debug_utf8 else '')
  993. txfile = get_file_with_ext(self.tr.trash_dir, ext, delete=False, no_dot=True)
  994. return self.user_txbump('bob',
  995. self.tr.trash_dir,
  996. txfile,
  997. '8s',
  998. has_label = False,
  999. signed_tx = False,
  1000. one_output = True)
  1001. def bob_send_maybe_rbf(self):
  1002. outputs_cl = self._create_tx_outputs('alice', (('L', 1, ', 60'), ('C', 1, ', 40')))
  1003. outputs_cl += [self._user_sid('bob')+':'+rtBobOp3]
  1004. return self.user_txdo(
  1005. user = 'bob',
  1006. fee = rtFee[1],
  1007. outputs_cl = outputs_cl, # alice_sid:L:1,60, alice_sid:C:1,40
  1008. outputs_list = '3',
  1009. extra_args = [] if self.proto.cap('rbf') else ['--no-rbf'],
  1010. used_chg_addr_resp = 'y')
  1011. def bob_send_non_mmgen(self):
  1012. keyfile = joinpath(self.tmpdir, 'non-mmgen.keys')
  1013. atype = 'S' if self.proto.cap('segwit') else 'L'
  1014. outputs_cl = self._create_tx_outputs('alice', ((atype, 2, ', 10'), (atype, 3, '')))
  1015. return self.user_txdo(
  1016. user = 'bob',
  1017. fee = rtFee[3],
  1018. outputs_cl = outputs_cl, # alice_sid:S:2,10, alice_sid:S:3
  1019. outputs_list = '1,4-10',
  1020. extra_args = [f'--keys-from-file={keyfile}', '--vsize-adj=1.02'])
  1021. def alice_send_estimatefee(self):
  1022. outputs_cl = self._create_tx_outputs('bob', (('L', 1, ''),)) # bob_sid:L:1
  1023. return self.user_txdo('alice', None, outputs_cl, '1', extra_args=['--verbose']) # fee=None
  1024. def user_txbump(
  1025. self,
  1026. user,
  1027. outdir,
  1028. txfile,
  1029. fee,
  1030. add_args = [],
  1031. has_label = True,
  1032. signed_tx = True,
  1033. one_output = False):
  1034. if not self.proto.cap('rbf'):
  1035. return 'skip'
  1036. t = self.spawn('mmgen-txbump',
  1037. ['-d', outdir, '--'+user, '--fee='+fee, '--output-to-reduce=c'] + add_args + [txfile])
  1038. if not one_output:
  1039. t.expect('OK? (Y/n): ', 'y') # output OK?
  1040. t.expect('OK? (Y/n): ', 'y') # fee OK?
  1041. t.do_comment(False, has_label=has_label)
  1042. if signed_tx:
  1043. t.passphrase(dfl_wcls.desc, rt_pw)
  1044. t.written_to_file('Signed transaction')
  1045. self.txsend_ui_common(t, caller='txdo', bogus_send=False, file_desc='Signed transaction')
  1046. else:
  1047. t.expect('Save fee-bumped transaction? (y/N): ', 'y')
  1048. t.written_to_file('Fee-bumped transaction')
  1049. return t
  1050. def bob_rbf_bump(self):
  1051. ext = ',{}]{x}.regtest.sigtx'.format(rtFee[1][:-1], x='-α' if cfg.debug_utf8 else '')
  1052. txfile = self.get_file_with_ext(ext, delete=False, no_dot=True)
  1053. return self.user_txbump('bob', self.tmpdir, txfile, rtFee[2], add_args=['--send'])
  1054. def generate(self, num_blocks=1):
  1055. int(num_blocks)
  1056. t = self.spawn('mmgen-regtest', ['generate', str(num_blocks)])
  1057. t.expect(f'Mined {num_blocks} block')
  1058. return t
  1059. def _get_mempool(self):
  1060. ret = self.spawn(
  1061. 'mmgen-regtest',
  1062. ['mempool'],
  1063. env = os.environ if cfg.debug_utf8 else get_env_without_debug_vars(),
  1064. ).read()
  1065. m = re.search(r'(\[\s*"[a-f0-9]{64}"\s*])', ret) # allow for extra output by handler at end
  1066. return json.loads(m.group(1))
  1067. def get_mempool1(self):
  1068. mp = self._get_mempool()
  1069. if len(mp) != 1:
  1070. die(4, 'Mempool has more or less than one TX!')
  1071. self.write_to_tmpfile('rbf_txid', mp[0]+'\n')
  1072. return 'ok'
  1073. def bob_rbf_status(self, fee, exp1, exp2='', exit_val=None):
  1074. if not self.proto.cap('rbf'):
  1075. return 'skip'
  1076. ext = ',{}]{x}.regtest.sigtx'.format(fee[:-1], x='-α' if cfg.debug_utf8 else '')
  1077. txfile = self.get_file_with_ext(ext, delete=False, no_dot=True)
  1078. return self.user_txsend_status('bob', txfile, exp1, exp2, exit_val=exit_val)
  1079. def bob_rbf_status1(self):
  1080. if not self.proto.cap('rbf'):
  1081. return 'skip'
  1082. return self.bob_rbf_status(rtFee[1], 'in mempool, replaceable')
  1083. def get_mempool2(self):
  1084. if not self.proto.cap('rbf'):
  1085. return 'skip'
  1086. mp = self._get_mempool()
  1087. if len(mp) != 1:
  1088. die(4, 'Mempool has more or less than one TX!')
  1089. chk = self.read_from_tmpfile('rbf_txid')
  1090. if chk.strip() == mp[0]:
  1091. die(4, 'TX in mempool has not changed! RBF bump failed')
  1092. self.write_to_tmpfile('rbf_txid2', mp[0]+'\n')
  1093. return 'ok'
  1094. def bob_rbf_status2(self):
  1095. if not self.proto.cap('rbf'):
  1096. return 'skip'
  1097. new_txid = self.read_from_tmpfile('rbf_txid2').strip()
  1098. return self.bob_rbf_status(
  1099. rtFee[1],
  1100. 'Transaction has been replaced',
  1101. f'{new_txid} in mempool',
  1102. exit_val = 0)
  1103. def bob_rbf_status3(self):
  1104. if not self.proto.cap('rbf'):
  1105. return 'skip'
  1106. return self.bob_rbf_status(rtFee[2], 'status: in mempool, replaceable')
  1107. def bob_rbf_status4(self):
  1108. if not self.proto.cap('rbf'):
  1109. return 'skip'
  1110. new_txid = self.read_from_tmpfile('rbf_txid2').strip()
  1111. return self.bob_rbf_status(rtFee[1],
  1112. 'Replacement transaction has 1 confirmation',
  1113. rf'Replacing transactions:\s+{new_txid}')
  1114. def bob_rbf_status5(self):
  1115. if not self.proto.cap('rbf'):
  1116. return 'skip'
  1117. return self.bob_rbf_status(rtFee[2], 'Transaction has 1 confirmation')
  1118. def bob_rbf_status6(self):
  1119. if not self.proto.cap('rbf'):
  1120. return 'skip'
  1121. new_txid = self.read_from_tmpfile('rbf_txid2').strip()
  1122. return self.bob_rbf_status(rtFee[1],
  1123. 'Replacement transaction has 2 confirmations',
  1124. rf'Replacing transactions:\s+{new_txid}')
  1125. def _gen_pairs(self, n):
  1126. from mmgen.tool.api import tool_api
  1127. t = tool_api(cfg)
  1128. t.init_coin(self.proto.coin, self.proto.network)
  1129. def gen_addr(Type):
  1130. t.addrtype = Type
  1131. wif = t.hex2wif(getrandhex(32))
  1132. return (wif, t.wif2addr(wif))
  1133. return [gen_addr('legacy')] + [gen_addr('compressed') for i in range(n-1)]
  1134. def bob_pre_import(self):
  1135. pairs = self._gen_pairs(5)
  1136. self.write_to_tmpfile('non-mmgen.keys', '\n'.join([a[0] for a in pairs])+'\n')
  1137. self.write_to_tmpfile('non-mmgen.addrs', '\n'.join([a[1] for a in pairs])+'\n')
  1138. return self.user_txdo('bob', rtFee[4], [pairs[0][1]], '3')
  1139. def user_import(self, user, args, nAddr):
  1140. t = self.spawn('mmgen-addrimport', ['--'+user] + args)
  1141. if cfg.debug:
  1142. t.expect("Type uppercase 'YES' to confirm: ", 'YES\n')
  1143. t.expect(f'Importing {nAddr} address')
  1144. if '--rescan' in args:
  1145. if not '--quiet' in args:
  1146. t.expect('Continue? (Y/n): ', 'y')
  1147. t.expect('Rescanning block')
  1148. return t
  1149. def bob_import_addr(self):
  1150. addr = self.read_from_tmpfile('non-mmgen.addrs').split()[0]
  1151. return self.user_import('bob', ['--quiet', '--address='+addr], nAddr=1)
  1152. def bob_import_list_rescan_aio(self):
  1153. addrfile = joinpath(self.tmpdir, 'non-mmgen.addrs')
  1154. return self.user_import('bob', ['--rescan', '--rpc-backend=aio', '--addrlist', addrfile], nAddr=5)
  1155. def bob_resolve_addr(self):
  1156. mmaddr = '{}:C:1'.format(self._user_sid('bob'))
  1157. t = self.spawn('mmgen-tool', ['--bob', 'resolve_address', mmaddr])
  1158. coinaddr = re.search(r'[0-9A-Za-z]{30,}', t.read())[0]
  1159. t = self.spawn('mmgen-tool', ['--bob', 'resolve_address', coinaddr], no_msg=True)
  1160. mmaddr_res = re.search(r'[0-9A-F]{8}:C:1', t.read())[0]
  1161. assert mmaddr == mmaddr_res, f'{mmaddr} != {mmaddr_res}'
  1162. return t
  1163. def bob_import_list(self):
  1164. addrfile = joinpath(self.tmpdir, 'non-mmgen.addrs')
  1165. return self.user_import('bob', ['--quiet', '--addrlist', addrfile], nAddr=5)
  1166. def bob_import_list_rescan(self):
  1167. addrfile = joinpath(self.tmpdir, 'non-mmgen.addrs')
  1168. return self.user_import('bob', ['--quiet', '--rescan', '--addrlist', addrfile], nAddr=5)
  1169. def bob_rescan_addr(self):
  1170. sid = self._user_sid('bob')
  1171. t = self.spawn('mmgen-tool', ['--bob', 'rescan_address', f'{sid}:C:1'])
  1172. t.expect('Found 1 unspent output')
  1173. t.expect('updated successfully')
  1174. return t
  1175. def _usr_rescan_blockchain(self, user, add_args, expect=None):
  1176. t = self.spawn('mmgen-tool', [f'--{user}', 'rescan_blockchain'] + add_args)
  1177. if expect:
  1178. t.expect(f'Scanning blocks {expect}')
  1179. t.expect('Done')
  1180. return t
  1181. def bob_rescan_blockchain_all(self):
  1182. return self._usr_rescan_blockchain('bob', [], '300-396')
  1183. def bob_rescan_blockchain_gb(self):
  1184. return self._usr_rescan_blockchain('bob', ['start_block=0', 'stop_block=0'], '0-0')
  1185. def bob_rescan_blockchain_one(self):
  1186. return self._usr_rescan_blockchain('bob', ['start_block=300', 'stop_block=300'], '300-300')
  1187. def bob_rescan_blockchain_ss(self):
  1188. return self._usr_rescan_blockchain('bob', ['start_block=300', 'stop_block=302'], '300-302')
  1189. def bob_twexport(self, add_args=[]):
  1190. t = self.spawn('mmgen-tool', ['--bob', f'--outdir={self.tmpdir}', 'twexport'] + add_args)
  1191. t.written_to_file('JSON data')
  1192. return t
  1193. def bob_twexport_noamt(self):
  1194. return self.bob_twexport(add_args=['include_amts=0'])
  1195. def bob_twexport_pretty(self):
  1196. return self.bob_twexport(add_args=['pretty=1'])
  1197. def _bob_twprune(
  1198. self,
  1199. prune_spec,
  1200. npruned,
  1201. expect_menu = (),
  1202. expect = (),
  1203. expect2 = (),
  1204. warn_used = False,
  1205. non_segwit_ok = False):
  1206. if not (non_segwit_ok or self.proto.cap('segwit')):
  1207. return 'skip'
  1208. t = self.spawn(
  1209. 'mmgen-tool',
  1210. ['--bob', f'--outdir={self.tr.trash_dir}', 'twexport', 'prune=1']
  1211. + (['warn_used=1'] if warn_used else []))
  1212. for s in expect_menu:
  1213. t.expect('prune list:\b', s)
  1214. t.expect('prune list:\b', 'p')
  1215. t.expect('addresses to prune: ', f'{prune_spec}\n')
  1216. for p, s in expect:
  1217. t.expect(p, s, regex=True)
  1218. t.expect('prune list:\b', 'q')
  1219. for p, s in expect2:
  1220. t.expect(p, s, regex=True)
  1221. if npruned:
  1222. t.expect(f'Pruned {npruned} addresses')
  1223. taddr = 35 if self.proto.cap('segwit') else 25
  1224. t.expect(f'Exporting {taddr-npruned} addresses')
  1225. t.written_to_file('JSON data')
  1226. return t
  1227. def bob_twprune_noask(self):
  1228. return self._bob_twprune(
  1229. expect_menu = 'a', # sort by amt to make address order deterministic
  1230. prune_spec = '35,12,18,3-5',
  1231. npruned = 6)
  1232. def bob_twprune_all(self):
  1233. taddr = 35 if self.proto.cap('segwit') else 25
  1234. return self._bob_twprune(
  1235. prune_spec = f'1-{taddr}',
  1236. npruned = taddr,
  1237. expect = [('all with balance: ', 'P')],
  1238. non_segwit_ok = True)
  1239. def bob_twprune_skip(self):
  1240. return self._bob_twprune(
  1241. prune_spec = '',
  1242. npruned = 0,
  1243. non_segwit_ok = True)
  1244. def bob_twprune_skipamt(self):
  1245. return self._bob_twprune(
  1246. prune_spec = '1-35',
  1247. npruned = 32,
  1248. expect = [('all with balance: ', 'S')])
  1249. def bob_twprune_skipused(self):
  1250. return self._bob_twprune(
  1251. prune_spec = '1-35',
  1252. npruned = 18,
  1253. expect = [('all used: ', 'S')],
  1254. warn_used = True)
  1255. def bob_twprune_allamt(self):
  1256. return self._bob_twprune(
  1257. prune_spec = '1-35',
  1258. npruned = 35,
  1259. expect = [('all with balance: ', 'P')],
  1260. expect2 = [('Warning: pruned address .* has a balance', None)])
  1261. def bob_twprune_allused(self):
  1262. return self._bob_twprune(
  1263. prune_spec = '1-35',
  1264. npruned = 32,
  1265. expect = [('all used: ', 'P'), ('all with balance: ', 'S')],
  1266. expect2 = [('Warning: pruned address .* used', None)],
  1267. warn_used = True)
  1268. @property
  1269. def _b_start(self):
  1270. """
  1271. SIDs sort non-deterministically, so we must search for start of main (not subseeds) group, i.e. ':B:1'
  1272. """
  1273. assert self.proto.cap('segwit')
  1274. if not hasattr(self, '_b_start_'):
  1275. t = self.spawn('mmgen-tool', ['--color=0', '--bob', 'listaddresses'], no_msg=True)
  1276. self._b_start_ = int([e for e in t.read().split('\n') if ':B:1' in e][0].split()[0].rstrip(')'))
  1277. t.close()
  1278. return self._b_start_
  1279. def _bob_twprune_selected(self, resp, npruned):
  1280. if not self.proto.cap('segwit'):
  1281. return 'skip'
  1282. B = self._b_start
  1283. a, b, c, d, e, f = resp
  1284. return self._bob_twprune(
  1285. expect_menu = 'a', # sort by amt to make address order deterministic
  1286. prune_spec = f'31-32,{B+14},{B+9},{B}-{B+4}',
  1287. npruned = npruned,
  1288. expect = [
  1289. ('all used: ', a),
  1290. ('all used: ', b),
  1291. ('all with balance: ', c),
  1292. ('all with balance: ', d),
  1293. ('all used: ', e),
  1294. ('all used: ', f),
  1295. ],
  1296. warn_used = True)
  1297. def bob_twprune1(self):
  1298. return self._bob_twprune_selected(resp='sssssS', npruned=3)
  1299. def bob_twprune2(self):
  1300. return self._bob_twprune_selected(resp='sppPsS', npruned=3)
  1301. def bob_twprune3(self):
  1302. return self._bob_twprune_selected(resp='sssPpS', npruned=3)
  1303. def bob_twprune4(self):
  1304. return self._bob_twprune_selected(resp='sssPpP', npruned=9)
  1305. def bob_twprune5(self):
  1306. return self._bob_twprune_selected(resp='pppPpP', npruned=9)
  1307. def bob_twprune6(self):
  1308. return self._bob_twprune_selected(resp='sssSpP', npruned=7)
  1309. def bob_edit_json_twdump(self):
  1310. self.spawn('', msg_only=True)
  1311. from mmgen.tw.json import TwJSON
  1312. fn = TwJSON.Base(cfg, self.proto).dump_fn
  1313. text = json.loads(self.read_from_tmpfile(fn))
  1314. text['data']['entries'][3][3] = f'edited comment [фубар] [{gr_uc}]'
  1315. self.write_to_tmpfile(fn, json.dumps(text, indent=4))
  1316. return 'ok'
  1317. def carol_twimport(
  1318. self,
  1319. rpc_backend = 'http',
  1320. add_parms = [],
  1321. expect_str = None,
  1322. expect_str2 = 'Found 1 unspent output'):
  1323. from mmgen.tw.json import TwJSON
  1324. fn = joinpath(self.tmpdir, TwJSON.Base(cfg, self.proto).dump_fn)
  1325. t = self.spawn(
  1326. 'mmgen-tool',
  1327. ([f'--rpc-backend={rpc_backend}'] if rpc_backend else [])
  1328. + ['--carol', 'twimport', fn]
  1329. + add_parms)
  1330. t.expect('(y/N): ', 'y')
  1331. if expect_str:
  1332. t.expect(expect_str)
  1333. elif 'batch=true' in add_parms:
  1334. t.expect('{} addresses imported'.format(10 if self.proto.coin == 'BCH' else 20))
  1335. else:
  1336. t.expect('import completed OK')
  1337. t.expect(expect_str2)
  1338. return t
  1339. def carol_twimport_nochksum(self):
  1340. return self.carol_twimport(rpc_backend=None, add_parms=['ignore_checksum=true'])
  1341. def carol_twimport_batch(self):
  1342. return self.carol_twimport(add_parms=['batch=true'])
  1343. def carol_twimport_pretty(self):
  1344. return self.carol_twimport(add_parms=['ignore_checksum=true'], expect_str='ignoring incorrect checksum')
  1345. def carol_listaddresses(self):
  1346. return self.spawn('mmgen-tool', ['--carol', 'listaddresses', 'showempty=1'])
  1347. async def carol_delete_wallet(self):
  1348. imsg('Unloading Carol’s tracking wallet')
  1349. if self.proto.coin == 'BCH':
  1350. time.sleep(0.2)
  1351. t = self.spawn('mmgen-regtest', ['cli', 'unloadwallet', 'carol'])
  1352. t.ok()
  1353. wdir = joinpath((await self.rt.rpc).daemon.network_datadir, 'wallets', 'carol')
  1354. from shutil import rmtree
  1355. imsg('Deleting Carol’s tracking wallet')
  1356. rmtree(wdir)
  1357. return 'silent'
  1358. def bob_split2(self):
  1359. addrs = self.read_from_tmpfile('non-mmgen.addrs').split()
  1360. amts = (1.12345678, 2.87654321, 3.33443344, 4.00990099, 5.43214321)
  1361. outputs1 = list(map('{},{}'.format, addrs, amts))
  1362. sid = self._user_sid('bob')
  1363. l1, l2 = (
  1364. (':S', ':B') if 'B' in self.proto.mmtypes else
  1365. (':S', ':S') if self.proto.cap('segwit') else
  1366. (':L', ':L'))
  1367. outputs2 = [sid+':C:2,6.333', sid+':L:3,6.667', sid+l1+':4,0.123', sid+l2+':5']
  1368. return self.user_txdo('bob', rtFee[5], outputs1+outputs2, '1-2')
  1369. def user_add_comment(self, user, addr, comment):
  1370. t = self.spawn('mmgen-tool', ['--'+user, 'add_label', addr, comment])
  1371. t.expect('Added label.*in tracking wallet', regex=True)
  1372. return t
  1373. def user_remove_comment(self, user, addr):
  1374. t = self.spawn('mmgen-tool', ['--'+user, 'remove_label', addr])
  1375. t.expect('Removed label.*in tracking wallet', regex=True)
  1376. return t
  1377. def bob_bad_locktime1(self):
  1378. return self._bob_bad_locktime(123456789, 'non-final', 2) # > current block height
  1379. def bob_bad_locktime2(self):
  1380. return self._bob_bad_locktime(7_000_000_000, 'invalid', 2, return_early=True) # > 4 bytes
  1381. def bob_bad_locktime3(self):
  1382. return self._bob_bad_locktime(0xffffffff, 'non-final', 2, return_early=False) # > cur time
  1383. def _bob_bad_locktime(self, locktime, expect, exit_val, return_early=False):
  1384. sid = self._user_sid('bob')
  1385. t = self.user_txdo(
  1386. user = 'bob',
  1387. fee = '20s',
  1388. outputs_cl = [self.burn_addr+', 0.1', sid+':C:5'],
  1389. outputs_list = '1',
  1390. extra_args = [f'--locktime={locktime}'],
  1391. return_early = return_early,
  1392. add_comment = False,
  1393. return_after_send = True,
  1394. exit_val = exit_val)
  1395. if expect:
  1396. t.expect(expect)
  1397. return t
  1398. def bob_add_comment1(self):
  1399. sid = self._user_sid('bob')
  1400. return self.user_add_comment('bob', sid+':C:1', tw_comment_lat_cyr_gr)
  1401. def alice_add_comment1(self):
  1402. sid = self._user_sid('alice')
  1403. return self.user_add_comment('alice', sid+':C:1', 'Original Label - 月へ')
  1404. def alice_add_comment2(self):
  1405. sid = self._user_sid('alice')
  1406. return self.user_add_comment('alice', sid+':C:1', 'Replacement Label')
  1407. def _user_chk_comment(self, user, addr, comment, extra_args=[]):
  1408. t = self.spawn('mmgen-tool', ['--'+user, 'listaddresses', 'all_labels=1']+extra_args)
  1409. ret = strip_ansi_escapes(t.expect_getend(addr)).strip().split(None, 2)[2]
  1410. cmp_or_die( # squeezed display, double-width chars, so truncate to min field width
  1411. ret[:3].strip(),
  1412. comment[:3].strip())
  1413. return t
  1414. def alice_add_comment_coinaddr(self):
  1415. mmid = self._user_sid('alice') + (':S:1', ':L:1')[self.proto.coin=='BCH']
  1416. t = self.spawn('mmgen-tool', ['--alice', 'listaddress', mmid, 'wide=true'], no_msg=True)
  1417. addr = [i for i in strip_ansi_escapes(t.read()).splitlines() if re.search(rf'\b{mmid}\b', i)][0].split()[3]
  1418. return self.user_add_comment('alice', addr, 'Label added using coin address of MMGen address')
  1419. def alice_chk_comment_coinaddr(self):
  1420. mmid = self._user_sid('alice') + (':S:1', ':L:1')[self.proto.coin=='BCH']
  1421. return self._user_chk_comment('alice', mmid, 'Label added using coin address of MMGen address')
  1422. def alice_add_comment_badaddr(self, addr, reply, exit_val):
  1423. if os.getenv('PYTHONOPTIMIZE'):
  1424. omsg(yellow(f'PYTHONOPTIMIZE set, skipping test {self.test_name!r}'))
  1425. return 'skip'
  1426. t = self.spawn(
  1427. 'mmgen-tool',
  1428. ['--alice', 'add_label', addr, '(none)'],
  1429. exit_val = exit_val)
  1430. t.expect(reply, regex=True)
  1431. return t
  1432. def alice_add_comment_badaddr1(self):
  1433. return self.alice_add_comment_badaddr(rt_pw, 'invalid address', 2)
  1434. def alice_add_comment_badaddr2(self):
  1435. # mainnet zero address:
  1436. addr = init_proto(cfg, self.proto.coin, network='mainnet').pubhash2addr(bytes(20), 'p2pkh')
  1437. return self.alice_add_comment_badaddr(addr, 'invalid address', 2)
  1438. def alice_add_comment_badaddr3(self):
  1439. addr = self._user_sid('alice') + ':C:123'
  1440. return self.alice_add_comment_badaddr(addr, f'MMGen address {addr!r} not found in tracking wallet', 2)
  1441. def alice_add_comment_badaddr4(self):
  1442. addr = self.proto.pubhash2addr(bytes(20), 'p2pkh') # regtest (testnet) zero address
  1443. return self.alice_add_comment_badaddr(addr, f'Coin address {addr!r} not found in tracking wallet', 2)
  1444. def alice_remove_comment1(self):
  1445. sid = self._user_sid('alice')
  1446. mmid = sid + (':S:3', ':L:3')[self.proto.coin=='BCH']
  1447. return self.user_remove_comment('alice', mmid)
  1448. def alice_chk_comment1(self):
  1449. sid = self._user_sid('alice')
  1450. return self._user_chk_comment('alice', sid+':C:1', 'Original Label - 月へ')
  1451. def alice_chk_comment2(self):
  1452. sid = self._user_sid('alice')
  1453. return self._user_chk_comment('alice', sid+':C:1', 'Replacement Label', extra_args=['age_fmt=block'])
  1454. def alice_edit_comment1(self):
  1455. return self.user_edit_comment('alice', '4', tw_comment_lat_cyr_gr)
  1456. def alice_edit_comment2(self):
  1457. return self.user_edit_comment('alice', '3', tw_comment_zh)
  1458. def alice_chk_comment3(self):
  1459. sid = self._user_sid('alice')
  1460. mmid = sid + (':S:3', ':L:3')[self.proto.coin=='BCH']
  1461. return self._user_chk_comment('alice', mmid, tw_comment_lat_cyr_gr, extra_args=['age_fmt=date'])
  1462. def alice_chk_comment4(self):
  1463. sid = self._user_sid('alice')
  1464. mmid = sid + (':S:3', ':L:3')[self.proto.coin=='BCH']
  1465. return self._user_chk_comment('alice', mmid, '-', extra_args=['age_fmt=date_time'])
  1466. def user_edit_comment(self, user, output, comment):
  1467. t = self.spawn('mmgen-txcreate', ['-B', '--'+user, '-i'])
  1468. t.expect(r'add \[l\]abel:.', 'M', regex=True)
  1469. t.expect(r'add \[l\]abel:.', 'l', regex=True)
  1470. t.expect(r'Enter unspent.*return to main menu\):.', output+'\n', regex=True)
  1471. t.expect(r'Enter label text.*:.', comment+'\n', regex=True)
  1472. t.expect(r'\[q\]uit menu, .*?:.', 'q', regex=True)
  1473. return t
  1474. def alice_listaddresses_scroll(self):
  1475. t = self.spawn(
  1476. 'mmgen-tool', [
  1477. '--alice',
  1478. '--scroll',
  1479. f'--outdir={self.tr.trash_dir}',
  1480. 'listaddresses',
  1481. 'interactive=1',
  1482. ]
  1483. )
  1484. prompt = 'abel:\b'
  1485. t.expect(prompt, 'p')
  1486. ret = t.expect([prompt, 'to confirm: '])
  1487. if ret == 1:
  1488. t.send('YES\n')
  1489. t.expect(prompt)
  1490. t.send('l')
  1491. t.expect(
  1492. 'main menu): ',
  1493. '{}\n'.format(2 if self.proto.coin == 'BCH' else 1))
  1494. t.expect('for address.*: ', '\n', regex=True)
  1495. t.expect('unchanged')
  1496. t.expect(prompt, 'q')
  1497. return t
  1498. def _alice_listaddresses_interactive(self, expect=(), expect_menu=()):
  1499. t = self.spawn('mmgen-tool', ['--alice', 'listaddresses', 'interactive=1'])
  1500. prompt = 'abel:\b'
  1501. for e in expect:
  1502. t.expect(*e, regex=True)
  1503. for s in expect_menu:
  1504. t.expect(prompt, s)
  1505. return t
  1506. def alice_listaddresses_cashaddr(self):
  1507. if self.proto.coin != 'BCH':
  1508. return 'skip'
  1509. prompt = 'abel:\b'
  1510. expect = (
  1511. [rf'[{b32a}]{{8}}'],
  1512. [prompt, 'h'],
  1513. [rf'[{b58a}]{{8}}'],
  1514. [prompt, 'q']
  1515. )
  1516. return self._alice_listaddresses_interactive(expect=expect)
  1517. def alice_listaddresses_empty(self):
  1518. return self._alice_listaddresses_interactive(expect_menu='uuEq')
  1519. def alice_listaddresses_menu(self):
  1520. return self._alice_listaddresses_interactive(expect_menu='aAMrDDDDLeq')
  1521. def alice_listaddresses(self, args, expect):
  1522. t = self.spawn('mmgen-tool', ['--alice', 'listaddresses', 'showempty=1'] + args)
  1523. expect_str = r'\D{}\D.*\b{}'.format(*expect)
  1524. t.expect(expect_str, regex=True)
  1525. return t
  1526. def alice_listaddresses1(self):
  1527. return self.alice_listaddresses(
  1528. args = [],
  1529. expect = (rtAmts[1], r'\d+'))
  1530. def alice_listaddresses_days(self):
  1531. return self.alice_listaddresses(
  1532. args = ['age_fmt=days'],
  1533. expect = (rtAmts[1], r'\d+'))
  1534. def alice_listaddresses_date(self):
  1535. return self.alice_listaddresses(
  1536. args = ['age_fmt=date'],
  1537. expect = (rtAmts[1], pat_date))
  1538. def alice_listaddresses_date_time(self):
  1539. return self.alice_listaddresses(
  1540. args = ['age_fmt=date_time'],
  1541. expect = (rtAmts[1], pat_date_time))
  1542. def alice_twview(self, args, expect):
  1543. t = self.spawn('mmgen-tool', ['--alice', 'twview'] + args)
  1544. expect_str = r'\D{}\D.*\b{}'.format(*expect)
  1545. t.expect(expect_str, regex=True)
  1546. return t
  1547. def alice_twview1(self):
  1548. return self.alice_twview(
  1549. args = [],
  1550. expect = (rtAmts[0], r'\d+'))
  1551. def alice_twview_days(self):
  1552. return self.alice_twview(
  1553. args = ['age_fmt=days'],
  1554. expect = (rtAmts[0], r'\d+'))
  1555. def alice_twview_date(self):
  1556. return self.alice_twview(
  1557. args = ['age_fmt=date'],
  1558. expect = (rtAmts[0], pat_date))
  1559. def alice_twview_date_time(self):
  1560. return self.alice_twview(
  1561. args = ['age_fmt=date_time'],
  1562. expect = (rtAmts[0], pat_date_time))
  1563. def alice_twview_interactive_cashaddr(self):
  1564. if self.proto.coin != 'BCH':
  1565. return 'skip'
  1566. t = self.spawn('mmgen-tool', ['--alice', 'twview', 'interactive=true'])
  1567. prompt = 'abel:\b'
  1568. t.expect(rf'[{b32a}]{{8}}', regex=True)
  1569. t.expect(prompt, 'h')
  1570. t.expect(rf'[{b58a}]{{8}}', regex=True)
  1571. t.expect(prompt, 'q')
  1572. return t
  1573. def alice_txcreate_info(self, pexpect_spawn=False):
  1574. t = self.spawn('mmgen-txcreate', ['--alice', '-Bi'], pexpect_spawn=pexpect_spawn)
  1575. pats = (
  1576. (r'\d+', 'w'),
  1577. (r'\d+', 'D'),
  1578. (r'\d+', 'D'),
  1579. (r'\d+', 'D'),
  1580. (pat_date, 'q'))
  1581. for d, s in pats:
  1582. t.expect(
  1583. r'\D{}\D.*\b{}\b'.format(rtAmts[0], d),
  1584. s,
  1585. regex=True)
  1586. if t.pexpect_spawn and s == 'w':
  1587. t.expect(r'Total.*', 'q', regex=True, delay=1 if cfg.exact_output else t.send_delay)
  1588. time.sleep(t.send_delay)
  1589. t.send('e')
  1590. return t
  1591. def alice_txcreate_info_term(self):
  1592. if self.skip_for_win('no pexpect_spawn'):
  1593. return 'skip'
  1594. return self.alice_txcreate_info(pexpect_spawn=True)
  1595. # send one TX to 2 addrs in Alice’s wallet - required for alice_twview_grouped() (group by TxID)
  1596. def bob_send_to_alice_2addr(self):
  1597. outputs_cl = self._create_tx_outputs('alice', [('C', 1, ',0.02'), ('C', 2, ',0.2')])
  1598. outputs_cl += [self._user_sid('bob')+':C:5']
  1599. return self.user_txdo('bob', '25s', outputs_cl, '1')
  1600. # send to a used addr in Alice’s wallet - required for alice_twview_grouped() (group by address)
  1601. def bob_send_to_alice_reuse(self):
  1602. outputs_cl = self._create_tx_outputs('alice', [('C', 1, ',0.0111')])
  1603. outputs_cl += [self._user_sid('bob')+':C:5']
  1604. return self.user_txdo('bob', '25s', outputs_cl, '1')
  1605. def alice_twview_grouped(self):
  1606. t = self.spawn('mmgen-tool', ['--alice', 'twview', 'interactive=1'])
  1607. prompt = 'abel:\b'
  1608. for grouped, send in (
  1609. (False, 'o'), # 'o' = group display
  1610. (False, 'M'), # grouped address
  1611. (True, 't'), # grouped TxID
  1612. (True, 'q')):
  1613. if grouped:
  1614. t.expect('........')
  1615. t.expect(prompt, send)
  1616. return t
  1617. def bob_msgcreate(self):
  1618. sid1 = self._user_sid('bob')
  1619. sid2 = self._get_user_subsid('bob', '29L')
  1620. return self.spawn(
  1621. 'mmgen-msg', [
  1622. '--bob',
  1623. f'--outdir={self.tmpdir}',
  1624. 'create',
  1625. '16/3/2022 Earthquake strikes Fukushima coast',
  1626. f'{sid1}:{self.dfl_mmtype}:1-4',
  1627. f'{sid2}:C:3-7,87,98'
  1628. ])
  1629. def bob_msgsign(self, wallets=[]):
  1630. fn = get_file_with_ext(self.tmpdir, 'rawmsg.json')
  1631. t = self.spawn(
  1632. 'mmgen-msg', [
  1633. '--bob',
  1634. f'--outdir={self.tmpdir}',
  1635. 'sign',
  1636. fn
  1637. ] + wallets)
  1638. if not wallets:
  1639. t.passphrase(dfl_wcls.desc, rt_pw)
  1640. return t
  1641. def bob_walletconv_words(self):
  1642. t = self.spawn(
  1643. 'mmgen-walletconv',
  1644. ['--bob', f'--outdir={self.tmpdir}', '--out-fmt=words'],
  1645. no_passthru_opts = True)
  1646. t.passphrase(dfl_wcls.desc, rt_pw)
  1647. t.written_to_file('data')
  1648. return t
  1649. def bob_subwalletgen_bip39(self):
  1650. t = self.spawn(
  1651. 'mmgen-subwalletgen',
  1652. ['--bob', f'--outdir={self.tmpdir}', '--out-fmt=bip39', '29L'],
  1653. no_passthru_opts = True)
  1654. t.passphrase(dfl_wcls.desc, rt_pw)
  1655. t.written_to_file('data')
  1656. return t
  1657. def bob_msgsign_userwallet(self):
  1658. fn1 = get_file_with_ext(self.tmpdir, 'mmwords')
  1659. return self.bob_msgsign([fn1])
  1660. def bob_msgsign_userwallets(self):
  1661. fn1 = get_file_with_ext(self.tmpdir, 'mmwords')
  1662. fn2 = get_file_with_ext(self.tmpdir, 'bip39')
  1663. return self.bob_msgsign([fn2, fn1])
  1664. def bob_msgverify(
  1665. self,
  1666. addr = None,
  1667. ext = 'sigmsg.json',
  1668. cmd = 'verify',
  1669. msgfile = None,
  1670. exit_val = None):
  1671. return self.spawn(
  1672. 'mmgen-msg', [
  1673. '--bob',
  1674. f'--outdir={self.tmpdir}',
  1675. cmd,
  1676. msgfile or get_file_with_ext(self.tmpdir, ext),
  1677. ]
  1678. + ([addr] if addr else []),
  1679. exit_val = exit_val)
  1680. def bob_msgverify_raw(self):
  1681. t = self.bob_msgverify(ext='rawmsg.json', exit_val=1)
  1682. t.expect('No signatures')
  1683. return t
  1684. def bob_msgverify_single(self):
  1685. sid = self._user_sid('bob')
  1686. return self.bob_msgverify(addr=f'{sid}:{self.dfl_mmtype}:1')
  1687. def bob_msgexport(self, addr=None):
  1688. t = self.bob_msgverify(addr=addr, cmd='export')
  1689. t.written_to_file('data')
  1690. return t
  1691. def bob_msgexport_single(self):
  1692. sid = self._user_sid('bob')
  1693. return self.bob_msgexport(addr=f'{sid}:{self.dfl_mmtype}:1')
  1694. def bob_msgverify_export(self):
  1695. return self.bob_msgverify(
  1696. msgfile = os.path.join(self.tmpdir, 'signatures.json')
  1697. )
  1698. def bob_msgverify_export_single(self):
  1699. sid = self._user_sid('bob')
  1700. mmid = f'{sid}:{self.dfl_mmtype}:1'
  1701. args = ['--bob', '--color=0', 'listaddress', mmid, 'wide=true']
  1702. imsg(f'Running mmgen-tool {fmt_list(args,fmt="bare")}')
  1703. t = self.spawn('mmgen-tool', args, no_msg=True)
  1704. addr = t.expect_getend(mmid).split()[1]
  1705. t.close()
  1706. return self.bob_msgverify(
  1707. addr = addr,
  1708. msgfile = os.path.join(self.tmpdir, 'signatures.json')
  1709. )
  1710. def bob_auto_chg_split(self):
  1711. if not self.proto.cap('segwit'):
  1712. return 'skip'
  1713. sid = self._user_sid('bob')
  1714. return self.user_txdo(
  1715. user = 'bob',
  1716. fee = '23s',
  1717. outputs_cl = [sid+':C:5,0.0135', sid+':L:4'],
  1718. outputs_list = '1')
  1719. def bob_auto_chg_generate(self):
  1720. if not self.proto.cap('segwit'):
  1721. return 'skip'
  1722. return self.generate()
  1723. def _usr_auto_chg(
  1724. self,
  1725. user,
  1726. mmtype,
  1727. idx,
  1728. by_mmtype = False,
  1729. include_dest = True,
  1730. ignore_labels = False,
  1731. add_args = []):
  1732. if mmtype in ('S', 'B') and not self.proto.cap('segwit'):
  1733. return 'skip'
  1734. sid = self._user_sid('bob')
  1735. t = self.spawn(
  1736. 'mmgen-txcreate',
  1737. [f'--outdir={self.tr.trash_dir}', '--no-blank', f'--{user}']
  1738. + (['--autochg-ignore-labels'] if ignore_labels else [])
  1739. + [mmtype if by_mmtype else f'{sid}:{mmtype}']
  1740. + ([self.burn_addr+',0.01'] if include_dest else [])
  1741. + add_args
  1742. )
  1743. return self.txcreate_ui_common(t,
  1744. menu = [],
  1745. inputs = '1',
  1746. interactive_fee = '20s',
  1747. auto_chg_addr = f'{sid}:{mmtype}:{idx}')
  1748. def bob_auto_chg1(self):
  1749. return self._usr_auto_chg('bob', 'C', '3')
  1750. def bob_auto_chg2(self):
  1751. return self._usr_auto_chg('bob', 'B', '2')
  1752. def bob_auto_chg3(self):
  1753. return self._usr_auto_chg('bob', 'S', '1')
  1754. def bob_auto_chg4(self):
  1755. return self._usr_auto_chg('bob', 'C', '3', include_dest=False)
  1756. def bob_auto_chg_addrtype1(self):
  1757. return self._usr_auto_chg('bob', 'C', '3', True)
  1758. def bob_auto_chg_addrtype2(self):
  1759. return self._usr_auto_chg('bob', 'B', '2', True)
  1760. def bob_auto_chg_addrtype3(self):
  1761. return self._usr_auto_chg('bob', 'S', '1', True)
  1762. def bob_auto_chg_addrtype4(self):
  1763. return self._usr_auto_chg('bob', 'C', '3', True, include_dest=False)
  1764. def _bob_add_comment_uua(self, addrspec, comment):
  1765. sid = self._user_sid('bob')
  1766. return self.user_add_comment('bob', sid+addrspec, comment)
  1767. def bob_add_comment_uua1(self):
  1768. return self._bob_add_comment_uua(':C:3', 'comment for unused address')
  1769. def bob_auto_chg5(self):
  1770. return self._usr_auto_chg('bob', 'C', '4')
  1771. def bob_auto_chg_addrtype5(self):
  1772. return self._usr_auto_chg('bob', 'C', '4', True)
  1773. def bob_auto_chg6(self):
  1774. return self._usr_auto_chg('bob', 'C', '3', ignore_labels=True)
  1775. def bob_auto_chg7(self):
  1776. sid = self._user_sid('bob')
  1777. return self._usr_auto_chg('bob', 'S', '3', add_args=[f'{sid}:S:1,0.00345'])
  1778. def bob_auto_chg_addrtype6(self):
  1779. return self._usr_auto_chg('bob', 'C', '3', True, ignore_labels=True)
  1780. def _bob_remove_comment_uua(self, addrspec):
  1781. sid = self._user_sid('bob')
  1782. return self.user_remove_comment('bob', sid+addrspec)
  1783. def bob_remove_comment_uua1(self):
  1784. return self._bob_remove_comment_uua(':C:3')
  1785. def _usr_auto_chg_bad(self, user, al_id, expect, add_args=[]):
  1786. t = self.spawn(
  1787. 'mmgen-txcreate',
  1788. ['-d', self.tr.trash_dir, '-B', f'--{user}']
  1789. + [f'{self.burn_addr},0.01']
  1790. + ([al_id] if al_id else [])
  1791. + add_args,
  1792. exit_val = 2)
  1793. t.expect(expect)
  1794. return t
  1795. def bob_auto_chg_bad1(self):
  1796. return self._usr_auto_chg_bad(
  1797. 'bob',
  1798. 'FFFFFFFF:C',
  1799. 'contains no addresses')
  1800. def bob_auto_chg_bad2(self):
  1801. return self._usr_auto_chg_bad(
  1802. 'bob',
  1803. '00000000:C',
  1804. 'contains no addresses')
  1805. def bob_auto_chg_bad3(self):
  1806. return self._usr_auto_chg_bad(
  1807. 'bob',
  1808. self._user_sid('bob') + ':L',
  1809. 'contains no unused addresses from address list')
  1810. def bob_auto_chg_bad4(self):
  1811. return self._usr_auto_chg_bad(
  1812. 'bob',
  1813. 'L',
  1814. 'contains no unused addresses of address type')
  1815. def bob_auto_chg_bad5(self):
  1816. sid = self._user_sid('bob')
  1817. return self._usr_auto_chg_bad(
  1818. 'bob',
  1819. None,
  1820. 'More than one change address listed',
  1821. add_args = [f'{sid}:C:4', f'{sid}:C:5'])
  1822. def bob_auto_chg_bad6(self):
  1823. sid = self._user_sid('bob')
  1824. return self._usr_auto_chg_bad(
  1825. 'bob',
  1826. 'L',
  1827. 'More than one',
  1828. add_args = [f'{sid}:C:4'])
  1829. def bob_auto_chg_bad7(self):
  1830. return self._usr_auto_chg_bad(
  1831. 'bob',
  1832. 'L',
  1833. 'More than one change address requested',
  1834. add_args = ['B' if self.proto.cap('segwit') else 'C'])
  1835. def carol_twimport2(self):
  1836. u, b = (4, 3) if self.proto.cap('segwit') else (3, 2)
  1837. return self.carol_twimport(
  1838. rpc_backend = None,
  1839. add_parms = ['ignore_checksum=true'],
  1840. expect_str2 = f'Found {u} unspent outputs in {b} blocks')
  1841. def carol_rescan_blockchain(self):
  1842. return self._usr_rescan_blockchain('carol', [])
  1843. def carol_auto_chg1(self):
  1844. return self._usr_auto_chg('carol', 'C', '3')
  1845. def carol_auto_chg2(self):
  1846. return self._usr_auto_chg('carol', 'B', '2')
  1847. def carol_auto_chg3(self):
  1848. return self._usr_auto_chg('carol', 'S', '1')
  1849. def carol_auto_chg_addrtype1(self):
  1850. return self._usr_auto_chg('carol', 'C', '3', True)
  1851. def carol_auto_chg_addrtype2(self):
  1852. return self._usr_auto_chg('carol', 'B', '2', True)
  1853. def carol_auto_chg_addrtype3(self):
  1854. return self._usr_auto_chg('carol', 'S', '1', True)
  1855. def carol_auto_chg_addrtype4(self):
  1856. sid = self._user_sid('bob')
  1857. return self._usr_auto_chg('carol', 'S', '3', True, add_args=[f'{sid}:S:1,0.00345'])
  1858. def carol_auto_chg_bad1(self):
  1859. return self._usr_auto_chg_bad(
  1860. 'carol',
  1861. self._user_sid('bob') + ':L',
  1862. 'contains no unused addresses from address list')
  1863. def carol_auto_chg_bad2(self):
  1864. return self._usr_auto_chg_bad(
  1865. 'carol',
  1866. 'L',
  1867. 'contains no unused addresses of address type')
  1868. def stop(self):
  1869. self.spawn('', msg_only=True)
  1870. if cfg.no_daemon_stop:
  1871. msg_r('(leaving regtest daemon running by user request)')
  1872. imsg('')
  1873. else:
  1874. stop_test_daemons(self.proto.network_id, remove_datadir=True)
  1875. ok()
  1876. return 'ok'
  1877. class CmdTestRegtestBDBWallet(CmdTestRegtest):
  1878. bdb_wallet = True