From 7f97f9ea4ba9163a2b5b330ac86d96bccbee5008 Mon Sep 17 00:00:00 2001 From: Lorenz Herzberger Date: Thu, 17 Nov 2022 13:59:01 +0100 Subject: [PATCH] added to and from static methods to asset, input model and removed logic from tools Signed-off-by: Lorenz Herzberger --- planetmint/backend/models/asset.py | 8 +++- planetmint/backend/models/input.py | 41 ++++++++++++++++++- planetmint/backend/tarantool/query.py | 35 +++++----------- .../backend/tarantool/transaction/tools.py | 35 +--------------- planetmint/lib.py | 2 +- 5 files changed, 59 insertions(+), 62 deletions(-) diff --git a/planetmint/backend/models/asset.py b/planetmint/backend/models/asset.py index 8e3dfb1..6dc120c 100644 --- a/planetmint/backend/models/asset.py +++ b/planetmint/backend/models/asset.py @@ -3,10 +3,16 @@ # SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) # Code is Apache-2.0 and docs are CC-BY-4.0 +from __future__ import annotations +import json from dataclasses import dataclass @dataclass class Asset: id: str = "" tx_id: str = "" - data: str = "" \ No newline at end of file + data: str = "" + + @staticmethod + def from_tuple(asset_tuple: tuple) -> Asset: + return Asset(asset_tuple[2], asset_tuple[1], json.loads(asset_tuple[0])) \ No newline at end of file diff --git a/planetmint/backend/models/input.py b/planetmint/backend/models/input.py index c51191b..fcc0491 100644 --- a/planetmint/backend/models/input.py +++ b/planetmint/backend/models/input.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 __future__ import annotations from dataclasses import dataclass, field from typing import Optional @@ -13,4 +14,42 @@ class Input: tx_id: str = "" fulfills: Optional[Fulfills] = None owners_before: list[str] = field(default_factory=list) - fulfillment: str = "" \ No newline at end of file + fulfillment: str = "" + + @staticmethod + def from_dict(input_dict: dict, tx_id: str = "") -> Input: + fulfills = None + + if input_dict["fulfills"]: + fulfills = Fulfills( + input_dict["fulfills"]["transaction_id"], + input_dict["fulfills"]["output_index"] + ) + + return Input(tx_id, fulfills, input_dict["owners_before"], input_dict["fulfillment"]) + + @staticmethod + def from_tuple(input_tuple: tuple) -> Input: + tx_id = input_tuple[0] + fulfillment = input_tuple[1] + owners_before = input_tuple[2] + fulfills = None + fulfills_tx_id = input_tuple[3] + + if fulfills_tx_id: + # TODO: the output_index should be an unsigned int + fulfills = Fulfills(fulfills_tx_id, int(input_tuple[4])) + + return Input(tx_id, fulfills, owners_before, fulfillment) + + def to_input_dict(self) -> dict: + fulfills = { + "transaction_id": self.fulfills.transaction_id, + "output_index": self.fulfills.output_index + } if self.fulfills else None + + return { + "fulfills": fulfills, + "fulfillment": self.fulfillment, + "owners_before": self.owners_before + } \ No newline at end of file diff --git a/planetmint/backend/tarantool/query.py b/planetmint/backend/tarantool/query.py index 451e101..8d0b958 100644 --- a/planetmint/backend/tarantool/query.py +++ b/planetmint/backend/tarantool/query.py @@ -29,18 +29,16 @@ def _group_transaction_by_ids(connection, txids: list): if len(_txobject) == 0: continue _txobject = _txobject[0] - _txinputs = connection.run(connection.space(TARANT_TABLE_INPUT).select(txid, index=TARANT_ID_SEARCH)) + _txinputs = get_inputs_by_tx_id(connection, txid) _txoutputs = connection.run(connection.space(TARANT_TABLE_OUTPUT).select(txid, index=TARANT_ID_SEARCH)) _txkeys = connection.run(connection.space(TARANT_TABLE_KEYS).select(txid, index=TARANT_TX_ID_SEARCH)) _txassets = connection.run(connection.space(TARANT_TABLE_ASSETS).select(txid, index=TARANT_TX_ID_SEARCH)) _txmeta = connection.run(connection.space(TARANT_TABLE_META_DATA).select(txid, index=TARANT_ID_SEARCH)) _txscript = connection.run(connection.space(TARANT_TABLE_SCRIPT).select(txid, index=TARANT_TX_ID_SEARCH)) - _txinputs = sorted(_txinputs, key=itemgetter(6), reverse=False) _txoutputs = sorted(_txoutputs, key=itemgetter(8), reverse=False) result_map = { TARANT_TABLE_TRANSACTION: _txobject, - TARANT_TABLE_INPUT: _txinputs, TARANT_TABLE_OUTPUT: _txoutputs, TARANT_TABLE_KEYS: _txkeys, TARANT_TABLE_ASSETS: _txassets, @@ -49,6 +47,7 @@ def _group_transaction_by_ids(connection, txids: list): } tx_compose = TransactionCompose(db_results=result_map) _transaction = tx_compose.convert_to_dict() + _transaction[TARANT_TABLE_INPUT] = [input.to_input_dict() for input in _txinputs] _transactions.append(_transaction) return _transactions @@ -56,23 +55,7 @@ def _group_transaction_by_ids(connection, txids: list): def get_inputs_by_tx_id(connection, tx_id: str) -> list[Input]: _inputs = connection.run(connection.space(TARANT_TABLE_INPUT).select(tx_id, index=TARANT_ID_SEARCH)) _sorted_inputs = sorted(_inputs, key=itemgetter(6)) - - inputs = [] - - for input in _sorted_inputs: - tx_id = input[0] - fulfillment = input[1] - owners_before = input[2] - fulfills = None - fulfills_tx_id = input[3] - - if fulfills_tx_id: - # TODO: the output_index should be an unsigned int - fulfills = Fulfills(fulfills_tx_id, int(input[4])) - - inputs.append(Input(tx_id, fulfills, owners_before, fulfillment)) - - return inputs + return [Input.from_tuple(input) for input in _sorted_inputs] @register_query(TarantoolDBConnection) def store_transaction_inputs(connection, inputs: list[Input]): @@ -97,8 +80,10 @@ def store_transactions(connection, signed_transactions: list): connection.run(connection.space(TARANT_TABLE_TRANSACTION).insert(txtuples[TARANT_TABLE_TRANSACTION]), only_data=False) except: # This is used for omitting duplicate error in database for test -> test_bigchain_api::test_double_inclusion # noqa: E501, E722 continue - for _in in txtuples[TARANT_TABLE_INPUT]: - connection.run(connection.space(TARANT_TABLE_INPUT).insert(_in), only_data=False) + + inputs = [Input.from_dict(input, transaction["id"]) for input in transaction[TARANT_TABLE_INPUT]] + store_transaction_inputs(connection, inputs) + for _out in txtuples[TARANT_TABLE_OUTPUT]: connection.run(connection.space(TARANT_TABLE_OUTPUT).insert(_out), only_data=False) @@ -111,7 +96,7 @@ def store_transactions(connection, signed_transactions: list): if txtuples[TARANT_TABLE_ASSETS] is not None: connection.run(connection.space(TARANT_TABLE_ASSETS).insert(txtuples[TARANT_TABLE_ASSETS]), only_data=False) - if txtuples["script"] is not None: + if txtuples[TARANT_TABLE_SCRIPT] is not None: connection.run(connection.space(TARANT_TABLE_SCRIPT).insert(txtuples["script"]), only_data=False) @@ -169,7 +154,7 @@ def store_assets(connection, assets: list): @register_query(TarantoolDBConnection) def get_asset(connection, asset_id: str) -> Asset: _data = connection.run(connection.space(TARANT_TABLE_ASSETS).select(asset_id, index=TARANT_TX_ID_SEARCH)) - return Asset(_data[0][2], _data[0][1], json.loads(_data[0][0])) + return Asset.from_tuple(_data[0]) @register_query(TarantoolDBConnection) @@ -180,7 +165,7 @@ def get_assets(connection, assets_ids: list) -> list[Asset]: _returned_data.append(res[0]) sorted_assets = sorted(_returned_data, key=lambda k: k[1], reverse=False) - return [Asset(asset[2], asset[1], json.loads(asset[0])) for asset in sorted_assets] + return [Asset.from_tuple(asset) for asset in sorted_assets] @register_query(TarantoolDBConnection) diff --git a/planetmint/backend/tarantool/transaction/tools.py b/planetmint/backend/tarantool/transaction/tools.py index d4725f7..6311349 100644 --- a/planetmint/backend/tarantool/transaction/tools.py +++ b/planetmint/backend/tarantool/transaction/tools.py @@ -72,25 +72,6 @@ class TransactionDecompose: asset_id = _asset[0]["id"] if _asset[0].get("id") is not None else self._transaction["id"] self._tuple_transaction[TARANT_TABLE_ASSETS] = (json.dumps(_asset), self._transaction["id"], asset_id) - def __prepare_inputs(self): - _inputs = [] - input_index = 0 - for _input in self._transaction[TARANT_TABLE_INPUT]: - - _inputs.append( - ( - self._transaction["id"], - _input["fulfillment"], - _input["owners_before"], - _input["fulfills"]["transaction_id"] if _input["fulfills"] is not None else "", - str(_input["fulfills"]["output_index"]) if _input["fulfills"] is not None else "", - self.__create_hash(7), - input_index, - ) - ) - input_index = input_index + 1 - return _inputs - def __prepare_outputs(self): _outputs = [] _keys = [] @@ -145,7 +126,6 @@ class TransactionDecompose: self._metadata_check() self.__asset_check() self._tuple_transaction[TARANT_TABLE_TRANSACTION] = self.__prepare_transaction() - self._tuple_transaction[TARANT_TABLE_INPUT] = self.__prepare_inputs() keys, outputs = self.__prepare_outputs() self._tuple_transaction[TARANT_TABLE_OUTPUT] = outputs self._tuple_transaction[TARANT_TABLE_KEYS] = keys @@ -175,18 +155,6 @@ class TransactionCompose: def _get_metadata(self): return json.loads(self.db_results[TARANT_TABLE_META_DATA][0][1]) if len(self.db_results[TARANT_TABLE_META_DATA]) == 1 else None - def _get_inputs(self): - _inputs = [] - for _input in self.db_results[TARANT_TABLE_INPUT]: - _in = copy.deepcopy(self._map[TARANT_TABLE_INPUT][_input[-1]]) - _in["fulfillment"] = _input[1] - if _in["fulfills"] is not None: - _in["fulfills"]["transaction_id"] = _input[3] - _in["fulfills"]["output_index"] = int(_input[4]) - _in["owners_before"] = _input[2] - _inputs.append(_in) - return _inputs - def _get_outputs(self): _outputs = [] for _output in self.db_results[TARANT_TABLE_OUTPUT]: @@ -217,10 +185,9 @@ class TransactionCompose: transaction = {k: None for k in list(self._map.keys())} transaction["id"] = self._get_transaction_id() transaction[TARANT_TABLE_ASSETS] = self._get_asset() - transaction[TARANT_TABLE_META_DATA] = self._get_metadata() + transaction["metadata"] = self._get_metadata() transaction["version"] = self._get_transaction_version() transaction["operation"] = self._get_transaction_operation() - transaction[TARANT_TABLE_INPUT] = self._get_inputs() transaction[TARANT_TABLE_OUTPUT] = self._get_outputs() if self._get_script(): transaction[TARANT_TABLE_SCRIPT] = self._get_script() diff --git a/planetmint/lib.py b/planetmint/lib.py index bd72a1a..751ea3c 100644 --- a/planetmint/lib.py +++ b/planetmint/lib.py @@ -259,7 +259,7 @@ class Planetmint(object): if "metadata" not in transaction: metadata = metadata[0] if metadata else None if metadata: - metadata = metadata.get("metadata") + metadata = metadata.metadata transaction.update({"metadata": metadata})