commit 3c4968756967eed4355c22ac94dbb7cbb5ad9f8a
parent b1096ba20a46fadee35e63682ead3ab0603df39f
Author: lash <dev@holbrook.no>
Date: Sun, 6 Aug 2023 13:48:38 +0100
Enable jsonrpc batch requests
Diffstat:
8 files changed, 26 insertions(+), 5 deletions(-)
diff --git a/CHANGELOG b/CHANGELOG
@@ -1,3 +1,5 @@
+- 0.4.19
+ * Enable JSON-RPC batch requests
- 0.4.18
* Add interactive passphrase input when missing for keyfile dependent actions
* Add -z flag to indicate empty passphrase
diff --git a/chainlib/cli/arg.py b/chainlib/cli/arg.py
@@ -159,3 +159,5 @@ class Arg(BaseArg):
self.add('o', 'tab', typ=str, action='append', help='Include output by key name')
self.set_long('o', 'output-key')
+
+ self.add_long('rpc-batch-limit', 'provider', typ=int, help='limit of bundled rpc requests (0 = no limit, 1 = default)')
diff --git a/chainlib/cli/config.py b/chainlib/cli/config.py
@@ -64,6 +64,7 @@ def process_config(config, arg, args, flags, positional_name=None):
if arg.match('provider', flags):
args_override['RPC_PROVIDER'] = getattr(args, 'p')
args_override['RPC_DIALECT'] = getattr(args, 'rpc_dialect')
+ args_override['RPC_BATCH_LIMIT'] = getattr(args, 'rpc_batch_limit')
if arg.match('chain_spec', flags):
args_override['CHAIN_SPEC'] = getattr(args, 'i')
diff --git a/chainlib/connection.py b/chainlib/connection.py
@@ -338,9 +338,17 @@ class JSONRPCHTTPConnection(HTTPConnection):
resp = r.read()
logg.debug('(HTTP) recv {}'.format(resp.decode('utf-8')))
result = json.loads(resp)
- if o['id'] != result['id']:
- raise ValueError('RPC id mismatch; sent {} received {}'.format(o['id'], result['id']))
- return jsonrpc_result(result, error_parser)
+ if type(result).__name__ != 'list':
+ if o['id'] != result['id']:
+ raise ValueError('RPC id mismatch; sent {} received {}'.format(o['id'], result['id']))
+ return jsonrpc_result(result, error_parser)
+
+ results = []
+ for i in range(len(o)):
+ if o[i]['id'] != result[i]['id']:
+ raise ValueError('RPC id mismatch; sent {} received {}'.format(o[i]['id'], result[i]['id']))
+ results.append(jsonrpc_result(result[i], error_parser))
+ return results
class JSONRPCUnixConnection(UnixConnection):
diff --git a/chainlib/data/config/config.ini b/chainlib/data/config/config.ini
@@ -7,6 +7,7 @@ scheme = http
verify = 1
timeout = 10.0
proxy =
+batch_limit = 1
[chain]
spec =
diff --git a/chainlib/interface.py b/chainlib/interface.py
@@ -20,7 +20,8 @@ class ChainInterface:
raise NotImplementedError()
- def __init__(self, dialect_filter=None):
+ def __init__(self, dialect_filter=None, batch_limit=1):
+ self.batch_limit = batch_limit
self._block_latest = self.__unimplemented
self._block_by_hash = self.__unimplemented
self._block_by_number = self.__unimplemented
diff --git a/chainlib/settings.py b/chainlib/settings.py
@@ -39,6 +39,11 @@ def process_settings_dialect(settings, config):
return settings
+def process_settings_batch(settings, config):
+ settings.set('RPC_BATCH_LIMIT', int(config.get('RPC_BATCH_LIMIT')))
+ return settings
+
+
def process_settings_common(settings, config):
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
settings.set('CHAIN_SPEC', chain_spec)
@@ -72,6 +77,7 @@ def process_settings_value(settings, config):
def process_settings(settings, config):
settings = process_settings_common(settings, config)
settings = process_settings_value(settings, config)
+ settings = process_settings_batch(settings, config)
settings = process_settings_dialect(settings, config)
settings = process_settings_signer_check(settings, config)
return settings
diff --git a/setup.cfg b/setup.cfg
@@ -3,7 +3,7 @@ name=chainlib
license=AGPLv3+
author_email=dev@holbrook.no
description=Generic blockchain access library and tooling
-version=0.4.18
+version=0.4.19
url=https://git.defalsify.org/chainlib
author=Louis Holbrook