def test_block_dupe_tx(self, b, alice): from bigchaindb.models import Block, Transaction from bigchaindb.common.exceptions import DuplicateTransaction tx = Transaction.create([alice.public_key], [([alice.public_key], 1)]) block = Block([tx, tx], alice.public_key) block.sign(alice.private_key) b.store_block(block.to_dict()) with raises(DuplicateTransaction): block.validate(b)
def test_sign_block(self, b, alice): from bigchaindb.common.crypto import PrivateKey, PublicKey from bigchaindb.common.utils import gen_timestamp, serialize from bigchaindb.models import Block, Transaction transactions = [ Transaction.create([alice.public_key], [([alice.public_key], 1)]) ] timestamp = gen_timestamp() voters = ['Qaaa', 'Qbbb'] expected_block = { 'timestamp': timestamp, 'transactions': [tx.to_dict() for tx in transactions], 'node_pubkey': alice.public_key, 'voters': voters, } expected_block_serialized = serialize(expected_block).encode() expected = PrivateKey( alice.private_key).sign(expected_block_serialized) block = Block(transactions, alice.public_key, timestamp, voters) block = block.sign(alice.private_key) assert block.signature == expected.decode() public_key = PublicKey(alice.public_key) assert public_key.verify(expected_block_serialized, block.signature)
def create_block(self, validated_transactions): """Creates a block given a list of `validated_transactions`. Note that this method does not validate the transactions. Transactions should be validated before calling create_block. Args: validated_transactions (list(Transaction)): list of validated transactions. Returns: Block: created block. """ # Prevent the creation of empty blocks if len(validated_transactions) == 0: raise exceptions.OperationError('Empty block creation is not ' 'allowed') voters = self.nodes_except_me + [self.me] block = Block(validated_transactions, self.me, gen_timestamp(), voters) block = block.sign(self.me_private) return block