MoneroRPCClient: make call(), call_raw() methods non-async

This commit is contained in:
The MMGen Project 2023-03-24 20:31:10 +00:00
commit 47db716d7b
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
5 changed files with 48 additions and 45 deletions

View file

@ -51,24 +51,24 @@ class MoneroRPCClient(RPCClient):
self.daemon = daemon
async def call(self,method,*params,**kwargs):
def call(self,method,*params,**kwargs):
assert params == (), f'{type(self).__name__}.call() accepts keyword arguments only'
return self.process_http_resp(await self.backend.run(
return self.process_http_resp(self.backend.run_noasync(
payload = {'id': 0, 'jsonrpc': '2.0', 'method': method, 'params': kwargs },
timeout = 3600, # allow enough time to sync ≈1,000,000 blocks
host_path = '/json_rpc'
))
async def call_raw(self,method,*params,**kwargs):
def call_raw(self,method,*params,**kwargs):
assert params == (), f'{type(self).__name__}.call() accepts keyword arguments only'
return self.process_http_resp(await self.backend.run(
return self.process_http_resp(self.backend.run_noasync(
payload = kwargs,
timeout = self.timeout,
host_path = f'/{method}'
),json_rpc=False)
async def do_stop_daemon(self,silent=False):
return await self.call_raw('stop_daemon')
return self.call_raw('stop_daemon')
rpcmethods = ( 'get_info', )
rpcmethods_raw = ( 'get_height', 'send_raw_transaction', 'stop_daemon' )
@ -109,4 +109,4 @@ class MoneroWalletRPCClient(MoneroRPCClient):
NB: the 'stop_wallet' RPC call closes the open wallet before shutting down the daemon,
returning an error if no wallet is open
"""
return await self.call('stop_wallet')
return self.call('stop_wallet')

View file

@ -149,7 +149,10 @@ class RPCBackends:
'https': f'socks5h://{self.proxy}'
})
async def run(self,payload,timeout,host_path):
async def run(self,*args,**kwargs):
return self.run_noasync(*args,**kwargs)
def run_noasync(self,payload,timeout,host_path):
dmsg_rpc_backend(self.host_url,host_path,payload)
res = self.session.post(
url = self.host_url + host_path,

View file

@ -426,7 +426,7 @@ class MoneroWalletOps:
async def open_wallet(self,desc,refresh=True):
gmsg_r(f'\n Opening {desc} wallet...')
await self.c.call( # returns {}
self.c.call( # returns {}
'open_wallet',
filename=os.path.basename(self.fn),
password=self.d.wallet_passwd )
@ -434,14 +434,14 @@ class MoneroWalletOps:
if refresh:
gmsg_r(f' Refreshing {desc} wallet...')
ret = await self.c.call('refresh')
ret = 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')
self.c.call('close_wallet')
gmsg_r('done')
async def stop_wallet(self,desc):
@ -465,9 +465,9 @@ class MoneroWalletOps:
))
async def get_accts(self,print=True):
data = await self.c.call('get_accounts')
data = self.c.call('get_accounts')
addrs_data = [
await self.c.call('get_address',account_index=i)
self.c.call('get_address',account_index=i)
for i in range(len(data['subaddress_accounts']))
]
if print:
@ -476,7 +476,7 @@ class MoneroWalletOps:
async def create_acct(self,label=None):
msg('\n Creating new account...')
ret = await self.c.call(
ret = self.c.call(
'create_account',
label = label or 'Sweep from {}:{} [{}]'.format(
self.parent.source.idx,
@ -494,7 +494,7 @@ class MoneroWalletOps:
return (ret['account_index'], ret['base_address'])
async def print_addrs(self,accts_data,account):
ret = await self.c.call('get_address',account_index=account)
ret = self.c.call('get_address',account_index=account)
d = ret['addresses']
msg('\n Addresses of account #{} ({}):'.format(
account,
@ -511,7 +511,7 @@ class MoneroWalletOps:
async def create_new_addr(self,account,label=None):
msg_r('\n Creating new address: ')
ret = await self.c.call(
ret = self.c.call(
'create_address',
account_index = account,
label = label or f'Sweep from this account [{make_timestr()}]',
@ -522,17 +522,17 @@ class MoneroWalletOps:
async def get_last_addr(self,account,display=True):
if display:
msg('\n Getting last address:')
ret = (await self.c.call(
ret = self.c.call(
'get_address',
account_index = account,
))['addresses']
)['addresses']
addr = ret[-1]['address']
if display:
msg(' ' + cyan(addr))
return ( addr, len(ret) - 1 )
async def make_transfer_tx(self,account,addr,amt):
res = await self.c.call(
res = self.c.call(
'transfer',
account_index = account,
destinations = [{
@ -558,7 +558,7 @@ class MoneroWalletOps:
)
async def make_sweep_tx(self,account,dest_acct,dest_addr_idx,addr):
res = await self.c.call(
res = self.c.call(
'sweep_all',
address = addr,
account_index = account,
@ -588,7 +588,7 @@ class MoneroWalletOps:
)
async def relay_tx(self,tx_hex):
ret = await self.c.call('relay_tx',hex=tx_hex)
ret = self.c.call('relay_tx',hex=tx_hex)
try:
msg('\n Relayed {}'.format( CoinTxID(ret['tx_hash']).hl() ))
except:
@ -608,7 +608,7 @@ class MoneroWalletOps:
msg_r('') # for pexpect
from .xmrseed import xmrseed
ret = await self.c.call(
ret = self.c.call(
'restore_deterministic_wallet',
filename = os.path.basename(fn),
password = d.wallet_passwd,
@ -636,20 +636,20 @@ class MoneroWalletOps:
self.accts_data = {}
async def process_wallet(self,d,fn,last):
chain_height = (await self.dc.call_raw('get_height'))['height']
chain_height = self.dc.call_raw('get_height')['height']
msg(f' Chain height: {chain_height}')
t_start = time.time()
msg_r(' Opening wallet...')
await self.c.call(
self.c.call(
'open_wallet',
filename=os.path.basename(fn),
password=d.wallet_passwd )
msg('done')
msg_r(' Getting wallet height (be patient, this could take a long time)...')
wallet_height = (await self.c.call('get_height'))['height']
wallet_height = self.c.call('get_height')['height']
msg_r('\r' + ' '*68 + '\r')
msg(f' Wallet height: {wallet_height} ')
@ -657,7 +657,7 @@ class MoneroWalletOps:
if behind > 1000:
msg_r(f' Wallet is {behind} blocks behind chain tip. Please be patient. Syncing...')
ret = await self.c.call('refresh')
ret = self.c.call('refresh')
if behind > 1000:
msg('done')
@ -666,15 +666,15 @@ class MoneroWalletOps:
msg(' Wallet has received funds')
for i in range(2):
wallet_height = (await self.c.call('get_height'))['height']
wallet_height = self.c.call('get_height')['height']
if wallet_height >= chain_height:
break
ymsg(f' Wallet failed to sync (wallet height [{wallet_height}] < chain height [{chain_height}])')
if i or not uopt.rescan_blockchain:
break
msg_r(' Rescanning blockchain, please be patient...')
await self.c.call('rescan_blockchain')
await self.c.call('refresh')
self.c.call('rescan_blockchain')
self.c.call('refresh')
msg('done')
t_elapsed = int(time.time() - t_start)
@ -696,7 +696,7 @@ class MoneroWalletOps:
t_elapsed % 60 ))
if not last:
await self.c.call('close_wallet')
self.c.call('close_wallet')
return wallet_height >= chain_height
@ -951,7 +951,7 @@ class MoneroWalletOps:
self.display_tx_relay_info()
if keypress_confirm('Relay transaction?'):
res = await self.dc.call_raw(
res = self.dc.call_raw(
'send_raw_transaction',
tx_as_hex = self.tx.data.blob
)

View file

@ -534,7 +534,7 @@ class TestSuiteXMRWallet(TestSuiteBase):
kal = KeyAddrList(self.proto,data.kafile,key_address_validity_check=False)
end_silence()
self.users[user].wd.start(silent=not (opt.exact_output or opt.verbose))
return await data.wd_rpc.call(
return data.wd_rpc.call(
'open_wallet',
filename = os.path.basename(data.walletfile_fs.format(wnum)),
password = kal.entry(wnum).wallet_passwd )
@ -550,7 +550,7 @@ class TestSuiteXMRWallet(TestSuiteBase):
addr = read_from_file(data.addrfile_fs.format(1)) # mine to wallet #1, account 0
for i in range(20):
ret = await data.md_rpc.call_raw(
ret = data.md_rpc.call_raw(
'start_mining',
do_background_mining = False, # run mining in background or foreground
ignore_battery = True, # ignore battery state (on laptop)
@ -568,7 +568,7 @@ class TestSuiteXMRWallet(TestSuiteBase):
die(2,'Max retries exceeded')
async def stop_mining(self):
ret = await self.users['miner'].md_rpc.call_raw('stop_mining')
ret = self.users['miner'].md_rpc.call_raw('stop_mining')
return self.get_status(ret)
async def mine_chk(self,user,wnum,account,test,test_desc,random_txs=None,return_amt=False):
@ -585,7 +585,7 @@ class TestSuiteXMRWallet(TestSuiteBase):
u = self.users['miner']
for i in range(20):
try:
return (await u.md_rpc.call('get_last_block_header'))['block_header']['height']
return u.md_rpc.call('get_last_block_header')['block_header']['height']
except Exception as e:
if 'onnection refused' in str(e):
omsg(f'{e}\nMonerod appears to have crashed. Attempting to restart...')
@ -625,10 +625,10 @@ class TestSuiteXMRWallet(TestSuiteBase):
async def get_balance(dest,count):
data = self.users[dest.user]
await data.wd_rpc.call('refresh')
data.wd_rpc.call('refresh')
if count and not count % 20:
await data.wd_rpc.call('rescan_blockchain')
ret = await data.wd_rpc.call('get_accounts')
data.wd_rpc.call('rescan_blockchain')
ret = data.wd_rpc.call('get_accounts')
return XMRAmt(ret['subaddress_accounts'][dest.account]['unlocked_balance'],from_unit='atomic')
# start execution:
@ -690,7 +690,7 @@ class TestSuiteXMRWallet(TestSuiteBase):
)
async def transfer(self,user,amt,addr):
return await self.users[user].wd_rpc.call('transfer',destinations=[{'amount':amt,'address':addr}])
return self.users[user].wd_rpc.call('transfer',destinations=[{'amount':amt,'address':addr}])
# daemon start/stop methods

View file

@ -159,16 +159,16 @@ class unit_tests:
def xmrwallet(self,name,ut):
async def test_monerod_rpc(md):
md = MoneroRPCClient(
def test_monerod_rpc(md):
rpc = MoneroRPCClient(
host = md.host,
port = md.rpc_port,
user = None,
passwd = None,
daemon = md,
)
await md.call_raw('get_height')
await md.call('get_last_block_header')
rpc.call_raw('get_height')
rpc.call('get_last_block_header')
async def run():
networks = init_proto('xmr').networks
@ -186,18 +186,18 @@ class unit_tests:
md.start()
wd.start()
await test_monerod_rpc(md)
test_monerod_rpc(md)
c = MoneroWalletRPCClient(daemon=wd)
fn = f'monero-{wd.network}-junk-wallet'
qmsg(f'Creating {wd.network} wallet')
await c.call(
c.call(
'restore_deterministic_wallet',
filename = fn,
password = 'foo',
seed = xmrseed().fromhex('beadface'*8,tostr=True) )
qmsg(f'Opening {wd.network} wallet')
await c.call( 'open_wallet', filename=fn, password='foo' )
c.call( 'open_wallet', filename=fn, password='foo' )
for md,wd in daemons:
wd.wait = False