From 17cf9178cee27007a883c7bfe27cba52766f00a7 Mon Sep 17 00:00:00 2001 From: Scott Sadler Date: Wed, 24 May 2017 11:38:16 +0200 Subject: [PATCH] restore tx validation in block --- bigchaindb/pipelines/block.py | 15 +++++++++++---- tests/pipelines/test_block_creation.py | 13 +++++++------ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/bigchaindb/pipelines/block.py b/bigchaindb/pipelines/block.py index 40e5afe8..0fe327bb 100644 --- a/bigchaindb/pipelines/block.py +++ b/bigchaindb/pipelines/block.py @@ -12,7 +12,7 @@ from multipipes import Pipeline, Node, Pipe import bigchaindb from bigchaindb import backend from bigchaindb.backend.changefeed import ChangeFeed -from bigchaindb.models import FastTransaction +from bigchaindb.models import Transaction from bigchaindb.common.exceptions import ValidationError from bigchaindb import Bigchain @@ -57,11 +57,11 @@ class BlockPipeline: tx (dict): the transaction to validate. Returns: - :class:`~bigchaindb.models.FastTransaction`: The transaction if valid, + :class:`~bigchaindb.models.Transaction`: The transaction if valid, ``None`` otherwise. """ try: - tx = FastTransaction(tx) + tx = Transaction.from_dict(tx) except ValidationError: return None @@ -71,7 +71,14 @@ class BlockPipeline: self.bigchain.delete_transaction(tx.id) return None - return tx + # If transaction is not valid it should not be included + try: + tx.validate(self.bigchain) + return tx + except ValidationError as e: + logger.warning('Invalid tx: %s', e) + self.bigchain.delete_transaction(tx.id) + return None def create(self, tx, timeout=False): """Create a block. diff --git a/tests/pipelines/test_block_creation.py b/tests/pipelines/test_block_creation.py index ffd423e3..27efc65d 100644 --- a/tests/pipelines/test_block_creation.py +++ b/tests/pipelines/test_block_creation.py @@ -28,14 +28,15 @@ def test_filter_by_assignee(b, signed_create_tx): @pytest.mark.bdb -def test_validate_transaction(b): +def test_validate_transaction(b, create_tx): from bigchaindb.pipelines.block import BlockPipeline - # validate_tx doesn't actually validate schema anymore. - tx = {'id': 'a'} - block_maker = BlockPipeline() - assert block_maker.validate_tx(tx).data == tx + + assert block_maker.validate_tx(create_tx.to_dict()) is None + + valid_tx = create_tx.sign([b.me_private]) + assert block_maker.validate_tx(valid_tx.to_dict()) == valid_tx def test_validate_transaction_handles_exceptions(b, signed_create_tx): @@ -49,7 +50,7 @@ def test_validate_transaction_handles_exceptions(b, signed_create_tx): tx_dict = signed_create_tx.to_dict() - with patch('bigchaindb.models.FastTransaction.__init__') as validate: + with patch('bigchaindb.models.Transaction.validate') as validate: # Assert that validationerror gets caught validate.side_effect = ValidationError() assert block_maker.validate_tx(tx_dict) is None