xmrwallet.py: perform late initialization of TX relay daemon

This commit is contained in:
The MMGen Project 2021-06-27 20:58:00 +00:00
commit e40317a8f8
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
2 changed files with 45 additions and 61 deletions

View file

@ -164,8 +164,6 @@ class MoneroWalletOps:
passwd = self.wd.passwd
)
self.post_init()
def create_addr_data(self):
if uarg.wallets:
idxs = AddrIdxList(uarg.wallets)
@ -181,8 +179,6 @@ class MoneroWalletOps:
if uopt.tx_relay_daemon:
self.wd2.stop()
def post_init(self): pass
def get_wallet_fn(self,d):
return os.path.join(
opt.outdir or '.','{}-{}-MoneroWallet{}{}'.format(
@ -214,14 +210,21 @@ class MoneroWalletOps:
self.d = 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...')
ret = await self.c.call( # returns {}
await self.c.call( # returns {}
'open_wallet',
filename=os.path.basename(self.fn),
password=self.d.wallet_passwd )
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):
gmsg_r(f'\n Closing {desc} wallet...')
await self.c.call('close_wallet')
@ -231,14 +234,14 @@ class MoneroWalletOps:
d = data['subaddress_accounts']
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)
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):
msg(fs.format(
str(e['account_index']),
e['base_address'][:15] + '...',
len(addrs_data[i]['addresses']),
e['label'],
fmt_amt(e['balance']),
fmt_amt(e['unlocked_balance']),
))
async def get_accts(self,print=True):
@ -389,6 +392,14 @@ class MoneroWalletOps:
desc = 'Sync'
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):
chain_height = (await self.dc.call('get_height'))['height']
@ -440,11 +451,6 @@ class MoneroWalletOps:
await self.c.call('close_wallet')
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):
d = self.accts_data
@ -505,32 +511,30 @@ class MoneroWalletOps:
self.dest_addr = CoinAddr(self.proto,m[3])
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):
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')
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}')
@ -590,10 +594,11 @@ class MoneroWalletOps:
w_desc = 'source'
if uopt.tx_relay_daemon:
await h.close_wallet('source')
self.c = self.c2
msg('')
self.init_tx_relay_daemon()
h = self.rpc(self,self.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()
msg_r(f'\n Relaying {self.name} transaction...')
await h.relay_tx(tx_metadata)

View file

@ -63,8 +63,7 @@ class TestSuiteXMRWallet(TestSuiteBase):
('mine_blocks', 'mining blocks'),
('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)'),
('mine_blocks', 'mining blocks'),
@ -325,10 +324,7 @@ class TestSuiteXMRWallet(TestSuiteBase):
return 'ok'
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):
data = self.users['alice']
@ -443,7 +439,7 @@ class TestSuiteXMRWallet(TestSuiteBase):
ret = await self.users['miner'].md_rpc.call('stop_mining')
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
- optionally create and broadcast random TXs
@ -470,18 +466,6 @@ class TestSuiteXMRWallet(TestSuiteBase):
else:
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():
from mmgen.tool import tool_api
t = tool_api()
@ -528,8 +512,6 @@ class TestSuiteXMRWallet(TestSuiteBase):
while True:
ub = await get_balance(self.dest)
if self.dest.test(ub):
if extra_blocks:
await mine_extra_blocks()
imsg('')
oqmsg_r('+')
print_balance(self.dest,ub)
@ -551,9 +533,6 @@ class TestSuiteXMRWallet(TestSuiteBase):
async def mine_blocks_tx(self):
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
def get_status(self,ret):