mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-25 06:55:45 +00:00
changed script queries
Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
parent
18e6cf2335
commit
198bcf46b8
@ -1,4 +1,10 @@
|
||||
# Copyright © 2020 Interplanetary Database Association e.V.,
|
||||
# Planetmint and IPDB software contributors.
|
||||
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
||||
# Code is Apache-2.0 and docs are CC-BY-4.0
|
||||
|
||||
from .asset import Asset
|
||||
from .fulfills import Fulfills
|
||||
from .input import Input
|
||||
from .metadata import MetaData
|
||||
from .script import Script
|
||||
19
planetmint/backend/models/script.py
Normal file
19
planetmint/backend/models/script.py
Normal file
@ -0,0 +1,19 @@
|
||||
# Copyright © 2020 Interplanetary Database Association e.V.,
|
||||
# Planetmint and IPDB software contributors.
|
||||
# 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
|
||||
from typing import Optional
|
||||
|
||||
@dataclass
|
||||
class Script:
|
||||
id: str = ""
|
||||
script: Optional[str] = None
|
||||
|
||||
@staticmethod
|
||||
def from_tuple(script_tuple: tuple) -> Script:
|
||||
return Script(script_tuple[0], script_tuple[1])
|
||||
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
|
||||
from functools import singledispatch
|
||||
from planetmint.backend.exceptions import OperationError
|
||||
from planetmint.backend.interfaces import Asset, Block, MetaData, Input
|
||||
from planetmint.backend.interfaces import Asset, Block, MetaData, Input, Script
|
||||
|
||||
@singledispatch
|
||||
def store_asset(connection, asset: dict) -> Asset:
|
||||
@ -461,3 +461,8 @@ def store_transaction_inputs(connection, inputs: list[Input]):
|
||||
def _group_transaction_by_ids(txids: list, connection):
|
||||
"""Returns the transactions object (JSON TYPE), from list of ids."""
|
||||
raise NotImplementedError
|
||||
|
||||
@singledispatch
|
||||
def get_script_by_tx_id(connection, tx_id: str) -> Script:
|
||||
"""Retrieve script for a transaction by its id"""
|
||||
raise NotImplementedError
|
||||
|
||||
@ -14,7 +14,7 @@ from planetmint.backend.tarantool.const import TARANT_TABLE_META_DATA, TARANT_TA
|
||||
TARANT_TABLE_TRANSACTION, TARANT_TABLE_INPUT, TARANT_TABLE_OUTPUT, TARANT_TABLE_SCRIPT, TARANT_TX_ID_SEARCH, \
|
||||
TARANT_ID_SEARCH
|
||||
from planetmint.backend.utils import module_dispatch_registrar
|
||||
from planetmint.backend.models import Asset, MetaData, Input, Fulfills
|
||||
from planetmint.backend.models import Asset, MetaData, Input, Script
|
||||
from planetmint.backend.tarantool.connection import TarantoolDBConnection
|
||||
from planetmint.backend.tarantool.transaction.tools import TransactionCompose, TransactionDecompose
|
||||
|
||||
@ -35,20 +35,21 @@ def _group_transaction_by_ids(connection, txids: list):
|
||||
_txassets = connection.run(connection.space(TARANT_TABLE_ASSETS).select(txid, index=TARANT_TX_ID_SEARCH))
|
||||
_txassets = get_assets(connection, [txid])
|
||||
_txmeta = get_metadata_by_tx_id(connection, txid)
|
||||
_txscript = connection.run(connection.space(TARANT_TABLE_SCRIPT).select(txid, index=TARANT_TX_ID_SEARCH))
|
||||
_txscript = get_script_by_tx_id(connection, txid)
|
||||
|
||||
_txoutputs = sorted(_txoutputs, key=itemgetter(8), reverse=False)
|
||||
result_map = {
|
||||
TARANT_TABLE_TRANSACTION: _txobject,
|
||||
TARANT_TABLE_OUTPUT: _txoutputs,
|
||||
TARANT_TABLE_KEYS: _txkeys,
|
||||
TARANT_TABLE_SCRIPT: _txscript,
|
||||
}
|
||||
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]
|
||||
_transaction["assets"] = [asset.data for asset in _txassets]
|
||||
_transaction["metadata"] = _txmeta.metadata
|
||||
if _txscript.script:
|
||||
_transaction[TARANT_TABLE_SCRIPT] = _txscript.script
|
||||
_transactions.append(_transaction)
|
||||
return _transactions
|
||||
|
||||
@ -97,8 +98,8 @@ def store_transactions(connection, signed_transactions: list):
|
||||
assets.append(Asset(id, transaction["id"], asset))
|
||||
store_assets(connection, assets)
|
||||
|
||||
if txtuples[TARANT_TABLE_SCRIPT] is not None:
|
||||
connection.run(connection.space(TARANT_TABLE_SCRIPT).insert(txtuples["script"]), only_data=False)
|
||||
if TARANT_TABLE_SCRIPT in transaction:
|
||||
connection.run(connection.space(TARANT_TABLE_SCRIPT).insert((transaction["id"], transaction[TARANT_TABLE_SCRIPT])), only_data=False)
|
||||
|
||||
|
||||
@register_query(TarantoolDBConnection)
|
||||
@ -496,3 +497,8 @@ def get_latest_abci_chain(connection):
|
||||
return None
|
||||
_chain = sorted(_all_chains, key=itemgetter(0), reverse=True)[0]
|
||||
return {"height": _chain[0], "is_synced": _chain[1], "chain_id": _chain[2]}
|
||||
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_script_by_tx_id(connection, tx_id: str) -> Script:
|
||||
script = connection.run(connection.space(TARANT_TABLE_SCRIPT).select(tx_id, index=TARANT_TX_ID_SEARCH))
|
||||
return Script.from_tuple(script[0]) if len(script) < 0 else Script(tx_id)
|
||||
@ -102,18 +102,11 @@ class TransactionDecompose:
|
||||
_map = self.get_map()
|
||||
return (self._transaction["id"], self._transaction["operation"], self._transaction["version"], _map)
|
||||
|
||||
def __prepare_script(self):
|
||||
try:
|
||||
return (self._transaction["id"], self._transaction[TARANT_TABLE_SCRIPT])
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
def convert_to_tuple(self):
|
||||
self._tuple_transaction[TARANT_TABLE_TRANSACTION] = self.__prepare_transaction()
|
||||
keys, outputs = self.__prepare_outputs()
|
||||
self._tuple_transaction[TARANT_TABLE_OUTPUT] = outputs
|
||||
self._tuple_transaction[TARANT_TABLE_KEYS] = keys
|
||||
self._tuple_transaction[TARANT_TABLE_SCRIPT] = self.__prepare_script()
|
||||
return self._tuple_transaction
|
||||
|
||||
|
||||
@ -151,18 +144,10 @@ class TransactionCompose:
|
||||
_outputs.append(_out)
|
||||
return _outputs
|
||||
|
||||
def _get_script(self):
|
||||
if self.db_results[TARANT_TABLE_SCRIPT]:
|
||||
return self.db_results[TARANT_TABLE_SCRIPT][0][1]
|
||||
else:
|
||||
return None
|
||||
|
||||
def convert_to_dict(self):
|
||||
transaction = {k: None for k in list(self._map.keys())}
|
||||
transaction["id"] = self._get_transaction_id()
|
||||
transaction["version"] = self._get_transaction_version()
|
||||
transaction["operation"] = self._get_transaction_operation()
|
||||
transaction[TARANT_TABLE_OUTPUT] = self._get_outputs()
|
||||
if self._get_script():
|
||||
transaction[TARANT_TABLE_SCRIPT] = self._get_script()
|
||||
return transaction
|
||||
|
||||
@ -12,12 +12,10 @@ from collections import namedtuple
|
||||
from uuid import uuid4
|
||||
from planetmint.backend.connection import Connection
|
||||
|
||||
import rapidjson
|
||||
from hashlib import sha3_256
|
||||
import json
|
||||
import rapidjson
|
||||
import requests
|
||||
import planetmint
|
||||
|
||||
from collections import namedtuple, OrderedDict
|
||||
from uuid import uuid4
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user