ts_xmrwallet.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2021 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. ts_xmrwallet.py: xmrwallet tests for the test.py test suite
  20. """
  21. import sys,os,atexit,asyncio
  22. from subprocess import run,PIPE
  23. from mmgen.globalvars import g
  24. from mmgen.opts import opt
  25. from mmgen.obj import MMGenRange,XMRAmt
  26. from mmgen.addr 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 = ('coin',)
  36. tmpdir_nums = [29]
  37. dfl_random_txs = 3
  38. color = True
  39. cmd_group = (
  40. ('gen_kafiles', 'generating key-address files'),
  41. ('create_wallets_miner', 'creating Monero wallets (Miner)'),
  42. ('mine_initial_coins', 'mining initial coins'),
  43. ('create_wallets_alice', 'creating Monero wallets (Alice)'),
  44. ('fund_alice', 'sending funds'),
  45. ('sync_wallets_all', 'syncing all wallets'),
  46. ('sync_wallets_selected', 'syncing selected wallets'),
  47. ('sweep_to_address_proxy', 'sweeping to new address (via TX relay + proxy)'),
  48. ('sweep_to_account', 'sweeping to new account'),
  49. ('sweep_to_address_noproxy', 'sweeping to new address (via TX relay, no proxy)'),
  50. ('transfer_to_miner_proxy', 'transferring funds to Miner (via TX relay + proxy)'),
  51. ('transfer_to_miner_noproxy', 'transferring funds to Miner (via TX relay, no proxy)'),
  52. )
  53. def __init__(self,trunner,cfgs,spawn):
  54. TestSuiteBase.__init__(self,trunner,cfgs,spawn)
  55. if trunner == None:
  56. return
  57. from mmgen.protocol import init_proto
  58. self.proto = init_proto('XMR',network='testnet')
  59. self.datadir_base = os.path.join('test','daemons','xmrtest')
  60. self.long_opts = ['--testnet=1', '--monero-wallet-rpc-password=passw0rd']
  61. self.init_users()
  62. self.init_daemon_args()
  63. for v in self.users.values():
  64. run(['mkdir','-p',v.udir])
  65. self.init_proxy()
  66. self.tx_relay_daemon_parm = 'localhost:{}'.format(self.users['bob'].md.rpc_port)
  67. self.tx_relay_daemon_proxy_parm = (
  68. self.tx_relay_daemon_parm + f':127.0.0.1:{self.socks_port}' # proxy must be IP, not 'localhost'
  69. if self.use_proxy else None )
  70. if not opt.no_daemon_autostart:
  71. self.start_daemons()
  72. self.start_wallet_daemons()
  73. if not opt.no_daemon_stop:
  74. atexit.register(self.stop_daemons)
  75. atexit.register(self.stop_wallet_daemons)
  76. self.balance = None
  77. # init methods
  78. def init_proxy(self):
  79. def port_in_use(port):
  80. import socket
  81. try: socket.create_connection(('localhost',port)).close()
  82. except: return False
  83. else: return True
  84. def start_proxy():
  85. if not opt.no_daemon_autostart:
  86. run(a+b2)
  87. omsg(f'SSH SOCKS server started, listening at localhost:{self.socks_port}')
  88. def kill_proxy():
  89. omsg(f'Killing SSH SOCKS server at localhost:{self.socks_port}')
  90. cmd = [ 'pkill', '-f', ' '.join(a + b2) ]
  91. run(cmd)
  92. self.use_proxy = False
  93. self.socks_port = 9060
  94. a = ['ssh','-x','-o','ExitOnForwardFailure=True','-D',f'localhost:{self.socks_port}']
  95. b0 = ['-o','PasswordAuthentication=False']
  96. b1 = ['localhost','true']
  97. b2 = ['-fN','-E','txrelay-proxy.debug','localhost']
  98. if port_in_use(self.socks_port):
  99. omsg(f'Port {self.socks_port} already in use. Assuming SSH SOCKS server is running')
  100. self.use_proxy = True
  101. else:
  102. cp = run(a+b0+b1,stdout=PIPE,stderr=PIPE)
  103. err = cp.stderr.decode()
  104. if err:
  105. omsg(err)
  106. if cp.returncode == 0:
  107. start_proxy()
  108. self.use_proxy = True
  109. elif 'onnection refused' in err:
  110. die(2,fmt("""
  111. The SSH daemon must be running and listening on localhost in order to test
  112. XMR TX relaying via SOCKS proxy. If sshd is not running, please start it.
  113. Otherwise, add the line 'ListenAddress 127.0.0.1' to your sshd_config, and
  114. then restart the daemon.
  115. """,indent=' '))
  116. elif 'ermission denied' in err:
  117. msg(fmt(f"""
  118. In order to test XMR TX relaying via SOCKS proxy, it’s desirable to enable
  119. SSH to localhost without a password, which is not currently supported by
  120. your configuration. Your possible courses of action:
  121. 1. Continue by answering 'y' at this prompt, and enter your system password
  122. at the following prompt;
  123. 2. Exit the test here, add your user SSH public key to your user
  124. 'authorized_keys' file, and restart the test; or
  125. 3. Exit the test here, start the SSH SOCKS proxy manually by entering the
  126. following command, and restart the test:
  127. {' '.join(a+b2)}
  128. """,indent=' ',strip_char='\t'))
  129. if keypress_confirm('Continue?'):
  130. start_proxy()
  131. self.use_proxy = True
  132. else:
  133. die(1,'Exiting at user request')
  134. else:
  135. die(2,fmt(f"""
  136. Please start the SSH SOCKS proxy by entering the following command:
  137. {' '.join(a+b2)}
  138. Then restart the test.
  139. """,indent=' '))
  140. if not opt.no_daemon_stop:
  141. atexit.register(kill_proxy)
  142. def init_users(self):
  143. from mmgen.daemon import CoinDaemon,MoneroWalletDaemon
  144. from mmgen.rpc import MoneroRPCClient,MoneroRPCClientRaw,MoneroWalletRPCClient
  145. self.users = {}
  146. n = self.tmpdir_nums[0]
  147. ud = namedtuple('user_data',[
  148. 'sid',
  149. 'mmwords',
  150. 'udir',
  151. 'datadir',
  152. 'kal_range',
  153. 'kafile',
  154. 'walletfile_fs',
  155. 'addrfile_fs',
  156. 'md',
  157. 'md_rpc',
  158. 'md_json_rpc',
  159. 'wd',
  160. 'wd_rpc',
  161. ])
  162. for user,sid,shift,kal_range in ( # kal_range must be None, a single digit, or a single hyphenated range
  163. ('miner', '98831F3A', 130, '1-2'),
  164. ('bob', '1378FC64', 140, None),
  165. ('alice', 'FE3C6545', 150, '1-4'),
  166. ):
  167. udir = os.path.join('test',f'tmp{n}',user)
  168. datadir = os.path.join(self.datadir_base,user)
  169. md = CoinDaemon(
  170. proto = self.proto,
  171. test_suite = True,
  172. port_shift = shift,
  173. opts = ['online'],
  174. datadir = datadir
  175. )
  176. md_rpc = MoneroRPCClientRaw(
  177. host = md.host,
  178. port = md.rpc_port,
  179. user = None,
  180. passwd = None,
  181. test_connection = False,
  182. )
  183. md_json_rpc = MoneroRPCClient(
  184. host = md.host,
  185. port = md.rpc_port,
  186. user = None,
  187. passwd = None,
  188. test_connection = False,
  189. )
  190. wd = MoneroWalletDaemon(
  191. user = 'foo',
  192. passwd = 'bar',
  193. wallet_dir = udir,
  194. test_suite = True,
  195. port_shift = shift,
  196. datadir = os.path.join('test','daemons'),
  197. daemon_addr = f'127.0.0.1:{md.rpc_port}',
  198. testnet = True
  199. )
  200. wd_rpc = MoneroWalletRPCClient(
  201. host = wd.host,
  202. port = wd.rpc_port,
  203. user = wd.user,
  204. passwd = wd.passwd,
  205. test_connection = False,
  206. )
  207. self.users[user] = ud(
  208. sid = sid,
  209. mmwords = f'test/ref/{sid}.mmwords',
  210. udir = udir,
  211. datadir = datadir,
  212. kal_range = kal_range,
  213. kafile = f'{udir}/{sid}-XMR-M[{kal_range}].testnet.akeys',
  214. walletfile_fs = f'{udir}/{sid}-{{}}-MoneroWallet.testnet',
  215. addrfile_fs = f'{udir}/{sid}-{{}}-MoneroWallet.testnet.address.txt',
  216. md = md,
  217. md_rpc = md_rpc,
  218. md_json_rpc = md_json_rpc,
  219. wd = wd,
  220. wd_rpc = wd_rpc,
  221. )
  222. def init_daemon_args(self):
  223. common_args = ['--p2p-bind-ip=127.0.0.1','--fixed-difficulty=1'] # ,'--rpc-ssl-allow-any-cert']
  224. for u in self.users:
  225. other_ports = [self.users[u2].md.p2p_port for u2 in self.users if u2 != u]
  226. node_args = [f'--add-exclusive-node=127.0.0.1:{p}' for p in other_ports]
  227. self.users[u].md.usr_coind_args = common_args + node_args
  228. # cmd_group methods
  229. def gen_kafiles(self):
  230. for user,data in self.users.items():
  231. if not data.kal_range:
  232. continue
  233. run(['mkdir','-p',data.udir])
  234. run(f'rm -f {data.kafile}',shell=True)
  235. t = self.spawn(
  236. 'mmgen-keygen', [
  237. '--testnet=1','-q', '--accept-defaults', '--coin=xmr',
  238. f'--outdir={data.udir}', data.mmwords, data.kal_range
  239. ],
  240. extra_desc = f'({capfirst(user)})' )
  241. t.read()
  242. t.ok()
  243. t.skip_ok = True
  244. return t
  245. def create_wallets_miner(self): return self.create_wallets('miner')
  246. def create_wallets_alice(self): return self.create_wallets('alice')
  247. def create_wallets(self,user,wallet=None):
  248. assert wallet is None or is_int(wallet), 'wallet arg'
  249. data = self.users[user]
  250. run('rm -f {}*'.format( data.walletfile_fs.format(wallet or '*') ),shell=True)
  251. dir_opt = [f'--outdir={data.udir}']
  252. t = self.spawn(
  253. 'mmgen-xmrwallet',
  254. self.long_opts + dir_opt + [ 'create', data.kafile, (wallet or data.kal_range) ] )
  255. t.expect('Check key-to-address validity? (y/N): ','n')
  256. for i in MMGenRange(wallet or data.kal_range).items:
  257. t.expect('Address: ')
  258. t.read()
  259. return t
  260. async def mine_initial_coins(self):
  261. return await self.mine_chk('miner',1,0,lambda x: x > 20,'unlocked balance > 20')
  262. async def fund_alice(self):
  263. await self.open_wallet_user('miner',1)
  264. await self.transfer(
  265. 'miner',
  266. 1234567891234,
  267. read_from_file(self.users['alice'].addrfile_fs.format(1)),
  268. )
  269. bal = '1.234567891234'
  270. return await self.mine_chk(
  271. 'alice',1,0,
  272. lambda x: str(x) == bal,f'unlocked balance == {bal}',
  273. random_txs = self.dfl_random_txs
  274. )
  275. def sync_wallets_all(self):
  276. return self.sync_wallets('alice',add_opts=['--rescan-blockchain'])
  277. def sync_wallets_selected(self):
  278. return self.sync_wallets('alice',wallets='1-2,4')
  279. def sync_wallets(self,user,wallets=None,add_opts=None):
  280. data = self.users[user]
  281. cmd_opts = list_gen(
  282. [f'--outdir={data.udir}'],
  283. [f'--daemon=localhost:{data.md.rpc_port}'],
  284. )
  285. t = self.spawn(
  286. 'mmgen-xmrwallet',
  287. self.long_opts + cmd_opts + (add_opts or []) + [ 'sync', data.kafile ] + ([wallets] if wallets else []) )
  288. t.expect('Check key-to-address validity? (y/N): ','n')
  289. wlist = AddrIdxList(wallets) if wallets else MMGenRange(data.kal_range).items
  290. for n,wnum in enumerate(wlist):
  291. t.expect('Syncing wallet {}/{} ({})'.format(
  292. n+1,
  293. len(wlist),
  294. os.path.basename(data.walletfile_fs.format(wnum)),
  295. ))
  296. t.expect('Chain height: ')
  297. t.expect('Wallet height: ')
  298. t.expect('Balance: ')
  299. t.read()
  300. return t
  301. def do_op(self, op, user, arg2,
  302. tx_relay_parm = None,
  303. return_amt = False,
  304. reuse_acct = False,
  305. add_desc = None,
  306. do_ret = False ):
  307. data = self.users[user]
  308. cmd_opts = list_gen(
  309. [f'--outdir={data.udir}'],
  310. [f'--daemon=localhost:{data.md.rpc_port}'],
  311. [f'--tx-relay-daemon={tx_relay_parm}', tx_relay_parm]
  312. )
  313. add_desc = (', ' + add_desc) if add_desc else ''
  314. t = self.spawn(
  315. 'mmgen-xmrwallet',
  316. self.long_opts + cmd_opts + [ op, data.kafile, arg2 ],
  317. extra_desc = f'({capfirst(user)}{add_desc})' )
  318. t.expect('Check key-to-address validity? (y/N): ','n')
  319. if op == 'sweep':
  320. t.expect(
  321. 'Create new {} .* \(y/N\): '.format(('address','account')[',' in arg2]),
  322. ('y','n')[reuse_acct],
  323. regex=True )
  324. if reuse_acct:
  325. t.expect( 'to last existing account .* \(y/N\): ','y', regex=True )
  326. if return_amt:
  327. amt = XMRAmt(strip_ansi_escapes(t.expect_getend('Amt: ')).replace('XMR','').strip())
  328. t.expect(f'Relay {op} transaction? (y/N): ','y')
  329. t.read()
  330. return t if do_ret else amt if return_amt else t.ok()
  331. def sweep_to_address_proxy(self):
  332. self.do_op('sweep','alice','1:0',self.tx_relay_daemon_proxy_parm)
  333. return self.mine_chk('alice',1,0,lambda x: x > 1,'unlocked balance > 1')
  334. def sweep_to_account(self):
  335. self.do_op('sweep','alice','1:0,2')
  336. return self.mine_chk('alice',2,1,lambda x: x > 1,'unlocked balance > 1')
  337. def sweep_to_address_noproxy(self):
  338. self.do_op('sweep','alice','2:1',self.tx_relay_daemon_parm)
  339. return self.mine_chk('alice',2,1,lambda x: x > 0.9,'unlocked balance > 0.9')
  340. def transfer_to_miner_proxy(self):
  341. addr = read_from_file(self.users['miner'].addrfile_fs.format(2))
  342. amt = '0.135'
  343. self.do_op('transfer','alice',f'2:1:{addr},{amt}',self.tx_relay_daemon_proxy_parm)
  344. return self.mine_chk('miner',2,0,lambda x: str(x) == amt,f'unlocked balance == {amt}')
  345. def transfer_to_miner_noproxy(self):
  346. addr = read_from_file(self.users['miner'].addrfile_fs.format(2))
  347. self.do_op('transfer','alice',f'2:1:{addr},0.0995',self.tx_relay_daemon_parm)
  348. return self.mine_chk('miner',2,0,lambda x: str(x) == '0.2345','unlocked balance == 0.2345')
  349. # wallet methods
  350. async def open_wallet_user(self,user,wnum):
  351. data = self.users[user]
  352. silence()
  353. kal = KeyAddrList(self.proto,data.kafile,skip_key_address_validity_check=True)
  354. end_silence()
  355. if user != 'miner':
  356. self.users[user].wd.start(silent=not (opt.exact_output or opt.verbose))
  357. return await data.wd_rpc.call(
  358. 'open_wallet',
  359. filename = os.path.basename(data.walletfile_fs.format(wnum)),
  360. password = kal.entry(wnum).wallet_passwd )
  361. async def close_wallet_user(self,user):
  362. ret = await self.users[user].wd_rpc.call('close_wallet')
  363. if user != 'miner':
  364. self.users[user].wd.stop(silent=not (opt.exact_output or opt.verbose))
  365. return 'ok'
  366. # mining methods
  367. async def start_mining(self):
  368. data = self.users['miner']
  369. addr = read_from_file(data.addrfile_fs.format(1)) # mine to wallet #1, account 0
  370. for i in range(20):
  371. ret = await data.md_rpc.call(
  372. 'start_mining',
  373. do_background_mining = False, # run mining in background or foreground
  374. ignore_battery = True, # ignore battery state (on laptop)
  375. miner_address = addr, # account address to mine to
  376. threads_count = 3 ) # number of mining threads to run
  377. status = self.get_status(ret)
  378. if status == 'OK':
  379. return True
  380. elif status == 'BUSY':
  381. await asyncio.sleep(5)
  382. omsg('Daemon busy. Attempting to start mining...')
  383. else:
  384. die(2,f'Monerod returned status {status}')
  385. else:
  386. die(2,'Max retries exceeded')
  387. async def stop_mining(self):
  388. ret = await self.users['miner'].md_rpc.call('stop_mining')
  389. return self.get_status(ret)
  390. async def mine_chk(self,user,wnum,account,test,test_desc,random_txs=None):
  391. """
  392. - open destination wallet
  393. - optionally create and broadcast random TXs
  394. - start mining
  395. - mine until funds appear in wallet
  396. - stop mining
  397. - close wallet
  398. """
  399. async def get_height():
  400. u = self.users['miner']
  401. for i in range(20):
  402. try:
  403. return (await u.md_json_rpc.call('get_last_block_header'))['block_header']['height']
  404. except Exception as e:
  405. if 'onnection refused' in str(e):
  406. omsg(f'{e}\nMonerod appears to have crashed. Attempting to restart...')
  407. await asyncio.sleep(5)
  408. u.md.restart()
  409. await asyncio.sleep(5)
  410. await self.start_mining()
  411. else:
  412. raise
  413. else:
  414. die(2,'Restart attempt limit exceeded')
  415. async def send_random_txs():
  416. from mmgen.tool import tool_api
  417. t = tool_api()
  418. t.init_coin('XMR','testnet')
  419. t.usr_randchars = 0
  420. imsg_r(f'Sending random transactions: ')
  421. for i in range(random_txs):
  422. await self.transfer(
  423. 'miner',
  424. 123456789,
  425. t.randpair()[1],
  426. )
  427. imsg_r(f'{i+1} ')
  428. oqmsg_r('+')
  429. await asyncio.sleep(0.5)
  430. imsg('')
  431. def print_balance(dest,ub):
  432. imsg('Total balance in {}’s wallet {}, account #{}: {}'.format(
  433. capfirst(dest.user),
  434. dest.wnum,
  435. dest.account,
  436. ub.hl()
  437. ))
  438. async def get_balance(dest,count):
  439. data = self.users[dest.user]
  440. await data.wd_rpc.call('refresh')
  441. if count and not count % 20:
  442. await data.wd_rpc.call('rescan_blockchain')
  443. ret = await data.wd_rpc.call('get_accounts')
  444. return XMRAmt(ret['subaddress_accounts'][dest.account]['unlocked_balance'],from_unit='atomic')
  445. # start execution:
  446. self.do_msg(extra_desc =
  447. (f'sending {random_txs} random TXs, ' if random_txs else '') +
  448. f'mining, checking wallet {user}:{wnum}:{account}' )
  449. dest = namedtuple(
  450. 'dest_info',['user','wnum','account','test','test_desc'])(user,wnum,account,test,test_desc)
  451. await self.open_wallet_user(dest.user,dest.wnum)
  452. ub_start = await get_balance(dest,0)
  453. chk_bal_chg = dest.test(ub_start) == 'chk_bal_chg'
  454. if random_txs:
  455. await send_random_txs()
  456. await self.start_mining()
  457. h = await get_height()
  458. imsg_r(f'Chain height: {h} ')
  459. for count in range(500):
  460. ub = await get_balance(dest,count)
  461. if dest.test(ub) is True or ( chk_bal_chg and ub != ub_start ):
  462. imsg('')
  463. oqmsg_r('+')
  464. print_balance(dest,ub)
  465. break
  466. await asyncio.sleep(2)
  467. h = await get_height()
  468. imsg_r(f'{h} ')
  469. oqmsg_r('+')
  470. else:
  471. die(2,'Timeout exceeded, balance {ub!r}')
  472. await self.stop_mining()
  473. await self.close_wallet_user(dest.user)
  474. return ub if chk_bal_chg else 'ok'
  475. # util methods
  476. def get_status(self,ret):
  477. if ret['status'] != 'OK':
  478. imsg( 'RPC status: {}'.format(ret['status']) )
  479. return ret['status']
  480. def do_msg(self,extra_desc=None):
  481. self.spawn(
  482. '',
  483. msg_only = True,
  484. extra_desc = f'({extra_desc})' if extra_desc else None
  485. )
  486. async def transfer(self,user,amt,addr):
  487. return await self.users[user].wd_rpc.call('transfer',destinations=[{'amount':amt,'address':addr}])
  488. # daemon start/stop methods
  489. def start_daemons(self):
  490. self.stop_daemons()
  491. for v in self.users.values():
  492. run(['mkdir','-p',v.datadir])
  493. v.md.start()
  494. def stop_daemons(self):
  495. for v in self.users.values():
  496. if v.md.state != 'stopped':
  497. v.md.stop()
  498. run(['rm','-rf',self.datadir_base])
  499. def start_wallet_daemons(self):
  500. self.users['miner'].wd.start()
  501. def stop_wallet_daemons(self):
  502. self.users['miner'].wd.stop()