ts_xmrwallet.py 24 KB

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