Browse Source

*RPCConnection(): define RPC methods at instance creation time

MMGen 6 years ago
parent
commit
82cd194ed3
2 changed files with 20 additions and 20 deletions
  1. 19 18
      mmgen/rpc.py
  2. 1 2
      mmgen/util.py

+ 19 - 18
mmgen/rpc.py

@@ -32,11 +32,13 @@ class RPCFailure(Exception): pass
 
 
 class CoinDaemonRPCConnection(object):
 class CoinDaemonRPCConnection(object):
 
 
-	def __init__(self,host=None,port=None,user=None,passwd=None,auth_cookie=None,auth=True):
+	auth = True
+	db_fs = '    host [{h}] port [{p}] user [{u}] passwd [{pw}] auth_cookie [{c}]\n'
+
+	def __init__(self,host=None,port=None,user=None,passwd=None,auth_cookie=None):
 
 
 		dmsg_rpc('=== {}.__init__() debug ==='.format(type(self).__name__))
 		dmsg_rpc('=== {}.__init__() debug ==='.format(type(self).__name__))
-		dmsg_rpc('    host [{}] port [{}] user [{}] passwd [{}] auth_cookie [{}]\n'.format(
-			host,port,user,passwd,auth_cookie))
+		dmsg_rpc(self.db_fs.format(h=host,p=port,u=user,pw=passwd,c=auth_cookie))
 
 
 		import socket
 		import socket
 		try:
 		try:
@@ -44,8 +46,8 @@ class CoinDaemonRPCConnection(object):
 		except:
 		except:
 			die(1,'Unable to connect to {}:{}'.format(host,port))
 			die(1,'Unable to connect to {}:{}'.format(host,port))
 
 
-		if not auth:
-			self.auth_str = ''
+		if not self.auth:
+			pass
 		elif user and passwd:
 		elif user and passwd:
 			self.auth_str = '{}:{}'.format(user,passwd)
 			self.auth_str = '{}:{}'.format(user,passwd)
 		elif auth_cookie:
 		elif auth_cookie:
@@ -67,6 +69,10 @@ class CoinDaemonRPCConnection(object):
 		self.host = host
 		self.host = host
 		self.port = port
 		self.port = port
 
 
+		for method in self.rpcmethods:
+			exec '{c}.{m} = lambda self,*args,**kwargs: self.request("{m}",*args,**kwargs)'.format(
+						c=type(self).__name__,m=method)
+
 	# Normal mode: call with arg list unrolled, exactly as with cli
 	# Normal mode: call with arg list unrolled, exactly as with cli
 	# Batch mode:  call with list of arg lists as first argument
 	# Batch mode:  call with list of arg lists as first argument
 	# kwargs are for local use and are not passed to server
 	# kwargs are for local use and are not passed to server
@@ -113,12 +119,11 @@ class CoinDaemonRPCConnection(object):
 				return json.JSONEncoder.default(self,obj)
 				return json.JSONEncoder.default(self,obj)
 
 
 		http_hdr = { 'Content-Type': 'application/json' }
 		http_hdr = { 'Content-Type': 'application/json' }
-		if self.auth_str:
-			dmsg_rpc('    RPC AUTHORIZATION data ==> raw: [{}]\n{}enc: [Basic {}]\n'.format(
-				self.auth_str,' '*31,base64.b64encode(self.auth_str)))
-			http_hdr.update({
-				'Host': self.host,
-				'Authorization': 'Basic {}'.format(base64.b64encode(self.auth_str)) })
+		if self.auth:
+			fs = '    RPC AUTHORIZATION data ==> raw: [{}]\n{:>31}enc: [Basic {}]\n'
+			as_enc = base64.b64encode(self.auth_str)
+			dmsg_rpc(fs.format(self.auth_str,'',as_enc))
+			http_hdr.update({ 'Host':self.host, 'Authorization':'Basic {}'.format(as_enc) })
 
 
 		try:
 		try:
 			hc.request('POST','/',json.dumps(p,cls=MyJSONEncoder),http_hdr)
 			hc.request('POST','/',json.dumps(p,cls=MyJSONEncoder),http_hdr)
@@ -198,11 +203,11 @@ class CoinDaemonRPCConnection(object):
 		'walletpassphrase',
 		'walletpassphrase',
 	)
 	)
 
 
-	for name in rpcmethods:
-		exec "def {n}(self,*a,**k):return self.request('{n}',*a,**k)\n".format(n=name)
-
 class EthereumRPCConnection(CoinDaemonRPCConnection):
 class EthereumRPCConnection(CoinDaemonRPCConnection):
 
 
+	auth = False
+	db_fs = '    host [{h}] port [{p}]\n'
+
 	rpcmethods = (
 	rpcmethods = (
 		'eth_accounts',
 		'eth_accounts',
 		'eth_blockNumber',
 		'eth_blockNumber',
@@ -234,10 +239,6 @@ class EthereumRPCConnection(CoinDaemonRPCConnection):
 		'parity_versionInfo',
 		'parity_versionInfo',
 	)
 	)
 
 
-	for name in rpcmethods:
-		exec "def {n}(self,*a,**k):return self.request('{n}',*a,**k)\n".format(n=name)
-
-
 def rpc_error(ret):
 def rpc_error(ret):
 	return type(ret) is tuple and ret and ret[0] == 'rpcfail'
 	return type(ret) is tuple and ret and ret[0] == 'rpcfail'
 
 

+ 1 - 2
mmgen/util.py

@@ -842,8 +842,7 @@ def rpc_init(reinit=False):
 	if g.coin == 'ETH':
 	if g.coin == 'ETH':
 		conn = mmgen.rpc.EthereumRPCConnection(
 		conn = mmgen.rpc.EthereumRPCConnection(
 					g.rpc_host or 'localhost',
 					g.rpc_host or 'localhost',
-					g.rpc_port or g.proto.rpc_port,
-					auth=False)
+					g.rpc_port or g.proto.rpc_port)
 		if not g.daemon_version: # First call
 		if not g.daemon_version: # First call
 			g.daemon_version = conn.parity_versionInfo()['version'] # fail immediately if daemon is geth
 			g.daemon_version = conn.parity_versionInfo()['version'] # fail immediately if daemon is geth
 			g.chain = conn.parity_chain()
 			g.chain = conn.parity_chain()