Browse Source

CoinProtocol.base: new `rpc_type` attribute

The MMGen Project 6 months ago
parent
commit
70810aff69
3 changed files with 27 additions and 29 deletions
  1. 2 1
      mmgen/proto/rune/params.py
  2. 1 0
      mmgen/protocol.py
  3. 24 28
      mmgen/rpc/__init__.py

+ 2 - 1
mmgen/proto/rune/params.py

@@ -28,12 +28,13 @@ class mainnet(CoinProtocol.Secp256k1):
 	coin_amt        = 'UniAmt'
 	coin_amt        = 'UniAmt'
 	max_tx_fee      = 1 # TODO
 	max_tx_fee      = 1 # TODO
 	caps            = ()
 	caps            = ()
-	mmcaps          = ('tw', 'rpc_init', 'rpc_remote')
+	mmcaps          = ('tw', 'rpc', 'rpc_init')
 	base_proto      = 'THORChain'
 	base_proto      = 'THORChain'
 	base_proto_coin = 'RUNE'
 	base_proto_coin = 'RUNE'
 	base_coin       = 'RUNE'
 	base_coin       = 'RUNE'
 	bech32_hrp      = 'thor'
 	bech32_hrp      = 'thor'
 	sign_mode       = 'standalone'
 	sign_mode       = 'standalone'
+	rpc_type        = 'remote'
 	avg_bdi         = 6 # TODO
 	avg_bdi         = 6 # TODO
 	address_reuse_ok = False
 	address_reuse_ok = False
 
 

+ 1 - 0
mmgen/protocol.py

@@ -57,6 +57,7 @@ class CoinProtocol(MMGenObject):
 		is_fork_of = None
 		is_fork_of = None
 		chain_names = None
 		chain_names = None
 		is_evm = False
 		is_evm = False
+		rpc_type = 'local'
 		networks   = ('mainnet', 'testnet', 'regtest')
 		networks   = ('mainnet', 'testnet', 'regtest')
 		decimal_prec = 28
 		decimal_prec = 28
 		_set_ok = ('tokensym',)
 		_set_ok = ('tokensym',)

+ 24 - 28
mmgen/rpc/__init__.py

@@ -31,38 +31,34 @@ async def rpc_init(
 	if not 'rpc_init' in proto.mmcaps:
 	if not 'rpc_init' in proto.mmcaps:
 		die(1, f'rpc_init() not supported for {proto.name} protocol!')
 		die(1, f'rpc_init() not supported for {proto.name} protocol!')
 
 
-	mod, clsname = (
-		('local', 'RPCClient') if 'rpc' in proto.mmcaps else
-		('remote', 'RemoteRPCClient') if 'rpc_remote' in proto.mmcaps else
-		(None, None))
+	if proto.rpc_type == 'remote':
+		return getattr(importlib.import_module(
+			f'mmgen.proto.{proto.base_proto_coin.lower()}.rpc.remote'),
+				proto.base_proto + 'RemoteRPCClient')(cfg=cfg, proto=proto)
 
 
-	cls = getattr(
-		importlib.import_module(f'mmgen.proto.{proto.base_proto_coin.lower()}.rpc.{mod}'),
-			proto.base_proto + clsname)
+	from ..daemon import CoinDaemon
 
 
-	if mod == 'local':
-		from ..daemon import CoinDaemon
-		rpc = await cls(
-			cfg           = cfg,
-			proto         = proto,
-			daemon        = daemon or CoinDaemon(cfg, proto=proto, test_suite=cfg.test_suite),
-			backend       = backend or cfg.rpc_backend,
-			ignore_wallet = ignore_wallet)
+	rpc = await getattr(importlib.import_module(
+			f'mmgen.proto.{proto.base_proto_coin.lower()}.rpc.local'),
+				proto.base_proto + 'RPCClient')(
+		cfg           = cfg,
+		proto         = proto,
+		daemon        = daemon or CoinDaemon(cfg, proto=proto, test_suite=cfg.test_suite),
+		backend       = backend or cfg.rpc_backend,
+		ignore_wallet = ignore_wallet)
 
 
-		if rpc.daemon_version > rpc.daemon.coind_version:
-			rpc.handle_unsupported_daemon_version(
-				proto.name,
-				ignore_daemon_version or proto.ignore_daemon_version or cfg.ignore_daemon_version)
+	if rpc.daemon_version > rpc.daemon.coind_version:
+		rpc.handle_unsupported_daemon_version(
+			proto.name,
+			ignore_daemon_version or proto.ignore_daemon_version or cfg.ignore_daemon_version)
 
 
-		if rpc.chain not in proto.chain_names:
-			die('RPCChainMismatch', '\n' + fmt(f"""
-				Protocol:           {proto.cls_name}
-				Valid chain names:  {fmt_list(proto.chain_names, fmt='bare')}
-				RPC client chain:   {rpc.chain}
-				""", indent='  ').rstrip())
+	if rpc.chain not in proto.chain_names:
+		die('RPCChainMismatch', '\n' + fmt(f"""
+			Protocol:           {proto.cls_name}
+			Valid chain names:  {fmt_list(proto.chain_names, fmt='bare')}
+			RPC client chain:   {rpc.chain}
+			""", indent='  ').rstrip())
 
 
-		rpc.blockcount = NonNegativeInt(rpc.blockcount)
-	else:
-		rpc = cls(cfg=cfg, proto=proto)
+	rpc.blockcount = NonNegativeInt(rpc.blockcount)
 
 
 	return rpc
 	return rpc