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

* Adjust imports to bigchaindb_common * Adjust get_spent function signature * Adjust block serialization * Fix BigchainApi Test * Fix TestTransactionValidation tests * Fix TestBlockValidation tests * WIP: TestMultipleInputs * Adjust tests to tx-model interface changes - Fix old tests - Fix tests in TestMultipleInputs class * Remove fulfillment message tests * Fix TransactionMalleability tests * Remove Cryptoconditions tests * Remove create_transaction * Remove signing logic * Remove consensus plugin * Fix block_creation pipeline * Fix election pipeline * Replace some util functions with bdb_common ones - timestamp ==> gen_timestamp - serialize. * Implement Block model * Simplify function signatures for vote functions Change parameter interface for the following functions: - has_previous_vote - verify_vote_signature - block_election_status so that they take a block's id and voters instead of a fake block. * Integrate Block and Transaction model * Fix leftover tests and cleanup conftest * Add bigchaindb-common to install_requires * Delete transactions after block is written (#609) * delete transactions after block is written * cleanup transaction_exists * check for duplicate transactions * delete invalid tx from backlog * test duplicate transaction * Remove dead code * Test processes.py * Test invalid tx in on server * Fix tests for core.py * Fix models tests * Test commands main fn * Add final coverage to vote pipeline * Add more tests to voting pipeline * Remove consensus plugin docs and misc * Post rebase fixes * Fix rebase mess * Remove extra blank line * Improve docstring * Remove comment handled in bigchaindb/cryptoconditions#27; see https://github.com/bigchaindb/cryptoconditions/issues/27 * Fix block serialization in block creation * Add signed_ prefix to transfer_tx * Improve docs * Add library documentation page on pipelines * PR feedback for models.py * Impr. readability of get_last_voted_block * Use dict comprehension * Add docker-compose file to build and serve docs locally for development purposes * Change private_key for signing_key * Improve docstrings * Remove consensus docs * Document new consensus module * Create different transactions for the block * Cleanup variable names in block.py * Create different transactions for the block * Cleanup variable names in block.py
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)
|
|
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
|
|
|
|
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
|