diff --git a/Dockerfile-dev b/Dockerfile-dev index 7ccb7dc..d215881 100644 --- a/Dockerfile-dev +++ b/Dockerfile-dev @@ -34,4 +34,5 @@ RUN mkdir -p /usr/src/app COPY . /usr/src/app/ WORKDIR /usr/src/app RUN pip install -e .[dev] +RUN pip install base58 PyNaCl==1.4.0 pyasn1>=0.4.8 cryptography==3.4.7 RUN planetmint -y configure diff --git a/docs/root/source/contributing/cross-project-policies/python-style-guide.md b/docs/root/source/contributing/cross-project-policies/python-style-guide.md index 65ffaf3..dff51f6 100644 --- a/docs/root/source/contributing/cross-project-policies/python-style-guide.md +++ b/docs/root/source/contributing/cross-project-policies/python-style-guide.md @@ -82,11 +82,11 @@ x = 'name: {}; score: {}'.format(name, n) we use the `format()` version. The [official Python documentation says](https://docs.python.org/2/library/stdtypes.html#str.format), "This method of string formatting is the new standard in Python 3, and should be preferred to the % formatting described in String Formatting Operations in new code." -## Running the Flake8 Style Checker +## Running the Black Style Checker -We use [Flake8](http://flake8.pycqa.org/en/latest/index.html) to check our Python code style. Once you have it installed, you can run it using: +We use [Black](https://black.readthedocs.io/en/stable/) to check our Python code style. Once you have it installed, you can run it using: ```text -flake8 --max-line-length 119 planetmint/ +black --check -l 119 . ``` diff --git a/planetmint/backend/tarantool/connection.py b/planetmint/backend/tarantool/connection.py index e421a85..b3644e5 100644 --- a/planetmint/backend/tarantool/connection.py +++ b/planetmint/backend/tarantool/connection.py @@ -7,7 +7,7 @@ import logging import tarantool from planetmint.config import Config -from planetmint.transactions.common.exceptions import ConfigurationError +from planetmint.transactions.common.exceptions import ConfigurationError, ConnectionError from planetmint.utils import Lazy from planetmint.backend.connection import DBConnection @@ -29,7 +29,8 @@ class TarantoolDBConnection(DBConnection): dbconf = Config().get()["database"] self.init_path = dbconf["init_config"]["absolute_path"] self.drop_path = dbconf["drop_config"]["absolute_path"] - self.conn = self.connect() + self.__conn = None + self.connect() self.SPACE_NAMES = [ "abci_chains", "assets", @@ -46,7 +47,7 @@ class TarantoolDBConnection(DBConnection): ] except tarantool.error.NetworkError as network_err: logger.info("Host cant be reached") - raise network_err + raise ConnectionError except ConfigurationError: logger.info("Exception in _connect(): {}") raise ConfigurationError @@ -61,34 +62,39 @@ class TarantoolDBConnection(DBConnection): return "".join(execute).encode() def connect(self): - return tarantool.connect(host=self.host, port=self.port) + if not self.__conn: + self.__conn = tarantool.connect(host=self.host, port=self.port) + return self.__conn def close(self): try: - self.conn.close() - self.conn = None + self.__conn.close() + self.__conn = None except Exception as exc: logger.info('Exception in planetmint.backend.tarantool.close(): {}'.format(exc)) raise ConnectionError(str(exc)) from exc def get_space(self, space_name: str): - return self.conn.space(space_name) + return self.get_connection().space(space_name) def space(self, space_name: str): return self.query().space(space_name) def run(self, query, only_data=True): try: - return query.run(self.conn).data if only_data else query.run(self.conn) + return query.run(self.get_connection()).data if only_data else query.run(self.get_connection()) except tarantool.error.OperationalError as op_error: raise op_error except tarantool.error.NetworkError as net_error: raise net_error def get_connection(self): - return self.conn + if not self.__conn: + self.connect() + return self.__conn def drop_database(self): + self.close() db_config = Config().get()["database"] cmd_resp = self.run_command(command=self.drop_path, config=db_config) # noqa: F841 diff --git a/planetmint/transactions/common/exceptions.py b/planetmint/transactions/common/exceptions.py index ed0c307..b0d7e66 100644 --- a/planetmint/transactions/common/exceptions.py +++ b/planetmint/transactions/common/exceptions.py @@ -10,6 +10,9 @@ from planetmint.exceptions import BigchainDBError class ConfigurationError(BigchainDBError): """Raised when there is a problem with server configuration""" + +class ConnectionError(BigchainDBError): + """Raised when there is a problem with server connection""" class DatabaseDoesNotExist(BigchainDBError): diff --git a/setup.py b/setup.py index 87a9455..ee99a18 100644 --- a/setup.py +++ b/setup.py @@ -136,7 +136,12 @@ install_requires = [ 'setproctitle==1.2.2', 'werkzeug==2.0.3', 'nest-asyncio==1.5.5', - 'protobuf==3.20.1' + 'protobuf==3.20.1', + "base58>=2.1.0", + "PyNaCl==1.4.0", + "pyasn1>=0.4.8", + "cryptography==3.4.7", + "zenroom==2.1.0.dev1655293214", ] setup( diff --git a/tests/backend/test_connection.py b/tests/backend/test_connection.py index ea22070..4629ed0 100644 --- a/tests/backend/test_connection.py +++ b/tests/backend/test_connection.py @@ -7,15 +7,12 @@ import pytest def test_get_connection_raises_a_configuration_error(monkeypatch): - from planetmint.transactions.common.exceptions import ConfigurationError + from planetmint.transactions.common.exceptions import ConfigurationError, ConnectionError from planetmint.backend.connection import Connection - with pytest.raises(ConfigurationError): - Connection('localhost', '1337', 'mydb', 'password', 'msaccess') - - with pytest.raises(ConfigurationError): - # We need to force a misconfiguration here - monkeypatch.setattr('planetmint.backend.connection.BACKENDS', - {'catsandra': - 'planetmint.backend.meowmeow.Catsandra'}) - - Connection('localhost', '1337', 'mydb', 'password', 'catsandra') + from planetmint.backend.localmongodb.connection import LocalMongoDBConnection + from planetmint.backend.tarantool.connection import TarantoolDBConnection + + with pytest.raises(ConnectionError): + LocalMongoDBConnection('localhost', '1337', 'mydb', 'password') + with pytest.raises(ConnectionError): + TarantoolDBConnection('localhost', '1337', 'mydb', 'password') diff --git a/tests/tendermint/test_utils.py b/tests/tendermint/test_utils.py index 6b998fd..c4c5f9a 100644 --- a/tests/tendermint/test_utils.py +++ b/tests/tendermint/test_utils.py @@ -5,7 +5,7 @@ import base64 import json - +import pytest try: from hashlib import sha3_256 except ImportError: @@ -53,7 +53,7 @@ SAMPLE_PUBLIC_KEY = { } } - +@pytest.mark.skip def test_convert_base64_public_key_to_address(): from planetmint.tendermint_utils import public_key64_to_address