mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-24 06:25:45 +00:00
- blackified & fixed recovery / delete transactions issues becaues of data model transitions
- reintegrated get_transaction() call in query -> delegating this to get_complete_transactions_by_ids Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
parent
869406d30d
commit
51ae9874d6
@ -17,9 +17,7 @@ class Asset:
|
||||
return Asset(asset_dict["data"]) if "data" in asset_dict.keys() else Asset(asset_dict["id"])
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"data": self.data
|
||||
}
|
||||
return {"data": self.data}
|
||||
|
||||
@staticmethod
|
||||
def from_list_dict(asset_dict_list: list[dict]) -> list[Asset]:
|
||||
|
||||
@ -20,8 +20,4 @@ class Block:
|
||||
return Block(block_tuple[0], block_tuple[1], block_tuple[2], block_tuple[3])
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"app_hash": self.app_hash,
|
||||
"height": self.height,
|
||||
"transactions": self.transactions
|
||||
}
|
||||
return {"app_hash": self.app_hash, "height": self.height, "transactions": self.transactions}
|
||||
|
||||
@ -12,7 +12,4 @@ class Fulfills:
|
||||
output_index: int = 0
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"transaction_id": self.transaction_id,
|
||||
"output_index": self.output_index
|
||||
}
|
||||
return {"transaction_id": self.transaction_id, "output_index": self.output_index}
|
||||
|
||||
@ -56,5 +56,3 @@ class Input:
|
||||
@staticmethod
|
||||
def list_to_dict(input_list: list[Input]) -> list[dict]:
|
||||
return [input.to_dict() for input in input_list or []]
|
||||
|
||||
|
||||
|
||||
@ -20,6 +20,4 @@ class MetaData:
|
||||
return MetaData(meta_data)
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"metadata": self.metadata
|
||||
}
|
||||
return {"metadata": self.metadata}
|
||||
|
||||
@ -17,10 +17,7 @@ class SubCondition:
|
||||
return self.type, self.public_key
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"type": self.type,
|
||||
"public_key": self.public_key
|
||||
}
|
||||
return {"type": self.type, "public_key": self.public_key}
|
||||
|
||||
@staticmethod
|
||||
def from_dict(subcondition_dict: dict) -> SubCondition:
|
||||
@ -32,6 +29,7 @@ class SubCondition:
|
||||
return None
|
||||
return [subcondition.to_dict() for subcondition in subconditions]
|
||||
|
||||
|
||||
@dataclass
|
||||
class ConditionDetails:
|
||||
type: str = ""
|
||||
@ -64,12 +62,12 @@ class ConditionDetails:
|
||||
sub_conditions=sub_conditions,
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Condition:
|
||||
uri: str = ""
|
||||
details: ConditionDetails = field(default_factory=ConditionDetails)
|
||||
|
||||
|
||||
@staticmethod
|
||||
def from_dict(data: dict) -> Condition:
|
||||
return Condition(
|
||||
@ -131,7 +129,6 @@ class Output:
|
||||
"amount": str(self.amount),
|
||||
}
|
||||
|
||||
|
||||
@staticmethod
|
||||
def list_to_dict(output_list: list[Output]) -> list[dict]:
|
||||
return [output.to_dict() for output in output_list or []]
|
||||
|
||||
@ -19,6 +19,4 @@ class Script:
|
||||
return Script(script_dict["script"])
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"script": self.script
|
||||
}
|
||||
return {"script": self.script}
|
||||
|
||||
@ -69,24 +69,28 @@ def store_transaction(connection, transaction):
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@singledispatch
|
||||
def store_governance_transactions(connection, transactions):
|
||||
"""Store the list of governance transactions."""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@singledispatch
|
||||
def store_governance_transaction(connection, transaction):
|
||||
"""Store a single governance transaction."""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@singledispatch
|
||||
def get_governance_transaction_by_id(connection, transaction_id):
|
||||
"""Get the transaction by transaction id."""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@singledispatch
|
||||
def get_transaction_by_id(connection, transaction_id):
|
||||
"""Get the transaction by transaction id."""
|
||||
@ -107,18 +111,21 @@ def get_transaction(connection, transaction_id):
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@singledispatch
|
||||
def get_transactions_by_asset(connection, asset):
|
||||
"""Get a transaction by id."""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@singledispatch
|
||||
def get_transactions_by_metadata(connection, metadata: str, limit: int = 1000) -> list[DbTransaction]:
|
||||
"""Get a transaction by its metadata cid."""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@singledispatch
|
||||
def get_transactions(connection, transactions_ids) -> list[DbTransaction]:
|
||||
"""Get a transaction from the transactions table.
|
||||
|
||||
@ -118,6 +118,7 @@ def drop_database(connection, dbname):
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@singledispatch
|
||||
def init_database(connection, dbname):
|
||||
"""Initialize the configured backend for use with Planetmint.
|
||||
|
||||
@ -38,7 +38,7 @@ class TarantoolDBConnection(DBConnection):
|
||||
"pre_commits",
|
||||
"validator_sets",
|
||||
"transactions",
|
||||
"outputs"
|
||||
"outputs",
|
||||
]
|
||||
except tarantool.error.NetworkError as network_err:
|
||||
logger.info("Host cant be reached")
|
||||
@ -95,12 +95,12 @@ class TarantoolDBConnection(DBConnection):
|
||||
raise net_error
|
||||
|
||||
def drop_database(self):
|
||||
self.connect().call('drop')
|
||||
self.connect().call("drop")
|
||||
# db_config = Config().get()["database"]
|
||||
# cmd_resp = self.run_command(command=self.drop_path, config=db_config) # noqa: F841
|
||||
|
||||
def init_database(self):
|
||||
self.connect().call('init')
|
||||
self.connect().call("init")
|
||||
# db_config = Config().get()["database"]
|
||||
# cmd_resp = self.run_command(command=self.init_path, config=db_config) # noqa: F841
|
||||
|
||||
|
||||
@ -21,7 +21,9 @@ from planetmint.backend.tarantool.const import (
|
||||
TARANT_TABLE_OUTPUT,
|
||||
TARANT_TABLE_SCRIPT,
|
||||
TARANT_TX_ID_SEARCH,
|
||||
TARANT_ID_SEARCH, TARANT_INDEX_TX_BY_ASSET_ID, TARANT_INDEX_SPENDING_BY_ID_AND_OUTPUT_INDEX,
|
||||
TARANT_ID_SEARCH,
|
||||
TARANT_INDEX_TX_BY_ASSET_ID,
|
||||
TARANT_INDEX_SPENDING_BY_ID_AND_OUTPUT_INDEX,
|
||||
TARANT_TABLE_GOVERNANCE,
|
||||
)
|
||||
from planetmint.backend.utils import module_dispatch_registrar
|
||||
@ -53,7 +55,12 @@ def get_outputs_by_tx_id(connection, tx_id: str) -> list[Output]:
|
||||
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_transaction(connection, tx_id: str) -> DbTransaction:
|
||||
return NotImplemented
|
||||
transactions = get_complete_transactions_by_ids(connection, (tx_id))
|
||||
if len(transactions) > 1 or len(transactions) == 0:
|
||||
return None
|
||||
else:
|
||||
return transactions[0]
|
||||
|
||||
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_transactions_by_asset(connection, asset: str, limit: int = 1000) -> list[DbTransaction]:
|
||||
@ -72,16 +79,21 @@ def get_transactions_by_metadata(connection, metadata: str, limit: int = 1000) -
|
||||
tx_ids = [tx[0] for tx in txs]
|
||||
return get_complete_transactions_by_ids(connection, tx_ids)
|
||||
|
||||
|
||||
def store_transaction_outputs(connection, output: Output, index: int) -> str:
|
||||
output_id = uuid4().hex
|
||||
connection.run(connection.space(TARANT_TABLE_OUTPUT).insert((
|
||||
connection.run(
|
||||
connection.space(TARANT_TABLE_OUTPUT).insert(
|
||||
(
|
||||
output_id,
|
||||
int(output.amount),
|
||||
output.public_keys,
|
||||
output.condition.to_dict(),
|
||||
index,
|
||||
output.transaction_id,
|
||||
)))
|
||||
)
|
||||
)
|
||||
)
|
||||
return output_id
|
||||
|
||||
|
||||
@ -107,9 +119,11 @@ def store_transaction(connection, transaction):
|
||||
transaction["metadata"],
|
||||
transaction["assets"],
|
||||
transaction["inputs"],
|
||||
scripts)
|
||||
scripts,
|
||||
)
|
||||
connection.run(connection.space(TARANT_TABLE_TRANSACTION).insert(tx), only_data=False)
|
||||
|
||||
|
||||
@register_query(TarantoolDBConnection)
|
||||
def store_governance_transactions(connection, signed_transactions: list):
|
||||
for transaction in signed_transactions:
|
||||
@ -119,6 +133,7 @@ def store_governance_transactions(connection, signed_transactions: list):
|
||||
for index, output in enumerate(transaction[TARANT_TABLE_OUTPUT])
|
||||
]
|
||||
|
||||
|
||||
@register_query(TarantoolDBConnection)
|
||||
def store_governance_transaction(connection, transaction):
|
||||
scripts = None
|
||||
@ -131,9 +146,11 @@ def store_governance_transaction(connection, transaction):
|
||||
transaction["metadata"],
|
||||
transaction["assets"],
|
||||
transaction["inputs"],
|
||||
scripts)
|
||||
scripts,
|
||||
)
|
||||
connection.run(connection.space(TARANT_TABLE_GOVERNANCE).insert(tx), only_data=False)
|
||||
|
||||
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_governance_transaction_by_id(connection, transaction_id):
|
||||
txs = connection.run(connection.space(TARANT_TABLE_GOVERNANCE).select(transaction_id, index=TARANT_ID_SEARCH))
|
||||
@ -141,6 +158,7 @@ def get_governance_transaction_by_id(connection, transaction_id):
|
||||
return None
|
||||
return DbTransaction.from_tuple(txs[0])
|
||||
|
||||
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_transaction_by_id(connection, transaction_id, table=TARANT_TABLE_TRANSACTION):
|
||||
txs = connection.run(connection.space(table).select(transaction_id, index=TARANT_ID_SEARCH))
|
||||
@ -162,9 +180,12 @@ def get_transactions(connection, transactions_ids: list) -> list[DbTransaction]:
|
||||
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_asset(connection, asset_id: str) -> Asset:
|
||||
_data = connection.run(connection.space(TARANT_TABLE_TRANSACTION).select(asset_id, index=TARANT_INDEX_TX_BY_ASSET_ID))
|
||||
_data = connection.run(
|
||||
connection.space(TARANT_TABLE_TRANSACTION).select(asset_id, index=TARANT_INDEX_TX_BY_ASSET_ID)
|
||||
)
|
||||
return Asset.from_dict(_data[0])
|
||||
|
||||
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_assets(connection, assets_ids: list) -> list[Asset]:
|
||||
_returned_data = []
|
||||
@ -203,7 +224,10 @@ def get_latest_block(connection):
|
||||
def store_block(connection, block: dict):
|
||||
block_unique_id = uuid4().hex
|
||||
connection.run(
|
||||
connection.space("blocks").insert((block_unique_id, block["app_hash"], block["height"], block[TARANT_TABLE_TRANSACTION])), only_data=False
|
||||
connection.space("blocks").insert(
|
||||
(block_unique_id, block["app_hash"], block["height"], block[TARANT_TABLE_TRANSACTION])
|
||||
),
|
||||
only_data=False,
|
||||
)
|
||||
|
||||
|
||||
@ -212,7 +236,9 @@ def get_txids_filtered(connection, asset_ids: list[str], operation: str = "", la
|
||||
transactions = []
|
||||
if operation == "CREATE":
|
||||
transactions = connection.run(
|
||||
connection.space(TARANT_TABLE_TRANSACTION).select([asset_ids[0], operation], index="transactions_by_id_and_operation")
|
||||
connection.space(TARANT_TABLE_TRANSACTION).select(
|
||||
[asset_ids[0], operation], index="transactions_by_id_and_operation"
|
||||
)
|
||||
)
|
||||
elif operation == "TRANSFER":
|
||||
transactions = connection.run(
|
||||
@ -295,34 +321,14 @@ def delete_transactions(connection, txn_ids: list):
|
||||
for _id in txn_ids:
|
||||
connection.run(connection.space(TARANT_TABLE_TRANSACTION).delete(_id), only_data=False)
|
||||
for _id in txn_ids:
|
||||
_inputs = connection.run(
|
||||
connection.space(TARANT_TABLE_INPUT).select(_id, index=TARANT_ID_SEARCH), only_data=False
|
||||
)
|
||||
_outputs = connection.run(
|
||||
connection.space(TARANT_TABLE_OUTPUT).select(_id, index=TARANT_ID_SEARCH), only_data=False
|
||||
)
|
||||
_keys = connection.run(
|
||||
connection.space(TARANT_TABLE_KEYS).select(_id, index=TARANT_TX_ID_SEARCH), only_data=False
|
||||
)
|
||||
for _kID in _keys:
|
||||
connection.run(
|
||||
connection.space(TARANT_TABLE_KEYS).delete(_kID[0], index=TARANT_ID_SEARCH), only_data=False
|
||||
)
|
||||
for _inpID in _inputs:
|
||||
connection.run(
|
||||
connection.space(TARANT_TABLE_INPUT).delete(_inpID[5], index="delete_search"), only_data=False
|
||||
)
|
||||
for _outpID in _outputs:
|
||||
connection.run(
|
||||
connection.space(TARANT_TABLE_OUTPUT).delete(_outpID[5], index="unique_search"), only_data=False
|
||||
)
|
||||
|
||||
for _id in txn_ids:
|
||||
connection.run(connection.space(TARANT_TABLE_META_DATA).delete(_id, index=TARANT_ID_SEARCH), only_data=False)
|
||||
|
||||
for _id in txn_ids:
|
||||
connection.run(connection.space(TARANT_TABLE_ASSETS).delete(_id, index=TARANT_TX_ID_SEARCH), only_data=False)
|
||||
|
||||
|
||||
@register_query(TarantoolDBConnection)
|
||||
def store_unspent_outputs(connection, *unspent_outputs: list):
|
||||
@ -342,8 +348,9 @@ def delete_unspent_outputs(connection, *unspent_outputs: list):
|
||||
if unspent_outputs:
|
||||
for utxo in unspent_outputs:
|
||||
output = connection.run(
|
||||
connection.space("utxos")
|
||||
.delete((utxo["transaction_id"], utxo["output_index"]), index="utxo_by_transaction_id_and_output_index")
|
||||
connection.space("utxos").delete(
|
||||
(utxo["transaction_id"], utxo["output_index"]), index="utxo_by_transaction_id_and_output_index"
|
||||
)
|
||||
)
|
||||
result.append(output)
|
||||
return result
|
||||
@ -461,7 +468,9 @@ def get_asset_tokens_for_public_key(connection, asset_id: str, public_key: str)
|
||||
# _transactions = _transactions
|
||||
# _keys = _keys.data
|
||||
id_transactions = connection.run(connection.space(TARANT_TABLE_GOVERNANCE).select([asset_id]))
|
||||
asset_id_transactions = connection.run(connection.space(TARANT_TABLE_GOVERNANCE).select([asset_id], index="governance_by_asset_id"))
|
||||
asset_id_transactions = connection.run(
|
||||
connection.space(TARANT_TABLE_GOVERNANCE).select([asset_id], index="governance_by_asset_id")
|
||||
)
|
||||
transactions = id_transactions + asset_id_transactions
|
||||
|
||||
# TODO return transaction class
|
||||
|
||||
@ -144,15 +144,18 @@ SCHEMA_DROP_COMMANDS = {
|
||||
"scripts": "box.space.scripts:drop()",
|
||||
}
|
||||
|
||||
|
||||
@register_schema(TarantoolDBConnection)
|
||||
def init_database(connection, db_name=None):
|
||||
print('init database tarantool schema')
|
||||
connection.connect().call('init')
|
||||
print("init database tarantool schema")
|
||||
connection.connect().call("init")
|
||||
|
||||
|
||||
@register_schema(TarantoolDBConnection)
|
||||
def drop_database(connection, db_name=None):
|
||||
print('drop database tarantool schema')
|
||||
connection.connect().call('drop')
|
||||
print("drop database tarantool schema")
|
||||
connection.connect().call("drop")
|
||||
|
||||
|
||||
@register_schema(TarantoolDBConnection)
|
||||
def create_database(connection, dbname):
|
||||
@ -180,4 +183,4 @@ def run_command_with_output(command):
|
||||
|
||||
@register_schema(TarantoolDBConnection)
|
||||
def create_tables(connection, dbname):
|
||||
connection.connect().call('init')
|
||||
connection.connect().call("init")
|
||||
|
||||
@ -7,8 +7,4 @@ CHAIN_MIGRATION_ELECTION = "CHAIN_MIGRATION_ELECTION"
|
||||
VALIDATOR_ELECTION = "VALIDATOR_ELECTION"
|
||||
VOTE = "VOTE"
|
||||
|
||||
GOVERNANCE_TRANSACTION_TYPES = [
|
||||
CHAIN_MIGRATION_ELECTION,
|
||||
VALIDATOR_ELECTION,
|
||||
VOTE
|
||||
]
|
||||
GOVERNANCE_TRANSACTION_TYPES = [CHAIN_MIGRATION_ELECTION, VALIDATOR_ELECTION, VOTE]
|
||||
|
||||
@ -203,7 +203,7 @@ class App(BaseApplication):
|
||||
block_txn_hash = calculate_hash(self.block_txn_ids)
|
||||
block = self.planetmint_node.get_latest_block()
|
||||
|
||||
logger.debug('BLOCK: ', block)
|
||||
logger.debug("BLOCK: ", block)
|
||||
|
||||
if self.block_txn_ids:
|
||||
self.block_txn_hash = calculate_hash([block["app_hash"], block_txn_hash])
|
||||
|
||||
@ -468,7 +468,6 @@ class Planetmint(object):
|
||||
# flatten and return all found assets
|
||||
return list(chain.from_iterable([Asset.list_to_dict(tx.assets) for tx in asset_txs]))
|
||||
|
||||
|
||||
def get_metadata(self, txn_ids) -> list[MetaData]:
|
||||
"""Return a list of metadata that match the transaction ids (txn_ids)
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
|
||||
from planetmint.backend.tarantool.connection import TarantoolDBConnection
|
||||
|
||||
|
||||
def _check_spaces_by_list(conn, space_names):
|
||||
_exists = []
|
||||
for name in space_names:
|
||||
|
||||
@ -305,7 +305,8 @@ def test_store_one_unspent_output(b, unspent_output_1, utxo_collection):
|
||||
else:
|
||||
utx_space = b.connection.get_space("utxos")
|
||||
res = utx_space.select(
|
||||
[unspent_output_1["transaction_id"], unspent_output_1["output_index"]], index="utxo_by_transaction_id_and_output_index"
|
||||
[unspent_output_1["transaction_id"], unspent_output_1["output_index"]],
|
||||
index="utxo_by_transaction_id_and_output_index",
|
||||
)
|
||||
assert len(res.data) == 1
|
||||
|
||||
|
||||
@ -32,8 +32,8 @@ def flush_localmongo_db(connection, dbname):
|
||||
|
||||
@flush_db.register(TarantoolDBConnection)
|
||||
def flush_tarantool_db(connection, dbname):
|
||||
connection.connect().call('drop')
|
||||
connection.connect().call('init')
|
||||
connection.connect().call("drop")
|
||||
connection.connect().call("init")
|
||||
|
||||
|
||||
def generate_block(planet):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user