Example #1
0
    def test_wrong_operations(self):
        operations = self.add_operations()
        blocks = self.add_blocks(operations)
        bc = get_blockchain()

        with patch.object(BlockChain, '_get_new_blocks', return_value=blocks):
            bc.update_blocks()

        with self.assertRaisesRegex(Block.VerifyError, "some of the new operations have been added to the block already"):
            blocks[5].extend_operations([operations[9]])

        illegal_operation = Operation(operations[7].get_rev(), 'illegal address', [])
        sign_object(self.public_keys[2], self.private_keys[2], illegal_operation)

        blocks[5].extend_operations([illegal_operation])
        blocks[5].mine()

        with self.assertRaisesRegex(Block.ChainError, "operations are creating tree inside the block"):
            blocks[5].put()

        blocks[2].extend_operations([operations[6]])
        blocks[2].mine()
        with self.assertRaisesRegex(Block.ChainError, "operation's previous_operation_rev is not pointing at "
                                                      "the last operation on current blockchain"):
            blocks[2].put()
Example #2
0
    def test_delete_blocks(self):
        blocks = self.add_blocks(self.add_operations())
        bc = get_blockchain()

        with patch.object(BlockChain, '_get_new_blocks', return_value=blocks):
            bc.update_blocks()

        with self.assertRaisesRegex(Block.ChainOperationBlockedError, "can't remove: blocked by another block"):
            blocks[4].remove()

        self.assertEqual(bc.max_depth, 5)
        self.assertEqual(bc.head, blocks[5].id)

        for block_to_remove, max_depth, heads in (
                (blocks[5], 5, [blocks[6].id]),
                (blocks[6], 4, [blocks[3].id, blocks[4].id]),
                (blocks[4], 4, [blocks[3].id]),
                (blocks[3], 3, [blocks[2].id]),
                (blocks[2], 2, [blocks[1].id]),
                (blocks[1], 1, [blocks[0].id]),
                (blocks[0], 0, [BlockRev().id])
        ):
            block_to_remove.remove()
            self.assertEqual(bc.max_depth, max_depth)
            self.assertIn(bc.head, heads)

        with patch.object(BlockChain, '_get_new_blocks', return_value=blocks):
            bc.update_blocks()

        self.assertEqual(bc.max_depth, 5)
        self.assertEqual(bc.head, blocks[5].id)

        self.assertCountEqual([op.uuid for op in blocks[0].operations + blocks[2].operations[1:2]],
                              Identifier.get_uuid_list())
Example #3
0
    def test_update_blocks(self):
        blocks = self.add_blocks(self.add_operations())
        bc = get_blockchain()

        with patch.object(BlockChain, '_get_new_blocks', return_value=blocks):
            bc.update_blocks()

        self.assertEqual(bc.max_depth, 5)
        self.assertEqual(bc.head, blocks[5].id)

        for block in blocks:
            self.assertEqual(bc.get(block.id).previous_id, block.previous_block_rev.id)
Example #4
0
    def test_multiple_update_blocks(self):
        blocks = self.add_blocks(self.add_operations())
        bc = get_blockchain()

        def patched_update_blocks(block_list):
            with patch.object(BlockChain, '_get_new_blocks', return_value=block_list):
                bc.update_blocks()

        for blocks_to_add, max_depth, head in (
                (blocks[0:2], 2, blocks[1].id),
                (blocks[2:3], 3, blocks[2].id),
                (blocks[4:5], 4, blocks[4].id),
                (blocks[3:4], 4, blocks[4].id),
                (blocks[5:6], 5, blocks[5].id),
                (blocks[6:7], 5, blocks[5].id)
        ):
            patched_update_blocks(blocks_to_add)
            self.assertEqual(bc.max_depth, max_depth)
            self.assertEqual(bc.head, head)
Example #5
0
def test():
    initialise_database('test_database_file')

    private_keys = [SigningKey.generate() for _ in range(3)]
    public_keys = [PublicKey.from_signing_key(private_key) for private_key in private_keys]
    uuids = [uuid4() for _ in range(3)]

    # BUILD    
    # blocks = add_blocks()
    #
    # for block in blocks:
    #     block.put()
    #
    # block_chain = BlockChain()
    #
    # block_chain_records_pattern = [
    #     BlockChain.Record(1, b'\x00'*32, [blocks[1].id]),
    #     BlockChain.Record(2, blocks[0].id, [blocks[2].id]),
    #     BlockChain.Record(3, blocks[1].id, sorted([blocks[3].id, blocks[4].id])),
    #     BlockChain.Record(4, blocks[2].id, [blocks[5].id]),
    #     BlockChain.Record(4, blocks[2].id, [blocks[6].id]),
    #     BlockChain.Record(5, blocks[3].id, []),
    #     BlockChain.Record(5, blocks[4].id, [])
    # ]
    #
    # for i in range(6):
    #     assertEqual(block_chain.get(blocks[i].id), block_chain_records_pattern[i])

    # UPDATE BLOCKS
    # blocks = add_blocks()
    # bc = get_blockchain()
    #
    # with patch.object(BlockChain, '_get_new_blocks', return_value=blocks):
    #     bc.update_blocks()
    #
    # assertEqual(bc.max_depth, 5)
    # assertEqual(bc.head, blocks[5].id)
    #
    # for block in blocks:
    #     assertEqual(bc.get(block.id).previous_id, block.previous_block_rev.id)

    # MULTIPLE UPDATE BLOCKS
    # blocks = add_blocks()
    # bc = get_blockchain()
    #
    # def patched_update_blocks(block_list):
    #     with patch.object(BlockChain, '_get_new_blocks', return_value=block_list):
    #         bc.update_blocks()
    #
    # for blocks_to_add, max_depth, head in (
    #         (blocks[0:2], 2, blocks[1].id),
    #         (blocks[2:3], 3, blocks[2].id),
    #         (blocks[4:5], 4, blocks[4].id),
    #         (blocks[3:4], 4, blocks[4].id),
    #         (blocks[5:6], 5, blocks[5].id),
    #         (blocks[6:7], 5, blocks[5].id)
    # ):
    #     patched_update_blocks(blocks_to_add)
    #     assertEqual(bc.max_depth, max_depth)
    #     assertEqual(bc.head, head)

    # DELETE

    blocks = add_blocks(uuids, private_keys, public_keys)
    bc = get_blockchain()

    with patch.object(BlockChain, '_get_new_blocks', return_value=blocks):
        bc.update_blocks()

    try:
        blocks[4].remove()
        raise AssertionError
    except Block.ChainOperationBlockedError:
        pass

    assert bc.max_depth == 5
    assert bc.head == blocks[5].id

    for block_to_remove, max_depth, heads in (
            (blocks[5], 5, [blocks[6].id]),
            (blocks[6], 4, [blocks[3].id, blocks[4].id]),
            (blocks[4], 4, [blocks[3].id]),
            (blocks[3], 3, [blocks[2].id]),
            (blocks[2], 2, [blocks[1].id]),
            (blocks[1], 1, [blocks[0].id]),
            (blocks[0], 0, [BlockRev().id])
    ):
        block_to_remove.remove()
        assert bc.max_depth == max_depth
        assert bc.head in heads

    with patch.object(BlockChain, '_get_new_blocks', return_value=blocks):
        bc.update_blocks()

    assert bc.max_depth == 5
    assert bc.head == blocks[5].id

    assert sorted([op.uuid for op in blocks[0].operations + blocks[2].operations[1:2]]) == sorted(
        Identifier.get_uuid_list())

    close_database()
    os.remove('test_database_file')