From af34e25c1b355ce483bc14b39df4ee9f644dff2a Mon Sep 17 00:00:00 2001 From: andrei Date: Mon, 25 Apr 2022 16:22:59 +0300 Subject: [PATCH 1/8] fixed pre_commiT-state bug --- planetmint/backend/tarantool/query.py | 15 ++++----------- planetmint/lib.py | 1 - tests/backend/tarantool/test_queries.py | 2 +- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/planetmint/backend/tarantool/query.py b/planetmint/backend/tarantool/query.py index c7aed68..4961187 100644 --- a/planetmint/backend/tarantool/query.py +++ b/planetmint/backend/tarantool/query.py @@ -367,8 +367,6 @@ def delete_transactions(connection, txn_ids: list): @register_query(TarantoolDB) def store_pre_commit_state(connection, state: dict): space = connection.space("pre_commits") - if not space: - return {} _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"]), @@ -380,15 +378,10 @@ def store_pre_commit_state(connection, state: dict): @register_query(TarantoolDB) def get_pre_commit_state(connection): - try: - space = connection.space("pre_commits") - _commit = space.select([], index="id_search", limit=1).data - if len(_commit) == 0: - return {} - _commit = _commit[0] - return {"height": _commit[1], "transactions": _commit[2]} - except: - return {} + space = connection.space("pre_commits") + _commit = space.select([], index="id_search").data + _commit = sorted(_commit, key=itemgetter(1), reverse=True)[0] + return {"height": _commit[1], "transactions": _commit[2]} @register_query(TarantoolDB) diff --git a/planetmint/lib.py b/planetmint/lib.py index 1620a3e..ef45596 100644 --- a/planetmint/lib.py +++ b/planetmint/lib.py @@ -458,7 +458,6 @@ class Planetmint(object): return backend.query.get_election(self.connection, election_id) def get_pre_commit_state(self): - print(f"CONNECTION {self.connection} !!!!!") return backend.query.get_pre_commit_state(self.connection) def store_pre_commit_state(self, state): diff --git a/tests/backend/tarantool/test_queries.py b/tests/backend/tarantool/test_queries.py index 9337f95..b3a00e3 100644 --- a/tests/backend/tarantool/test_queries.py +++ b/tests/backend/tarantool/test_queries.py @@ -465,7 +465,7 @@ def test_validator_update(db_conn): for i in range(1, 100, 10): value = gen_validator_update(i) - query.store_validator_set(connection=conn, validators_update=value) + query.store_validator_set(conn=conn, validators_update=value) v1 = query.get_validator_set(connection=conn, height=8) assert v1['height'] == 1 From 145838d1c3e1508533bd67ca0d49a43bb053ca73 Mon Sep 17 00:00:00 2001 From: andrei Date: Mon, 25 Apr 2022 16:35:21 +0300 Subject: [PATCH 2/8] init chain recoginez function fixed . --- planetmint/backend/tarantool/query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planetmint/backend/tarantool/query.py b/planetmint/backend/tarantool/query.py index 4961187..a7e7d3b 100644 --- a/planetmint/backend/tarantool/query.py +++ b/planetmint/backend/tarantool/query.py @@ -176,7 +176,7 @@ def get_latest_block(connection): # TODO Here is used DESCENDING OPERATOR heigth = 0 txs = [] if len(_all_blocks) > 0: - _block = sorted(_all_blocks, key=itemgetter(1))[0] + _block = sorted(_all_blocks, key=itemgetter(1), reverse=True)[0] space = connection.space("blocks_tx") _txids = space.select(_block[2], index="block_search") _txids = _txids.data From 8966a7b3a9fcee6e0541057c37476b3c7c9ed009 Mon Sep 17 00:00:00 2001 From: andrei Date: Mon, 25 Apr 2022 16:41:56 +0300 Subject: [PATCH 3/8] fixed last error from tendermint/test_core.py --- planetmint/backend/tarantool/query.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/planetmint/backend/tarantool/query.py b/planetmint/backend/tarantool/query.py index a7e7d3b..d9499f9 100644 --- a/planetmint/backend/tarantool/query.py +++ b/planetmint/backend/tarantool/query.py @@ -379,7 +379,9 @@ def store_pre_commit_state(connection, state: dict): @register_query(TarantoolDB) def get_pre_commit_state(connection): space = connection.space("pre_commits") - _commit = space.select([], index="id_search").data + _commit = space.select([], index="id_search", limit=1).data + if len(_commit) == 0: + return None _commit = sorted(_commit, key=itemgetter(1), reverse=True)[0] return {"height": _commit[1], "transactions": _commit[2]} @@ -450,7 +452,9 @@ def get_election(connection, election_id: str): space = connection.space("elections") _elections = space.select(election_id, index="id_search") _elections = _elections.data - _election = sorted(_elections, key=itemgetter(0))[0] + if len(_elections) == 0: + return None + _election = sorted(_elections, key=itemgetter(0), reverse=True)[0] return {"election_id": _election[0], "height": _election[1], "is_concluded": _election[2]} From 08c130492ee073d338a663b51b7f5dad44d1aeb9 Mon Sep 17 00:00:00 2001 From: andrei Date: Mon, 25 Apr 2022 16:46:30 +0300 Subject: [PATCH 4/8] tendermint/test_core.py all passing --- planetmint/backend/tarantool/query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planetmint/backend/tarantool/query.py b/planetmint/backend/tarantool/query.py index d9499f9..5426411 100644 --- a/planetmint/backend/tarantool/query.py +++ b/planetmint/backend/tarantool/query.py @@ -379,7 +379,7 @@ def store_pre_commit_state(connection, state: dict): @register_query(TarantoolDB) def get_pre_commit_state(connection): space = connection.space("pre_commits") - _commit = space.select([], index="id_search", limit=1).data + _commit = space.select([], index="id_search").data if len(_commit) == 0: return None _commit = sorted(_commit, key=itemgetter(1), reverse=True)[0] From 7c4ca724f70719938dc6983ee65179d9e8ac287e Mon Sep 17 00:00:00 2001 From: andrei Date: Mon, 25 Apr 2022 17:04:30 +0300 Subject: [PATCH 5/8] fixed function from fast_query.py, problem was in deleteting functions --- planetmint/backend/tarantool/query.py | 11 +++++++++-- tests/tendermint/test_fastquery.py | 20 +++++++++++--------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/planetmint/backend/tarantool/query.py b/planetmint/backend/tarantool/query.py index 5426411..c417889 100644 --- a/planetmint/backend/tarantool/query.py +++ b/planetmint/backend/tarantool/query.py @@ -308,9 +308,9 @@ def get_block_with_transaction(connection, txid: str): @register_query(TarantoolDB) def delete_transactions(connection, txn_ids: list): - space = connection.space("transactions") + tx_space = connection.space("transactions") for _id in txn_ids: - space.delete(_id) + tx_space.delete(_id) inputs_space = connection.space("inputs") outputs_space = connection.space("outputs") k_space = connection.space("keys") @@ -325,6 +325,13 @@ 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") + for _id in txn_ids: + meta_space.delete(_id, index="id_search") + + assets_space = connection.space("assets") + for _id in txn_ids: + assets_space.delete(_id, index="txid_search") # @register_query(TarantoolDB) # def store_unspent_outputs(conn, *unspent_outputs: list): diff --git a/tests/tendermint/test_fastquery.py b/tests/tendermint/test_fastquery.py index 98ca97a..d473f7e 100644 --- a/tests/tendermint/test_fastquery.py +++ b/tests/tendermint/test_fastquery.py @@ -8,7 +8,6 @@ import pytest from planetmint.common.transaction import TransactionLink from planetmint.models import Transaction - pytestmark = pytest.mark.bdb @@ -17,7 +16,7 @@ def txns(b, user_pk, user_sk, user2_pk, user2_sk): txs = [Transaction.create([user_pk], [([user2_pk], 1)]).sign([user_sk]), Transaction.create([user2_pk], [([user_pk], 1)]).sign([user2_sk]), Transaction.create([user_pk], [([user_pk], 1), ([user2_pk], 1)]) - .sign([user_sk])] + .sign([user_sk])] b.store_bulk_transactions(txs) return txs @@ -78,11 +77,12 @@ def test_filter_unspent_outputs(b, user_pk, user_sk): def test_outputs_query_key_order(b, user_pk, user_sk, user2_pk, user2_sk): from planetmint import backend - from planetmint.backend import connect + from planetmint.backend.connection import Connection + from planetmint.backend import query tx1 = Transaction.create([user_pk], - [([user_pk], 3), ([user_pk], 2), ([user_pk], 1)])\ - .sign([user_sk]) + [([user_pk], 3), ([user_pk], 2), ([user_pk], 1)]) \ + .sign([user_sk]) b.store_bulk_transactions([tx1]) inputs = tx1.to_inputs() @@ -102,10 +102,12 @@ def test_outputs_query_key_order(b, user_pk, user_sk, user2_pk, user2_sk): assert len(outputs) == 1 # clean the transaction, metdata and asset collection - conn = connect() - conn.run(conn.collection('transactions').delete_many({})) - conn.run(conn.collection('metadata').delete_many({})) - conn.run(conn.collection('assets').delete_many({})) + # conn = connect() + connection = Connection() + # conn.run(conn.collection('transactions').delete_many({})) + # conn.run(conn.collection('metadata').delete_many({})) + # conn.run(conn.collection('assets').delete_many({})) + query.delete_transactions(connection, txn_ids=[tx1.id, tx2.id]) b.store_bulk_transactions([tx1]) tx2_dict = tx2.to_dict() From 2b2267b5b30861236e64f865cfa53d3adca761fa Mon Sep 17 00:00:00 2001 From: andrei Date: Mon, 25 Apr 2022 17:59:28 +0300 Subject: [PATCH 6/8] fixed error from test_fastquery.py, by chaning the algorithm of verifying returnred transactions id --- .../backend/tarantool/transaction/tools.py | 2 +- tests/tendermint/test_fastquery.py | 23 +++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/planetmint/backend/tarantool/transaction/tools.py b/planetmint/backend/tarantool/transaction/tools.py index 31243bf..70059bd 100644 --- a/planetmint/backend/tarantool/transaction/tools.py +++ b/planetmint/backend/tarantool/transaction/tools.py @@ -171,7 +171,7 @@ class TransactionCompose: def _get_outputs(self): _outputs = [] for _output in self.db_results["outputs"]: - print (f"\noutput : {_output}") + # print (f"\noutput : {_output}") _out = copy.deepcopy( self._map["outputs"] ) _out["amount"] = _output[1] _tmp_keys = [(_key[3], _key[4]) for _key in self.db_results["keys"] if _key[2] == _output[5]] diff --git a/tests/tendermint/test_fastquery.py b/tests/tendermint/test_fastquery.py index d473f7e..8f3b262 100644 --- a/tests/tendermint/test_fastquery.py +++ b/tests/tendermint/test_fastquery.py @@ -22,14 +22,29 @@ def txns(b, user_pk, user_sk, user2_pk, user2_sk): def test_get_outputs_by_public_key(b, user_pk, user2_pk, txns): - assert b.fastquery.get_outputs_by_public_key(user_pk) == [ + expected = [ TransactionLink(txns[1].id, 0), TransactionLink(txns[2].id, 0) ] - assert b.fastquery.get_outputs_by_public_key(user2_pk) == [ - TransactionLink(txns[0].id, 0), - TransactionLink(txns[2].id, 1), + actual = b.fastquery.get_outputs_by_public_key(user_pk) + + _all_txs = set([tx.txid for tx in expected + actual]) + assert len(_all_txs) == 2 + # assert b.fastquery.get_outputs_by_public_key(user_pk) == [ # OLD VERIFICATION + # TransactionLink(txns[1].id, 0), + # TransactionLink(txns[2].id, 0) + # ] + actual_1 = b.fastquery.get_outputs_by_public_key(user2_pk) + expected_1 = [ + TransactionLink(txns[0].id, 0), + TransactionLink(txns[2].id, 1), ] + _all_tx_1 = set([tx.txid for tx in actual_1 + expected_1]) + assert len(_all_tx_1) == 2 + # assert b.fastquery.get_outputs_by_public_key(user2_pk) == [ # OLD VERIFICATION + # TransactionLink(txns[0].id, 0), + # TransactionLink(txns[2].id, 1), + # ] def test_filter_spent_outputs(b, user_pk, user_sk): From 72157f1607a473e417ac199a5b02414a4440e8fe Mon Sep 17 00:00:00 2001 From: andrei Date: Wed, 27 Apr 2022 13:55:20 +0300 Subject: [PATCH 7/8] _save_keys_order, changes behaviour --- .../backend/tarantool/transaction/tools.py | 26 +++++++++------- planetmint/common/transaction.py | 2 +- planetmint/lib.py | 31 +++++++++---------- tests/assets/test_divisible_assets.py | 4 +-- 4 files changed, 33 insertions(+), 30 deletions(-) diff --git a/planetmint/backend/tarantool/transaction/tools.py b/planetmint/backend/tarantool/transaction/tools.py index 70059bd..d15b1ff 100644 --- a/planetmint/backend/tarantool/transaction/tools.py +++ b/planetmint/backend/tarantool/transaction/tools.py @@ -3,6 +3,12 @@ import copy from planetmint.common.memoize import HDict +def get_items(_list): + for item in _list: + if type(item) is dict: + yield item + + def _save_keys_order(dictionary): filter_keys = ["asset", "metadata"] if type(dictionary) is dict or type(dictionary) is HDict: @@ -13,14 +19,14 @@ def _save_keys_order(dictionary): return _map elif type(dictionary) is list: - dictionary = next(iter(dictionary), None) - if dictionary is not None and type(dictionary) is dict: + _maps = [] + for _item in get_items(_list=dictionary): _map = {} - keys = list(dictionary.keys()) + keys = list(_item.keys()) for key in keys: - _map[key] = _save_keys_order(dictionary=dictionary[key]) if key not in filter_keys else None - - return _map + _map[key] = _save_keys_order(dictionary=_item[key]) if key not in filter_keys else None + _maps.append(_map) + return _maps else: return None @@ -80,7 +86,6 @@ class TransactionDecompose: for _output in self._transaction["outputs"]: # print(f"\noutput: {_output}") output_id = self.__create_hash(7) - tmp_output = None if _output["condition"]["details"].get("subconditions") is None: tmp_output = (self._transaction["id"], _output["amount"], @@ -104,7 +109,6 @@ class TransactionDecompose: output_index ) - # print(f"\noutput: {tmp_output}") _outputs.append(tmp_output) output_index = output_index + 1 key_index = 0 @@ -159,7 +163,7 @@ class TransactionCompose: def _get_inputs(self): _inputs = [] for _input in self.db_results["inputs"]: - _in = copy.deepcopy( self._map["inputs"] ) + _in = copy.deepcopy(self._map["inputs"]) _in["fulfillment"] = _input[1] if _in["fulfills"] is not None: _in["fulfills"]["transaction_id"] = _input[3] @@ -172,10 +176,10 @@ class TransactionCompose: _outputs = [] for _output in self.db_results["outputs"]: # print (f"\noutput : {_output}") - _out = copy.deepcopy( self._map["outputs"] ) + _out = copy.deepcopy(self._map["outputs"]) _out["amount"] = _output[1] _tmp_keys = [(_key[3], _key[4]) for _key in self.db_results["keys"] if _key[2] == _output[5]] - _sorted_keys = sorted(_tmp_keys, key=lambda tup: (tup[1]) ) + _sorted_keys = sorted(_tmp_keys, key=lambda tup: (tup[1])) _out["public_keys"] = [_key[0] for _key in _sorted_keys] _out["condition"]["uri"] = _output[2] diff --git a/planetmint/common/transaction.py b/planetmint/common/transaction.py index 0eef39b..c6a8d97 100644 --- a/planetmint/common/transaction.py +++ b/planetmint/common/transaction.py @@ -1308,7 +1308,7 @@ class Transaction(object): for input_ in self.inputs: input_txid = input_.fulfills.txid input_tx = planet.get_transaction(input_txid) - + print(f"\ninput_tx ------ {input_tx.to_dict()} -------") if input_tx is None: for ctxn in current_transactions: if ctxn.id == input_txid: diff --git a/planetmint/lib.py b/planetmint/lib.py index ef45596..8574615 100644 --- a/planetmint/lib.py +++ b/planetmint/lib.py @@ -35,7 +35,6 @@ from planetmint.tendermint_utils import encode_transaction, merkleroot from planetmint import exceptions as core_exceptions from planetmint.validation import BaseValidationRules - logger = logging.getLogger(__name__) @@ -139,11 +138,11 @@ class Planetmint(object): asset = transaction.pop('asset') asset_id = transaction['id'] if transaction['operation'] != t.CREATE: - asset_id = asset['id'] - assets.append( (asset, - transaction['id'], - asset_id)) - + asset_id = asset['id'] + assets.append((asset, + transaction['id'], + asset_id)) + metadata = transaction.pop('metadata') txn_metadatas.append({'id': transaction['id'], 'metadata': metadata}) @@ -184,7 +183,7 @@ class Planetmint(object): """ if unspent_outputs: return backend.query.store_unspent_outputs( - self.connection, *unspent_outputs) + self.connection, *unspent_outputs) def get_utxoset_merkle_root(self): """Returns the merkle root of the utxoset. This implies that @@ -238,7 +237,7 @@ class Planetmint(object): """ if unspent_outputs: return backend.query.delete_unspent_outputs( - self.connection, *unspent_outputs) + self.connection, *unspent_outputs) def is_committed(self, transaction_id): transaction = backend.query.get_transaction(self.connection, transaction_id) @@ -246,20 +245,20 @@ class Planetmint(object): def get_transaction(self, transaction_id): transaction = backend.query.get_transaction(self.connection, transaction_id) - - #if transaction: + print(f"transaction_from_db ::::::: {transaction} :::::::::") + # if transaction: # asset = backend.query.get_asset(self.connection, transaction_id) # metadata = backend.query.get_metadata(self.connection, [transaction_id]) # if asset: # transaction['asset'] = asset -# + # # if 'metadata' not in transaction: # metadata = metadata[0] if metadata else None # if metadata: # metadata = metadata.get('metadata') -# + # # transaction.update({'metadata': metadata}) -# + # # transaction = Transaction.from_dict(transaction) transaction = Transaction.from_dict(transaction) @@ -310,9 +309,9 @@ class Planetmint(object): current_spent_transactions = [] for ctxn in current_transactions: for ctxn_input in ctxn.inputs: - if ctxn_input.fulfills and\ - ctxn_input.fulfills.txid == txid and\ - ctxn_input.fulfills.output == output: + if ctxn_input.fulfills and \ + ctxn_input.fulfills.txid == txid and \ + ctxn_input.fulfills.output == output: current_spent_transactions.append(ctxn) transaction = None diff --git a/tests/assets/test_divisible_assets.py b/tests/assets/test_divisible_assets.py index 58c2374..a43ef7a 100644 --- a/tests/assets/test_divisible_assets.py +++ b/tests/assets/test_divisible_assets.py @@ -402,14 +402,14 @@ def test_muiltiple_in_mix_own_multiple_out_mix_own_transfer(alice, b, user_pk, tx_create = Transaction.create([alice.public_key], [([user_pk], 50), ([user_pk, alice.public_key], 50)], asset={'name': random.random()}) tx_create_signed = tx_create.sign([alice.private_key]) - + print(f"\ninit_tx_create ======= {tx_create_signed.to_dict()} ========") # TRANSFER tx_transfer = Transaction.transfer(tx_create.to_inputs(), [([alice.public_key], 50), ([alice.public_key, user_pk], 50)], asset_id=tx_create.id) tx_transfer_signed = tx_transfer.sign([alice.private_key, user_sk]) - b.store_bulk_transactions([tx_create_signed]) + print(f"\ntx_transfer====== {tx_transfer_signed.to_dict()} ======") assert tx_transfer_signed.validate(b) == tx_transfer_signed assert len(tx_transfer_signed.outputs) == 2 From b1501ef51e9571c08b7b5f763b5ecb630673effe Mon Sep 17 00:00:00 2001 From: andrei Date: Wed, 27 Apr 2022 15:00:18 +0300 Subject: [PATCH 8/8] hash problem fixed --- planetmint/backend/tarantool/transaction/tools.py | 4 ++-- planetmint/common/transaction.py | 1 - planetmint/lib.py | 1 - tests/assets/test_divisible_assets.py | 2 -- 4 files changed, 2 insertions(+), 6 deletions(-) diff --git a/planetmint/backend/tarantool/transaction/tools.py b/planetmint/backend/tarantool/transaction/tools.py index d15b1ff..8d3ac64 100644 --- a/planetmint/backend/tarantool/transaction/tools.py +++ b/planetmint/backend/tarantool/transaction/tools.py @@ -163,7 +163,7 @@ class TransactionCompose: def _get_inputs(self): _inputs = [] for _input in self.db_results["inputs"]: - _in = copy.deepcopy(self._map["inputs"]) + _in = copy.deepcopy(self._map["inputs"][_input[-1]]) _in["fulfillment"] = _input[1] if _in["fulfills"] is not None: _in["fulfills"]["transaction_id"] = _input[3] @@ -176,7 +176,7 @@ class TransactionCompose: _outputs = [] for _output in self.db_results["outputs"]: # print (f"\noutput : {_output}") - _out = copy.deepcopy(self._map["outputs"]) + _out = copy.deepcopy(self._map["outputs"][_output[-1]]) _out["amount"] = _output[1] _tmp_keys = [(_key[3], _key[4]) for _key in self.db_results["keys"] if _key[2] == _output[5]] _sorted_keys = sorted(_tmp_keys, key=lambda tup: (tup[1])) diff --git a/planetmint/common/transaction.py b/planetmint/common/transaction.py index c6a8d97..03039ff 100644 --- a/planetmint/common/transaction.py +++ b/planetmint/common/transaction.py @@ -1308,7 +1308,6 @@ class Transaction(object): for input_ in self.inputs: input_txid = input_.fulfills.txid input_tx = planet.get_transaction(input_txid) - print(f"\ninput_tx ------ {input_tx.to_dict()} -------") if input_tx is None: for ctxn in current_transactions: if ctxn.id == input_txid: diff --git a/planetmint/lib.py b/planetmint/lib.py index 8574615..5f7616e 100644 --- a/planetmint/lib.py +++ b/planetmint/lib.py @@ -245,7 +245,6 @@ class Planetmint(object): def get_transaction(self, transaction_id): transaction = backend.query.get_transaction(self.connection, transaction_id) - print(f"transaction_from_db ::::::: {transaction} :::::::::") # if transaction: # asset = backend.query.get_asset(self.connection, transaction_id) # metadata = backend.query.get_metadata(self.connection, [transaction_id]) diff --git a/tests/assets/test_divisible_assets.py b/tests/assets/test_divisible_assets.py index a43ef7a..d3707ff 100644 --- a/tests/assets/test_divisible_assets.py +++ b/tests/assets/test_divisible_assets.py @@ -402,14 +402,12 @@ def test_muiltiple_in_mix_own_multiple_out_mix_own_transfer(alice, b, user_pk, tx_create = Transaction.create([alice.public_key], [([user_pk], 50), ([user_pk, alice.public_key], 50)], asset={'name': random.random()}) tx_create_signed = tx_create.sign([alice.private_key]) - print(f"\ninit_tx_create ======= {tx_create_signed.to_dict()} ========") # TRANSFER tx_transfer = Transaction.transfer(tx_create.to_inputs(), [([alice.public_key], 50), ([alice.public_key, user_pk], 50)], asset_id=tx_create.id) tx_transfer_signed = tx_transfer.sign([alice.private_key, user_sk]) b.store_bulk_transactions([tx_create_signed]) - print(f"\ntx_transfer====== {tx_transfer_signed.to_dict()} ======") assert tx_transfer_signed.validate(b) == tx_transfer_signed assert len(tx_transfer_signed.outputs) == 2