def test_unsigned_block_return_false(self):

        # Arrange
        block = Block()

        # Assert
        assert block.is_signed() is False
    def test_signed_block_return_true(self):

        # Arrange
        block = Block()
        random_sign(block, REF_PUB_KEY, REF_PRIV_KEY)

        # Assert
        assert block.is_signed() is True
    def test_raise_exception_if_block_is_already_signed(self):

        # Arrange
        block = Block()
        tx = Transaction(VERSION, Transaction.GUZI_CREATE, EMPTY_HASH, 0)
        random_sign(block, REF_PUB_KEY, REF_PRIV_KEY)

        # Act
        with pytest.raises(FullBlockError):
            block.add_transaction(tx)
    def test_increase_transaction_count(self):

        # Arrange
        block = Block()
        tx = Transaction(VERSION, Transaction.GUZI_CREATE, EMPTY_HASH, 0)

        # Act
        block.add_transaction(tx)

        # Assert
        assert len(block.transactions) == 1
    def test_compute_merkle_root_0_tx(self):
        """
        If there is 0 transaction, merkle root should be None
        """
        # Arrange
        block = Block()

        # Act
        result = block.compute_merkle_root()

        # Assert
        assert result is None
    def test_sign(self):

        # Arrange
        vk = ecdsa.VerifyingKey.from_string(REF_PUB_KEY, curve=ecdsa.SECP256k1)

        block = Block()
        data = block.hash()

        # Act
        signature = sign(data, REF_PRIV_KEY)

        # Assert
        assert vk.verify(signature, data) is True
    def test_return_true_if_transaction_in(self):

        # Arrange
        tx0 = Transaction(VERSION, Transaction.GUZI_CREATE, KEY_POOL[0]["pub"],
                          0)
        tx1 = Transaction(VERSION, Transaction.GUZI_CREATE, KEY_POOL[1]["pub"],
                          0)
        b = Block()

        # Act
        b.add_transactions([tx0, tx1])

        # Assert
        assert b._containUser(KEY_POOL[2]["pub"]) is False
    def test_transaction_not_found(self):

        # Arrange
        tx0 = Transaction(VERSION, Transaction.GUZI_CREATE, NEW_USER_PUB_KEY,
                          0)
        tx1 = Transaction(VERSION, Transaction.GUZIBOX_CREATE,
                          NEW_USER_PUB_KEY, 0)
        tx2 = Transaction(VERSION, Transaction.PAYMENT, NEW_USER_PUB_KEY, 0)
        b = Block()

        # Act
        b.add_transactions([tx1, tx2])

        # Assert
        assert b._contain_tx(tx0) is False
    def test_compute_merkle_root_1_tx(self):
        """
        If there is only 1 transaction, merkle root should be :
        hash(hash0 + hash0)
        """
        # Arrange
        tx = Transaction(VERSION, Transaction.GUZI_CREATE, NEW_USER_PUB_KEY, 0)
        block = Block()
        block.add_transaction(tx)

        expected_merkle_root = guzi_hash(tx.to_hash() + tx.to_hash())

        # Act
        result = block.compute_merkle_root()

        # Assert
        assert result == expected_merkle_root
    def test_unfound_transaction(self):
        block = Block()
        tx1 = Transaction(VERSION, Transaction.GUZI_CREATE, NEW_USER_PUB_KEY,
                          0)
        tx1.date = date(2011, 12, 13)
        tx2 = Transaction(VERSION, Transaction.GUZI_CREATE, NEW_USER_PUB_KEY,
                          0)
        tx2.date = date(2012, 11, 14)
        tx3 = Transaction(VERSION, Transaction.GUZI_CREATE, NEW_USER_PUB_KEY,
                          0)
        tx3.date = date(2013, 10, 15)

        # Act
        block.add_transactions([tx1, tx2, tx3])
        result = block.find_transaction(Transaction.GUZI_CREATE,
                                        date(2014, 11, 14))

        # Assert
        assert result is None
    def test_compute_merkle_root_2_tx(self):
        """
        If there are 2 transactions, merkle root should be :
        hash(hash0 + hash1)
        """
        # Arrange
        tx0 = Transaction(VERSION, Transaction.GUZI_CREATE, NEW_USER_PUB_KEY,
                          0)
        tx1 = Transaction(VERSION, Transaction.GUZI_CREATE, NEW_USER_PUB_KEY,
                          0)
        block = Block()
        block.add_transactions([tx0, tx1])

        expected_merkle_root = guzi_hash(tx0.to_hash() + tx1.to_hash())

        # Act
        result = block.compute_merkle_root()

        # Assert
        assert result == expected_merkle_root
    def test_raise_exception_if_too_much_transactions_in_last_block(self):

        # Arrange
        block = Block()
        tx = Transaction(VERSION, Transaction.PAYMENT, NEW_USER_PUB_KEY, 0)

        for i in range(MAX_TX_IN_BLOCK):
            block.add_transaction(
                Transaction(VERSION, Transaction.PAYMENT, NEW_USER_PUB_KEY, i))

        # Act
        with pytest.raises(FullBlockError):
            block.add_transaction(tx)
    def test_shouldnt_add_existing_transaction_twice(self):

        # Arrange
        block = Block()
        tx = Transaction(VERSION, Transaction.GUZI_CREATE, EMPTY_HASH, 0)

        # Act
        block.add_transaction(tx)
        block.add_transaction(tx)
        block.add_transaction(tx)

        # Assert
        assert len(block.transactions) == 1