diff --git a/planetmint/backend/tarantool/query.py b/planetmint/backend/tarantool/query.py index f302c3f..6a1b5b0 100644 --- a/planetmint/backend/tarantool/query.py +++ b/planetmint/backend/tarantool/query.py @@ -5,6 +5,7 @@ """Query implementation for Tarantool""" from secrets import token_hex +from hashlib import sha256 from operator import itemgetter import tarantool.error @@ -15,6 +16,7 @@ from planetmint.backend.tarantool.connection import TarantoolDBConnection from planetmint.backend.tarantool.transaction.tools import TransactionCompose, TransactionDecompose from json import dumps, loads + register_query = module_dispatch_registrar(query) @@ -515,20 +517,25 @@ def get_asset_tokens_for_public_key(connection, asset_id: str, @register_query(TarantoolDBConnection) def store_abci_chain(connection, height: int, chain_id: str, is_synced: bool = True): - connection.run(connection.space("abci_chains").delete(chain_id), only_data=False) - connection.run(connection.space("abci_chains").insert((height, is_synced, chain_id)), only_data=False) + hash_id_primarykey = sha256(dumps(obj={"height": height}).encode()).hexdigest() + connection.run( + connection.space("abci_chains").upsert((height, is_synced, chain_id, hash_id_primarykey), + op_list=[ + ('=', 0, height), + ('=', 1, is_synced), + ('=', 2, chain_id) + ]), + only_data=False + ) @register_query(TarantoolDBConnection) def delete_abci_chain(connection, height: int): - _chains = connection.run( - connection.space("abci_chains").select(height, index="height_search") + hash_id_primarykey = sha256(dumps(obj={"height": height}).encode()).hexdigest() + connection.run( + connection.space("abci_chains").delete(hash_id_primarykey), + only_data=False ) - for _chain in _chains: - connection.run( - connection.space("abci_chains").delete(_chain[2]), - only_data=False - ) @register_query(TarantoolDBConnection) diff --git a/planetmint/backend/tarantool/schema.py b/planetmint/backend/tarantool/schema.py index d51148d..73b13dc 100644 --- a/planetmint/backend/tarantool/schema.py +++ b/planetmint/backend/tarantool/schema.py @@ -32,8 +32,8 @@ SPACE_COMMANDS = { INDEX_COMMANDS = { "abci_chains": { - "id_search": "abci_chains:create_index('id_search' ,{type='hash', parts={'chain_id'}})", - "height_search": "abci_chains:create_index('height_search' ,{type='tree',unique=false, parts={'height'}})" + "id_search": "abci_chains:create_index('id_search' ,{type='hash', parts={'id'}})", + "height_search": "abci_chains:create_index('height_search' ,{type='tree', unique=false, parts={'height'}})" }, "assets": { @@ -105,7 +105,7 @@ INDEX_COMMANDS = { SCHEMA_COMMANDS = { "abci_chains": - "abci_chains:format({{name='height' , type='integer'},{name='is_synched' , type='boolean'},{name='chain_id',type='string'}})", + "abci_chains:format({{name='height' , type='integer'},{name='is_synched' , type='boolean'},{name='chain_id',type='string'}, {name='id', type='string'}})", "assets": "assets:format({{name='data' , type='any'}, {name='tx_id', type='string'}, {name='asset_id', type='string'}})", "blocks": diff --git a/tests/utils.py b/tests/utils.py index 1dc9806..1355da6 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -39,7 +39,7 @@ def flush_tarantool_db(connection, dbname): for _id in _all_data: if "assets" == s: connection.run(connection.space(s).delete(_id[1]), only_data=False) - elif s in ["blocks", "abci_chains"]: + elif s == "blocks": connection.run(connection.space(s).delete(_id[2]), only_data=False) elif s == "inputs": connection.run(connection.space(s).delete(_id[-2]), only_data=False) @@ -47,6 +47,8 @@ def flush_tarantool_db(connection, dbname): connection.run(connection.space(s).delete(_id[-4]), only_data=False) elif s == "utxos": connection.run(connection.space(s).delete([_id[0], _id[1]]), only_data=False) + elif s == "abci_chains": + connection.run(connection.space(s).delete(_id[-1]), only_data=False) else: connection.run(connection.space(s).delete(_id[0]), only_data=False)