From bd9979618d8d08748e835f267f4e649967bb1a0a Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 18 Aug 2016 16:04:35 +0200 Subject: [PATCH] return block status --- bigchaindb/core.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/bigchaindb/core.py b/bigchaindb/core.py index 6b15d489..cd8a013d 100644 --- a/bigchaindb/core.py +++ b/bigchaindb/core.py @@ -16,9 +16,14 @@ class Bigchain(object): Create, read, sign, write transactions to the database """ + # return if a block has been voted invalid BLOCK_INVALID = 'invalid' - BLOCK_VALID = 'valid' - BLOCK_UNDECIDED = 'undecided' + # return if a block is valid, or tx is in valid block + BLOCK_VALID = TX_VALID = 'valid' + # return if block is undecided, or tx is in undecided block + BLOCK_UNDECIDED = TX_UNDECIDED = 'undecided' + # return if transaction is in backlog + TX_IN_BACKLOG = 'backlog' def __init__(self, host=None, port=None, dbname=None, public_key=None, private_key=None, keyring=[], @@ -135,15 +140,17 @@ class Bigchain(object): def get_transaction(self, txid): """Retrieve a transaction with `txid` from bigchain. - Queries the bigchain for a transaction that was already included in a block. + Queries the bigchain for a transaction, if it's in a valid or invalid + block. Args: txid (str): transaction id of the transaction to query Returns: A dict with the transaction details if the transaction was found. - - If no transaction with that `txid` was found it returns `None` + Will add the transaction status to payload ('valid', 'undecided', + or 'backlog'). If no transaction with that `txid` was found it + returns `None` """ validity = self.get_blocks_status_containing_tx(txid) @@ -161,17 +168,24 @@ class Bigchain(object): for _id in validity: target_block_id = _id if validity[_id] == Bigchain.BLOCK_VALID: + tx_status = self.TX_VALID break + else: + tx_status = self.TX_UNDECIDED # Query the transaction in the target block and return response = r.table('bigchain', read_mode=self.read_mode).get(target_block_id)\ .get_field('block').get_field('transactions')\ - .filter(lambda tx: tx['id'] == txid).run(self.conn) - - return response[0] + .filter(lambda tx: tx['id'] == txid).run(self.conn)[0] + response['validity'] = tx_status + return response else: - return None + # Otherwise, check the backlog + response = r.table('backlog').get(txid).run(self.conn) + if response: + response['validity'] = self.TX_IN_BACKLOG + return response def search_block_election_on_index(self, value, index): """Retrieve block election information given a secondary index and value