mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-24 14:35:45 +00:00
added to and from static methods to asset, input model and removed logic from tools
Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
parent
9a0823001b
commit
7f97f9ea4b
@ -3,6 +3,8 @@
|
||||
# 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
|
||||
@ -10,3 +12,7 @@ class Asset:
|
||||
id: str = ""
|
||||
tx_id: str = ""
|
||||
data: str = ""
|
||||
|
||||
@staticmethod
|
||||
def from_tuple(asset_tuple: tuple) -> Asset:
|
||||
return Asset(asset_tuple[2], asset_tuple[1], json.loads(asset_tuple[0]))
|
||||
@ -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
|
||||
|
||||
@ -14,3 +15,41 @@ class Input:
|
||||
fulfills: Optional[Fulfills] = None
|
||||
owners_before: list[str] = field(default_factory=list)
|
||||
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
|
||||
}
|
||||
@ -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)
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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})
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user