restore tx validation in block

This commit is contained in:
Scott Sadler 2017-05-24 11:38:16 +02:00
parent b6ae91c541
commit 17cf9178ce
2 changed files with 18 additions and 10 deletions

View File

@ -12,7 +12,7 @@ from multipipes import Pipeline, Node, Pipe
import bigchaindb import bigchaindb
from bigchaindb import backend from bigchaindb import backend
from bigchaindb.backend.changefeed import ChangeFeed from bigchaindb.backend.changefeed import ChangeFeed
from bigchaindb.models import FastTransaction from bigchaindb.models import Transaction
from bigchaindb.common.exceptions import ValidationError from bigchaindb.common.exceptions import ValidationError
from bigchaindb import Bigchain from bigchaindb import Bigchain
@ -57,11 +57,11 @@ class BlockPipeline:
tx (dict): the transaction to validate. tx (dict): the transaction to validate.
Returns: Returns:
:class:`~bigchaindb.models.FastTransaction`: The transaction if valid, :class:`~bigchaindb.models.Transaction`: The transaction if valid,
``None`` otherwise. ``None`` otherwise.
""" """
try: try:
tx = FastTransaction(tx) tx = Transaction.from_dict(tx)
except ValidationError: except ValidationError:
return None return None
@ -71,7 +71,14 @@ class BlockPipeline:
self.bigchain.delete_transaction(tx.id) self.bigchain.delete_transaction(tx.id)
return None 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): def create(self, tx, timeout=False):
"""Create a block. """Create a block.

View File

@ -28,14 +28,15 @@ def test_filter_by_assignee(b, signed_create_tx):
@pytest.mark.bdb @pytest.mark.bdb
def test_validate_transaction(b): def test_validate_transaction(b, create_tx):
from bigchaindb.pipelines.block import BlockPipeline from bigchaindb.pipelines.block import BlockPipeline
# validate_tx doesn't actually validate schema anymore.
tx = {'id': 'a'}
block_maker = BlockPipeline() 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): 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() 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 # Assert that validationerror gets caught
validate.side_effect = ValidationError() validate.side_effect = ValidationError()
assert block_maker.validate_tx(tx_dict) is None assert block_maker.validate_tx(tx_dict) is None