From fe7fd060179f3e87ccb1ce21efad1a654cdd634c Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Thu, 20 Feb 2020 20:47:17 +0000 Subject: [PATCH] [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. --- mmgen/rpc.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/mmgen/rpc.py b/mmgen/rpc.py index 9de35256..06a7ca06 100755 --- a/mmgen/rpc.py +++ b/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',