mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
bigchain.is_new_transaction method
This commit is contained in:
parent
dfb9f5a496
commit
843d65d233
@ -178,6 +178,22 @@ class Bigchain(object):
|
||||
exceptions.TransactionNotInValidBlock, exceptions.AmountError):
|
||||
return False
|
||||
|
||||
def is_new_transaction(self, txid, exclude_block_id=None):
|
||||
"""
|
||||
Return True if the transaction does not exist in any
|
||||
VALID or UNDECIDED block. Return False otherwise.
|
||||
|
||||
Args:
|
||||
txid (str): Transaction ID
|
||||
exclude_block_id (str): Exclude block from search
|
||||
"""
|
||||
block_statuses = self.get_blocks_status_containing_tx(txid)
|
||||
block_statuses.pop(exclude_block_id, None)
|
||||
for status in block_statuses.values():
|
||||
if status != self.BLOCK_INVALID:
|
||||
return False
|
||||
return True
|
||||
|
||||
def get_block(self, block_id, include_status=False):
|
||||
"""Get the block with the specified `block_id` (and optionally its status)
|
||||
|
||||
|
@ -1240,3 +1240,40 @@ def test_transaction_unicode(b):
|
||||
assert b.get_block(block.id) == block.to_dict()
|
||||
assert block.validate(b) == block
|
||||
assert beer_json in serialize(block.to_dict())
|
||||
|
||||
|
||||
@pytest.mark.bdb
|
||||
def test_is_new_transaction(b, genesis_block):
|
||||
from bigchaindb.models import Transaction
|
||||
|
||||
def write_tx(n):
|
||||
tx = Transaction.create([b.me], [([b.me], n)])
|
||||
tx = tx.sign([b.me_private])
|
||||
# Tx is new because it's not in any block
|
||||
assert b.is_new_transaction(tx.id)
|
||||
|
||||
block = b.create_block([tx])
|
||||
b.write_block(block)
|
||||
return tx, block
|
||||
|
||||
# test VALID case
|
||||
tx, block = write_tx(1)
|
||||
# Tx is now in undecided block
|
||||
assert not b.is_new_transaction(tx.id)
|
||||
assert b.is_new_transaction(tx.id, exclude_block_id=block.id)
|
||||
# After voting valid, should not be new
|
||||
vote = b.vote(block.id, genesis_block.id, True)
|
||||
b.write_vote(vote)
|
||||
assert not b.is_new_transaction(tx.id)
|
||||
assert b.is_new_transaction(tx.id, exclude_block_id=block.id)
|
||||
|
||||
# test INVALID case
|
||||
tx, block = write_tx(2)
|
||||
# Tx is now in undecided block
|
||||
assert not b.is_new_transaction(tx.id)
|
||||
assert b.is_new_transaction(tx.id, exclude_block_id=block.id)
|
||||
vote = b.vote(block.id, genesis_block.id, False)
|
||||
b.write_vote(vote)
|
||||
# Tx is new because it's only found in an invalid block
|
||||
assert b.is_new_transaction(tx.id)
|
||||
assert b.is_new_transaction(tx.id, exclude_block_id=block.id)
|
||||
|
Loading…
x
Reference in New Issue
Block a user