Browse Source

[msys2]: use Curl for RPC

Workaround necessitated by severe performance issues with the Python HTTP
module on MSYS2.  Other Python HTTP libraries seem to be affected too.
The MMGen Project 5 years ago
parent
commit
fe7fd06017
1 changed files with 33 additions and 0 deletions
  1. 33 0
      mmgen/rpc.py

+ 33 - 0
mmgen/rpc.py

@@ -123,6 +123,13 @@ class RPCConnection(MMGenObject):
 
 		data = json.dumps(p,cls=MyJSONEncoder)
 
+		if g.platform == 'win' and len(data) < 4096: # set way below ARG_MAX, just to be safe
+			return self.do_request_curl(data,cf)
+		else:
+			return self.do_request_httplib(data,cf)
+
+	def do_request_httplib(self,data,cf):
+
 		def do_fail(*args): # args[0] is either None or HTTPResponse object
 			if cf['on_fail'] in ('return','silent'): return 'rpcfail',args
 
@@ -184,6 +191,32 @@ class RPCConnection(MMGenObject):
 
 		return ret if cf['batch'] else ret[0]
 
+	def do_request_curl(self,data,cf):
+		from subprocess import run,PIPE
+		exec_cmd = ['curl', '--proxy', '', '--silent','--request', 'POST', '--data-binary', data]
+		for k,v in self.http_hdrs.items():
+			exec_cmd += ['--header', '{}: {}'.format(k,v)]
+		if self.auth:
+			exec_cmd += ['--user', self.auth_str]
+		exec_cmd += ['http://{}:{}/'.format(self.host,self.port)]
+
+		cp = run(exec_cmd,stdout=PIPE,check=True)
+		res = json.loads(cp.stdout,parse_float=Decimal)
+		dmsg_rpc('    RPC RESULT data ==>\n{}\n',res)
+
+		def do_fail(s):
+			if cf['on_fail'] in ('return','silent'):
+				return ('rpcfail',s)
+			raise RPCFailure(s)
+
+		for resp in ([res],res)[cf['batch']]:
+			if 'error' in resp and resp['error'] != None:
+				return do_fail('{} returned an error: {}'.format(g.proto.daemon_name,resp['error']))
+			elif 'result' not in resp:
+				return do_fail('Missing JSON-RPC result\n{!r}'.format(resp))
+
+		return [r['result'] for r in res] if cf['batch'] else res['result']
+
 	rpcmethods = (
 		'backupwallet',
 		'createrawtransaction',