From 98d0fff70ff716e1692db8b87ac0d9443ae6f47a Mon Sep 17 00:00:00 2001 From: MMGen Date: Mon, 23 Jul 2018 20:04:35 +0000 Subject: [PATCH] rpc_init() -> rpc_init_parity(),rpc_init_bitcoind() --- mmgen/util.py | 72 ++++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/mmgen/util.py b/mmgen/util.py index 54948f3e..65d473f7 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -822,12 +822,20 @@ def get_coin_daemon_auth_cookie(): f = os.path.join(g.proto.daemon_data_dir,g.proto.daemon_data_subdir,'.cookie') return get_lines_from_file(f,'')[0] if file_is_readable(f) else '' -def rpc_init(reinit=False): +def rpc_init_parity(): - if not 'rpc' in g.proto.mmcaps: - die(1,'Coin daemon operations not supported for coin {}!'.format(g.coin)) + from mmgen.rpc import EthereumRPCConnection + g.rpch = EthereumRPCConnection( + g.rpc_host or 'localhost', + g.rpc_port or g.proto.rpc_port) - if g.rpch != None and not reinit: return g.rpch + if not g.daemon_version: # First call + g.daemon_version = g.rpch.parity_versionInfo()['version'] # fail immediately if daemon is geth + g.chain = g.rpch.parity_chain() + + return g.rpch + +def rpc_init_bitcoind(): def check_chainfork_mismatch(conn): block0 = conn.getblockhash(0) @@ -849,38 +857,38 @@ def rpc_init(reinit=False): except Exception as e: die(1,'{}\nChain is {}!'.format(e,g.chain)) - import mmgen.rpc - if g.coin == 'ETH': - conn = mmgen.rpc.EthereumRPCConnection( - g.rpc_host or 'localhost', - g.rpc_port or g.proto.rpc_port) - if not g.daemon_version: # First call - g.daemon_version = conn.parity_versionInfo()['version'] # fail immediately if daemon is geth - g.chain = conn.parity_chain() - else: - cfg = get_daemon_cfg_options(('rpcuser','rpcpassword')) - conn = mmgen.rpc.CoinDaemonRPCConnection( - g.rpc_host or 'localhost', - g.rpc_port or g.proto.rpc_port, - g.rpc_user or cfg['rpcuser'], # MMGen's rpcuser,rpcpassword override coin daemon's - g.rpc_password or cfg['rpcpassword'], - auth_cookie=get_coin_daemon_auth_cookie()) + cfg = get_daemon_cfg_options(('rpcuser','rpcpassword')) - if not g.daemon_version: # First call - if g.bob or g.alice: - import regtest as rt - rt.user(('alice','bob')[g.bob],quiet=True) - g.daemon_version = int(conn.getnetworkinfo()['version']) - g.chain = conn.getblockchaininfo()['chain'] - if g.chain != 'regtest': g.chain += 'net' - assert g.chain in g.chains - check_chaintype_mismatch() - if g.chain == 'mainnet': # skip this for testnet, as Genesis block may change - check_chainfork_mismatch(conn) + from mmgen.rpc import CoinDaemonRPCConnection + conn = CoinDaemonRPCConnection( + g.rpc_host or 'localhost', + g.rpc_port or g.proto.rpc_port, + g.rpc_user or cfg['rpcuser'], # MMGen's rpcuser,rpcpassword override coin daemon's + g.rpc_password or cfg['rpcpassword'], + auth_cookie=get_coin_daemon_auth_cookie()) + + if not g.daemon_version: # First call + if g.bob or g.alice: + import regtest as rt + rt.user(('alice','bob')[g.bob],quiet=True) + g.daemon_version = int(conn.getnetworkinfo()['version']) + g.chain = conn.getblockchaininfo()['chain'] + if g.chain != 'regtest': g.chain += 'net' + assert g.chain in g.chains + check_chaintype_mismatch() + + if g.chain == 'mainnet': # skip this for testnet, as Genesis block may change + check_chainfork_mismatch(conn) - g.rpch = conn return conn +def rpc_init(reinit=False): + if not 'rpc' in g.proto.mmcaps: + die(1,'Coin daemon operations not supported for coin {}!'.format(g.coin)) + if g.rpch != None and not reinit: return g.rpch + g.rpch = globals()['rpc_init_'+g.proto.daemon_family]() + return g.rpch + def format_par(s,indent=0,width=80,as_list=False): words,lines = s.split(),[] assert width >= indent + 4,'width must be >= indent + 4'