mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Problem: key order agnostic queries not implemented
Solution: get_spent queries embedded documents which respect key order. This is not expected by the application hence the query should be altered to query any kind of key order
This commit is contained in:
parent
5da1cd8434
commit
fc1920cb7a
@ -91,8 +91,12 @@ def get_assets(conn, asset_ids):
|
||||
|
||||
@register_query(LocalMongoDBConnection)
|
||||
def get_spent(conn, transaction_id, output):
|
||||
query = {'inputs.fulfills': {'transaction_id': transaction_id,
|
||||
'output_index': output}}
|
||||
query_array = [{'transaction_id': transaction_id,
|
||||
'output_index': output},
|
||||
{'output_index': output,
|
||||
'transaction_id': transaction_id}]
|
||||
query = {'inputs.fulfills': {'$in': query_array}}
|
||||
|
||||
return conn.run(
|
||||
conn.collection('transactions')
|
||||
.find(query, {'_id': 0}))
|
||||
|
||||
@ -85,14 +85,13 @@ def test_outputs_query_key_order(b, user_pk, user_sk, user2_pk, user2_sk):
|
||||
b.store_bulk_transactions([tx1])
|
||||
|
||||
inputs = tx1.to_inputs()
|
||||
tx2 = Transaction.transfer([inputs[0]], [([user2_pk], 2)], tx1.id).sign([user_sk])
|
||||
tx2 = Transaction.transfer([inputs[1]], [([user2_pk], 2)], tx1.id).sign([user_sk])
|
||||
assert tx2.validate(b)
|
||||
|
||||
tx2_dict = tx2.to_dict()
|
||||
fulfills = tx2_dict['inputs'][0]['fulfills']
|
||||
|
||||
tx2_dict['inputs'][0]['fulfills'] = {'transaction_id': fulfills['transaction_id'],
|
||||
'output_index': fulfills['output_index']}
|
||||
|
||||
backend.query.store_transactions(b.connection, [tx2_dict])
|
||||
|
||||
outputs = b.get_outputs_filtered(user_pk, spent=False)
|
||||
@ -109,7 +108,6 @@ def test_outputs_query_key_order(b, user_pk, user_sk, user2_pk, user2_sk):
|
||||
|
||||
b.store_bulk_transactions([tx1])
|
||||
tx2_dict = tx2.to_dict()
|
||||
|
||||
tx2_dict['inputs'][0]['fulfills'] = {'output_index': fulfills['output_index'],
|
||||
'transaction_id': fulfills['transaction_id']}
|
||||
|
||||
|
||||
@ -471,3 +471,36 @@ def test_migrate_abci_chain_generates_new_chains(b, chain, block_height,
|
||||
b.migrate_abci_chain()
|
||||
latest_chain = b.get_latest_abci_chain()
|
||||
assert latest_chain == expected
|
||||
|
||||
|
||||
@pytest.mark.bdb
|
||||
def test_get_spent_key_order(b, user_pk, user_sk, user2_pk, user2_sk):
|
||||
from bigchaindb import backend
|
||||
from bigchaindb.models import Transaction
|
||||
from bigchaindb.common.crypto import generate_key_pair
|
||||
from bigchaindb.common.exceptions import DoubleSpend
|
||||
|
||||
alice = generate_key_pair()
|
||||
bob = generate_key_pair()
|
||||
|
||||
tx1 = Transaction.create([user_pk],
|
||||
[([alice.public_key], 3), ([user_pk], 2)],
|
||||
asset=None)\
|
||||
.sign([user_sk])
|
||||
b.store_bulk_transactions([tx1])
|
||||
|
||||
inputs = tx1.to_inputs()
|
||||
tx2 = Transaction.transfer([inputs[1]], [([user2_pk], 2)], tx1.id).sign([user_sk])
|
||||
assert tx2.validate(b)
|
||||
|
||||
tx2_dict = tx2.to_dict()
|
||||
fulfills = tx2_dict['inputs'][0]['fulfills']
|
||||
tx2_dict['inputs'][0]['fulfills'] = {'output_index': fulfills['output_index'],
|
||||
'transaction_id': fulfills['transaction_id']}
|
||||
|
||||
backend.query.store_transactions(b.connection, [tx2_dict])
|
||||
|
||||
tx3 = Transaction.transfer([inputs[1]], [([bob.public_key], 2)], tx1.id).sign([user_sk])
|
||||
|
||||
with pytest.raises(DoubleSpend):
|
||||
tx3.validate(b)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user