From 6c19cd38b075fff1a794688d96030f6a203d4061 Mon Sep 17 00:00:00 2001 From: nolash Date: Thu, 26 Aug 2021 17:10:39 +0200 Subject: [PATCH] Add openethereum error parser, add dialect processor to config from args --- chainlib/eth/cli.py | 20 ++++++++++++++++++++ chainlib/eth/connection.py | 6 ++---- chainlib/eth/data/config/config.ini | 1 + chainlib/eth/dialect/__init__.py | 9 +++++++++ chainlib/eth/dialect/openethereum.py | 17 +++++++++++++++++ chainlib/eth/error.py | 7 ------- setup.cfg | 2 +- 7 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 chainlib/eth/dialect/__init__.py create mode 100644 chainlib/eth/dialect/openethereum.py diff --git a/chainlib/eth/cli.py b/chainlib/eth/cli.py index bc37984..ac8a725 100644 --- a/chainlib/eth/cli.py +++ b/chainlib/eth/cli.py @@ -81,6 +81,11 @@ class Rpc(BaseRpc): else: self.fee_oracle = RPCGasOracle(self.conn, id_generator=self.id_generator) + error_parser = None + if config.get('RPC_DIALECT') == 'openethereum': + from chainlib.eth.dialect.openethereum import DialectErrorParser + self.error_parser = DialectErrorParser() + return self.conn @@ -93,3 +98,18 @@ class Config(BaseConfig): """ default_base_config_dir = os.path.join(script_dir, 'data', 'config') default_fee_limit = 21000 + + + @classmethod + def from_args(cls, args, arg_flags=0x0f, env=os.environ, extra_args={}, base_config_dir=None, default_config_dir=None, user_config_dir=None, default_fee_limit=None, logger=None, load_callback=None): + config = BaseConfig.from_args(args, arg_flags=arg_flags, env=env, extra_args=extra_args, base_config_dir=base_config_dir, default_config_dir=default_config_dir, user_config_dir=user_config_dir, default_fee_limit=default_fee_limit, logger=logger, load_callback=load_callback) + + if not config.get('RPC_DIALECT'): + config.add('default', 'RPC_DIALECT', exists_ok=True) + elif config.get('RPC_DIALECT') not in [ + 'openethereum', + 'default', + ]: + raise ValueError('unknown rpc dialect {}'.format(config.get('RPC_DIALECT'))) + + return config diff --git a/chainlib/eth/connection.py b/chainlib/eth/connection.py index 5397dfe..f0e198a 100644 --- a/chainlib/eth/connection.py +++ b/chainlib/eth/connection.py @@ -17,10 +17,8 @@ from hexathon import ( ) # local imports -from .error import ( - DefaultErrorParser, - RevertEthException, - ) +from .error import RevertEthException +from chainlib.eth.dialect import DefaultErrorParser from .sign import ( sign_transaction, ) diff --git a/chainlib/eth/data/config/config.ini b/chainlib/eth/data/config/config.ini index 70278d1..5be1dea 100644 --- a/chainlib/eth/data/config/config.ini +++ b/chainlib/eth/data/config/config.ini @@ -3,6 +3,7 @@ http_provider = http://localhost:8545 http_authentication = http_username = http_password = +dialect = default [chain] spec = evm:ethereum:1 diff --git a/chainlib/eth/dialect/__init__.py b/chainlib/eth/dialect/__init__.py new file mode 100644 index 0000000..e589c08 --- /dev/null +++ b/chainlib/eth/dialect/__init__.py @@ -0,0 +1,9 @@ +# local imports +from chainlib.eth.error import EthException + + +class DefaultErrorParser: + """Generate eth specific exception for the default json-rpc query error parser. + """ + def translate(self, error): + return EthException('default parser codeĀ {}'.format(error)) diff --git a/chainlib/eth/dialect/openethereum.py b/chainlib/eth/dialect/openethereum.py new file mode 100644 index 0000000..a9a73f2 --- /dev/null +++ b/chainlib/eth/dialect/openethereum.py @@ -0,0 +1,17 @@ +# standard imports +import logging + +# local imports +from chainlib.eth.dialect import DefaultErrorParser +from chainlib.error import RPCNonceException + +logg = logging.getLogger(__name__) + + +class DialectErrorParser(DefaultErrorParser): + + def translate(self, error): + if error['error']['code'] == -32010: + if 'nonce is too low' in error['error']['message']: + return RPCNonceException(error) + return super(DialectErrorParser, self).translate(error) diff --git a/chainlib/eth/error.py b/chainlib/eth/error.py index f33e44b..0d90c61 100644 --- a/chainlib/eth/error.py +++ b/chainlib/eth/error.py @@ -24,10 +24,3 @@ class RequestMismatchException(EthException): """Raised when a request data parser is given unexpected input data. """ pass - - -class DefaultErrorParser: - """Generate eth specific exception for the default json-rpc query error parser. - """ - def translate(self, error): - return EthException('default parser codeĀ {}'.format(error)) diff --git a/setup.cfg b/setup.cfg index ecffa54..c62e9d9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = chainlib-eth -version = 0.0.9a4 +version = 0.0.9a5 description = Ethereum implementation of the chainlib interface author = Louis Holbrook author_email = dev@holbrook.no