diff --git a/bigchaindb/README.md b/bigchaindb/README.md index dbb08afb..3ed61495 100644 --- a/bigchaindb/README.md +++ b/bigchaindb/README.md @@ -4,7 +4,7 @@ A high-level description of the files and subdirectories of BigchainDB. ## Files -### [`tendermint/lib.py`](./tendermint/lib.py) +### [`lib.py`](lib.py) The `BigchainDB` class is defined here. Most node-level operations and database interactions are found in this file. This is the place to start if you are interested in implementing a server API, since many of these class methods concern BigchainDB interacting with the outside world. diff --git a/bigchaindb/__init__.py b/bigchaindb/__init__.py index 011737fa..05bf85a0 100644 --- a/bigchaindb/__init__.py +++ b/bigchaindb/__init__.py @@ -2,6 +2,9 @@ import copy import logging from bigchaindb.log import DEFAULT_LOGGING_CONFIG as log_config +from bigchaindb.lib import BigchainDB # noqa +from bigchaindb.version import __version__ # noqa +from bigchaindb.core import App # noqa # from functools import reduce # PORT_NUMBER = reduce(lambda x, y: x * y, map(ord, 'BigchainDB')) % 2**16 @@ -84,5 +87,3 @@ config = { # the user wants to reconfigure the node. Check ``bigchaindb.config_utils`` # for more info. _config = copy.deepcopy(config) -from bigchaindb.tendermint import BigchainDB # noqa -from bigchaindb.version import __version__ # noqa diff --git a/bigchaindb/commands/bigchaindb.py b/bigchaindb/commands/bigchaindb.py index 188cb7b3..379b89a2 100644 --- a/bigchaindb/commands/bigchaindb.py +++ b/bigchaindb/commands/bigchaindb.py @@ -21,7 +21,7 @@ from bigchaindb.commands import utils from bigchaindb.commands.utils import (configure_bigchaindb, input_on_stderr) from bigchaindb.log import setup_logging -from bigchaindb.tendermint.utils import public_key_from_base64 +from bigchaindb.tendermint_utils import public_key_from_base64 logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) @@ -97,7 +97,7 @@ def run_configure(args): def run_upsert_validator(args): """Store validators which should be synced with Tendermint""" - b = bigchaindb.tendermint.BigchainDB() + b = bigchaindb.BigchainDB() public_key = public_key_from_base64(args.public_key) validator = {'pub_key': {'type': 'ed25519', 'data': public_key}, @@ -113,7 +113,7 @@ def run_upsert_validator(args): def _run_init(): - bdb = bigchaindb.tendermint.BigchainDB() + bdb = bigchaindb.BigchainDB() schema.init_database(connection=bdb.connection) @@ -170,7 +170,7 @@ def run_start(args): setup_logging() logger.info('BigchainDB Version %s', bigchaindb.__version__) - run_recover(bigchaindb.tendermint.lib.BigchainDB()) + run_recover(bigchaindb.lib.BigchainDB()) try: if not args.skip_initialize_database: @@ -180,7 +180,7 @@ def run_start(args): pass logger.info('Starting BigchainDB main process.') - from bigchaindb.tendermint.commands import start + from bigchaindb.start import start start() diff --git a/bigchaindb/core.py b/bigchaindb/core.py index e69de29b..354a39a0 100644 --- a/bigchaindb/core.py +++ b/bigchaindb/core.py @@ -0,0 +1,178 @@ +"""This module contains all the goodness to integrate BigchainDB +with Tendermint.""" +import logging + +from abci.application import BaseApplication +from abci.types_pb2 import ( + ResponseInitChain, + ResponseInfo, + ResponseCheckTx, + ResponseBeginBlock, + ResponseDeliverTx, + ResponseEndBlock, + ResponseCommit, + Validator, + PubKey +) + +from bigchaindb import BigchainDB +from bigchaindb.tendermint_utils import (decode_transaction, + calculate_hash) +from bigchaindb.lib import Block, PreCommitState +from bigchaindb.backend.query import PRE_COMMIT_ID + + +CodeTypeOk = 0 +CodeTypeError = 1 +logger = logging.getLogger(__name__) + + +class App(BaseApplication): + """Bridge between BigchainDB and Tendermint. + + The role of this class is to expose the BigchainDB + transactional logic to the Tendermint Consensus + State Machine.""" + + def __init__(self, bigchaindb=None): + self.bigchaindb = bigchaindb or BigchainDB() + self.block_txn_ids = [] + self.block_txn_hash = '' + self.block_transactions = [] + self.validators = None + self.new_height = None + + def init_chain(self, validators): + """Initialize chain with block of height 0""" + + block = Block(app_hash='', height=0, transactions=[]) + self.bigchaindb.store_block(block._asdict()) + return ResponseInitChain() + + def info(self, request): + """Return height of the latest committed block.""" + r = ResponseInfo() + block = self.bigchaindb.get_latest_block() + if block: + r.last_block_height = block['height'] + r.last_block_app_hash = block['app_hash'].encode('utf-8') + else: + r.last_block_height = 0 + r.last_block_app_hash = b'' + return r + + def check_tx(self, raw_transaction): + """Validate the transaction before entry into + the mempool. + + Args: + raw_tx: a raw string (in bytes) transaction.""" + + logger.benchmark('CHECK_TX_INIT') + logger.debug('check_tx: %s', raw_transaction) + transaction = decode_transaction(raw_transaction) + if self.bigchaindb.is_valid_transaction(transaction): + logger.debug('check_tx: VALID') + logger.benchmark('CHECK_TX_END, tx_id:%s', transaction['id']) + return ResponseCheckTx(code=CodeTypeOk) + else: + logger.debug('check_tx: INVALID') + logger.benchmark('CHECK_TX_END, tx_id:%s', transaction['id']) + return ResponseCheckTx(code=CodeTypeError) + + def begin_block(self, req_begin_block): + """Initialize list of transaction. + Args: + req_begin_block: block object which contains block header + and block hash. + """ + logger.benchmark('BEGIN BLOCK, height:%s, num_txs:%s', + req_begin_block.header.height, + req_begin_block.header.num_txs) + + self.block_txn_ids = [] + self.block_transactions = [] + return ResponseBeginBlock() + + def deliver_tx(self, raw_transaction): + """Validate the transaction before mutating the state. + + Args: + raw_tx: a raw string (in bytes) transaction.""" + logger.debug('deliver_tx: %s', raw_transaction) + transaction = self.bigchaindb.is_valid_transaction( + decode_transaction(raw_transaction), self.block_transactions) + + if not transaction: + logger.debug('deliver_tx: INVALID') + return ResponseDeliverTx(code=CodeTypeError) + else: + logger.debug('storing tx') + self.block_txn_ids.append(transaction.id) + self.block_transactions.append(transaction) + return ResponseDeliverTx(code=CodeTypeOk) + + def end_block(self, request_end_block): + """Calculate block hash using transaction ids and previous block + hash to be stored in the next block. + + Args: + height (int): new height of the chain.""" + + height = request_end_block.height + self.new_height = height + block_txn_hash = calculate_hash(self.block_txn_ids) + block = self.bigchaindb.get_latest_block() + + if self.block_txn_ids: + self.block_txn_hash = calculate_hash([block['app_hash'], block_txn_hash]) + else: + self.block_txn_hash = block['app_hash'] + + validator_updates = self.bigchaindb.get_validator_update() + validator_updates = [encode_validator(v) for v in validator_updates] + + # set sync status to true + self.bigchaindb.delete_validator_update() + + # Store pre-commit state to recover in case there is a crash + # during `commit` + pre_commit_state = PreCommitState(commit_id=PRE_COMMIT_ID, + height=self.new_height, + transactions=self.block_txn_ids) + logger.debug('Updating PreCommitState: %s', self.new_height) + self.bigchaindb.store_pre_commit_state(pre_commit_state._asdict()) + return ResponseEndBlock(validator_updates=validator_updates) + + def commit(self): + """Store the new height and along with block hash.""" + + data = self.block_txn_hash.encode('utf-8') + + # register a new block only when new transactions are received + if self.block_txn_ids: + self.bigchaindb.store_bulk_transactions(self.block_transactions) + block = Block(app_hash=self.block_txn_hash, + height=self.new_height, + transactions=self.block_txn_ids) + # NOTE: storing the block should be the last operation during commit + # this effects crash recovery. Refer BEP#8 for details + self.bigchaindb.store_block(block._asdict()) + + logger.debug('Commit-ing new block with hash: apphash=%s ,' + 'height=%s, txn ids=%s', data, self.new_height, + self.block_txn_ids) + logger.benchmark('COMMIT_BLOCK, height:%s', self.new_height) + return ResponseCommit(data=data) + + +def encode_validator(v): + ed25519_public_key = v['pub_key']['data'] + # NOTE: tendermint expects public to be encoded in go-amino format + + pub_key = PubKey(type='ed25519', + data=bytes.fromhex(ed25519_public_key)) + + return Validator(pub_key=pub_key, + address=b'', + power=v['power']) diff --git a/bigchaindb/tendermint/event_stream.py b/bigchaindb/event_stream.py similarity index 97% rename from bigchaindb/tendermint/event_stream.py rename to bigchaindb/event_stream.py index 9cbd28f2..8a68b7f1 100644 --- a/bigchaindb/tendermint/event_stream.py +++ b/bigchaindb/event_stream.py @@ -8,7 +8,7 @@ import aiohttp from bigchaindb import config from bigchaindb.common.utils import gen_timestamp from bigchaindb.events import EventTypes, Event -from bigchaindb.tendermint.utils import decode_transaction_base64 +from bigchaindb.tendermint_utils import decode_transaction_base64 HOST = config['tendermint']['host'] diff --git a/bigchaindb/tendermint/fastquery.py b/bigchaindb/fastquery.py similarity index 100% rename from bigchaindb/tendermint/fastquery.py rename to bigchaindb/fastquery.py diff --git a/bigchaindb/tendermint/lib.py b/bigchaindb/lib.py similarity index 99% rename from bigchaindb/tendermint/lib.py rename to bigchaindb/lib.py index 5e238c94..80e93bf3 100644 --- a/bigchaindb/tendermint/lib.py +++ b/bigchaindb/lib.py @@ -16,13 +16,12 @@ except ImportError: import requests import bigchaindb -from bigchaindb import backend, config_utils +from bigchaindb import backend, config_utils, fastquery from bigchaindb.models import Transaction from bigchaindb.common.exceptions import (SchemaValidationError, ValidationError, DoubleSpend) -from bigchaindb.tendermint.utils import encode_transaction, merkleroot -from bigchaindb.tendermint import fastquery +from bigchaindb.tendermint_utils import encode_transaction, merkleroot from bigchaindb import exceptions as core_exceptions from bigchaindb.consensus import BaseConsensusRules diff --git a/bigchaindb/models.py b/bigchaindb/models.py index 33689d0c..62c04517 100644 --- a/bigchaindb/models.py +++ b/bigchaindb/models.py @@ -14,7 +14,7 @@ class Transaction(Transaction): """Validate transaction spend Args: - bigchain (BigchainDB): an instantiated bigchaindb.tendermint.BigchainDB object. + bigchain (BigchainDB): an instantiated bigchaindb.BigchainDB object. Returns: The transaction (Transaction) if the transaction is valid else it @@ -108,7 +108,7 @@ class Transaction(Transaction): asset from the asset table and reconstructs the transaction. Args: - bigchain (:class:`~bigchaindb.tendermint.BigchainDB`): An instance + bigchain (:class:`~bigchaindb.BigchainDB`): An instance of BigchainDB used to perform database queries. tx_dict_list (:list:`dict` or :obj:`dict`): The transaction dict or list of transaction dict as returned from the database. diff --git a/bigchaindb/tendermint/commands.py b/bigchaindb/start.py similarity index 95% rename from bigchaindb/tendermint/commands.py rename to bigchaindb/start.py index d50f0f31..4bcc8218 100644 --- a/bigchaindb/tendermint/commands.py +++ b/bigchaindb/start.py @@ -3,10 +3,10 @@ import logging import setproctitle import bigchaindb -from bigchaindb.tendermint.lib import BigchainDB -from bigchaindb.tendermint.core import App +from bigchaindb.lib import BigchainDB +from bigchaindb.core import App from bigchaindb.web import server, websocket_server -from bigchaindb.tendermint import event_stream +from bigchaindb import event_stream from bigchaindb.events import Exchange, EventTypes from bigchaindb.utils import Process diff --git a/bigchaindb/tendermint/__init__.py b/bigchaindb/tendermint/__init__.py deleted file mode 100644 index b98dd439..00000000 --- a/bigchaindb/tendermint/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Code necessary for integrating with Tendermint.""" - -# Order is important! -# If we import core first, core will try to load BigchainDB from -# __init__ itself, causing a loop. -from bigchaindb.tendermint.lib import BigchainDB # noqa -from bigchaindb.tendermint.core import App # noqa diff --git a/bigchaindb/tendermint/core.py b/bigchaindb/tendermint/core.py deleted file mode 100644 index e6e43484..00000000 --- a/bigchaindb/tendermint/core.py +++ /dev/null @@ -1,178 +0,0 @@ -"""This module contains all the goodness to integrate BigchainDB -with Tendermint.""" -import logging - -from abci.application import BaseApplication -from abci.types_pb2 import ( - ResponseInitChain, - ResponseInfo, - ResponseCheckTx, - ResponseBeginBlock, - ResponseDeliverTx, - ResponseEndBlock, - ResponseCommit, - Validator, - PubKey -) - -from bigchaindb.tendermint import BigchainDB -from bigchaindb.tendermint.utils import (decode_transaction, - calculate_hash) -from bigchaindb.tendermint.lib import Block, PreCommitState -from bigchaindb.backend.query import PRE_COMMIT_ID - - -CodeTypeOk = 0 -CodeTypeError = 1 -logger = logging.getLogger(__name__) - - -class App(BaseApplication): - """Bridge between BigchainDB and Tendermint. - - The role of this class is to expose the BigchainDB - transactional logic to the Tendermint Consensus - State Machine.""" - - def __init__(self, bigchaindb=None): - self.bigchaindb = bigchaindb or BigchainDB() - self.block_txn_ids = [] - self.block_txn_hash = '' - self.block_transactions = [] - self.validators = None - self.new_height = None - - def init_chain(self, validators): - """Initialize chain with block of height 0""" - - block = Block(app_hash='', height=0, transactions=[]) - self.bigchaindb.store_block(block._asdict()) - return ResponseInitChain() - - def info(self, request): - """Return height of the latest committed block.""" - r = ResponseInfo() - block = self.bigchaindb.get_latest_block() - if block: - r.last_block_height = block['height'] - r.last_block_app_hash = block['app_hash'].encode('utf-8') - else: - r.last_block_height = 0 - r.last_block_app_hash = b'' - return r - - def check_tx(self, raw_transaction): - """Validate the transaction before entry into - the mempool. - - Args: - raw_tx: a raw string (in bytes) transaction.""" - - logger.benchmark('CHECK_TX_INIT') - logger.debug('check_tx: %s', raw_transaction) - transaction = decode_transaction(raw_transaction) - if self.bigchaindb.is_valid_transaction(transaction): - logger.debug('check_tx: VALID') - logger.benchmark('CHECK_TX_END, tx_id:%s', transaction['id']) - return ResponseCheckTx(code=CodeTypeOk) - else: - logger.debug('check_tx: INVALID') - logger.benchmark('CHECK_TX_END, tx_id:%s', transaction['id']) - return ResponseCheckTx(code=CodeTypeError) - - def begin_block(self, req_begin_block): - """Initialize list of transaction. - Args: - req_begin_block: block object which contains block header - and block hash. - """ - logger.benchmark('BEGIN BLOCK, height:%s, num_txs:%s', - req_begin_block.header.height, - req_begin_block.header.num_txs) - - self.block_txn_ids = [] - self.block_transactions = [] - return ResponseBeginBlock() - - def deliver_tx(self, raw_transaction): - """Validate the transaction before mutating the state. - - Args: - raw_tx: a raw string (in bytes) transaction.""" - logger.debug('deliver_tx: %s', raw_transaction) - transaction = self.bigchaindb.is_valid_transaction( - decode_transaction(raw_transaction), self.block_transactions) - - if not transaction: - logger.debug('deliver_tx: INVALID') - return ResponseDeliverTx(code=CodeTypeError) - else: - logger.debug('storing tx') - self.block_txn_ids.append(transaction.id) - self.block_transactions.append(transaction) - return ResponseDeliverTx(code=CodeTypeOk) - - def end_block(self, request_end_block): - """Calculate block hash using transaction ids and previous block - hash to be stored in the next block. - - Args: - height (int): new height of the chain.""" - - height = request_end_block.height - self.new_height = height - block_txn_hash = calculate_hash(self.block_txn_ids) - block = self.bigchaindb.get_latest_block() - - if self.block_txn_ids: - self.block_txn_hash = calculate_hash([block['app_hash'], block_txn_hash]) - else: - self.block_txn_hash = block['app_hash'] - - validator_updates = self.bigchaindb.get_validator_update() - validator_updates = [encode_validator(v) for v in validator_updates] - - # set sync status to true - self.bigchaindb.delete_validator_update() - - # Store pre-commit state to recover in case there is a crash - # during `commit` - pre_commit_state = PreCommitState(commit_id=PRE_COMMIT_ID, - height=self.new_height, - transactions=self.block_txn_ids) - logger.debug('Updating PreCommitState: %s', self.new_height) - self.bigchaindb.store_pre_commit_state(pre_commit_state._asdict()) - return ResponseEndBlock(validator_updates=validator_updates) - - def commit(self): - """Store the new height and along with block hash.""" - - data = self.block_txn_hash.encode('utf-8') - - # register a new block only when new transactions are received - if self.block_txn_ids: - self.bigchaindb.store_bulk_transactions(self.block_transactions) - block = Block(app_hash=self.block_txn_hash, - height=self.new_height, - transactions=self.block_txn_ids) - # NOTE: storing the block should be the last operation during commit - # this effects crash recovery. Refer BEP#8 for details - self.bigchaindb.store_block(block._asdict()) - - logger.debug('Commit-ing new block with hash: apphash=%s ,' - 'height=%s, txn ids=%s', data, self.new_height, - self.block_txn_ids) - logger.benchmark('COMMIT_BLOCK, height:%s', self.new_height) - return ResponseCommit(data=data) - - -def encode_validator(v): - ed25519_public_key = v['pub_key']['data'] - # NOTE: tendermint expects public to be encoded in go-amino format - - pub_key = PubKey(type='ed25519', - data=bytes.fromhex(ed25519_public_key)) - - return Validator(pub_key=pub_key, - address=b'', - power=v['power']) diff --git a/bigchaindb/tendermint/utils.py b/bigchaindb/tendermint_utils.py similarity index 100% rename from bigchaindb/tendermint/utils.py rename to bigchaindb/tendermint_utils.py diff --git a/bigchaindb/web/server.py b/bigchaindb/web/server.py index 646f6f17..348d3fec 100644 --- a/bigchaindb/web/server.py +++ b/bigchaindb/web/server.py @@ -11,7 +11,7 @@ from flask_cors import CORS import gunicorn.app.base from bigchaindb import utils -from bigchaindb.tendermint import BigchainDB +from bigchaindb import BigchainDB from bigchaindb.web.routes import add_routes from bigchaindb.web.strip_content_type_middleware import StripContentTypeMiddleware diff --git a/docs/server/generate_http_server_api_documentation.py b/docs/server/generate_http_server_api_documentation.py index 2256c355..9050d05c 100644 --- a/docs/server/generate_http_server_api_documentation.py +++ b/docs/server/generate_http_server_api_documentation.py @@ -5,8 +5,7 @@ import os import os.path from bigchaindb.common.transaction import Transaction, Input, TransactionLink -from bigchaindb.tendermint import BigchainDB -from bigchaindb.tendermint import lib +from bigchaindb import lib from bigchaindb.web import server diff --git a/docs/server/source/appendices/index.rst b/docs/server/source/appendices/index.rst index f96b365a..033a84d2 100755 --- a/docs/server/source/appendices/index.rst +++ b/docs/server/source/appendices/index.rst @@ -10,7 +10,6 @@ Appendices the-bigchaindb-class backend commands - tendermint-integration aws-setup generate-key-pair-for-ssh firewall-notes diff --git a/docs/server/source/appendices/tendermint-integration.rst b/docs/server/source/appendices/tendermint-integration.rst deleted file mode 100644 index bc800400..00000000 --- a/docs/server/source/appendices/tendermint-integration.rst +++ /dev/null @@ -1,26 +0,0 @@ -###################### -Tendermint Integration -###################### - - -.. automodule:: bigchaindb.tendermint - :special-members: __init__ - -.. automodule:: bigchaindb.tendermint.lib - :special-members: __init__ - :noindex: - -.. automodule:: bigchaindb.tendermint.core - :special-members: __init__ - -.. automodule:: bigchaindb.tendermint.event_stream - :special-members: __init__ - -.. automodule:: bigchaindb.tendermint.fastquery - :special-members: __init__ - -.. automodule:: bigchaindb.tendermint.commands - :special-members: __init__ - -.. automodule:: bigchaindb.tendermint.utils - :special-members: __init__ diff --git a/docs/server/source/appendices/the-bigchaindb-class.rst b/docs/server/source/appendices/the-bigchaindb-class.rst index 0054e3f4..8d604d2c 100644 --- a/docs/server/source/appendices/the-bigchaindb-class.rst +++ b/docs/server/source/appendices/the-bigchaindb-class.rst @@ -2,4 +2,4 @@ The BigchainDB Class #################### -.. autoclass:: bigchaindb.tendermint.BigchainDB +.. autoclass:: bigchaindb.BigchainDB diff --git a/tests/backend/localmongodb/test_queries.py b/tests/backend/localmongodb/test_queries.py index d0cfcbb7..a9e64ac0 100644 --- a/tests/backend/localmongodb/test_queries.py +++ b/tests/backend/localmongodb/test_queries.py @@ -229,7 +229,7 @@ def test_get_spending_transactions(user_pk, user_sk): def test_store_block(): from bigchaindb.backend import connect, query - from bigchaindb.tendermint.lib import Block + from bigchaindb.lib import Block conn = connect() block = Block(app_hash='random_utxo', @@ -242,7 +242,7 @@ def test_store_block(): def test_get_block(): from bigchaindb.backend import connect, query - from bigchaindb.tendermint.lib import Block + from bigchaindb.lib import Block conn = connect() block = Block(app_hash='random_utxo', @@ -345,7 +345,7 @@ def test_get_unspent_outputs(db_context, utxoset): def test_store_pre_commit_state(db_context): from bigchaindb.backend import query - from bigchaindb.tendermint.lib import PreCommitState + from bigchaindb.lib import PreCommitState state = PreCommitState(commit_id='test', height=3, @@ -359,7 +359,7 @@ def test_store_pre_commit_state(db_context): def test_get_pre_commit_state(db_context): from bigchaindb.backend import query - from bigchaindb.tendermint.lib import PreCommitState + from bigchaindb.lib import PreCommitState state = PreCommitState(commit_id='test2', height=3, diff --git a/tests/commands/test_commands.py b/tests/commands/test_commands.py index 17403e8c..578f06b1 100644 --- a/tests/commands/test_commands.py +++ b/tests/commands/test_commands.py @@ -90,7 +90,7 @@ def test_bigchain_run_init_when_db_exists(mocker, capsys): def test__run_init(mocker): from bigchaindb.commands.bigchaindb import _run_init bigchain_mock = mocker.patch( - 'bigchaindb.commands.bigchaindb.bigchaindb.tendermint.BigchainDB') + 'bigchaindb.commands.bigchaindb.bigchaindb.BigchainDB') init_db_mock = mocker.patch( 'bigchaindb.commands.bigchaindb.schema.init_database', autospec=True, @@ -274,7 +274,7 @@ def test_calling_main(start_mock, base_parser_mock, parse_args_mock, @patch('bigchaindb.config_utils.autoconfigure') @patch('bigchaindb.commands.bigchaindb.run_recover') -@patch('bigchaindb.tendermint.commands.start') +@patch('bigchaindb.start.start') def test_recover_db_on_start(mock_autoconfigure, mock_run_recover, mock_start, @@ -293,7 +293,7 @@ def test_recover_db_on_start(mock_autoconfigure, def test_run_recover(b, alice, bob): from bigchaindb.commands.bigchaindb import run_recover from bigchaindb.models import Transaction - from bigchaindb.tendermint.lib import Block, PreCommitState + from bigchaindb.lib import Block, PreCommitState from bigchaindb.backend.query import PRE_COMMIT_ID from bigchaindb.backend import query diff --git a/tests/conftest.py b/tests/conftest.py index d1ad5368..f4c05197 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -17,7 +17,7 @@ from pymongo import MongoClient from bigchaindb.common import crypto from bigchaindb.log import setup_logging -from bigchaindb.tendermint.lib import Block +from bigchaindb.lib import Block TEST_DB_NAME = 'bigchain_test' @@ -269,13 +269,13 @@ def merlin_pubkey(merlin): @pytest.fixture def b(): - from bigchaindb.tendermint import BigchainDB + from bigchaindb import BigchainDB return BigchainDB() @pytest.fixture def tb(): - from bigchaindb.tendermint import BigchainDB + from bigchaindb import BigchainDB return BigchainDB() @@ -514,7 +514,7 @@ def event_loop(request): @pytest.fixture(scope='session') def abci_server(): from abci import ABCIServer - from bigchaindb.tendermint.core import App + from bigchaindb.core import App from bigchaindb.utils import Process app = ABCIServer(app=App()) diff --git a/tests/db/test_bigchain_api.py b/tests/db/test_bigchain_api.py index c285401a..b1ff8594 100644 --- a/tests/db/test_bigchain_api.py +++ b/tests/db/test_bigchain_api.py @@ -280,7 +280,7 @@ class TestBigchainApi(object): @pytest.mark.usefixtures('inputs') def test_write_transaction(self, b, user_pk, user_sk): - from bigchaindb.tendermint import BigchainDB + from bigchaindb import BigchainDB from bigchaindb.models import Transaction input_tx = b.get_owned_ids(user_pk).pop() @@ -439,7 +439,7 @@ class TestBigchainApi(object): from bigchaindb.common.exceptions import InputDoesNotExist from bigchaindb.common.transaction import Input, TransactionLink from bigchaindb.models import Transaction - from bigchaindb.tendermint import BigchainDB + from bigchaindb import BigchainDB # Create an input for a non existing transaction input = Input(Ed25519Sha256(public_key=b58decode(user_pk)), @@ -970,8 +970,8 @@ class TestMultipleInputs(object): def test_get_owned_ids_calls_get_outputs_filtered(): - from bigchaindb.tendermint import BigchainDB - with patch('bigchaindb.tendermint.BigchainDB.get_outputs_filtered') as gof: + from bigchaindb import BigchainDB + with patch('bigchaindb.BigchainDB.get_outputs_filtered') as gof: b = BigchainDB() res = b.get_owned_ids('abc') gof.assert_called_once_with('abc', spent=False) @@ -981,13 +981,13 @@ def test_get_owned_ids_calls_get_outputs_filtered(): @pytest.mark.tendermint def test_get_outputs_filtered_only_unspent(): from bigchaindb.common.transaction import TransactionLink - from bigchaindb.tendermint.lib import BigchainDB + from bigchaindb.lib import BigchainDB - go = 'bigchaindb.tendermint.fastquery.FastQuery.get_outputs_by_public_key' + go = 'bigchaindb.fastquery.FastQuery.get_outputs_by_public_key' with patch(go) as get_outputs: get_outputs.return_value = [TransactionLink('a', 1), TransactionLink('b', 2)] - fs = 'bigchaindb.tendermint.fastquery.FastQuery.filter_spent_outputs' + fs = 'bigchaindb.fastquery.FastQuery.filter_spent_outputs' with patch(fs) as filter_spent: filter_spent.return_value = [TransactionLink('b', 2)] out = BigchainDB().get_outputs_filtered('abc', spent=False) @@ -998,12 +998,12 @@ def test_get_outputs_filtered_only_unspent(): @pytest.mark.tendermint def test_get_outputs_filtered_only_spent(): from bigchaindb.common.transaction import TransactionLink - from bigchaindb.tendermint.lib import BigchainDB - go = 'bigchaindb.tendermint.fastquery.FastQuery.get_outputs_by_public_key' + from bigchaindb.lib import BigchainDB + go = 'bigchaindb.fastquery.FastQuery.get_outputs_by_public_key' with patch(go) as get_outputs: get_outputs.return_value = [TransactionLink('a', 1), TransactionLink('b', 2)] - fs = 'bigchaindb.tendermint.fastquery.FastQuery.filter_unspent_outputs' + fs = 'bigchaindb.fastquery.FastQuery.filter_unspent_outputs' with patch(fs) as filter_spent: filter_spent.return_value = [TransactionLink('b', 2)] out = BigchainDB().get_outputs_filtered('abc', spent=True) @@ -1012,13 +1012,13 @@ def test_get_outputs_filtered_only_spent(): @pytest.mark.tendermint -@patch('bigchaindb.tendermint.fastquery.FastQuery.filter_unspent_outputs') -@patch('bigchaindb.tendermint.fastquery.FastQuery.filter_spent_outputs') +@patch('bigchaindb.fastquery.FastQuery.filter_unspent_outputs') +@patch('bigchaindb.fastquery.FastQuery.filter_spent_outputs') def test_get_outputs_filtered(filter_spent, filter_unspent): from bigchaindb.common.transaction import TransactionLink - from bigchaindb.tendermint.lib import BigchainDB + from bigchaindb.lib import BigchainDB - go = 'bigchaindb.tendermint.fastquery.FastQuery.get_outputs_by_public_key' + go = 'bigchaindb.fastquery.FastQuery.get_outputs_by_public_key' with patch(go) as get_outputs: get_outputs.return_value = [TransactionLink('a', 1), TransactionLink('b', 2)] diff --git a/tests/tendermint/conftest.py b/tests/tendermint/conftest.py index aca3c680..2d63d7c4 100644 --- a/tests/tendermint/conftest.py +++ b/tests/tendermint/conftest.py @@ -3,7 +3,7 @@ import pytest @pytest.fixture def b(): - from bigchaindb.tendermint import BigchainDB + from bigchaindb import BigchainDB return BigchainDB() diff --git a/tests/tendermint/test_core.py b/tests/tendermint/test_core.py index cb67a065..1a047e7d 100644 --- a/tests/tendermint/test_core.py +++ b/tests/tendermint/test_core.py @@ -6,7 +6,7 @@ from abci.types_pb2 import ( RequestEndBlock ) -from bigchaindb.tendermint.core import CodeTypeOk, CodeTypeError +from bigchaindb.core import CodeTypeOk, CodeTypeError pytestmark = [pytest.mark.tendermint, pytest.mark.bdb] @@ -17,7 +17,7 @@ def encode_tx_to_bytes(transaction): def test_check_tx__signed_create_is_ok(b): - from bigchaindb.tendermint import App + from bigchaindb import App from bigchaindb.models import Transaction from bigchaindb.common.crypto import generate_key_pair @@ -34,7 +34,7 @@ def test_check_tx__signed_create_is_ok(b): def test_check_tx__unsigned_create_is_error(b): - from bigchaindb.tendermint import App + from bigchaindb import App from bigchaindb.models import Transaction from bigchaindb.common.crypto import generate_key_pair @@ -51,7 +51,7 @@ def test_check_tx__unsigned_create_is_error(b): @pytest.mark.bdb def test_deliver_tx__valid_create_updates_db(b): - from bigchaindb.tendermint import App + from bigchaindb import App from bigchaindb.models import Transaction from bigchaindb.common.crypto import generate_key_pair @@ -84,7 +84,7 @@ def test_deliver_tx__valid_create_updates_db(b): def test_deliver_tx__double_spend_fails(b): - from bigchaindb.tendermint import App + from bigchaindb import App from bigchaindb.models import Transaction from bigchaindb.common.crypto import generate_key_pair @@ -113,7 +113,7 @@ def test_deliver_tx__double_spend_fails(b): def test_deliver_transfer_tx__double_spend_fails(b): - from bigchaindb.tendermint import App + from bigchaindb import App from bigchaindb.models import Transaction from bigchaindb.common.crypto import generate_key_pair @@ -157,9 +157,9 @@ def test_deliver_transfer_tx__double_spend_fails(b): def test_end_block_return_validator_updates(b): - from bigchaindb.tendermint import App + from bigchaindb import App from bigchaindb.backend import query - from bigchaindb.tendermint.core import encode_validator + from bigchaindb.core import encode_validator from bigchaindb.backend.query import VALIDATOR_UPDATE_ID app = App(b) @@ -183,7 +183,7 @@ def test_end_block_return_validator_updates(b): def test_store_pre_commit_state_in_end_block(b, alice): - from bigchaindb.tendermint import App + from bigchaindb import App from bigchaindb.backend import query from bigchaindb.models import Transaction from bigchaindb.backend.query import PRE_COMMIT_ID diff --git a/tests/tendermint/test_event_stream.py b/tests/tendermint/test_event_stream.py index c547a9f2..72a935c6 100644 --- a/tests/tendermint/test_event_stream.py +++ b/tests/tendermint/test_event_stream.py @@ -8,7 +8,7 @@ import pytest @pytest.mark.tendermint def test_process_event_new_block(): - from bigchaindb.tendermint.event_stream import process_event + from bigchaindb.event_stream import process_event event = '{"jsonrpc": "2.0", "id": "test_stream_id#event", "result": {'\ '"query": "tm.event=\'NewBlock\'", "data": { "type": "CF18EA939D3240",'\ @@ -46,7 +46,7 @@ def test_process_event_new_block(): @pytest.mark.tendermint def test_process_event_empty_block(): - from bigchaindb.tendermint.event_stream import process_event + from bigchaindb.event_stream import process_event event = '{"jsonrpc": "2.0", "id": "bigchaindb_stream_1524555674#event",'\ '"result": {"query": "tm.event=\'NewBlock\'", "data": {"type": '\ @@ -67,7 +67,7 @@ def test_process_event_empty_block(): @pytest.mark.tendermint def test_process_unknown_event(): - from bigchaindb.tendermint.event_stream import process_event + from bigchaindb.event_stream import process_event event = '{"jsonrpc": "2.0", "id": "test_stream_id#event",'\ ' "result": { "query": "tm.event=\'UnknownEvent\'" }}' @@ -80,7 +80,7 @@ def test_process_unknown_event(): @pytest.mark.asyncio @pytest.mark.abci async def test_subscribe_events(tendermint_ws_url, b): - from bigchaindb.tendermint.event_stream import subscribe_events + from bigchaindb.event_stream import subscribe_events from bigchaindb.common.crypto import generate_key_pair from bigchaindb.models import Transaction diff --git a/tests/tendermint/test_integration.py b/tests/tendermint/test_integration.py index edb8cfa2..4f6b530b 100644 --- a/tests/tendermint/test_integration.py +++ b/tests/tendermint/test_integration.py @@ -12,8 +12,8 @@ from io import BytesIO @pytest.mark.tendermint @pytest.mark.bdb def test_app(tb): - from bigchaindb.tendermint import App - from bigchaindb.tendermint.utils import calculate_hash + from bigchaindb import App + from bigchaindb.tendermint_utils import calculate_hash from bigchaindb.common.crypto import generate_key_pair from bigchaindb.models import Transaction @@ -103,7 +103,7 @@ def test_upsert_validator(b, alice): from bigchaindb.backend.query import VALIDATOR_UPDATE_ID from bigchaindb.backend import query, connect from bigchaindb.models import Transaction - from bigchaindb.tendermint.utils import public_key_to_base64 + from bigchaindb.tendermint_utils import public_key_to_base64 import time conn = connect() diff --git a/tests/tendermint/test_lib.py b/tests/tendermint/test_lib.py index 9c296e60..e6aa8f07 100644 --- a/tests/tendermint/test_lib.py +++ b/tests/tendermint/test_lib.py @@ -47,7 +47,7 @@ def test_asset_is_separated_from_transaciton(b): @pytest.mark.bdb def test_get_latest_block(tb): - from bigchaindb.tendermint.lib import Block + from bigchaindb.lib import Block b = tb for i in range(10): @@ -63,7 +63,7 @@ def test_get_latest_block(tb): @pytest.mark.bdb @patch('bigchaindb.backend.query.get_block', return_value=None) -@patch('bigchaindb.tendermint.lib.BigchainDB.get_latest_block', return_value={'height': 10}) +@patch('bigchaindb.BigchainDB.get_latest_block', return_value={'height': 10}) def test_get_empty_block(_0, _1, tb): assert tb.get_block(5) == {'height': 5, 'transactions': []} @@ -86,7 +86,7 @@ def test_validation_error(b): def test_write_and_post_transaction(mock_post, b): from bigchaindb.models import Transaction from bigchaindb.common.crypto import generate_key_pair - from bigchaindb.tendermint.utils import encode_transaction + from bigchaindb.tendermint_utils import encode_transaction alice = generate_key_pair() tx = Transaction.create([alice.public_key], diff --git a/tests/tendermint/test_utils.py b/tests/tendermint/test_utils.py index 01d91bd6..8e4c7d1f 100644 --- a/tests/tendermint/test_utils.py +++ b/tests/tendermint/test_utils.py @@ -12,7 +12,7 @@ pytestmark = pytest.mark.tendermint def test_encode_decode_transaction(b): - from bigchaindb.tendermint.utils import (encode_transaction, + from bigchaindb.tendermint_utils import (encode_transaction, decode_transaction) asset = { @@ -30,7 +30,7 @@ def test_encode_decode_transaction(b): def test_calculate_hash_no_key(b): - from bigchaindb.tendermint.utils import calculate_hash + from bigchaindb.tendermint_utils import calculate_hash # pass an empty list assert calculate_hash([]) == '' @@ -38,7 +38,7 @@ def test_calculate_hash_no_key(b): # TODO test for the case of an empty list of hashes, and possibly other cases. def test_merkleroot(): - from bigchaindb.tendermint.utils import merkleroot + from bigchaindb.tendermint_utils import merkleroot hashes = [sha3_256(i.encode()).digest() for i in 'abc'] assert merkleroot(hashes) == ( '78c7c394d3158c218916b7ae0ebdea502e0f4e85c08e3b371e3dfd824d389fa3') @@ -54,14 +54,14 @@ SAMPLE_PUBLIC_KEY = { def test_convert_base64_public_key_to_address(): - from bigchaindb.tendermint.utils import public_key64_to_address + from bigchaindb.tendermint_utils import public_key64_to_address address = public_key64_to_address(SAMPLE_PUBLIC_KEY['pub_key']['value']) assert address == SAMPLE_PUBLIC_KEY['address'] def test_public_key_encoding_decoding(): - from bigchaindb.tendermint.utils import (public_key_from_base64, + from bigchaindb.tendermint_utils import (public_key_from_base64, public_key_to_base64) public_key = public_key_from_base64(SAMPLE_PUBLIC_KEY['pub_key']['value']) diff --git a/tests/test_config_utils.py b/tests/test_config_utils.py index 30dcfd60..f5bce81d 100644 --- a/tests/test_config_utils.py +++ b/tests/test_config_utils.py @@ -35,7 +35,7 @@ def test_bigchain_instance_raises_when_not_configured(request, monkeypatch): import bigchaindb from bigchaindb import config_utils from bigchaindb.common import exceptions - from bigchaindb.tendermint import BigchainDB + from bigchaindb import BigchainDB assert 'CONFIGURED' not in bigchaindb.config # We need to disable ``bigchaindb.config_utils.autoconfigure`` to avoid reading diff --git a/tests/test_core.py b/tests/test_core.py index 5f89baa1..7a648d83 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -31,7 +31,7 @@ def config(request, monkeypatch): @pytest.mark.skipif(reason='will be fixed in another PR') def test_bigchain_class_default_initialization(config): - from bigchaindb.tendermint import BigchainDB + from bigchaindb import BigchainDB from bigchaindb.consensus import BaseConsensusRules from bigchaindb.backend.connection import Connection bigchain = BigchainDB() @@ -44,7 +44,7 @@ def test_bigchain_class_default_initialization(config): @pytest.mark.skipif(reason='will be fixed in another PR') def test_bigchain_class_initialization_with_parameters(config): - from bigchaindb.tendermint import BigchainDB + from bigchaindb import BigchainDB from bigchaindb.backend import connect from bigchaindb.consensus import BaseConsensusRules init_db_kwargs = { @@ -65,7 +65,7 @@ def test_bigchain_class_initialization_with_parameters(config): @pytest.mark.skipif(reason='will be fixed in another PR') def test_get_blocks_status_containing_tx(monkeypatch): from bigchaindb.backend import query as backend_query - from bigchaindb.tendermint import BigchainDB + from bigchaindb import BigchainDB blocks = [ {'id': 1}, {'id': 2} ] diff --git a/tests/web/conftest.py b/tests/web/conftest.py index efb0f81b..72659f15 100644 --- a/tests/web/conftest.py +++ b/tests/web/conftest.py @@ -4,7 +4,7 @@ import pytest @pytest.fixture def app(request): from bigchaindb.web import server - from bigchaindb.tendermint.lib import BigchainDB + from bigchaindb.lib import BigchainDB if request.config.getoption('--database-backend') == 'localmongodb': app = server.create_app(debug=True, bigchaindb_factory=BigchainDB) diff --git a/tests/web/test_block_tendermint.py b/tests/web/test_block_tendermint.py index 1542ccae..d627c1fd 100644 --- a/tests/web/test_block_tendermint.py +++ b/tests/web/test_block_tendermint.py @@ -1,7 +1,7 @@ import pytest from bigchaindb.models import Transaction -from bigchaindb.tendermint.lib import Block +from bigchaindb.lib import Block BLOCKS_ENDPOINT = '/api/v1/blocks/' diff --git a/tests/web/test_outputs.py b/tests/web/test_outputs.py index d4d9c4b0..39b9d95a 100644 --- a/tests/web/test_outputs.py +++ b/tests/web/test_outputs.py @@ -11,7 +11,7 @@ def test_get_outputs_endpoint(client, user_pk): m = MagicMock() m.txid = 'a' m.output = 0 - with patch('bigchaindb.tendermint.lib.BigchainDB.get_outputs_filtered') as gof: + with patch('bigchaindb.BigchainDB.get_outputs_filtered') as gof: gof.return_value = [m, m] res = client.get(OUTPUTS_ENDPOINT + '?public_key={}'.format(user_pk)) assert res.json == [ @@ -27,7 +27,7 @@ def test_get_outputs_endpoint_unspent(client, user_pk): m = MagicMock() m.txid = 'a' m.output = 0 - with patch('bigchaindb.tendermint.lib.BigchainDB.get_outputs_filtered') as gof: + with patch('bigchaindb.BigchainDB.get_outputs_filtered') as gof: gof.return_value = [m] params = '?spent=False&public_key={}'.format(user_pk) res = client.get(OUTPUTS_ENDPOINT + params) @@ -41,7 +41,7 @@ def test_get_outputs_endpoint_spent(client, user_pk): m = MagicMock() m.txid = 'a' m.output = 0 - with patch('bigchaindb.tendermint.lib.BigchainDB.get_outputs_filtered') as gof: + with patch('bigchaindb.BigchainDB.get_outputs_filtered') as gof: gof.return_value = [m] params = '?spent=true&public_key={}'.format(user_pk) res = client.get(OUTPUTS_ENDPOINT + params) diff --git a/tests/web/test_transactions.py b/tests/web/test_transactions.py index 7c52ea94..8582ed16 100644 --- a/tests/web/test_transactions.py +++ b/tests/web/test_transactions.py @@ -372,7 +372,7 @@ def test_transactions_get_list_good(client): asset_id = '1' * 64 - with patch('bigchaindb.tendermint.BigchainDB.get_transactions_filtered', get_txs_patched): + with patch('bigchaindb.BigchainDB.get_transactions_filtered', get_txs_patched): url = TX_ENDPOINT + '?asset_id=' + asset_id assert client.get(url).json == [ ['asset_id', asset_id], @@ -389,7 +389,7 @@ def test_transactions_get_list_good(client): def test_transactions_get_list_bad(client): def should_not_be_called(): assert False - with patch('bigchaindb.tendermint.BigchainDB.get_transactions_filtered', + with patch('bigchaindb.BigchainDB.get_transactions_filtered', lambda *_, **__: should_not_be_called()): # Test asset id validated url = TX_ENDPOINT + '?asset_id=' + '1' * 63 @@ -404,7 +404,7 @@ def test_transactions_get_list_bad(client): @pytest.mark.tendermint def test_return_only_valid_transaction(client): - from bigchaindb.tendermint import BigchainDB + from bigchaindb import BigchainDB def get_transaction_patched(status): def inner(self, tx_id, include_status): @@ -415,12 +415,12 @@ def test_return_only_valid_transaction(client): # UNDECIDED or VALID block, as well as transactions from the backlog. # As the endpoint uses `get_transaction`, we don't have to test # against invalid transactions here. - with patch('bigchaindb.tendermint.BigchainDB.get_transaction', + with patch('bigchaindb.BigchainDB.get_transaction', get_transaction_patched(BigchainDB.TX_UNDECIDED)): url = '{}{}'.format(TX_ENDPOINT, '123') assert client.get(url).status_code == 404 - with patch('bigchaindb.tendermint.BigchainDB.get_transaction', + with patch('bigchaindb.BigchainDB.get_transaction', get_transaction_patched(BigchainDB.TX_IN_BACKLOG)): url = '{}{}'.format(TX_ENDPOINT, '123') assert client.get(url).status_code == 404