mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-25 15:05:49 +00:00
moved field removal methods to DbTransaction
redefined strcuture of DbTransction.to_dict() to be equal to the one of Transactions.to_dict() Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
parent
61c35bd0fb
commit
d05acebb2f
@ -8,7 +8,6 @@ from dataclasses import dataclass, field
|
|||||||
|
|
||||||
from planetmint.backend.models import Asset, MetaData, Input, Script, Output
|
from planetmint.backend.models import Asset, MetaData, Input, Script, Output
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class DbTransaction:
|
class DbTransaction:
|
||||||
id: str = ""
|
id: str = ""
|
||||||
@ -43,15 +42,31 @@ class DbTransaction:
|
|||||||
inputs=Input.from_list_dict(transaction[5]),
|
inputs=Input.from_list_dict(transaction[5]),
|
||||||
script=Script.from_dict(transaction[6]),
|
script=Script.from_dict(transaction[6]),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def remove_generated_fields(tx_dict: dict):
|
||||||
|
tx_dict["outputs"] = [DbTransaction.remove_generated_or_none_output_keys(output) for output in tx_dict["outputs"]]
|
||||||
|
if "script" in tx_dict and tx_dict["script"] is None:
|
||||||
|
tx_dict.pop("script")
|
||||||
|
return tx_dict
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def remove_generated_or_none_output_keys(output):
|
||||||
|
output["condition"]["details"] = {k: v for k, v in output["condition"]["details"].items() if v is not None}
|
||||||
|
output.pop("id")
|
||||||
|
return output
|
||||||
|
|
||||||
|
|
||||||
def to_dict(self) -> dict:
|
def to_dict(self) -> dict:
|
||||||
return {
|
tx = {
|
||||||
"id": self.id,
|
|
||||||
"operation": self.operation,
|
|
||||||
"version": self.version,
|
|
||||||
"inputs": Input.list_to_dict(self.inputs),
|
"inputs": Input.list_to_dict(self.inputs),
|
||||||
"outputs": Output.list_to_dict(self.outputs),
|
"outputs": Output.list_to_dict(self.outputs),
|
||||||
"assets": Asset.list_to_dict(self.assets),
|
"operation": self.operation,
|
||||||
"metadata": self.metadata.to_dict() if self.metadata is not None else None,
|
"metadata": self.metadata.to_dict() if self.metadata is not None else None,
|
||||||
"script": self.script.to_dict() if self.script is not None else None,
|
"assets": Asset.list_to_dict(self.assets),
|
||||||
|
"version": self.version,
|
||||||
|
"id": self.id,
|
||||||
|
"script": self.script.to_dict() if self.script is not None else None,
|
||||||
}
|
}
|
||||||
|
tx = DbTransaction.remove_generated_fields(tx)
|
||||||
|
return tx
|
||||||
|
|||||||
@ -39,7 +39,7 @@ from transactions.common.transaction_mode_types import BROADCAST_TX_COMMIT, BROA
|
|||||||
from transactions.types.elections.election import Election
|
from transactions.types.elections.election import Election
|
||||||
from transactions.types.elections.validator_utils import election_id_to_public_key
|
from transactions.types.elections.validator_utils import election_id_to_public_key
|
||||||
|
|
||||||
from planetmint.backend.models import Output
|
from planetmint.backend.models import Output, DbTransaction
|
||||||
from planetmint.backend.tarantool.const import TARANT_TABLE_GOVERNANCE, TARANT_TABLE_TRANSACTION
|
from planetmint.backend.tarantool.const import TARANT_TABLE_GOVERNANCE, TARANT_TABLE_TRANSACTION
|
||||||
from planetmint.config import Config
|
from planetmint.config import Config
|
||||||
from planetmint import backend, config_utils, fastquery
|
from planetmint import backend, config_utils, fastquery
|
||||||
@ -401,7 +401,7 @@ class Planetmint(object):
|
|||||||
input_conditions.append(output)
|
input_conditions.append(output)
|
||||||
tx_dict = input_tx.to_dict()
|
tx_dict = input_tx.to_dict()
|
||||||
tx_dict["outputs"] = Output.list_to_dict(_output)
|
tx_dict["outputs"] = Output.list_to_dict(_output)
|
||||||
tx_dict = remove_generated_fields(tx_dict)
|
tx_dict = DbTransaction.remove_generated_fields(tx_dict)
|
||||||
pm_transaction = Transaction.from_dict(tx_dict, False)
|
pm_transaction = Transaction.from_dict(tx_dict, False)
|
||||||
input_txs.append(pm_transaction)
|
input_txs.append(pm_transaction)
|
||||||
|
|
||||||
@ -940,15 +940,3 @@ class Planetmint(object):
|
|||||||
|
|
||||||
Block = namedtuple("Block", ("app_hash", "height", "transactions"))
|
Block = namedtuple("Block", ("app_hash", "height", "transactions"))
|
||||||
|
|
||||||
|
|
||||||
def remove_generated_fields(tx_dict: dict):
|
|
||||||
tx_dict["outputs"] = [remove_generated_or_none_output_keys(output) for output in tx_dict["outputs"]]
|
|
||||||
if tx_dict["script"] is None:
|
|
||||||
tx_dict.pop("script")
|
|
||||||
return tx_dict
|
|
||||||
|
|
||||||
|
|
||||||
def remove_generated_or_none_output_keys(output):
|
|
||||||
output["condition"]["details"] = {k: v for k, v in output["condition"]["details"].items() if v is not None}
|
|
||||||
output.pop("id")
|
|
||||||
return output
|
|
||||||
|
|||||||
@ -11,7 +11,7 @@ from transactions.common.transaction import Transaction
|
|||||||
from transactions.types.assets.create import Create
|
from transactions.types.assets.create import Create
|
||||||
from transactions.types.assets.transfer import Transfer
|
from transactions.types.assets.transfer import Transfer
|
||||||
from planetmint.backend.interfaces import Asset, MetaData
|
from planetmint.backend.interfaces import Asset, MetaData
|
||||||
from planetmint.lib import remove_generated_fields
|
from planetmint.backend.models import DbTransaction
|
||||||
|
|
||||||
pytestmark = pytest.mark.bdb
|
pytestmark = pytest.mark.bdb
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ def test_get_owned_ids(signed_create_tx, user_pk, db_conn):
|
|||||||
|
|
||||||
txns = query.get_owned_ids(connection=db_conn, owner=user_pk)
|
txns = query.get_owned_ids(connection=db_conn, owner=user_pk)
|
||||||
tx_dict = signed_create_tx.to_dict()
|
tx_dict = signed_create_tx.to_dict()
|
||||||
owned_tx = remove_generated_fields(txns[0].to_dict())
|
owned_tx = DbTransaction.remove_generated_fields(txns[0].to_dict())
|
||||||
assert owned_tx == tx_dict
|
assert owned_tx == tx_dict
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user