Browse Source

xmrwallet.py: perform late initialization of TX relay daemon

The MMGen Project 3 years ago
parent
commit
e40317a8f8
2 changed files with 45 additions and 61 deletions
  1. 42 37
      mmgen/xmrwallet.py
  2. 3 24
      test/test_py_d/ts_xmrwallet.py

+ 42 - 37
mmgen/xmrwallet.py

@@ -164,8 +164,6 @@ class MoneroWalletOps:
 				passwd = self.wd.passwd
 				passwd = self.wd.passwd
 			)
 			)
 
 
-			self.post_init()
-
 		def create_addr_data(self):
 		def create_addr_data(self):
 			if uarg.wallets:
 			if uarg.wallets:
 				idxs = AddrIdxList(uarg.wallets)
 				idxs = AddrIdxList(uarg.wallets)
@@ -181,8 +179,6 @@ class MoneroWalletOps:
 				if uopt.tx_relay_daemon:
 				if uopt.tx_relay_daemon:
 					self.wd2.stop()
 					self.wd2.stop()
 
 
-		def post_init(self): pass
-
 		def get_wallet_fn(self,d):
 		def get_wallet_fn(self,d):
 			return os.path.join(
 			return os.path.join(
 				opt.outdir or '.','{}-{}-MoneroWallet{}{}'.format(
 				opt.outdir or '.','{}-{}-MoneroWallet{}{}'.format(
@@ -214,14 +210,21 @@ class MoneroWalletOps:
 				self.d = d
 				self.d = d
 				self.fn = parent.get_wallet_fn(d)
 				self.fn = parent.get_wallet_fn(d)
 
 
-			async def open_wallet(self,desc):
+			async def open_wallet(self,desc,refresh=True):
 				gmsg_r(f'\n  Opening {desc} wallet...')
 				gmsg_r(f'\n  Opening {desc} wallet...')
-				ret = await self.c.call( # returns {}
+				await self.c.call( # returns {}
 					'open_wallet',
 					'open_wallet',
 					filename=os.path.basename(self.fn),
 					filename=os.path.basename(self.fn),
 					password=self.d.wallet_passwd )
 					password=self.d.wallet_passwd )
 				gmsg('done')
 				gmsg('done')
 
 
+				if refresh:
+					gmsg_r(f'  Refreshing {desc} wallet...')
+					ret = await self.c.call('refresh')
+					gmsg('done')
+					if ret['received_money']:
+						msg('  Wallet has received funds')
+
 			async def close_wallet(self,desc):
 			async def close_wallet(self,desc):
 				gmsg_r(f'\n  Closing {desc} wallet...')
 				gmsg_r(f'\n  Closing {desc} wallet...')
 				await self.c.call('close_wallet')
 				await self.c.call('close_wallet')
@@ -231,14 +234,14 @@ class MoneroWalletOps:
 				d = data['subaddress_accounts']
 				d = data['subaddress_accounts']
 				msg('\n' + indent + f'Accounts of wallet {os.path.basename(self.fn)}:')
 				msg('\n' + indent + f'Accounts of wallet {os.path.basename(self.fn)}:')
 				fs = indent + '  {:6}  {:18}  {:<6} {:%s}  {}' % max(len(e['label']) for e in d)
 				fs = indent + '  {:6}  {:18}  {:<6} {:%s}  {}' % max(len(e['label']) for e in d)
-				msg(fs.format('Index ','Base Address','nAddrs','Label','Balance'))
+				msg(fs.format('Index ','Base Address','nAddrs','Label','Unlocked Balance'))
 				for i,e in enumerate(d):
 				for i,e in enumerate(d):
 					msg(fs.format(
 					msg(fs.format(
 						str(e['account_index']),
 						str(e['account_index']),
 						e['base_address'][:15] + '...',
 						e['base_address'][:15] + '...',
 						len(addrs_data[i]['addresses']),
 						len(addrs_data[i]['addresses']),
 						e['label'],
 						e['label'],
-						fmt_amt(e['balance']),
+						fmt_amt(e['unlocked_balance']),
 					))
 					))
 
 
 			async def get_accts(self,print=True):
 			async def get_accts(self,print=True):
@@ -389,6 +392,14 @@ class MoneroWalletOps:
 		desc    = 'Sync'
 		desc    = 'Sync'
 		past    = 'synced'
 		past    = 'synced'
 
 
+		def __init__(self,uarg_tuple,uopt_tuple):
+
+			super().__init__(uarg_tuple,uopt_tuple)
+
+			host,port = uopt.daemon.split(':') if uopt.daemon else ('localhost',self.wd.daemon_port)
+			self.dc = MoneroRPCClientRaw(host=host, port=int(port), user=None, passwd=None)
+			self.accts_data = {}
+
 		async def process_wallet(self,d,fn):
 		async def process_wallet(self,d,fn):
 
 
 			chain_height = (await self.dc.call('get_height'))['height']
 			chain_height = (await self.dc.call('get_height'))['height']
@@ -440,11 +451,6 @@ class MoneroWalletOps:
 			await self.c.call('close_wallet')
 			await self.c.call('close_wallet')
 			return True
 			return True
 
 
-		def post_init(self):
-			host,port = uopt.daemon.split(':') if uopt.daemon else ('localhost',self.wd.daemon_port)
-			self.dc = MoneroRPCClientRaw(host=host, port=int(port), user=None, passwd=None)
-			self.accts_data = {}
-
 		def post_main(self):
 		def post_main(self):
 			d = self.accts_data
 			d = self.accts_data
 
 
@@ -505,32 +511,30 @@ class MoneroWalletOps:
 				self.dest_addr = CoinAddr(self.proto,m[3])
 				self.dest_addr = CoinAddr(self.proto,m[3])
 				self.amount = self.proto.coin_amt(m[4])
 				self.amount = self.proto.coin_amt(m[4])
 
 
-		def post_init(self):
+		def init_tx_relay_daemon(self):
 
 
-			if uopt.tx_relay_daemon:
-				m = re.fullmatch(uarg_info['tx_relay_daemon'].pat,uopt.tx_relay_daemon,re.ASCII)
+			m = re.fullmatch(uarg_info['tx_relay_daemon'].pat,uopt.tx_relay_daemon,re.ASCII)
 
 
-				self.wd2 = MoneroWalletDaemon(
-					wallet_dir = opt.outdir or '.',
-					test_suite = g.test_suite,
-					daemon_addr = m[1],
-					proxy = m[2],
-					port_shift = 16,
-					testnet = g.testnet,
-				)
+			self.wd2 = MoneroWalletDaemon(
+				wallet_dir = opt.outdir or '.',
+				test_suite = g.test_suite,
+				daemon_addr = m[1],
+				proxy = m[2],
+				port_shift = 16,
+				testnet = g.testnet,
+			)
 
 
-				if g.test_suite:
-					self.wd2.usr_daemon_args = ['--daemon-ssl-allow-any-cert']
+			if g.test_suite:
+				self.wd2.usr_daemon_args = ['--daemon-ssl-allow-any-cert']
 
 
-				if not uopt.no_start_wallet_daemon:
-					self.wd2.restart()
+			self.wd2.start()
 
 
-				self.c2 = MoneroWalletRPCClient(
-					host   = self.wd2.host,
-					port   = self.wd2.rpc_port,
-					user   = self.wd2.user,
-					passwd = self.wd2.passwd
-				)
+			self.c = MoneroWalletRPCClient(
+				host   = self.wd2.host,
+				port   = self.wd2.rpc_port,
+				user   = self.wd2.user,
+				passwd = self.wd2.passwd
+			)
 
 
 		async def main(self):
 		async def main(self):
 			gmsg(f'\n{self.desc}ing account #{self.account} of wallet {self.source.idx}' + (
 			gmsg(f'\n{self.desc}ing account #{self.account} of wallet {self.source.idx}' + (
@@ -575,7 +579,7 @@ class MoneroWalletOps:
 					die(1,'Exiting at user request')
 					die(1,'Exiting at user request')
 
 
 				await h2.close_wallet('destination')
 				await h2.close_wallet('destination')
-				await h.open_wallet('source')
+				await h.open_wallet('source',refresh=False)
 
 
 			msg_r(f'\n    Creating {self.name} transaction: wallet {self.source.idx}, account #{self.account}')
 			msg_r(f'\n    Creating {self.name} transaction: wallet {self.source.idx}, account #{self.account}')
 
 
@@ -590,10 +594,11 @@ class MoneroWalletOps:
 				w_desc = 'source'
 				w_desc = 'source'
 				if uopt.tx_relay_daemon:
 				if uopt.tx_relay_daemon:
 					await h.close_wallet('source')
 					await h.close_wallet('source')
-					self.c = self.c2
+					msg('')
+					self.init_tx_relay_daemon()
 					h = self.rpc(self,self.source)
 					h = self.rpc(self,self.source)
 					w_desc = 'TX relay source'
 					w_desc = 'TX relay source'
-					await h.open_wallet(w_desc)
+					await h.open_wallet(w_desc,refresh=False)
 					h.display_tx_relay_info()
 					h.display_tx_relay_info()
 				msg_r(f'\n    Relaying {self.name} transaction...')
 				msg_r(f'\n    Relaying {self.name} transaction...')
 				await h.relay_tx(tx_metadata)
 				await h.relay_tx(tx_metadata)

+ 3 - 24
test/test_py_d/ts_xmrwallet.py

@@ -63,8 +63,7 @@ class TestSuiteXMRWallet(TestSuiteBase):
 		('mine_blocks',               'mining blocks'),
 		('mine_blocks',               'mining blocks'),
 
 
 		('transfer_to_miner_proxy',   'transferring funds to Miner (via TX relay + proxy)'),
 		('transfer_to_miner_proxy',   'transferring funds to Miner (via TX relay + proxy)'),
-		('mine_blocks_extra',         'mining blocks'),
-		('sync_wallet_2',             'syncing Alice’s wallet #2'),
+		('mine_blocks',               'mining blocks'),
 
 
 		('transfer_to_miner_noproxy', 'transferring funds to Miner (via TX relay)'),
 		('transfer_to_miner_noproxy', 'transferring funds to Miner (via TX relay)'),
 		('mine_blocks',               'mining blocks'),
 		('mine_blocks',               'mining blocks'),
@@ -325,10 +324,7 @@ class TestSuiteXMRWallet(TestSuiteBase):
 		return 'ok'
 		return 'ok'
 
 
 	def sync_wallets_selected(self):
 	def sync_wallets_selected(self):
-		return self.sync_wallets(wallets='1,3-4')
-
-	def sync_wallet_2(self):
-		return self.sync_wallets(wallets='2')
+		return self.sync_wallets(wallets='1-2,4')
 
 
 	def sync_wallets(self,wallets=None):
 	def sync_wallets(self,wallets=None):
 		data = self.users['alice']
 		data = self.users['alice']
@@ -443,7 +439,7 @@ class TestSuiteXMRWallet(TestSuiteBase):
 		ret = await self.users['miner'].md_rpc.call('stop_mining')
 		ret = await self.users['miner'].md_rpc.call('stop_mining')
 		return self.get_status(ret)
 		return self.get_status(ret)
 
 
-	async def mine_blocks(self,random_txs=None,extra_blocks=None):
+	async def mine_blocks(self,random_txs=None):
 		"""
 		"""
 		- open destination wallet
 		- open destination wallet
 		- optionally create and broadcast random TXs
 		- optionally create and broadcast random TXs
@@ -470,18 +466,6 @@ class TestSuiteXMRWallet(TestSuiteBase):
 			else:
 			else:
 				die(2,'Restart attempt limit exceeded')
 				die(2,'Restart attempt limit exceeded')
 
 
-		async def mine_extra_blocks():
-			h_start = await get_height()
-			imsg_r(f'[+{extra_blocks} blocks]: ')
-			oqmsg_r('|')
-			while True:
-				await asyncio.sleep(2)
-				h = await get_height()
-				imsg_r(f'{h} ')
-				oqmsg_r('+')
-				if h - h_start > extra_blocks:
-					break
-
 		async def send_random_txs():
 		async def send_random_txs():
 			from mmgen.tool import tool_api
 			from mmgen.tool import tool_api
 			t = tool_api()
 			t = tool_api()
@@ -528,8 +512,6 @@ class TestSuiteXMRWallet(TestSuiteBase):
 		while True:
 		while True:
 			ub = await get_balance(self.dest)
 			ub = await get_balance(self.dest)
 			if self.dest.test(ub):
 			if self.dest.test(ub):
-				if extra_blocks:
-					await mine_extra_blocks()
 				imsg('')
 				imsg('')
 				oqmsg_r('+')
 				oqmsg_r('+')
 				print_balance(self.dest,ub)
 				print_balance(self.dest,ub)
@@ -551,9 +533,6 @@ class TestSuiteXMRWallet(TestSuiteBase):
 	async def mine_blocks_tx(self):
 	async def mine_blocks_tx(self):
 		return await self.mine_blocks(random_txs=self.dfl_random_txs)
 		return await self.mine_blocks(random_txs=self.dfl_random_txs)
 
 
-	async def mine_blocks_extra(self):
-		return await self.mine_blocks(extra_blocks=100) # TODO: 100 is arbitrary value
-
 	# util methods
 	# util methods
 
 
 	def get_status(self,ret):
 	def get_status(self,ret):