query rewrited pre_commit + tests error fixed

This commit is contained in:
andrei 2022-02-21 13:17:05 +02:00
parent c6feac5754
commit 134d608f46
4 changed files with 154 additions and 142 deletions

View File

@ -1,5 +1,3 @@
box.cfg{listen = 3301}
abci_chains = box.schema.space.create('abci_chains',{engine = 'memtx' , is_sync = false})
abci_chains:format({{name='height' , type='integer'},{name='is_synched' , type='boolean'},{name='chain_id',type='string'}})
abci_chains:create_index('id_search' ,{type='hash', parts={'chain_id'}})

View File

@ -349,66 +349,65 @@ def delete_transactions(connection, txn_ids: list):
for _outpID in _outputs:
outputs_space.delete(_outpID[5], index="unique_search")
# @register_query(LocalMongoDBConnection)
def store_unspent_outputs(conn, *unspent_outputs: list):
if unspent_outputs:
try:
return conn.run(
conn.collection('utxos').insert_many(
unspent_outputs,
ordered=False,
)
)
except DuplicateKeyError:
# TODO log warning at least
pass
# # @register_query(LocalMongoDBConnection)
# def store_unspent_outputs(conn, *unspent_outputs: list):
# if unspent_outputs:
# try:
# return conn.run(
# conn.collection('utxos').insert_many(
# unspent_outputs,
# ordered=False,
# )
# )
# except DuplicateKeyError:
# # TODO log warning at least
# pass
#
#
# # @register_query(LocalMongoDBConnection)
# def delete_unspent_outputs(conn, *unspent_outputs: list):
# if unspent_outputs:
# return conn.run(
# conn.collection('utxos').delete_many({
# '$or': [{
# '$and': [
# {'transaction_id': unspent_output['transaction_id']},
# {'output_index': unspent_output['output_index']},
# ],
# } for unspent_output in unspent_outputs]
# })
# )
#
#
# # @register_query(LocalMongoDBConnection)
# def get_unspent_outputs(conn, *, query=None):
# if query is None:
# query = {}
# return conn.run(conn.collection('utxos').find(query,
# projection={'_id': False}))
# @register_query(LocalMongoDBConnection)
def delete_unspent_outputs(conn, *unspent_outputs: list):
if unspent_outputs:
return conn.run(
conn.collection('utxos').delete_many({
'$or': [{
'$and': [
{'transaction_id': unspent_output['transaction_id']},
{'output_index': unspent_output['output_index']},
],
} for unspent_output in unspent_outputs]
})
)
# @register_query(LocalMongoDBConnection)
def get_unspent_outputs(conn, *, query=None):
if query is None:
query = {}
return conn.run(conn.collection('utxos').find(query,
projection={'_id': False}))
# @register_query(LocalMongoDBConnection)
def store_pre_commit_state(state: dict, connection):
space = connection.space("pre_commits")
_precommit = space.select(state["height"], index="height_search", limit=1)
unique_id = token_hex(8) if (len(_precommit.data) == 0) else _precommit.data[0][0]
space.upsert((unique_id, state["height"], state["transactions"]),
op_list=[('=', 0, unique_id),
# precommit = space.select(state["height"], index="height_search", limit=1)
# unique_id = token_hex(8) if (len(_precommit.data) == 0) else _precommit.data[0][0]
space.upsert((state["commit_id"], state["height"], state["transactions"]),
op_list=[('=', 0, state["id"]),
('=', 1, state["height"]),
('=', 2, state["transactions"])],
limit=1)
# @register_query(LocalMongoDBConnection)
def get_pre_commit_state(connection):
space = connection.space("pre_commit_tx")
_commits_tx = space.select(limit=1)
_commits_tx = _commits_tx.data
def get_pre_commit_state(connection) -> dict:
space = connection.space("pre_commits")
_commit = space.select(_commits_tx[0][1], index="id_search")
_commit = _commit.data[0]
return {"height": _commit[0], "transactions": [_cmt[0] for _cmt in _commits_tx]}
_commit = space.select([], index="id_search", limit=1).data
if len(_commit) == 0:
return {}
_commit = _commit[0]
return {"height": _commit[0], "transactions": _commit[1]}
# @register_query(LocalMongoDBConnection)

View File

@ -326,103 +326,118 @@ def test_get_block():
assert block['height'] == 3
def test_delete_zero_unspent_outputs(db_context, utxoset):
from planetmint.backend import query
unspent_outputs, utxo_collection = utxoset
delete_res = query.delete_unspent_outputs(db_context.conn)
assert delete_res is None
assert utxo_collection.count_documents({}) == 3
assert utxo_collection.count_documents(
{'$or': [
{'transaction_id': 'a', 'output_index': 0},
{'transaction_id': 'b', 'output_index': 0},
{'transaction_id': 'a', 'output_index': 1},
]}
) == 3
def test_delete_one_unspent_outputs(db_context, utxoset):
from planetmint.backend import query
unspent_outputs, utxo_collection = utxoset
delete_res = query.delete_unspent_outputs(db_context.conn,
unspent_outputs[0])
assert delete_res.raw_result['n'] == 1
assert utxo_collection.count_documents(
{'$or': [
{'transaction_id': 'a', 'output_index': 1},
{'transaction_id': 'b', 'output_index': 0},
]}
) == 2
assert utxo_collection.count_documents(
{'transaction_id': 'a', 'output_index': 0}) == 0
def test_delete_many_unspent_outputs(db_context, utxoset):
from planetmint.backend import query
unspent_outputs, utxo_collection = utxoset
delete_res = query.delete_unspent_outputs(db_context.conn,
*unspent_outputs[::2])
assert delete_res.raw_result['n'] == 2
assert utxo_collection.count_documents(
{'$or': [
{'transaction_id': 'a', 'output_index': 0},
{'transaction_id': 'b', 'output_index': 0},
]}
) == 0
assert utxo_collection.count_documents(
{'transaction_id': 'a', 'output_index': 1}) == 1
def test_store_zero_unspent_output(db_context, utxo_collection):
from planetmint.backend import query
res = query.store_unspent_outputs(db_context.conn)
assert res is None
assert utxo_collection.count_documents({}) == 0
def test_store_one_unspent_output(db_context,
unspent_output_1, utxo_collection):
from planetmint.backend import query
res = query.store_unspent_outputs(db_context.conn, unspent_output_1)
assert res.acknowledged
assert len(res.inserted_ids) == 1
assert utxo_collection.count_documents(
{'transaction_id': unspent_output_1['transaction_id'],
'output_index': unspent_output_1['output_index']}
) == 1
def test_store_many_unspent_outputs(db_context,
unspent_outputs, utxo_collection):
from planetmint.backend import query
res = query.store_unspent_outputs(db_context.conn, *unspent_outputs)
assert res.acknowledged
assert len(res.inserted_ids) == 3
assert utxo_collection.count_documents(
{'transaction_id': unspent_outputs[0]['transaction_id']}
) == 3
def test_get_unspent_outputs(db_context, utxoset):
from planetmint.backend import query
cursor = query.get_unspent_outputs(db_context.conn)
assert cursor.collection.count_documents({}) == 3
retrieved_utxoset = list(cursor)
unspent_outputs, utxo_collection = utxoset
assert retrieved_utxoset == list(
utxo_collection.find(projection={'_id': False}))
assert retrieved_utxoset == unspent_outputs
# def test_delete_zero_unspent_outputs(db_context, utxoset):
# from planetmint.backend.tarantool import query
# return
#
# unspent_outputs, utxo_collection = utxoset
#
# delete_res = query.delete_unspent_outputs(db_context.conn)
#
# assert delete_res is None
# assert utxo_collection.count_documents({}) == 3
# assert utxo_collection.count_documents(
# {'$or': [
# {'transaction_id': 'a', 'output_index': 0},
# {'transaction_id': 'b', 'output_index': 0},
# {'transaction_id': 'a', 'output_index': 1},
# ]}
# ) == 3
#
#
# def test_delete_one_unspent_outputs(db_context, utxoset):
# return
# from planetmint.backend import query
# unspent_outputs, utxo_collection = utxoset
# delete_res = query.delete_unspent_outputs(db_context.conn,
# unspent_outputs[0])
# assert delete_res.raw_result['n'] == 1
# assert utxo_collection.count_documents(
# {'$or': [
# {'transaction_id': 'a', 'output_index': 1},
# {'transaction_id': 'b', 'output_index': 0},
# ]}
# ) == 2
# assert utxo_collection.count_documents(
# {'transaction_id': 'a', 'output_index': 0}) == 0
#
#
# def test_delete_many_unspent_outputs(db_context, utxoset):
# return
# from planetmint.backend import query
# unspent_outputs, utxo_collection = utxoset
# delete_res = query.delete_unspent_outputs(db_context.conn,
# *unspent_outputs[::2])
# assert delete_res.raw_result['n'] == 2
# assert utxo_collection.count_documents(
# {'$or': [
# {'transaction_id': 'a', 'output_index': 0},
# {'transaction_id': 'b', 'output_index': 0},
# ]}
# ) == 0
# assert utxo_collection.count_documents(
# {'transaction_id': 'a', 'output_index': 1}) == 1
#
#
# def test_store_zero_unspent_output(db_context, utxo_collection):
# return
# from planetmint.backend import query
# res = query.store_unspent_outputs(db_context.conn)
# assert res is None
# assert utxo_collection.count_documents({}) == 0
#
#
# def test_store_one_unspent_output(db_context,
# unspent_output_1, utxo_collection):
# return
# from planetmint.backend import query
# res = query.store_unspent_outputs(db_context.conn, unspent_output_1)
# assert res.acknowledged
# assert len(res.inserted_ids) == 1
# assert utxo_collection.count_documents(
# {'transaction_id': unspent_output_1['transaction_id'],
# 'output_index': unspent_output_1['output_index']}
# ) == 1
#
#
# def test_store_many_unspent_outputs(db_context,
# unspent_outputs, utxo_collection):
# return
# from planetmint.backend import query
# res = query.store_unspent_outputs(db_context.conn, *unspent_outputs)
# assert res.acknowledged
# assert len(res.inserted_ids) == 3
# assert utxo_collection.count_documents(
# {'transaction_id': unspent_outputs[0]['transaction_id']}
# ) == 3
#
#
# def test_get_unspent_outputs(db_context, utxoset):
# return
# from planetmint.backend import query
# cursor = query.get_unspent_outputs(db_context.conn)
# assert cursor.collection.count_documents({}) == 3
# retrieved_utxoset = list(cursor)
# unspent_outputs, utxo_collection = utxoset
# assert retrieved_utxoset == list(
# utxo_collection.find(projection={'_id': False}))
# assert retrieved_utxoset == unspent_outputs
def test_store_pre_commit_state(db_context):
from planetmint.backend import query
from planetmint.backend import connect
from planetmint.backend.tarantool import query
conn = connect().get_connection()
state = dict(height=3, transactions=[])
query.store_pre_commit_state(db_context.conn, state)
cursor = db_context.conn.db.pre_commit.find({'commit_id': 'test'},
projection={'_id': False})
assert cursor.collection.count_documents({}) == 1
query.store_pre_commit_state(connection=conn, state=state)
commit = query.get_pre_commit_state(connection=conn)
assert len(list(commit)) == 1
# cursor = db_context.conn.db.pre_commit.find({'commit_id': 'test'},
# projection={'_id': False})
def test_get_pre_commit_state(db_context):

View File

@ -510,8 +510,8 @@ def unspent_outputs(unspent_output_0, unspent_output_1, unspent_output_2):
@pytest.fixture
def mongo_client(db_context):
return MongoClient(host=db_context.host, port=db_context.port)
def mongo_client(db_context): # TODO Here add TarantoolConnectionClass
return None # MongoClient(host=db_context.host, port=db_context.port)
@pytest.fixture