mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-25 15:05:49 +00:00
implemented decorator for running queries in try catch
This commit is contained in:
parent
32b27015a1
commit
632ea7ffc1
@ -19,7 +19,21 @@ from json import dumps, loads
|
||||
register_query = module_dispatch_registrar(query)
|
||||
|
||||
|
||||
@register_query(TarantoolDBConnection)
|
||||
def run_handled_query(func):
|
||||
def wrapper_run_query(*args, **kwargs):
|
||||
try:
|
||||
resp = func(*args, **kwargs)
|
||||
return resp
|
||||
except tarantool.error.SchemaError:
|
||||
return None
|
||||
except tarantool.error.NetworkError:
|
||||
return None
|
||||
except Exception as err:
|
||||
raise err
|
||||
|
||||
return wrapper_run_query
|
||||
|
||||
|
||||
def _group_transaction_by_ids(connection, txids: list):
|
||||
txspace = connection.space("transactions")
|
||||
inxspace = connection.space("inputs")
|
||||
@ -55,6 +69,7 @@ def _group_transaction_by_ids(connection, txids: list):
|
||||
return _transactions
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def store_transactions(connection, signed_transactions: list):
|
||||
txspace = connection.space("transactions")
|
||||
@ -88,18 +103,21 @@ def store_transactions(connection, signed_transactions: list):
|
||||
assetsxspace.insert(txtuples["asset"])
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_transaction(connection, transaction_id: str):
|
||||
_transactions = _group_transaction_by_ids(txids=[transaction_id], connection=connection)
|
||||
return next(iter(_transactions), None)
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_transactions(connection, transactions_ids: list):
|
||||
_transactions = _group_transaction_by_ids(txids=transactions_ids, connection=connection)
|
||||
return _transactions
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def store_metadatas(connection, metadata: list):
|
||||
space = connection.space("meta_data")
|
||||
@ -107,6 +125,7 @@ def store_metadatas(connection, metadata: list):
|
||||
space.insert((meta["id"], meta["data"] if not "metadata" in meta else meta["metadata"]))
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_metadata(connection, transaction_ids: list):
|
||||
_returned_data = []
|
||||
@ -118,6 +137,7 @@ def get_metadata(connection, transaction_ids: list):
|
||||
return _returned_data if len(_returned_data) > 0 else None
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def store_asset(connection, asset):
|
||||
space = connection.space("assets")
|
||||
@ -128,6 +148,7 @@ def store_asset(connection, asset):
|
||||
print("DUPLICATE ERROR")
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def store_assets(connection, assets: list):
|
||||
space = connection.space("assets")
|
||||
@ -139,6 +160,7 @@ def store_assets(connection, assets: list):
|
||||
print(f"EXCEPTION : {ex} ")
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_asset(connection, asset_id: str):
|
||||
space = connection.space("assets")
|
||||
@ -147,6 +169,7 @@ def get_asset(connection, asset_id: str):
|
||||
return _data[0][0] if len(_data) > 0 else []
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_assets(connection, assets_ids: list) -> list:
|
||||
_returned_data = []
|
||||
@ -156,6 +179,7 @@ def get_assets(connection, assets_ids: list) -> list:
|
||||
return sorted(_returned_data, key=lambda k: k["id"], reverse=False)
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_spent(connection, fullfil_transaction_id: str, fullfil_output_index: str):
|
||||
space = connection.space("inputs")
|
||||
@ -165,31 +189,28 @@ def get_spent(connection, fullfil_transaction_id: str, fullfil_output_index: str
|
||||
return _transactions
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_latest_block(connection): # TODO Here is used DESCENDING OPERATOR
|
||||
try:
|
||||
space = connection.space("blocks")
|
||||
_all_blocks = space.select()
|
||||
_all_blocks = _all_blocks.data
|
||||
block = {"app_hash": '', "height": 0, "transactions": []}
|
||||
space = connection.space("blocks")
|
||||
_all_blocks = space.select()
|
||||
_all_blocks = _all_blocks.data
|
||||
block = {"app_hash": '', "height": 0, "transactions": []}
|
||||
|
||||
if len(_all_blocks) > 0:
|
||||
_block = sorted(_all_blocks, key=itemgetter(1), reverse=True)[0]
|
||||
space = connection.space("blocks_tx")
|
||||
_txids = space.select(_block[2], index="block_search")
|
||||
_txids = _txids.data
|
||||
block["app_hash"] = _block[0]
|
||||
block["height"] = _block[1]
|
||||
block["transactions"] = [tx[0] for tx in _txids]
|
||||
else:
|
||||
block = None
|
||||
return block
|
||||
except tarantool.error.SchemaError:
|
||||
return None
|
||||
except Exception as err:
|
||||
raise err
|
||||
if len(_all_blocks) > 0:
|
||||
_block = sorted(_all_blocks, key=itemgetter(1), reverse=True)[0]
|
||||
space = connection.space("blocks_tx")
|
||||
_txids = space.select(_block[2], index="block_search")
|
||||
_txids = _txids.data
|
||||
block["app_hash"] = _block[0]
|
||||
block["height"] = _block[1]
|
||||
block["transactions"] = [tx[0] for tx in _txids]
|
||||
else:
|
||||
block = None
|
||||
return block
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def store_block(connection, block: dict):
|
||||
space = connection.space("blocks")
|
||||
@ -202,6 +223,7 @@ def store_block(connection, block: dict):
|
||||
space.insert((txid, block_unique_id))
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_txids_filtered(connection, asset_id: str, operation: str = None,
|
||||
last_tx: any = None): # TODO here is used 'OR' operator
|
||||
@ -263,6 +285,7 @@ def _remove_text_score(asset):
|
||||
return asset
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_owned_ids(connection, owner: str):
|
||||
space = connection.space("keys")
|
||||
@ -274,6 +297,7 @@ def get_owned_ids(connection, owner: str):
|
||||
return _transactions
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_spending_transactions(connection, inputs):
|
||||
_transactions = []
|
||||
@ -287,12 +311,12 @@ def get_spending_transactions(connection, inputs):
|
||||
return _transactions
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_block(connection, block_id=[]):
|
||||
space = connection.space("blocks")
|
||||
_block = space.select(block_id, index="block_search", limit=1)
|
||||
_block = _block.data
|
||||
print(f"QUERY 1 :: {_block}")
|
||||
if len(_block) == 0:
|
||||
return []
|
||||
_block = _block[0]
|
||||
@ -302,6 +326,7 @@ def get_block(connection, block_id=[]):
|
||||
return {"app_hash": _block[0], "height": _block[1], "transactions": [_tx[0] for _tx in _txblock]}
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_block_with_transaction(connection, txid: str):
|
||||
space = connection.space("blocks_tx")
|
||||
@ -314,6 +339,7 @@ def get_block_with_transaction(connection, txid: str):
|
||||
return [{"height": _height[1]} for _height in _block.data]
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def delete_transactions(connection, txn_ids: list):
|
||||
tx_space = connection.space("transactions")
|
||||
@ -342,6 +368,7 @@ def delete_transactions(connection, txn_ids: list):
|
||||
assets_space.delete(_id, index="txid_search")
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def store_unspent_outputs(connection, *unspent_outputs: list):
|
||||
space = connection.space('utxos')
|
||||
@ -352,6 +379,8 @@ def store_unspent_outputs(connection, *unspent_outputs: list):
|
||||
result.append(output.data)
|
||||
return result
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def delete_unspent_outputs(connection, *unspent_outputs: list):
|
||||
space = connection.space('utxos')
|
||||
@ -363,6 +392,7 @@ def delete_unspent_outputs(connection, *unspent_outputs: list):
|
||||
return result
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_unspent_outputs(connection, query=None): # for now we don't have implementation for 'query'.
|
||||
space = connection.space('utxos')
|
||||
@ -370,6 +400,7 @@ def get_unspent_outputs(connection, query=None): # for now we don't have implem
|
||||
return [loads(utx[2]) for utx in _utxos]
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def store_pre_commit_state(connection, state: dict):
|
||||
space = connection.space("pre_commits")
|
||||
@ -382,21 +413,18 @@ def store_pre_commit_state(connection, state: dict):
|
||||
limit=1)
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_pre_commit_state(connection):
|
||||
try:
|
||||
space = connection.space("pre_commits")
|
||||
_commit = space.select([], index="id_search").data
|
||||
if len(_commit) == 0:
|
||||
return None
|
||||
_commit = sorted(_commit, key=itemgetter(1), reverse=True)[0]
|
||||
return {"height": _commit[1], "transactions": _commit[2]}
|
||||
except tarantool.error.SchemaError:
|
||||
space = connection.space("pre_commits")
|
||||
_commit = space.select([], index="id_search").data
|
||||
if len(_commit) == 0:
|
||||
return None
|
||||
except Exception as err:
|
||||
raise err
|
||||
_commit = sorted(_commit, key=itemgetter(1), reverse=True)[0]
|
||||
return {"height": _commit[1], "transactions": _commit[2]}
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def store_validator_set(conn, validators_update: dict):
|
||||
space = conn.space("validators")
|
||||
@ -409,6 +437,7 @@ def store_validator_set(conn, validators_update: dict):
|
||||
limit=1)
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def delete_validator_set(connection, height: int):
|
||||
space = connection.space("validators")
|
||||
@ -417,6 +446,7 @@ def delete_validator_set(connection, height: int):
|
||||
space.delete(_valid[0])
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def store_election(connection, election_id: str, height: int, is_concluded: bool):
|
||||
space = connection.space("elections")
|
||||
@ -427,6 +457,7 @@ def store_election(connection, election_id: str, height: int, is_concluded: bool
|
||||
limit=1)
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def store_elections(connection, elections: list):
|
||||
space = connection.space("elections")
|
||||
@ -436,6 +467,7 @@ def store_elections(connection, elections: list):
|
||||
election["is_concluded"]))
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def delete_elections(connection, height: int):
|
||||
space = connection.space("elections")
|
||||
@ -444,41 +476,34 @@ def delete_elections(connection, height: int):
|
||||
space.delete(_elec[0])
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_validator_set(connection, height: int = None):
|
||||
try:
|
||||
space = connection.space("validators")
|
||||
_validators = space.select()
|
||||
_validators = _validators.data
|
||||
if height is not None:
|
||||
_validators = [{"height": validator[1], "validators": validator[2]} for validator in _validators if
|
||||
validator[1] <= height]
|
||||
return next(iter(sorted(_validators, key=lambda k: k["height"], reverse=True)), None)
|
||||
else:
|
||||
_validators = [{"height": validator[1], "validators": validator[2]} for validator in _validators]
|
||||
return next(iter(sorted(_validators, key=lambda k: k["height"], reverse=True)), None)
|
||||
except tarantool.error.SchemaError:
|
||||
return None
|
||||
except Exception as err:
|
||||
raise err
|
||||
space = connection.space("validators")
|
||||
_validators = space.select()
|
||||
_validators = _validators.data
|
||||
if height is not None:
|
||||
_validators = [{"height": validator[1], "validators": validator[2]} for validator in _validators if
|
||||
validator[1] <= height]
|
||||
return next(iter(sorted(_validators, key=lambda k: k["height"], reverse=True)), None)
|
||||
else:
|
||||
_validators = [{"height": validator[1], "validators": validator[2]} for validator in _validators]
|
||||
return next(iter(sorted(_validators, key=lambda k: k["height"], reverse=True)), None)
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_election(connection, election_id: str):
|
||||
try:
|
||||
space = connection.space("elections")
|
||||
_elections = space.select(election_id, index="id_search")
|
||||
_elections = _elections.data
|
||||
if len(_elections) == 0:
|
||||
return None
|
||||
_election = sorted(_elections, key=itemgetter(0), reverse=True)[0]
|
||||
return {"election_id": _election[0], "height": _election[1], "is_concluded": _election[2]}
|
||||
except tarantool.error.SchemaError:
|
||||
space = connection.space("elections")
|
||||
_elections = space.select(election_id, index="id_search")
|
||||
_elections = _elections.data
|
||||
if len(_elections) == 0:
|
||||
return None
|
||||
except Exception as err:
|
||||
raise err
|
||||
_election = sorted(_elections, key=itemgetter(0), reverse=True)[0]
|
||||
return {"election_id": _election[0], "height": _election[1], "is_concluded": _election[2]}
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_asset_tokens_for_public_key(connection, asset_id: str, public_key: str):
|
||||
space = connection.space("keys")
|
||||
@ -491,6 +516,7 @@ def get_asset_tokens_for_public_key(connection, asset_id: str, public_key: str):
|
||||
return _grouped_transactions
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def store_abci_chain(connection, height: int, chain_id: str, is_synced: bool = True):
|
||||
space = connection.space("abci_chains")
|
||||
@ -501,6 +527,7 @@ def store_abci_chain(connection, height: int, chain_id: str, is_synced: bool = T
|
||||
limit=1)
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def delete_abci_chain(connection, height: int):
|
||||
space = connection.space("abci_chains")
|
||||
@ -509,16 +536,12 @@ def delete_abci_chain(connection, height: int):
|
||||
space.delete(_chain[2])
|
||||
|
||||
|
||||
@run_handled_query
|
||||
@register_query(TarantoolDBConnection)
|
||||
def get_latest_abci_chain(connection):
|
||||
try:
|
||||
space = connection.space("abci_chains")
|
||||
_all_chains = space.select().data
|
||||
if len(_all_chains) == 0:
|
||||
return None
|
||||
_chain = sorted(_all_chains, key=itemgetter(0), reverse=True)[0]
|
||||
return {"height": _chain[0], "is_synced": _chain[1], "chain_id": _chain[2]}
|
||||
except tarantool.error.SchemaError:
|
||||
space = connection.space("abci_chains")
|
||||
_all_chains = space.select().data
|
||||
if len(_all_chains) == 0:
|
||||
return None
|
||||
except Exception as err:
|
||||
raise err
|
||||
_chain = sorted(_all_chains, key=itemgetter(0), reverse=True)[0]
|
||||
return {"height": _chain[0], "is_synced": _chain[1], "chain_id": _chain[2]}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user