ct_xmrwallet.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  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_py_d.ct_xmrwallet: xmrwallet tests for the cmdtest.py test suite
  20. """
  21. import sys,os,time,re,atexit,asyncio,shutil
  22. from subprocess import run,PIPE
  23. from collections import namedtuple
  24. from mmgen.util import msg,fmt,async_run,capfirst,is_int,die,list_gen
  25. from mmgen.obj import MMGenRange
  26. from mmgen.amt import XMRAmt
  27. from mmgen.addrlist import ViewKeyAddrList,KeyAddrList,AddrIdxList
  28. from ..include.common import (
  29. cfg,
  30. omsg,
  31. oqmsg_r,
  32. ok,
  33. imsg,
  34. imsg_r,
  35. write_data_to_file,
  36. read_from_file,
  37. silence,
  38. end_silence,
  39. strip_ansi_escapes
  40. )
  41. from .common import get_file_with_ext
  42. from .ct_base import CmdTestBase
  43. # atexit functions:
  44. def stop_daemons(self):
  45. for v in self.users.values():
  46. if '--restricted-rpc' in v.md.start_cmd:
  47. v.md.stop()
  48. else:
  49. async_run(v.md_rpc.stop_daemon())
  50. def stop_miner_wallet_daemon(self):
  51. async_run(self.users['miner'].wd_rpc.stop_daemon())
  52. def kill_proxy(cls,args):
  53. if sys.platform == 'linux':
  54. omsg(f'Killing SSH SOCKS server at localhost:{cls.socks_port}')
  55. cmd = [ 'pkill', '-f', ' '.join(args) ]
  56. run(cmd)
  57. class CmdTestXMRWallet(CmdTestBase):
  58. """
  59. Monero wallet operations
  60. """
  61. networks = ('xmr',)
  62. passthru_opts = ()
  63. tmpdir_nums = [29]
  64. dfl_random_txs = 3
  65. color = True
  66. socks_port = 49237
  67. # Bob’s daemon is stopped via process kill, not RPC, so put Bob last in list:
  68. user_data = (
  69. ('miner', '98831F3A', False, 130, '1-2', []),
  70. ('alice', 'FE3C6545', False, 150, '1-4', []),
  71. ('bob', '1378FC64', False, 140, None, ['--restricted-rpc']),
  72. )
  73. tx_relay_user = 'bob'
  74. datadir_base = os.path.join('test','daemons','xmrtest')
  75. cmd_group = (
  76. ('daemon_version', 'checking daemon version'),
  77. ('gen_kafiles', 'generating key-address files'),
  78. ('create_wallets_miner', 'creating Monero wallets (Miner)'),
  79. ('set_label_miner', 'setting an address label (Miner, primary account)'),
  80. ('mine_initial_coins', 'mining initial coins'),
  81. ('create_wallets_alice', 'creating Monero wallets (Alice)'),
  82. ('fund_alice', 'sending funds'),
  83. ('sync_wallets_all', 'syncing all wallets'),
  84. ('new_account_alice', 'creating a new account (Alice)'),
  85. ('new_account_alice_label', 'creating a new account (Alice, with label)'),
  86. ('new_address_alice', 'creating a new address (Alice)'),
  87. ('new_address_alice_label', 'creating a new address (Alice, with label)'),
  88. ('remove_label_alice', 'removing an address label (Alice, subaddress)'),
  89. ('set_label_alice', 'setting an address label (Alice, subaddress)'),
  90. ('sync_wallets_selected', 'syncing selected wallets'),
  91. ('sweep_to_address_proxy', 'sweeping to new address (via TX relay + proxy)'),
  92. ('sweep_to_account', 'sweeping to new account'),
  93. ('sweep_to_address_noproxy', 'sweeping to new address (via TX relay, no proxy)'),
  94. ('transfer_to_miner_proxy', 'transferring funds to Miner (via TX relay + proxy)'),
  95. ('transfer_to_miner_noproxy', 'transferring funds to Miner (via TX relay, no proxy)'),
  96. ('transfer_to_miner_create1', 'transferring funds to Miner (create TX)'),
  97. ('transfer_to_miner_send1', 'transferring funds to Miner (send TX via proxy)'),
  98. ('transfer_to_miner_create2', 'transferring funds to Miner (create TX)'),
  99. ('transfer_to_miner_send2', 'transferring funds to Miner (send TX, no proxy)'),
  100. ('sweep_create_and_send', 'sweeping to new account (create TX + send TX, in stages)'),
  101. ('list_wallets_all', 'listing wallets'),
  102. ('stop_daemons', 'stopping all wallet and coin daemons'),
  103. )
  104. def __init__(self,trunner,cfgs,spawn):
  105. CmdTestBase.__init__(self,trunner,cfgs,spawn)
  106. if trunner is None:
  107. return
  108. from mmgen.protocol import init_proto
  109. self.proto = init_proto( cfg, 'XMR', network='mainnet' )
  110. self.extra_opts = ['--wallet-rpc-password=passw0rd']
  111. self.init_users()
  112. self.init_daemon_args()
  113. for v in self.users.values():
  114. run(['mkdir','-p',v.udir])
  115. self.tx_relay_daemon_parm = 'localhost:{}'.format( self.users[self.tx_relay_user].md.rpc_port )
  116. self.tx_relay_daemon_proxy_parm = (
  117. self.tx_relay_daemon_parm + f':127.0.0.1:{self.socks_port}' ) # must be IP, not 'localhost'
  118. if not cfg.no_daemon_stop:
  119. atexit.register(stop_daemons,self)
  120. atexit.register(stop_miner_wallet_daemon,self)
  121. if not cfg.no_daemon_autostart:
  122. stop_daemons(self)
  123. time.sleep(0.2)
  124. if os.path.exists(self.datadir_base):
  125. shutil.rmtree(self.datadir_base)
  126. os.makedirs(self.datadir_base)
  127. self.start_daemons()
  128. self.init_proxy()
  129. self.balance = None
  130. # init methods
  131. @classmethod
  132. def init_proxy(cls,external_call=False):
  133. def port_in_use(port):
  134. import socket
  135. try:
  136. socket.create_connection(('localhost',port)).close()
  137. except:
  138. return False
  139. else:
  140. return True
  141. def start_proxy():
  142. if external_call or not cfg.no_daemon_autostart:
  143. run(a+b2)
  144. omsg(f'SSH SOCKS server started, listening at localhost:{cls.socks_port}')
  145. debug_file = os.path.join('' if external_call else cls.datadir_base,'txrelay-proxy.debug')
  146. a = ['ssh','-x','-o','ExitOnForwardFailure=True','-D',f'localhost:{cls.socks_port}']
  147. b0 = ['-o','PasswordAuthentication=False']
  148. b1 = ['localhost','true']
  149. b2 = ['-fN','-E', debug_file, 'localhost']
  150. if port_in_use(cls.socks_port):
  151. omsg(f'Port {cls.socks_port} already in use. Assuming SSH SOCKS server is running')
  152. else:
  153. cp = run(a+b0+b1,stdout=PIPE,stderr=PIPE)
  154. err = cp.stderr.decode()
  155. if err:
  156. omsg(err)
  157. if cp.returncode == 0:
  158. start_proxy()
  159. elif 'onnection refused' in err:
  160. die(2,fmt("""
  161. The SSH daemon must be running and listening on localhost in order to test
  162. XMR TX relaying via SOCKS proxy. If sshd is not running, please start it.
  163. Otherwise, add the line 'ListenAddress 127.0.0.1' to your sshd_config, and
  164. then restart the daemon.
  165. """,indent=' '))
  166. elif 'ermission denied' in err:
  167. msg(fmt(f"""
  168. In order to test XMR TX relaying via SOCKS proxy, it’s desirable to enable
  169. SSH to localhost without a password, which is not currently supported by
  170. your configuration. Your possible courses of action:
  171. 1. Continue by answering 'y' at this prompt, and enter your system password
  172. at the following prompt;
  173. 2. Exit the test here, add your user SSH public key to your user
  174. 'authorized_keys' file, and restart the test; or
  175. 3. Exit the test here, start the SSH SOCKS proxy manually by entering the
  176. following command, and restart the test:
  177. {' '.join(a+b2)}
  178. """,indent=' ',strip_char='\t'))
  179. from mmgen.ui import keypress_confirm
  180. if keypress_confirm(cfg,'Continue?'):
  181. start_proxy()
  182. else:
  183. die(1,'Exiting at user request')
  184. else:
  185. die(2,fmt(f"""
  186. Please start the SSH SOCKS proxy by entering the following command:
  187. {' '.join(a+b2)}
  188. Then restart the test.
  189. """,indent=' '))
  190. if not (external_call or cfg.no_daemon_stop):
  191. atexit.unregister(kill_proxy)
  192. atexit.register(kill_proxy, cls, a + b2)
  193. return True
  194. def init_users(self):
  195. from mmgen.daemon import CoinDaemon
  196. from mmgen.proto.xmr.daemon import MoneroWalletDaemon
  197. from mmgen.proto.xmr.rpc import MoneroRPCClient,MoneroWalletRPCClient
  198. self.users = {}
  199. tmpdir_num = self.tmpdir_nums[0]
  200. ud = namedtuple('user_data',[
  201. 'sid',
  202. 'mmwords',
  203. 'autosign',
  204. 'udir',
  205. 'datadir',
  206. 'kal_range',
  207. 'kafile',
  208. 'walletfile_fs',
  209. 'addrfile_fs',
  210. 'md',
  211. 'md_rpc',
  212. 'wd',
  213. 'wd_rpc',
  214. 'add_coind_args',
  215. ])
  216. # kal_range must be None, a single digit, or a single hyphenated range
  217. for ( user,
  218. sid,
  219. autosign,
  220. shift,
  221. kal_range,
  222. add_coind_args ) in self.user_data:
  223. tmpdir = os.path.join('test','tmp',str(tmpdir_num))
  224. udir = os.path.join(tmpdir,user)
  225. datadir = os.path.join(self.datadir_base,user)
  226. md = CoinDaemon(
  227. cfg = cfg,
  228. proto = self.proto,
  229. test_suite = True,
  230. port_shift = shift,
  231. opts = ['online'],
  232. datadir = datadir
  233. )
  234. md_rpc = MoneroRPCClient(
  235. cfg = cfg,
  236. proto = self.proto,
  237. host = 'localhost',
  238. port = md.rpc_port,
  239. user = None,
  240. passwd = None,
  241. test_connection = False,
  242. daemon = md,
  243. )
  244. wd = MoneroWalletDaemon(
  245. cfg = cfg,
  246. proto = self.proto,
  247. test_suite = True,
  248. wallet_dir = udir,
  249. user = 'foo',
  250. passwd = 'bar',
  251. port_shift = shift,
  252. monerod_addr = f'127.0.0.1:{md.rpc_port}',
  253. )
  254. wd_rpc = MoneroWalletRPCClient(
  255. cfg = cfg,
  256. daemon = wd,
  257. test_connection = False,
  258. )
  259. if autosign:
  260. kafile_suf = 'vkeys'
  261. fn_stem = 'MoneroWatchOnlyWallet'
  262. kafile_dir = self.asi_online.xmr_dir
  263. else:
  264. kafile_suf = 'akeys'
  265. fn_stem = 'MoneroWallet'
  266. kafile_dir = udir
  267. self.users[user] = ud(
  268. sid = sid,
  269. mmwords = f'test/ref/{sid}.mmwords',
  270. autosign = autosign,
  271. udir = udir,
  272. datadir = datadir,
  273. kal_range = kal_range,
  274. kafile = f'{kafile_dir}/{sid}-XMR-M[{kal_range}].{kafile_suf}',
  275. walletfile_fs = f'{udir}/{sid}-{{}}-{fn_stem}',
  276. addrfile_fs = f'{udir}/{sid}-{{}}-{fn_stem}.address.txt',
  277. md = md,
  278. md_rpc = md_rpc,
  279. wd = wd,
  280. wd_rpc = wd_rpc,
  281. add_coind_args = add_coind_args,
  282. )
  283. def init_daemon_args(self):
  284. common_args = ['--p2p-bind-ip=127.0.0.1','--fixed-difficulty=1','--regtest'] # ,'--rpc-ssl-allow-any-cert']
  285. for u in self.users:
  286. other_ports = [self.users[u2].md.p2p_port for u2 in self.users if u2 != u]
  287. node_args = [f'--add-exclusive-node=127.0.0.1:{p}' for p in other_ports]
  288. self.users[u].md.usr_coind_args = (
  289. common_args +
  290. node_args +
  291. self.users[u].add_coind_args )
  292. # cmd_group methods
  293. def daemon_version(self):
  294. rpc_port = self.users['miner'].md.rpc_port
  295. return self.spawn( 'mmgen-tool', ['--coin=xmr', f'--rpc-port={rpc_port}', 'daemon_version'] )
  296. def gen_kafiles(self):
  297. for user,data in self.users.items():
  298. if data.autosign or not data.kal_range:
  299. continue
  300. run(['mkdir','-p',data.udir])
  301. run(f'rm -f {data.kafile}',shell=True)
  302. t = self.spawn(
  303. 'mmgen-keygen', [
  304. '-q', '--accept-defaults', '--coin=xmr',
  305. f'--outdir={data.udir}', data.mmwords, data.kal_range
  306. ],
  307. extra_desc = f'({capfirst(user)})' )
  308. t.read()
  309. t.ok()
  310. t.skip_ok = True
  311. return t
  312. def create_wallets_miner(self):
  313. return self.create_wallets('miner')
  314. def create_wallets_alice(self):
  315. return self.create_wallets('alice')
  316. def create_wallets(self,user,wallet=None,add_opts=[],op='create'):
  317. assert wallet is None or is_int(wallet), 'wallet arg'
  318. data = self.users[user]
  319. stem_glob = data.walletfile_fs.format(wallet or '*')
  320. for glob in (
  321. stem_glob,
  322. stem_glob + '.keys',
  323. stem_glob + '.address.txt' ):
  324. run( f'rm -f {glob}', shell=True )
  325. t = self.spawn(
  326. 'mmgen-xmrwallet',
  327. [f'--wallet-dir={data.udir}']
  328. + self.extra_opts
  329. + (self.autosign_opts if data.autosign else [])
  330. + add_opts
  331. + [op]
  332. + ([] if data.autosign else [data.kafile])
  333. + [wallet or data.kal_range]
  334. )
  335. for i in MMGenRange(wallet or data.kal_range).items:
  336. write_data_to_file(
  337. cfg,
  338. self.users[user].addrfile_fs.format(i),
  339. t.expect_getend('Address: '),
  340. quiet = True
  341. )
  342. return t
  343. def new_addr_alice(self,spec,cfg,expect,kafile=None):
  344. data = self.users['alice']
  345. t = self.spawn(
  346. 'mmgen-xmrwallet',
  347. self.extra_opts
  348. + [f'--wallet-dir={data.udir}']
  349. + [f'--daemon=localhost:{data.md.rpc_port}']
  350. + (['--no-start-wallet-daemon'] if cfg in ('continue','stop') else [])
  351. + (['--no-stop-wallet-daemon'] if cfg in ('start','continue') else [])
  352. + ['new', (kafile or data.kafile), spec])
  353. t.expect(expect, 'y', regex=True)
  354. return t
  355. na_idx = 1
  356. def new_account_alice(self):
  357. return self.new_addr_alice(
  358. '4',
  359. 'start',
  360. r'Creating new account for wallet .*4.* with label .*‘xmrwallet new account .*y/N\): ')
  361. def new_account_alice_label(self):
  362. return self.new_addr_alice(
  363. '4,Alice’s new account',
  364. 'continue',
  365. r'Creating new account for wallet .*4.* with label .*‘Alice’s new account .*y/N\): ')
  366. def new_address_alice(self):
  367. return self.new_addr_alice(
  368. '4:2',
  369. 'continue',
  370. r'Creating new address for wallet .*4.*, account .*#2.* with label .*‘xmrwallet new address .*y/N\): ')
  371. def new_address_alice_label(self):
  372. return self.new_addr_alice(
  373. '4:2,Alice’s new address',
  374. 'stop',
  375. r'Creating new address for wallet .*4.*, account .*#2.* with label .*‘Alice’s new address .*y/N\): ')
  376. async def mine_initial_coins(self):
  377. self.spawn('', msg_only=True, extra_desc='(opening wallet)')
  378. await self.open_wallet_user('miner',1)
  379. ok()
  380. # NB: a large balance is required to avoid ‘insufficient outputs’ error
  381. return await self.mine_chk('miner',1,0,lambda x: x.ub > 2000,'unlocked balance > 2000')
  382. async def fund_alice(self,wallet=1,check_bal=True):
  383. self.spawn('', msg_only=True, extra_desc='(transferring funds from Miner wallet)')
  384. await self.transfer(
  385. 'miner',
  386. 1234567891234,
  387. read_from_file(self.users['alice'].addrfile_fs.format(wallet)),
  388. )
  389. if not check_bal:
  390. return 'ok'
  391. ok()
  392. bal = '1.234567891234'
  393. return await self.mine_chk(
  394. 'alice',wallet,0,
  395. lambda x: str(x.ub) == bal,f'unlocked balance == {bal}',
  396. random_txs = self.dfl_random_txs
  397. )
  398. def set_label_miner(self):
  399. return self.set_label_user('miner', '1:0:0,"Miner’s new primary account label [1:0:0]"', 'updated')
  400. def remove_label_alice(self):
  401. return self.set_label_user('alice', '4:2:2,""', 'removed')
  402. def set_label_alice(self):
  403. return self.set_label_user('alice', '4:2:2,"Alice’s new subaddress label [4:2:2]"', 'set')
  404. def set_label_user(self,user,label_spec,expect):
  405. data = self.users[user]
  406. cmd_opts = [f'--wallet-dir={data.udir}', f'--daemon=localhost:{data.md.rpc_port}']
  407. t = self.spawn(
  408. 'mmgen-xmrwallet',
  409. self.extra_opts
  410. + cmd_opts
  411. + ['label', data.kafile, label_spec]
  412. )
  413. t.expect('(y/N): ','y')
  414. t.expect(f'Label successfully {expect}')
  415. return t
  416. def sync_wallets_all(self):
  417. return self.sync_wallets('alice',add_opts=['--rescan-blockchain'])
  418. def sync_wallets_selected(self):
  419. return self.sync_wallets('alice',wallets='1-2,4')
  420. def list_wallets_all(self):
  421. return self.sync_wallets('alice',op='list')
  422. def sync_wallets(self,user,op='sync',wallets=None,add_opts=[],bal_chk_func=None):
  423. data = self.users[user]
  424. if data.autosign:
  425. self.insert_device_online()
  426. cmd_opts = list_gen(
  427. [f'--wallet-dir={data.udir}'],
  428. [f'--daemon=localhost:{data.md.rpc_port}'],
  429. )
  430. t = self.spawn(
  431. 'mmgen-xmrwallet',
  432. self.extra_opts
  433. + cmd_opts
  434. + (self.autosign_opts if data.autosign else [])
  435. + add_opts
  436. + [op]
  437. + ([] if data.autosign else [data.kafile])
  438. + ([wallets] if wallets else [])
  439. )
  440. wlist = AddrIdxList(wallets) if wallets else MMGenRange(data.kal_range).items
  441. for n,wnum in enumerate(wlist,1):
  442. t.expect('ing wallet {}/{} ({})'.format(
  443. n,
  444. len(wlist),
  445. os.path.basename(data.walletfile_fs.format(wnum)),
  446. ))
  447. if op in ('view','listview'):
  448. t.expect('Wallet height: ')
  449. else:
  450. t.expect('Chain height: ')
  451. t.expect('Wallet height: ')
  452. res = strip_ansi_escapes(t.expect_getend('Balance: '))
  453. if bal_chk_func:
  454. m = re.match( r'(\S+) Unlocked balance: (\S+)', res, re.DOTALL )
  455. amts = [XMRAmt(amt) for amt in m.groups()]
  456. assert bal_chk_func(n,*amts), f'balance check for wallet {n} failed!'
  457. if data.autosign:
  458. t.read()
  459. self.remove_device_online()
  460. return t
  461. def do_op(
  462. self,
  463. op,
  464. user,
  465. arg2,
  466. tx_relay_parm = None,
  467. no_relay = False,
  468. return_amt = False,
  469. reuse_acct = False,
  470. add_opts = [],
  471. add_desc = None,
  472. do_ret = False):
  473. data = self.users[user]
  474. cmd_opts = list_gen(
  475. [f'--wallet-dir={data.udir}'],
  476. [f'--outdir={data.udir}', not data.autosign],
  477. [f'--daemon=localhost:{data.md.rpc_port}'],
  478. [f'--tx-relay-daemon={tx_relay_parm}', tx_relay_parm],
  479. ['--no-relay', no_relay and not data.autosign],
  480. )
  481. add_desc = (', ' + add_desc) if add_desc else ''
  482. t = self.spawn(
  483. 'mmgen-xmrwallet',
  484. self.extra_opts
  485. + cmd_opts
  486. + add_opts
  487. + (self.autosign_opts if data.autosign else [])
  488. + [op]
  489. + ([] if data.autosign else [data.kafile])
  490. + [arg2],
  491. extra_desc = f'({capfirst(user)}{add_desc})' )
  492. if op == 'sign':
  493. return t
  494. if op == 'sweep':
  495. t.expect(
  496. r'Create new {} .* \(y/N\): '.format(('address','account')[',' in arg2]),
  497. ('y','n')[reuse_acct],
  498. regex=True )
  499. if reuse_acct:
  500. t.expect( r'to last existing account .* \(y/N\): ','y', regex=True )
  501. if return_amt:
  502. amt = XMRAmt(strip_ansi_escapes(t.expect_getend('Amount: ')).replace('XMR','').strip())
  503. dtype = 'unsigned' if data.autosign else 'signed'
  504. t.expect(f'Save {dtype} transaction? (y/N): ','y')
  505. t.written_to_file(f'{dtype.capitalize()} transaction')
  506. if not no_relay:
  507. t.expect(f'Relay {op} transaction? (y/N): ','y')
  508. get_file_with_ext(self.users[user].udir,'sigtx',delete_all=True)
  509. t.read()
  510. return t if do_ret else amt if return_amt else t.ok()
  511. def sweep_to_address_proxy(self):
  512. self.do_op('sweep', 'alice', '1:0', self.tx_relay_daemon_proxy_parm, add_opts=['--priority=3'])
  513. return self.mine_chk('alice',1,0,lambda x: x.ub > 1,'unlocked balance > 1')
  514. def sweep_to_account(self):
  515. self.do_op('sweep','alice','1:0,2')
  516. return self.mine_chk('alice',2,1,lambda x: x.ub > 1,'unlocked balance > 1')
  517. def sweep_to_address_noproxy(self):
  518. self.do_op('sweep','alice','2:1',self.tx_relay_daemon_parm)
  519. return self.mine_chk('alice',2,1,lambda x: x.ub > 0.9,'unlocked balance > 0.9')
  520. async def transfer_to_miner_proxy(self):
  521. addr = read_from_file(self.users['miner'].addrfile_fs.format(2))
  522. amt = '0.135'
  523. self.do_op('transfer','alice',f'2:1:{addr},{amt}',self.tx_relay_daemon_proxy_parm)
  524. await self.stop_wallet_user('miner')
  525. await self.open_wallet_user('miner',2)
  526. await self.mine_chk('miner',2,0,lambda x: str(x.ub) == amt,f'unlocked balance == {amt}')
  527. ok()
  528. return await self.mine_chk('alice',2,1,lambda x: x.ub > 0.9,'unlocked balance > 0.9')
  529. async def transfer_to_miner_noproxy(self):
  530. addr = read_from_file(self.users['miner'].addrfile_fs.format(2))
  531. self.do_op('transfer','alice',f'2:1:{addr},0.0995',self.tx_relay_daemon_parm)
  532. await self.mine_chk('miner',2,0,lambda x: str(x.ub) == '0.2345','unlocked balance == 0.2345')
  533. ok()
  534. return await self.mine_chk('alice',2,1,lambda x: x.ub > 0.9,'unlocked balance > 0.9')
  535. def transfer_to_miner_create(self,amt):
  536. get_file_with_ext(self.users['alice'].udir,'sigtx',delete_all=True)
  537. addr = read_from_file(self.users['miner'].addrfile_fs.format(2))
  538. return self.do_op('transfer','alice',f'2:1:{addr},{amt}',no_relay=True,do_ret=True)
  539. def transfer_to_miner_create1(self):
  540. return self.transfer_to_miner_create('0.0111')
  541. def transfer_to_miner_create2(self):
  542. return self.transfer_to_miner_create('0.0012')
  543. def relay_tx(self,relay_opt,add_desc=None):
  544. user = 'alice'
  545. data = self.users[user]
  546. add_desc = (', ' + add_desc) if add_desc else ''
  547. t = self.spawn(
  548. 'mmgen-xmrwallet',
  549. self.extra_opts
  550. + [ relay_opt, 'relay', get_file_with_ext(data.udir,'sigtx') ],
  551. extra_desc = f'(relaying TX, {capfirst(user)}{add_desc})' )
  552. t.expect('Relay transaction? ','y')
  553. t.read()
  554. t.ok()
  555. return t
  556. async def transfer_to_miner_send1(self):
  557. self.relay_tx(f'--tx-relay-daemon={self.tx_relay_daemon_proxy_parm}',add_desc='via proxy')
  558. await self.mine_chk('miner',2,0,lambda x: str(x.ub) == '0.2456','unlocked balance == 0.2456')
  559. ok()
  560. return await self.mine_chk('alice',2,1,lambda x: x.ub > 0.9,'unlocked balance > 0.9')
  561. async def transfer_to_miner_send2(self):
  562. self.relay_tx(f'--tx-relay-daemon={self.tx_relay_daemon_parm}',add_desc='no proxy')
  563. await self.mine_chk('miner',2,0,lambda x: str(x.ub) == '0.2468','unlocked balance == 0.2468')
  564. ok()
  565. return await self.mine_chk('alice',2,1,lambda x: x.ub > 0.9,'unlocked balance > 0.9')
  566. async def sweep_create_and_send(self):
  567. bal = XMRAmt('0')
  568. min_bal = XMRAmt('0.9')
  569. for i in range(4):
  570. if i:
  571. ok()
  572. get_file_with_ext(self.users['alice'].udir,'sigtx',delete_all=True)
  573. send_amt = self.do_op(
  574. 'sweep','alice','2:1,3', # '2:1,3'
  575. no_relay = True,
  576. reuse_acct = True,
  577. add_desc = f'TX #{i+1}',
  578. return_amt = True )
  579. ok()
  580. self.relay_tx(f'--tx-relay-daemon={self.tx_relay_daemon_parm}',add_desc=f'send amt: {send_amt} XMR')
  581. await self.mine_chk('alice',2,1,lambda x: 'chk_bal_chg','balance has changed')
  582. ok()
  583. bal_info = await self.mine_chk('alice',3,0,lambda x,y=bal: x.ub > y, f'bal > {bal}',return_bal=True)
  584. bal += bal_info.ub
  585. if bal >= min_bal:
  586. return 'ok'
  587. return False
  588. # wallet methods
  589. async def open_wallet_user(self,user,wnum):
  590. data = self.users[user]
  591. if data.autosign:
  592. self.do_mount_online()
  593. silence()
  594. kal = (ViewKeyAddrList if data.autosign else KeyAddrList)(
  595. cfg = cfg,
  596. proto = self.proto,
  597. addrfile = data.kafile,
  598. skip_chksum_msg = True,
  599. key_address_validity_check = False )
  600. end_silence()
  601. if data.autosign:
  602. self.do_umount_online()
  603. self.users[user].wd.start(silent=not (cfg.exact_output or cfg.verbose))
  604. return data.wd_rpc.call(
  605. 'open_wallet',
  606. filename = os.path.basename(data.walletfile_fs.format(wnum)),
  607. password = kal.entry(wnum).wallet_passwd )
  608. async def stop_wallet_user(self,user):
  609. await self.users[user].wd_rpc.stop_daemon(silent=not (cfg.exact_output or cfg.verbose))
  610. return 'ok'
  611. # mining methods
  612. async def mine5(self):
  613. return await self.mine(5)
  614. async def _get_height(self):
  615. u = self.users['miner']
  616. for _ in range(20):
  617. try:
  618. return u.md_rpc.call('get_last_block_header')['block_header']['height']
  619. except Exception as e:
  620. if 'onnection refused' in str(e):
  621. omsg(f'{e}\nMonerod appears to have crashed. Attempting to restart...')
  622. await asyncio.sleep(5)
  623. u.md.restart()
  624. await asyncio.sleep(5)
  625. await self.start_mining()
  626. else:
  627. raise
  628. else:
  629. die(2,'Restart attempt limit exceeded')
  630. async def mine10(self):
  631. return await self.mine(10)
  632. async def mine30(self):
  633. return await self.mine(30)
  634. async def mine100(self):
  635. return await self.mine(100)
  636. async def mine(self,nblks):
  637. start_height = height = await self._get_height()
  638. imsg(f'Height: {height}')
  639. imsg_r(f'Mining {nblks} blocks...')
  640. await self.start_mining()
  641. while height < start_height + nblks:
  642. await asyncio.sleep(2)
  643. height = await self._get_height()
  644. imsg_r('.')
  645. ret = await self.stop_mining()
  646. imsg('done')
  647. imsg(f'Height: {height}')
  648. return 'ok' if ret == 'OK' else False
  649. async def start_mining(self):
  650. data = self.users['miner']
  651. addr = read_from_file(data.addrfile_fs.format(1)) # mine to wallet #1, account 0
  652. for _ in range(20):
  653. # NB: threads_count > 1 provides no benefit and leads to connection errors with MSWin/MSYS2
  654. ret = data.md_rpc.call_raw(
  655. 'start_mining',
  656. do_background_mining = False, # run mining in background or foreground
  657. ignore_battery = True, # ignore battery state (on laptop)
  658. miner_address = addr, # account address to mine to
  659. threads_count = 1 ) # number of mining threads to run
  660. status = self.get_status(ret)
  661. if status == 'OK':
  662. return True
  663. elif status == 'BUSY':
  664. await asyncio.sleep(5)
  665. omsg('Daemon busy. Attempting to start mining...')
  666. else:
  667. die(2,f'Monerod returned status {status}')
  668. else:
  669. die(2,'Max retries exceeded')
  670. async def stop_mining(self):
  671. ret = self.users['miner'].md_rpc.call_raw('stop_mining')
  672. return self.get_status(ret)
  673. async def mine_chk(
  674. self,
  675. user,
  676. wnum,
  677. account,
  678. test,
  679. test_desc,
  680. test2 = None,
  681. test2_desc = None,
  682. random_txs = None,
  683. return_bal = False ):
  684. """
  685. - open destination wallet
  686. - optionally create and broadcast random TXs
  687. - start mining
  688. - mine until funds appear in wallet
  689. - stop mining
  690. - close wallet
  691. """
  692. async def send_random_txs():
  693. from mmgen.tool.api import tool_api
  694. t = tool_api(cfg)
  695. t.init_coin('XMR','mainnet')
  696. t.usr_randchars = 0
  697. imsg_r('Sending random transactions: ')
  698. for i in range(random_txs):
  699. await self.transfer(
  700. 'miner',
  701. 123456789,
  702. t.randpair()[1],
  703. )
  704. imsg_r(f'{i+1} ')
  705. oqmsg_r('+')
  706. await asyncio.sleep(0.5)
  707. imsg('')
  708. def print_balance(dest,bal_info):
  709. imsg('Total balances in {}’s wallet {}, account #{}: {} (total), {} (unlocked)'.format(
  710. capfirst(dest.user),
  711. dest.wnum,
  712. dest.account,
  713. bal_info.b.hl(),
  714. bal_info.ub.hl(),
  715. ))
  716. async def get_balance(dest,count):
  717. data = self.users[dest.user]
  718. data.wd_rpc.call('refresh')
  719. if count and not count % 20:
  720. data.wd_rpc.call('rescan_blockchain')
  721. ret = data.wd_rpc.call('get_accounts')['subaddress_accounts'][dest.account]
  722. d_tup = namedtuple('bal_info',['b','ub'])
  723. return d_tup(
  724. b = XMRAmt(ret['balance'],from_unit='atomic'),
  725. ub = XMRAmt(ret['unlocked_balance'],from_unit='atomic')
  726. )
  727. # start execution:
  728. self.do_msg(extra_desc =
  729. (f'sending {random_txs} random TXs, ' if random_txs else '') +
  730. f'mining, checking wallet {user}:{wnum}:{account}' )
  731. dest = namedtuple(
  732. 'dest_info',['user','wnum','account','test','test_desc','test2','test2_desc'])(
  733. user,wnum,account,test,test_desc,test2,test2_desc)
  734. if dest.user != 'miner':
  735. await self.open_wallet_user(dest.user,dest.wnum)
  736. bal_info_start = await get_balance(dest,0)
  737. chk_bal_chg = dest.test(bal_info_start) == 'chk_bal_chg'
  738. if random_txs:
  739. await send_random_txs()
  740. await self.start_mining()
  741. h = await self._get_height()
  742. imsg_r(f'Chain height: {h} ')
  743. max_iterations,min_height = (300,64) if sys.platform == 'win32' else (50,300)
  744. verbose = False
  745. for count in range(max_iterations):
  746. bal_info = await get_balance(dest,count)
  747. if h > min_height:
  748. if dest.test(bal_info) is True or (chk_bal_chg and bal_info.ub != bal_info_start.ub):
  749. imsg('')
  750. oqmsg_r('+')
  751. print_balance(dest,bal_info)
  752. if dest.test2:
  753. assert dest.test2(bal_info) is True, f'test failed: {dest.test2_desc} ({bal_info})'
  754. break
  755. await asyncio.sleep(2)
  756. h = await self._get_height()
  757. if count > 12: # something might have gone wrong, so be more verbose
  758. if not verbose:
  759. imsg('')
  760. imsg_r(f'Height: {h}, ')
  761. print_balance(dest,bal_info)
  762. verbose = True
  763. else:
  764. imsg_r(f'{h} ')
  765. oqmsg_r('+')
  766. else:
  767. die(2,f'Timeout exceeded, balance {bal_info.ub!r}')
  768. await self.stop_mining()
  769. if user != 'miner':
  770. await self.stop_wallet_user(dest.user)
  771. return bal_info if return_bal else 'ok'
  772. # util methods
  773. def get_status(self,ret):
  774. if ret['status'] != 'OK':
  775. imsg( 'RPC status: {}'.format( ret['status'] ))
  776. return ret['status']
  777. def do_msg(self,extra_desc=None):
  778. self.spawn(
  779. '',
  780. msg_only = True,
  781. extra_desc = f'({extra_desc})' if extra_desc else None
  782. )
  783. async def transfer(self,user,amt,addr):
  784. return self.users[user].wd_rpc.call('transfer',destinations=[{'amount':amt,'address':addr}])
  785. # daemon start/stop methods
  786. def start_daemons(self):
  787. for v in self.users.values():
  788. run(['mkdir','-p',v.datadir])
  789. v.md.start()
  790. def stop_daemons(self):
  791. self.spawn('', msg_only=True)
  792. if cfg.no_daemon_stop:
  793. omsg('[not stopping daemons at user request]')
  794. else:
  795. omsg('')
  796. stop_daemons(self)
  797. atexit.unregister(stop_daemons)
  798. stop_miner_wallet_daemon(self)
  799. atexit.unregister(stop_miner_wallet_daemon)
  800. return 'silent'