def test_smaller_target_count(self):
        """Tests that setting a new, smaller target count works correctly."""
        block_num = 122351345
        block_info = create_block_info(block_num=block_num)
        self._send_request(block=block_info, target_count=128)
        config = create_config(latest_block=block_num - 1)
        self._expect_config_get(config)

        prev_block_info = create_block_info(block_num=block_num - 1,
                                            header_signature="0" * 128,
                                            timestamp=int(time.time() - 3))
        self._expect_block_get(prev_block_info)

        deletes = [
            create_block_address(i)
            for i in range(config.oldest_block, config.latest_block - 128 + 1)
        ]
        self._expect_delete(deletes)

        self._expect_set({
            create_block_address(block_num):
            block_info,
            CONFIG_ADDRESS:
            create_config(latest_block=block_num,
                          oldest_block=block_num - 128,
                          target_count=128),
        })

        self._expect_response("OK")
    def _expect_block_get(self, block):
        received = self.validator.expect(
            self.factory.create_get_request(
                addresses=[create_block_address(block.block_num)]))

        self.validator.respond(
            self.factory.create_get_response({
                create_block_address(block.block_num):
                block.SerializeToString()
            }), received)
    def test_first_block_info(self):
        """Tests that the first block info is set correctly, without changing
        sync tolerance or target count."""

        block_info = create_block_info(block_num=1)
        self._send_request(block=block_info)
        self._expect_config_get()
        self._expect_set({
            create_block_address(1):
            block_info,
            CONFIG_ADDRESS:
            create_config(latest_block=1, oldest_block=1),
        })
        self._expect_response("OK")
    def test_new_sync_tolerance(self):
        """Tests that setting a new sync tolerance works correctly."""
        block_num = 163134613422
        block_info = create_block_info(block_num=block_num)
        self._send_request(block=block_info, sync_tolerance=450)
        self._expect_config_get(create_config(latest_block=block_num - 1))

        prev_block_info = create_block_info(block_num=block_num - 1,
                                            header_signature="0" * 128,
                                            timestamp=int(time.time() - 3))
        self._expect_block_get(prev_block_info)

        self._expect_delete([
            create_block_address(block_num - 1 - DEFAULT_TARGET_COUNT),
        ])

        self._expect_set({
            create_block_address(block_num):
            block_info,
            CONFIG_ADDRESS:
            create_config(latest_block=block_num, sync_tolerance=450),
        })

        self._expect_response("OK")
    def test_new_block_info(self):
        """Tests that a new block info is set correctly, without changing
        sync tolerance or target count."""

        block_num = 8923521
        block_info = create_block_info(block_num=block_num)
        self._send_request(block=block_info)
        self._expect_config_get(create_config(latest_block=block_num - 1))

        prev_block_info = create_block_info(block_num=block_num - 1,
                                            header_signature="0" * 128,
                                            timestamp=int(time.time() - 3))
        self._expect_block_get(prev_block_info)

        self._expect_delete([
            create_block_address(block_num - 1 - DEFAULT_TARGET_COUNT),
        ])

        self._expect_set({
            create_block_address(block_num): block_info,
            CONFIG_ADDRESS: create_config(latest_block=block_num),
        })

        self._expect_response("OK")
    def test_bigger_target_count(self):
        """Tests that setting a new, bigger target count works correctly."""
        block_num = 122351345
        block_info = create_block_info(block_num=block_num)
        self._send_request(block=block_info, target_count=512)
        config = create_config(latest_block=block_num - 1)
        self._expect_config_get(config)

        prev_block_info = create_block_info(block_num=block_num - 1,
                                            header_signature="0" * 128,
                                            timestamp=int(time.time() - 3))
        self._expect_block_get(prev_block_info)

        self._expect_set({
            create_block_address(block_num):
            block_info,
            CONFIG_ADDRESS:
            create_config(latest_block=block_num,
                          oldest_block=config.oldest_block,
                          target_count=512),
        })

        self._expect_response("OK")
def get_block_info(block_num):
    bi = block_info_pb2.BlockInfo()
    bi.ParseFromString(get_state(create_block_address(block_num)))
    return bi