diff --git a/planetmint/backend/tarantool/connection.py b/planetmint/backend/tarantool/connection.py index e9c3786..11f22c9 100644 --- a/planetmint/backend/tarantool/connection.py +++ b/planetmint/backend/tarantool/connection.py @@ -48,9 +48,12 @@ class TarantoolDBConnection(Connection): def _reconnect(self): self.db_connect = tarantool.connect(host=self.host, port=self.port) - def space(self, space_name: str): + def get_space(self, space_name: str): return self.db_connect.space(space_name) + def space(self, space_name: str): + return self.query().space(space_name) + def get_connection(self): return self.db_connect diff --git a/planetmint/backend/tarantool/query.py b/planetmint/backend/tarantool/query.py index 26869ff..3a64e04 100644 --- a/planetmint/backend/tarantool/query.py +++ b/planetmint/backend/tarantool/query.py @@ -20,12 +20,12 @@ register_query = module_dispatch_registrar(query) def _group_transaction_by_ids(connection, txids: list): - txspace = connection.space("transactions") - inxspace = connection.space("inputs") - outxspace = connection.space("outputs") - keysxspace = connection.space("keys") - assetsxspace = connection.space("assets") - metaxspace = connection.space("meta_data") + txspace = connection.get_space("transactions") + inxspace = connection.get_space("inputs") + outxspace = connection.get_space("outputs") + keysxspace = connection.get_space("keys") + assetsxspace = connection.get_space("assets") + metaxspace = connection.get_space("meta_data") _transactions = [] for txid in txids: _txobject = txspace.select(txid, index="id_search") @@ -56,12 +56,12 @@ def _group_transaction_by_ids(connection, txids: list): @register_query(TarantoolDBConnection) def store_transactions(connection, signed_transactions: list): - txspace = connection.space("transactions") - inxspace = connection.space("inputs") - outxspace = connection.space("outputs") - keysxspace = connection.space("keys") - metadatasxspace = connection.space("meta_data") - assetsxspace = connection.space("assets") + txspace = connection.get_space("transactions") + inxspace = connection.get_space("inputs") + outxspace = connection.get_space("outputs") + keysxspace = connection.get_space("keys") + metadatasxspace = connection.get_space("meta_data") + assetsxspace = connection.get_space("assets") for transaction in signed_transactions: txprepare = TransactionDecompose(transaction) @@ -101,7 +101,7 @@ def get_transactions(connection, transactions_ids: list): @register_query(TarantoolDBConnection) def store_metadatas(connection, metadata: list): - space = connection.space("meta_data") + space = connection.get_space("meta_data") for meta in metadata: space.insert((meta["id"], meta["data"] if not "metadata" in meta else meta["metadata"])) @@ -109,7 +109,7 @@ def store_metadatas(connection, metadata: list): @register_query(TarantoolDBConnection) def get_metadata(connection, transaction_ids: list): _returned_data = [] - space = connection.space("meta_data") + space = connection.get_space("meta_data") for _id in transaction_ids: metadata = space.select(_id, index="id_search").data if len(metadata) > 0: @@ -119,7 +119,7 @@ def get_metadata(connection, transaction_ids: list): @register_query(TarantoolDBConnection) def store_asset(connection, asset): - space = connection.space("assets") + space = connection.get_space("assets") convert = lambda obj: obj if isinstance(obj, tuple) else (obj, obj["id"], obj["id"]) try: space.insert(convert(asset)) @@ -129,7 +129,7 @@ def store_asset(connection, asset): @register_query(TarantoolDBConnection) def store_assets(connection, assets: list): - space = connection.space("assets") + space = connection.get_space("assets") convert = lambda obj: obj if isinstance(obj, tuple) else (obj, obj["id"], obj["id"]) for asset in assets: try: @@ -140,7 +140,7 @@ def store_assets(connection, assets: list): @register_query(TarantoolDBConnection) def get_asset(connection, asset_id: str): - space = connection.space("assets") + space = connection.get_space("assets") _data = space.select(asset_id, index="txid_search") _data = _data.data return _data[0][0] if len(_data) > 0 else [] @@ -157,7 +157,7 @@ def get_assets(connection, assets_ids: list) -> list: @register_query(TarantoolDBConnection) def get_spent(connection, fullfil_transaction_id: str, fullfil_output_index: str): - space = connection.space("inputs") + space = connection.get_space("inputs") _inputs = space.select([fullfil_transaction_id, str(fullfil_output_index)], index="spent_search") _inputs = _inputs.data _transactions = _group_transaction_by_ids(txids=[inp[0] for inp in _inputs], connection=connection) @@ -166,14 +166,14 @@ def get_spent(connection, fullfil_transaction_id: str, fullfil_output_index: str @register_query(TarantoolDBConnection) def get_latest_block(connection): # TODO Here is used DESCENDING OPERATOR - space = connection.space("blocks") + space = connection.get_space("blocks") _all_blocks = space.select() _all_blocks = _all_blocks.data block = {"app_hash": '', "height": 0, "transactions": []} if len(_all_blocks) > 0: _block = sorted(_all_blocks, key=itemgetter(1), reverse=True)[0] - space = connection.space("blocks_tx") + space = connection.get_space("blocks_tx") _txids = space.select(_block[2], index="block_search") _txids = _txids.data block["app_hash"] = _block[0] @@ -186,12 +186,12 @@ def get_latest_block(connection): # TODO Here is used DESCENDING OPERATOR @register_query(TarantoolDBConnection) def store_block(connection, block: dict): - space = connection.space("blocks") + space = connection.get_space("blocks") block_unique_id = token_hex(8) space.insert((block["app_hash"], block["height"], block_unique_id)) - space = connection.space("blocks_tx") + space = connection.get_space("blocks_tx") for txid in block["transactions"]: space.insert((txid, block_unique_id)) @@ -206,8 +206,8 @@ def get_txids_filtered(connection, asset_id: str, operation: str = None, # 1 - operation, 2 - asset.id (linked mode) + OPERATOR OR None: {"sets": [asset_id, asset_id]} }[operation] - tx_space = connection.space("transactions") - assets_space = connection.space("assets") + tx_space = connection.get_space("transactions") + assets_space = connection.get_space("assets") _transactions = [] if actions["sets"][0] == "CREATE": # + _transactions = tx_space.select([operation, asset_id], index=actions["index"]) @@ -259,7 +259,7 @@ def _remove_text_score(asset): @register_query(TarantoolDBConnection) def get_owned_ids(connection, owner: str): - space = connection.space("keys") + space = connection.get_space("keys") _keys = space.select(owner, index="keys_search") if len(_keys.data) == 0: return [] @@ -283,13 +283,13 @@ def get_spending_transactions(connection, inputs): @register_query(TarantoolDBConnection) def get_block(connection, block_id=[]): - space = connection.space("blocks") + space = connection.get_space("blocks") _block = space.select(block_id, index="block_search", limit=1) _block = _block.data if len(_block) == 0: return [] _block = _block[0] - space = connection.space("blocks_tx") + space = connection.get_space("blocks_tx") _txblock = space.select(_block[2], index="block_search") _txblock = _txblock.data return {"app_hash": _block[0], "height": _block[1], "transactions": [_tx[0] for _tx in _txblock]} @@ -297,24 +297,24 @@ def get_block(connection, block_id=[]): @register_query(TarantoolDBConnection) def get_block_with_transaction(connection, txid: str): - space = connection.space("blocks_tx") + space = connection.get_space("blocks_tx") _all_blocks_tx = space.select(txid, index="id_search") _all_blocks_tx = _all_blocks_tx.data if len(_all_blocks_tx) == 0: return [] - space = connection.space("blocks") + space = connection.get_space("blocks") _block = space.select(_all_blocks_tx[0][1], index="block_id_search") return [{"height": _height[1]} for _height in _block.data] @register_query(TarantoolDBConnection) def delete_transactions(connection, txn_ids: list): - tx_space = connection.space("transactions") + tx_space = connection.get_space("transactions") for _id in txn_ids: tx_space.delete(_id) - inputs_space = connection.space("inputs") - outputs_space = connection.space("outputs") - k_space = connection.space("keys") + inputs_space = connection.get_space("inputs") + outputs_space = connection.get_space("outputs") + k_space = connection.get_space("keys") for _id in txn_ids: _inputs = inputs_space.select(_id, index="id_search") _outputs = outputs_space.select(_id, index="id_search") @@ -326,18 +326,18 @@ def delete_transactions(connection, txn_ids: list): for _outpID in _outputs: outputs_space.delete(_outpID[5], index="unique_search") - meta_space = connection.space("meta_data") + meta_space = connection.get_space("meta_data") for _id in txn_ids: meta_space.delete(_id, index="id_search") - assets_space = connection.space("assets") + assets_space = connection.get_space("assets") for _id in txn_ids: assets_space.delete(_id, index="txid_search") @register_query(TarantoolDBConnection) def store_unspent_outputs(connection, *unspent_outputs: list): - space = connection.space('utxos') + space = connection.get_space('utxos') result = [] if unspent_outputs: for utxo in unspent_outputs: @@ -348,7 +348,7 @@ def store_unspent_outputs(connection, *unspent_outputs: list): @register_query(TarantoolDBConnection) def delete_unspent_outputs(connection, *unspent_outputs: list): - space = connection.space('utxos') + space = connection.get_space('utxos') result = [] if unspent_outputs: for utxo in unspent_outputs: @@ -359,14 +359,14 @@ def delete_unspent_outputs(connection, *unspent_outputs: list): @register_query(TarantoolDBConnection) def get_unspent_outputs(connection, query=None): # for now we don't have implementation for 'query'. - space = connection.space('utxos') + space = connection.get_space('utxos') _utxos = space.select([]).data return [loads(utx[2]) for utx in _utxos] @register_query(TarantoolDBConnection) def store_pre_commit_state(connection, state: dict): - space = connection.space("pre_commits") + space = connection.get_space("pre_commits") _precommit = space.select(state["height"], index="height_search", limit=1) unique_id = token_hex(8) if (len(_precommit.data) == 0) else _precommit.data[0][0] space.upsert((unique_id, state["height"], state["transactions"]), @@ -378,7 +378,7 @@ def store_pre_commit_state(connection, state: dict): @register_query(TarantoolDBConnection) def get_pre_commit_state(connection): - space = connection.space("pre_commits") + space = connection.get_space("pre_commits") _commit = space.select([], index="id_search").data if len(_commit) == 0: return None @@ -388,7 +388,7 @@ def get_pre_commit_state(connection): @register_query(TarantoolDBConnection) def store_validator_set(conn, validators_update: dict): - space = conn.space("validators") + space = conn.get_space("validators") _validator = space.select(validators_update["height"], index="height_search", limit=1) unique_id = token_hex(8) if (len(_validator.data) == 0) else _validator.data[0][0] space.upsert((unique_id, validators_update["height"], validators_update["validators"]), @@ -400,7 +400,7 @@ def store_validator_set(conn, validators_update: dict): @register_query(TarantoolDBConnection) def delete_validator_set(connection, height: int): - space = connection.space("validators") + space = connection.get_space("validators") _validators = space.select(height, index="height_search") for _valid in _validators.data: space.delete(_valid[0]) @@ -408,7 +408,7 @@ def delete_validator_set(connection, height: int): @register_query(TarantoolDBConnection) def store_election(connection, election_id: str, height: int, is_concluded: bool): - space = connection.space("elections") + space = connection.get_space("elections") space.upsert((election_id, height, is_concluded), op_list=[('=', 0, election_id), ('=', 1, height), @@ -418,7 +418,7 @@ def store_election(connection, election_id: str, height: int, is_concluded: bool @register_query(TarantoolDBConnection) def store_elections(connection, elections: list): - space = connection.space("elections") + space = connection.get_space("elections") for election in elections: _election = space.insert((election["election_id"], election["height"], diff --git a/tests/backend/tarantool/test_queries.py b/tests/backend/tarantool/test_queries.py index 3a25c34..ef316fa 100644 --- a/tests/backend/tarantool/test_queries.py +++ b/tests/backend/tarantool/test_queries.py @@ -186,7 +186,7 @@ def test_write_metadata(db_conn): query.store_metadatas(connection=conn, metadata=metadata) # check that 3 assets were written to the database - space = conn.space("meta_data") + space = conn.get_space("meta_data") metadatas = [] for meta in metadata: _data = space.select(meta["id"]) @@ -306,7 +306,7 @@ def test_store_block(db_conn): transactions=[]) query.store_block(connection=conn, block=block._asdict()) # block = query.get_block(connection=conn) - blocks = conn.space("blocks").select([]) + blocks = conn.get_space("blocks").select([]) assert len(blocks.data) == 1 diff --git a/tests/backend/tarantool/test_schema.py b/tests/backend/tarantool/test_schema.py index c9d144f..d6e7485 100644 --- a/tests/backend/tarantool/test_schema.py +++ b/tests/backend/tarantool/test_schema.py @@ -10,7 +10,7 @@ def _check_spaces_by_list(conn, space_names): _exists = [] for name in space_names: try: - conn.space(name) + conn.get_space(name) _exists.append(name) except: pass diff --git a/tests/tendermint/test_lib.py b/tests/tendermint/test_lib.py index 592ce3e..3533835 100644 --- a/tests/tendermint/test_lib.py +++ b/tests/tendermint/test_lib.py @@ -157,7 +157,7 @@ def test_post_transaction_invalid_mode(b): @pytest.mark.bdb def test_update_utxoset(b, signed_create_tx, signed_transfer_tx, db_conn): b.update_utxoset(signed_create_tx) - utxoset = db_conn.space('utxos') + utxoset = db_conn.get_space('utxos') assert utxoset.select().rowcount == 1 utxo = utxoset.select().data assert utxo[0][0] == signed_create_tx.id @@ -318,7 +318,7 @@ def test_delete_one_unspent_outputs(b, utxoset): assert utxo_collection.count_documents( {'transaction_id': 'a', 'output_index': 0}) == 0 else: - utx_space = b.connection.space("utxos") + utx_space = b.connection.get_space("utxos") res1 = utx_space.select(['a', 1], index="id_search").data res2 = utx_space.select(['b', 0], index="id_search").data assert len(res1) + len(res2) == 2