mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00

* Planning release * Clean up after move * Add exceptions.py * Add crypto.py * Adjust setup to package structure * Fix tests * Add test coverage * Comply to flake8 * Add test coverage * Transfer-tx fulfillments validation * Remove condition and fulfillment ids * Fix signing logic Specifically for transfer-tx with multiple inputs and outputs. * Compliance to legacy BDB models * Adjust fulfillment validation interface * Add serialization validation for txids * Use __eq__ to compare objects * Heavy refactor to comply with current implementation * Add Transaction.create * Correct fulfillment validation logic * Add Transaction.create for hashlock conditions * Add hashlock condition serialization * Transaction.transfer add single input and outputs * Small adjustments to transfer-tx interface * Create transfer-tx interface * Increase test coverage * Adjust fulfillment (de)serialization * Catch CC Error for Fulfillment * Allow custom thresholds * PR feedback * Fix tests * Rename Data to Metadata * Add Asset exceptions * Add basic Asset model * More renaming of payload => data * Add Asset into work-flow-functions * Add Asset amount to condition * add fulfillment exception * initial integration of asset * Make transaction.py compy to 79 chars * Make util.py comply to 79 chars * Make exceptions.py comply to 80 chars * Renaming inp to input_ * fix pep8 issues * Correct raised error * Remove resolved TODOs * prevent adding None as fulfillment / condition to Transaction * Small modifications to support new cryptoconditions * Improve documentation (#42) * Add doc strings for Fulfillment cls * Add doc strings for TransactionLink cls * Add doc strings for Condition cls * Add doc strings for Data cls * Add doc strings for Transaction cls * Add doc strings for Asset cls * Extract common implementation * Tx model: Add test for empty inputs * WIP: Implement sign tx * Add tests for: - Conditions; and - Fulfillments Mostly on the (de)serialization part. * Finalize serialization logic for tx class * Add Tests for tx serialization logic * Add fulfillment validation * Add ThresholdCondition support * WIP transfer * Clean up after move * Adjust setup to package structure * Fix tests * Add test coverage * Add test coverage * Transfer-tx fulfillments validation * Remove condition and fulfillment ids * Fix signing logic Specifically for transfer-tx with multiple inputs and outputs. * Fix test case * Compliance to legacy BDB models * Adjust fulfillment validation interface * Add serialization validation for txids * Use __eq__ to compare objects * Heavy refactor to comply with current implementation * Add Transaction.create * Add validation tests * Add Transaction.create for hashlock conditions * Add hashlock condition serialization * Transaction.transfer add single input and outputs * Small adjustments to transfer-tx interface * Create transfer-tx interface * Increase test coverage * Adjust fulfillment (de)serialization * Catch CC Error for Fulfillment * Allow custom thresholds * Rename Data to Metadata * Add basic Asset model * Add Asset into work-flow-functions * Add Asset amount to condition * initial integration of asset * Make tests comply to 79 chars per line * Fixed tests * fix pep8 issues * Correct raised error * Add test for asset initialization * Remove resolved TODOs * prevent adding None as fulfillment / condition to Transaction * Small modifications to support new cryptoconditions * Extract common tests * Copy conftest from bigchaindb-common - by @timdaub * Replace bigchaindb_common pkg by bigchaindb.common
179 lines
5.8 KiB
Python
179 lines
5.8 KiB
Python
from pytest import raises
|
|
|
|
|
|
class TestTransactionModel(object):
|
|
def test_validating_an_invalid_transaction(self, b):
|
|
from bigchaindb.models import Transaction
|
|
|
|
tx = Transaction.create([b.me], [b.me])
|
|
tx.operation = 'something invalid'
|
|
|
|
with raises(TypeError):
|
|
tx.validate(b)
|
|
|
|
tx.operation = 'CREATE'
|
|
tx.fulfillments = []
|
|
with raises(ValueError):
|
|
tx.validate(b)
|
|
|
|
|
|
class TestBlockModel(object):
|
|
def test_block_initialization(self, monkeypatch):
|
|
from bigchaindb.models import Block
|
|
|
|
monkeypatch.setattr('time.time', lambda: 1)
|
|
|
|
block = Block()
|
|
assert block.transactions == []
|
|
assert block.voters == []
|
|
assert block.timestamp == '1'
|
|
assert block.node_pubkey is None
|
|
assert block.signature is None
|
|
|
|
with raises(TypeError):
|
|
Block('not a list or None')
|
|
with raises(TypeError):
|
|
Block(None, 'valid node_pubkey', 'valid timestamp',
|
|
'not a list or None')
|
|
|
|
def test_block_serialization(self, b):
|
|
from bigchaindb.common.crypto import hash_data
|
|
from bigchaindb.common.util import gen_timestamp, serialize
|
|
from bigchaindb.models import Block, Transaction
|
|
|
|
transactions = [Transaction.create([b.me], [b.me])]
|
|
timestamp = gen_timestamp()
|
|
voters = ['Qaaa', 'Qbbb']
|
|
expected_block = {
|
|
'timestamp': timestamp,
|
|
'transactions': [tx.to_dict() for tx in transactions],
|
|
'node_pubkey': b.me,
|
|
'voters': voters,
|
|
}
|
|
expected = {
|
|
'id': hash_data(serialize(expected_block)),
|
|
'block': expected_block,
|
|
'signature': None,
|
|
}
|
|
|
|
block = Block(transactions, b.me, timestamp, voters)
|
|
|
|
assert block.to_dict() == expected
|
|
|
|
def test_block_invalid_serializaton(self):
|
|
from bigchaindb.common.exceptions import OperationError
|
|
from bigchaindb.models import Block
|
|
|
|
block = Block([])
|
|
with raises(OperationError):
|
|
block.to_dict()
|
|
|
|
def test_block_deserialization(self, b):
|
|
from bigchaindb.common.crypto import hash_data
|
|
from bigchaindb.common.util import gen_timestamp, serialize
|
|
from bigchaindb.models import Block, Transaction
|
|
|
|
transactions = [Transaction.create([b.me], [b.me])]
|
|
timestamp = gen_timestamp()
|
|
voters = ['Qaaa', 'Qbbb']
|
|
expected = Block(transactions, b.me, timestamp, voters)
|
|
|
|
block = {
|
|
'timestamp': timestamp,
|
|
'transactions': [tx.to_dict() for tx in transactions],
|
|
'node_pubkey': b.me,
|
|
'voters': voters,
|
|
}
|
|
|
|
block_body = {
|
|
'id': hash_data(serialize(block)),
|
|
'block': block,
|
|
'signature': None,
|
|
}
|
|
|
|
assert expected == Block.from_dict(block_body)
|
|
|
|
def test_block_invalid_id_deserialization(self, b):
|
|
from bigchaindb.common.exceptions import InvalidHash
|
|
from bigchaindb.models import Block
|
|
|
|
block = {
|
|
'id': 'an invalid id',
|
|
'block': {
|
|
'node_pubkey': b.me,
|
|
}
|
|
}
|
|
|
|
with raises(InvalidHash):
|
|
Block.from_dict(block)
|
|
|
|
def test_block_invalid_signature_deserialization(self, b):
|
|
from bigchaindb.common.crypto import hash_data
|
|
from bigchaindb.common.exceptions import InvalidSignature
|
|
from bigchaindb.common.util import gen_timestamp, serialize
|
|
from bigchaindb.models import Block, Transaction
|
|
|
|
transactions = [Transaction.create([b.me], [b.me])]
|
|
timestamp = gen_timestamp()
|
|
voters = ['Qaaa', 'Qbbb']
|
|
|
|
block = {
|
|
'timestamp': timestamp,
|
|
'transactions': [tx.to_dict() for tx in transactions],
|
|
'node_pubkey': b.me,
|
|
'voters': voters,
|
|
}
|
|
|
|
block_body = {
|
|
'id': hash_data(serialize(block)),
|
|
'block': block,
|
|
'signature': 'an invalid signature',
|
|
}
|
|
|
|
with raises(InvalidSignature):
|
|
Block.from_dict(block_body)
|
|
|
|
def test_compare_blocks(self, b):
|
|
from bigchaindb.models import Block, Transaction
|
|
|
|
transactions = [Transaction.create([b.me], [b.me])]
|
|
|
|
assert Block() != 'invalid comparison'
|
|
assert Block(transactions) == Block(transactions)
|
|
|
|
def test_sign_block(self, b):
|
|
from bigchaindb.common.crypto import SigningKey, VerifyingKey
|
|
from bigchaindb.common.util import gen_timestamp, serialize
|
|
from bigchaindb.models import Block, Transaction
|
|
|
|
transactions = [Transaction.create([b.me], [b.me])]
|
|
timestamp = gen_timestamp()
|
|
voters = ['Qaaa', 'Qbbb']
|
|
expected_block = {
|
|
'timestamp': timestamp,
|
|
'transactions': [tx.to_dict() for tx in transactions],
|
|
'node_pubkey': b.me,
|
|
'voters': voters,
|
|
}
|
|
expected_block_serialized = serialize(expected_block).encode()
|
|
expected = SigningKey(b.me_private).sign(expected_block_serialized)
|
|
block = Block(transactions, b.me, timestamp, voters)
|
|
block = block.sign(b.me_private)
|
|
assert block.signature == expected.decode()
|
|
|
|
verifying_key = VerifyingKey(b.me)
|
|
assert verifying_key.verify(expected_block_serialized, block.signature)
|
|
|
|
def test_validate_already_voted_on_block(self, b, monkeypatch):
|
|
from unittest.mock import Mock
|
|
from bigchaindb.models import Transaction
|
|
|
|
tx = Transaction.create([b.me], [b.me])
|
|
block = b.create_block([tx])
|
|
|
|
has_previous_vote = Mock()
|
|
has_previous_vote.return_value = True
|
|
monkeypatch.setattr(b, 'has_previous_vote', has_previous_vote)
|
|
assert block == block.validate(b)
|
|
assert has_previous_vote.called is True
|