Problem: unhandled error when deleting zero utxo

Solution: only execute the query if *unspent_outputs is not empty
This commit is contained in:
Sylvain Bellemare 2018-02-28 23:17:31 +01:00
parent aaec67724a
commit 2d2182dd19
2 changed files with 42 additions and 11 deletions

View File

@ -243,17 +243,17 @@ def store_unspent_outputs(conn, *unspent_outputs):
@register_query(LocalMongoDBConnection)
def delete_unspent_outputs(conn, *unspent_outputs):
cursor = conn.run(
conn.collection('utxos').remove(
{'$or': [
{'$and': [
if unspent_outputs:
return conn.run(
conn.collection('utxos').remove({
'$or': [{
'$and': [
{'transaction_id': unspent_output['transaction_id']},
{'output_index': unspent_output['output_index']}
]}
for unspent_output in unspent_outputs
]}
))
return cursor
{'output_index': unspent_output['output_index']},
],
} for unspent_output in unspent_outputs]
})
)
@register_query(LocalMongoDBConnection)

View File

@ -224,7 +224,38 @@ def test_delete_latest_block(signed_create_tx, signed_transfer_tx):
assert query.get_block(conn, 51) is None
def test_delete_unspent_outputs(db_context, utxoset):
def test_delete_zero_unspent_outputs(db_context, utxoset):
from bigchaindb.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() == 3
assert utxo_collection.find(
{'$or': [
{'transaction_id': 'a', 'output_index': 0},
{'transaction_id': 'b', 'output_index': 0},
{'transaction_id': 'a', 'output_index': 1},
]}
).count() == 3
def test_delete_one_unspent_outputs(db_context, utxoset):
from bigchaindb.backend import query
unspent_outputs, utxo_collection = utxoset
delete_res = query.delete_unspent_outputs(db_context.conn,
unspent_outputs[0])
assert delete_res['n'] == 1
assert utxo_collection.find(
{'$or': [
{'transaction_id': 'a', 'output_index': 1},
{'transaction_id': 'b', 'output_index': 0},
]}
).count() == 2
assert utxo_collection.find(
{'transaction_id': 'a', 'output_index': 0}).count() == 0
def test_delete_many_unspent_outputs(db_context, utxoset):
from bigchaindb.backend import query
unspent_outputs, utxo_collection = utxoset
delete_res = query.delete_unspent_outputs(db_context.conn,