From 12b10aa646affdd3a2a43827b74661488ed79420 Mon Sep 17 00:00:00 2001 From: Sangat Das Date: Thu, 5 May 2022 01:04:14 -0700 Subject: [PATCH] Fixed some lib and web issues Signed-off-by: Sangat Das --- planetmint/backend/tarantool/init.lua | 6 +- planetmint/backend/tarantool/query.py | 60 ++++++--------- setup.py | 2 +- tests/backend/tarantool/test_schema.py | 6 +- tests/conftest.py | 5 +- tests/tendermint/test_lib.py | 100 ++++++++++++------------- 6 files changed, 82 insertions(+), 97 deletions(-) diff --git a/planetmint/backend/tarantool/init.lua b/planetmint/backend/tarantool/init.lua index 4da76c0..b6cb246 100644 --- a/planetmint/backend/tarantool/init.lua +++ b/planetmint/backend/tarantool/init.lua @@ -1,4 +1,4 @@ -abci_chains = box.schema.space.create('abci_chains',{engine = 'memtx' , is_sync = false}) +abci_chains = box.schema.space.create('abci_chains', {engine='memtx', is_sync = false}) abci_chains:format({{name='height' , type='integer'},{name='is_synched' , type='boolean'},{name='chain_id',type='string'}}) abci_chains:create_index('id_search' ,{type='hash', parts={'chain_id'}}) abci_chains:create_index('height_search' ,{type='tree',unique=false, parts={'height'}}) @@ -63,6 +63,8 @@ keys:create_index('keys_search', {type = 'tree', unique=false, parts={'public_ke keys:create_index('txid_search', {type = 'tree', unique=false, parts={'transaction_id'}}) keys:create_index('output_search', {type = 'tree', unique=false, parts={'output_id'}}) -utxos = box.schema.space.create('utxos') +utxos = box.schema.space.create('utxos', {engine = 'memtx' , is_sync = false}) utxos:format({{name='transaction_id' , type='string'}, {name='output_index' , type='integer'}}) utxos:create_index('id_search', {type='hash' , parts={'transaction_id', 'output_index'}}) +utxos:create_index('transaction_search', {type='tree', unique=false, parts={'transaction_id'}}) +utxos:create_index('index_search', {type='tree', unique=false, parts={'output_index'}}) \ No newline at end of file diff --git a/planetmint/backend/tarantool/query.py b/planetmint/backend/tarantool/query.py index c417889..e9394aa 100644 --- a/planetmint/backend/tarantool/query.py +++ b/planetmint/backend/tarantool/query.py @@ -9,6 +9,7 @@ from secrets import token_hex from operator import itemgetter from planetmint.backend import query +from planetmint.backend.exceptions import DuplicateKeyError from planetmint.backend.utils import module_dispatch_registrar from planetmint.backend.tarantool.connection import TarantoolDB from planetmint.backend.tarantool.transaction.tools import TransactionCompose, TransactionDecompose @@ -333,44 +334,31 @@ def delete_transactions(connection, txn_ids: list): for _id in txn_ids: assets_space.delete(_id, index="txid_search") -# @register_query(TarantoolDB) -# def store_unspent_outputs(conn, *unspent_outputs: list): -# if unspent_outputs: -# try: -# return conn.run( -# conn.collection('utxos').insert_many( -# unspent_outputs, -# ordered=False, -# ) -# ) -# except DuplicateKeyError: -# # TODO log warning at least -# pass -# -# -# @register_query(TarantoolDB) -# def delete_unspent_outputs(conn, *unspent_outputs: list): -# if unspent_outputs: -# return conn.run( -# conn.collection('utxos').delete_many({ -# '$or': [{ -# '$and': [ -# {'transaction_id': unspent_output['transaction_id']}, -# {'output_index': unspent_output['output_index']}, -# ], -# } for unspent_output in unspent_outputs] -# }) -# ) -# -# -# @register_query(TarantoolDB) -# def get_unspent_outputs(conn, *, query=None): -# if query is None: -# query = {} -# return conn.run(conn.collection('utxos').find(query, -# projection={'_id': False})) +@register_query(TarantoolDB) +def store_unspent_outputs(connection, *unspent_outputs: list): + space = connection.space('utxos') + if unspent_outputs: + for utxo in unspent_outputs: + try: + yield space.insert((utxo['transaction_id'], utxo['output_index'])) + except DuplicateKeyError: + # TODO log warning at least + pass +@register_query(TarantoolDB) +def delete_unspent_outputs(connection, *unspent_outputs: list): + space = connection.space('utxos') + if unspent_outputs: + for utxo in unspent_outputs: + yield space.delete((utxo['transaction_id'], utxo['output_index'])) + + +@register_query(TarantoolDB) +def get_unspent_outputs(connection): + space = connection.space('utxos') + return space.select() + @register_query(TarantoolDB) def store_pre_commit_state(connection, state: dict): space = connection.space("pre_commits") diff --git a/setup.py b/setup.py index 91447f2..fde1868 100644 --- a/setup.py +++ b/setup.py @@ -80,7 +80,7 @@ install_requires = [ 'planetmint-cryptoconditions>=0.9.0', 'flask-cors==3.0.10', 'flask-restful==0.3.9', - 'flask==2.0.1', + 'flask==2.1.2', 'gunicorn==20.1.0', 'jsonschema==3.2.0', 'logstats==0.3.0', diff --git a/tests/backend/tarantool/test_schema.py b/tests/backend/tarantool/test_schema.py index dc8d56c..71a21e4 100644 --- a/tests/backend/tarantool/test_schema.py +++ b/tests/backend/tarantool/test_schema.py @@ -17,15 +17,13 @@ def _check_spaces_by_list(conn, space_names): return _exists -def test_create_tables(): - db_conn = TarantoolDB("localhost", 3303) +def test_create_tables(db_conn): db_conn.drop_database() db_conn.init_database() assert db_conn.SPACE_NAMES == _check_spaces_by_list(conn=db_conn, space_names=db_conn.SPACE_NAMES) -def test_drop(): # remove dummy_db as argument - db_conn = TarantoolDB("localhost", 3303) +def test_drop(db_conn): # remove dummy_db as argument db_conn.drop_database() actual_spaces = _check_spaces_by_list(conn=db_conn, space_names=db_conn.SPACE_NAMES) assert [] == actual_spaces diff --git a/tests/conftest.py b/tests/conftest.py index 53917fa..1e4d54a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -103,10 +103,7 @@ def _configure_planetmint(request): config = { 'database': Config().get_db_map(backend), - 'tendermint': { - 'host': 'localhost', - 'port': 26657, - } + 'tendermint': Config()._private_real_config["tendermint"] } config['database']['name'] = test_db_name config = config_utils.env_config(config) diff --git a/tests/tendermint/test_lib.py b/tests/tendermint/test_lib.py index dbf2e0d..987ec0b 100644 --- a/tests/tendermint/test_lib.py +++ b/tests/tendermint/test_lib.py @@ -3,6 +3,7 @@ # SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) # Code is Apache-2.0 and docs are CC-BY-4.0 +from operator import index import os from unittest.mock import patch @@ -153,19 +154,18 @@ def test_post_transaction_invalid_mode(b): @pytest.mark.bdb -def test_update_utxoset(b, signed_create_tx, signed_transfer_tx, db_context): - mongo_client = MongoClient(host=db_context.host, port=db_context.port) +def test_update_utxoset(b, signed_create_tx, signed_transfer_tx, db_conn): b.update_utxoset(signed_create_tx) - utxoset = mongo_client[db_context.name]['utxos'] - assert utxoset.count_documents({}) == 1 - utxo = utxoset.find_one() - assert utxo['transaction_id'] == signed_create_tx.id - assert utxo['output_index'] == 0 + utxoset = db_conn.space('utxos') + assert utxoset.select().rowcount == 1 + utxo = utxoset.select().data + assert utxo[0][0] == signed_create_tx.id + assert utxo[0][1] == 0 b.update_utxoset(signed_transfer_tx) - assert utxoset.count_documents({}) == 1 - utxo = utxoset.find_one() - assert utxo['transaction_id'] == signed_transfer_tx.id - assert utxo['output_index'] == 0 + assert utxoset.select().rowcount == 1 + utxo = utxoset.select().data + assert utxo[0][0] == signed_transfer_tx.id + assert utxo[0][1] == 0 @pytest.mark.bdb @@ -271,73 +271,73 @@ def test_store_bulk_transaction(mocker, b, signed_create_tx, def test_delete_zero_unspent_outputs(b, utxoset): unspent_outputs, utxo_collection = utxoset delete_res = b.delete_unspent_outputs() - assert delete_res is None - assert utxo_collection.count_documents({}) == 3 - assert utxo_collection.count_documents( - {'$or': [ - {'transaction_id': 'a', 'output_index': 0}, - {'transaction_id': 'b', 'output_index': 0}, - {'transaction_id': 'a', 'output_index': 1}, - ]} - ) == 3 + # assert delete_res is None + assert utxo_collection.select().rowcount == 3 + # assert utxo_collection.count_documents( + # {'$or': [ + # {'transaction_id': 'a', 'output_index': 0}, + # {'transaction_id': 'b', 'output_index': 0}, + # {'transaction_id': 'a', 'output_index': 1}, + # ]} + # ) == 3 @pytest.mark.bdb def test_delete_one_unspent_outputs(b, utxoset): unspent_outputs, utxo_collection = utxoset delete_res = b.delete_unspent_outputs(unspent_outputs[0]) - assert delete_res.raw_result['n'] == 1 - assert utxo_collection.count_documents( - {'$or': [ - {'transaction_id': 'a', 'output_index': 1}, - {'transaction_id': 'b', 'output_index': 0}, - ]} - ) == 2 - assert utxo_collection.count_documents( - {'transaction_id': 'a', 'output_index': 0}) == 0 + assert len(list(delete_res)) == 1 + # assert utxo_collection.count_documents( + # {'$or': [ + # {'transaction_id': 'a', 'output_index': 1}, + # {'transaction_id': 'b', 'output_index': 0}, + # ]} + # ) == 2 + # assert utxo_collection.count_documents( + # {'transaction_id': 'a', 'output_index': 0}) == 0 @pytest.mark.bdb def test_delete_many_unspent_outputs(b, utxoset): unspent_outputs, utxo_collection = utxoset delete_res = b.delete_unspent_outputs(*unspent_outputs[::2]) - assert delete_res.raw_result['n'] == 2 - assert utxo_collection.count_documents( - {'$or': [ - {'transaction_id': 'a', 'output_index': 0}, - {'transaction_id': 'b', 'output_index': 0}, - ]} - ) == 0 - assert utxo_collection.count_documents( - {'transaction_id': 'a', 'output_index': 1}) == 1 + assert len(list(delete_res)) == 2 + # assert utxo_collection.count_documents( + # {'$or': [ + # {'transaction_id': 'a', 'output_index': 0}, + # {'transaction_id': 'b', 'output_index': 0}, + # ]} + # ) == 0 + # assert utxo_collection.count_documents( + # {'transaction_id': 'a', 'output_index': 1}) == 1 @pytest.mark.bdb def test_store_zero_unspent_output(b, utxo_collection): res = b.store_unspent_outputs() assert res is None - assert utxo_collection.count_documents({}) == 0 + assert utxo_collection.select().rowcount == 0 @pytest.mark.bdb def test_store_one_unspent_output(b, unspent_output_1, utxo_collection): res = b.store_unspent_outputs(unspent_output_1) - assert res.acknowledged - assert len(res.inserted_ids) == 1 - assert utxo_collection.count_documents( - {'transaction_id': unspent_output_1['transaction_id'], - 'output_index': unspent_output_1['output_index']} - ) == 1 + # assert res.acknowledged + assert len(list(res)) == 1 + # assert utxo_collection.count_documents( + # {'transaction_id': unspent_output_1['transaction_id'], + # 'output_index': unspent_output_1['output_index']} + # ) == 1 @pytest.mark.bdb def test_store_many_unspent_outputs(b, unspent_outputs, utxo_collection): res = b.store_unspent_outputs(*unspent_outputs) - assert res.acknowledged - assert len(res.inserted_ids) == 3 - assert utxo_collection.count_documents( - {'transaction_id': unspent_outputs[0]['transaction_id']} - ) == 3 + # assert res.acknowledged + assert len(list(res)) == 3 + # assert utxo_collection.count_documents( + # {'transaction_id': unspent_outputs[0]['transaction_id']} + # ) == 3 def test_get_utxoset_merkle_root_when_no_utxo(b):