mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
get_transaction_by_metadata_id now ignores invalid transactions
This commit is contained in:
parent
32a247552c
commit
98084f6f4a
@ -329,24 +329,32 @@ class Bigchain(object):
|
|||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_tx_by_metadata_id(self, metadata_id):
|
def get_transaction_by_metadata_id(self, metadata_id):
|
||||||
"""Retrieves transactions related to a metadata.
|
"""Retrieves valid or undecided transactions related to a particular
|
||||||
|
metadata.
|
||||||
|
|
||||||
When creating a transaction one of the optional arguments is the `metadata`. The metadata is a generic
|
When creating a transaction one of the optional arguments is the
|
||||||
dict that contains extra information that can be appended to the transaction.
|
`metadata`. The metadata is a generic dict that contains extra
|
||||||
|
information that can be appended to the transaction.
|
||||||
|
|
||||||
To make it easy to query the bigchain for that particular metadata we create a UUID for the metadata and
|
To make it easy to query the bigchain for that particular metadata we
|
||||||
store it with the transaction.
|
create a UUID for the metadata and store it with the transaction.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
metadata_id (str): the id for this particular metadata.
|
metadata_id (str): the id for this particular metadata.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
A list of transactions containing that metadata. If no transaction exists with that metadata it
|
A list of valid or undecided transactions containing that metadata.
|
||||||
returns an empty list `[]`
|
If no transaction exists with that metadata it returns an empty
|
||||||
|
list `[]`
|
||||||
"""
|
"""
|
||||||
cursor = self.backend.get_transactions_by_metadata_id(metadata_id)
|
txids = self.backend.get_txids_by_metadata_id(metadata_id)
|
||||||
return [Transaction.from_dict(tx) for tx in cursor]
|
transactions = []
|
||||||
|
for txid in txids:
|
||||||
|
tx = self.get_transaction(txid)
|
||||||
|
if tx:
|
||||||
|
transactions.append(tx)
|
||||||
|
return transactions
|
||||||
|
|
||||||
def get_txs_by_asset_id(self, asset_id):
|
def get_txs_by_asset_id(self, asset_id):
|
||||||
"""Retrieves transactions related to a particular asset.
|
"""Retrieves transactions related to a particular asset.
|
||||||
|
@ -138,27 +138,31 @@ class RethinkDBBackend:
|
|||||||
.get_all(transaction_id, index='transaction_id')
|
.get_all(transaction_id, index='transaction_id')
|
||||||
.pluck('votes', 'id', {'block': ['voters']}))
|
.pluck('votes', 'id', {'block': ['voters']}))
|
||||||
|
|
||||||
def get_transactions_by_metadata_id(self, metadata_id):
|
def get_txids_by_metadata_id(self, metadata_id):
|
||||||
"""Retrieves transactions related to a metadata.
|
"""Retrieves transaction ids related to a particular metadata.
|
||||||
|
|
||||||
When creating a transaction one of the optional arguments is the `metadata`. The metadata is a generic
|
When creating a transaction one of the optional arguments is the
|
||||||
dict that contains extra information that can be appended to the transaction.
|
`metadata`. The metadata is a generic dict that contains extra
|
||||||
|
information that can be appended to the transaction.
|
||||||
|
|
||||||
To make it easy to query the bigchain for that particular metadata we create a UUID for the metadata and
|
To make it easy to query the bigchain for that particular metadata we
|
||||||
store it with the transaction.
|
create a UUID for the metadata and store it with the transaction.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
metadata_id (str): the id for this particular metadata.
|
metadata_id (str): the id for this particular metadata.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
A list of transactions containing that metadata. If no transaction exists with that metadata it
|
A list of transaction ids containing that metadata. If no
|
||||||
returns an empty list `[]`
|
transaction exists with that metadata it returns an empty list `[]`
|
||||||
"""
|
"""
|
||||||
return self.connection.run(
|
return self.connection.run(
|
||||||
r.table('bigchain', read_mode=self.read_mode)
|
r.table('bigchain', read_mode=self.read_mode)
|
||||||
.get_all(metadata_id, index='metadata_id')
|
.get_all(metadata_id, index='metadata_id')
|
||||||
.concat_map(lambda block: block['block']['transactions'])
|
.concat_map(lambda block: block['block']['transactions'])
|
||||||
.filter(lambda transaction: transaction['transaction']['metadata']['id'] == metadata_id))
|
.filter(lambda transaction:
|
||||||
|
transaction['transaction']['metadata']['id'] ==
|
||||||
|
metadata_id)
|
||||||
|
.get_field('id'))
|
||||||
|
|
||||||
def get_transactions_by_asset_id(self, asset_id):
|
def get_transactions_by_asset_id(self, asset_id):
|
||||||
"""Retrieves transactions related to a particular asset.
|
"""Retrieves transactions related to a particular asset.
|
||||||
|
@ -88,11 +88,6 @@ class TestBigchainApi(object):
|
|||||||
|
|
||||||
assert b.has_previous_vote(block.id, block.voters) is True
|
assert b.has_previous_vote(block.id, block.voters) is True
|
||||||
|
|
||||||
|
|
||||||
def test_get_transactions_for_metadata_mismatch(self, b):
|
|
||||||
matches = b.get_tx_by_metadata_id('missing')
|
|
||||||
assert not matches
|
|
||||||
|
|
||||||
def test_get_spent_with_double_spend(self, b, monkeypatch):
|
def test_get_spent_with_double_spend(self, b, monkeypatch):
|
||||||
from bigchaindb.common.exceptions import DoubleSpend
|
from bigchaindb.common.exceptions import DoubleSpend
|
||||||
from bigchaindb.models import Transaction
|
from bigchaindb.models import Transaction
|
||||||
@ -190,12 +185,28 @@ class TestBigchainApi(object):
|
|||||||
block = b.create_block([tx])
|
block = b.create_block([tx])
|
||||||
b.write_block(block, durability='hard')
|
b.write_block(block, durability='hard')
|
||||||
|
|
||||||
matches = b.get_tx_by_payload_uuid(tx.metadata.data_id)
|
matches = b.get_transaction_by_metadata_id(tx.metadata.data_id)
|
||||||
assert len(matches) == 1
|
assert len(matches) == 1
|
||||||
assert matches[0].id == tx.id
|
assert matches[0].id == tx.id
|
||||||
|
|
||||||
def test_get_transactions_for_metadata(self, b, user_vk):
|
@pytest.mark.usefixtures('inputs')
|
||||||
matches = b.get_tx_by_metadata_id('missing')
|
def test_get_transactions_for_metadata_invalid_block(self, b, user_vk):
|
||||||
|
from bigchaindb.models import Transaction
|
||||||
|
|
||||||
|
metadata = {'msg': 'Hello BigchainDB!'}
|
||||||
|
tx = Transaction.create([b.me], [user_vk], metadata=metadata)
|
||||||
|
|
||||||
|
block = b.create_block([tx])
|
||||||
|
b.write_block(block, durability='hard')
|
||||||
|
# vote block invalid
|
||||||
|
vote = b.vote(block.id, b.get_last_voted_block().id, False)
|
||||||
|
b.write_vote(vote)
|
||||||
|
|
||||||
|
matches = b.get_transaction_by_metadata_id(tx.metadata.data_id)
|
||||||
|
assert len(matches) == 0
|
||||||
|
|
||||||
|
def test_get_transactions_for_metadata_mismatch(self, b):
|
||||||
|
matches = b.get_transaction_by_metadata_id('missing')
|
||||||
assert not matches
|
assert not matches
|
||||||
|
|
||||||
@pytest.mark.usefixtures('inputs')
|
@pytest.mark.usefixtures('inputs')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user