def test_update_shared_data_on_new_view_accepted_no_batches(
        internal_bus, view_change_service, is_master):
    # TODO: Need to decide on how we handle this case
    if not is_master:
        return

    view_change_service._data.waiting_for_new_view = False
    view_change_service._data.view_no = 1
    view_change_service._data.primary_name = "Alpha"
    view_change_service._data.primaries = ["Alpha", "Beta"]
    view_change_service._data.prev_view_prepare_cert = 1
    old_data = copy_shared_data(view_change_service._data)

    new_view = create_new_view(initial_view_no=3, stable_cp=200, batches=[])
    internal_bus.send(
        NewViewAccepted(view_no=4,
                        view_changes=new_view.viewChanges,
                        checkpoint=new_view.checkpoint,
                        batches=new_view.batches))

    new_data = copy_shared_data(view_change_service._data)
    # For now prev_view_prepare_cert is set on finish_view_change stage
    assert view_change_service._data.prev_view_prepare_cert == 1
    check_service_changed_only_owned_fields_in_shared_data(
        ViewChangeService, old_data, new_data)
Beispiel #2
0
def test_update_shared_data_on_new_view_accepted(internal_bus,
                                                 checkpoint_service,
                                                 checkpoints,
                                                 stable_checkpoint,
                                                 checkpoints_result):
    old_data = copy_shared_data(checkpoint_service._data)
    checkpoint_service._data.checkpoints.update(checkpoints)
    checkpoint_service._data.stable_checkpoint = stable_checkpoint
    checkpoint_service._data.low_watermark = stable_checkpoint
    checkpoint_service._data.high_watermark = checkpoint_service._data.low_watermark + 300

    initial_view_no = 3
    new_view = create_new_view(initial_view_no=initial_view_no, stable_cp=200)
    internal_bus.send(
        NewViewAccepted(view_no=initial_view_no + 1,
                        view_changes=new_view.viewChanges,
                        checkpoint=new_view.checkpoint,
                        batches=new_view.batches))

    new_data = copy_shared_data(checkpoint_service._data)
    check_service_changed_only_owned_fields_in_shared_data(
        CheckpointService, old_data, new_data)

    assert list(checkpoint_service._data.checkpoints) == checkpoints_result
    assert checkpoint_service._data.stable_checkpoint == 200
    assert checkpoint_service._data.low_watermark == 200
    assert checkpoint_service._data.high_watermark == checkpoint_service._data.low_watermark + 300
def test_update_shared_data_on_new_view_accepted(internal_bus, checkpoint_service,
                                                 checkpoints, stable_checkpoint,
                                                 checkpoints_result, stable_cp_result, is_master):
    # TODO: Need to decide on how we handle this case
    if not is_master:
        return

    old_data = copy_shared_data(checkpoint_service._data)
    checkpoint_service._data.checkpoints.clear()
    checkpoint_service._data.checkpoints.update(checkpoints)
    checkpoint_service._data.stable_checkpoint = stable_checkpoint
    checkpoint_service._data.low_watermark = stable_checkpoint
    checkpoint_service._data.high_watermark = checkpoint_service._data.low_watermark + 300

    initial_view_no = 3
    new_view = create_new_view(initial_view_no=initial_view_no, stable_cp=200)
    internal_bus.send(NewViewAccepted(view_no=initial_view_no + 1,
                                      view_changes=new_view.viewChanges,
                                      checkpoint=new_view.checkpoint,
                                      batches=new_view.batches))

    new_data = copy_shared_data(checkpoint_service._data)
    check_service_changed_only_owned_fields_in_shared_data(CheckpointService, old_data, new_data)

    assert list(checkpoint_service._data.checkpoints) == checkpoints_result
    assert checkpoint_service._data.stable_checkpoint == stable_cp_result
    assert checkpoint_service._data.low_watermark == stable_cp_result
    assert checkpoint_service._data.high_watermark == checkpoint_service._data.low_watermark + 300
def test_view_change_finished_is_sent_by_non_primary_once_view_change_certificate_is_reached_and_new_view_from_primary(
        internal_bus, external_bus, validators, primary,
        view_change_service_builder, initial_view_no, some_item, is_master):
    # TODO: Need to decide on how we handle this case
    if not is_master:
        return

    handler = Mock()
    internal_bus.subscribe(NewViewAccepted, handler)

    next_view_no = initial_view_no + 1
    primary_name = primary(next_view_no)
    non_primary_name = some_item(validators, exclude=[primary_name])
    service = view_change_service_builder(non_primary_name)
    vc = create_view_change(initial_view_no)
    service._data.preprepared = vc.preprepared
    service._data.prepared = vc.prepared
    service._data.stable_checkpoint = vc.stableCheckpoint
    service._data.checkpoints = vc.checkpoints
    old_data = copy_shared_data(service._data)

    # start view change
    internal_bus.send(NeedViewChange())
    external_bus.sent_messages.clear()

    # receive quorum of ViewChanges and ViewChangeAcks
    non_primaries = [item for item in validators if item != primary_name]
    non_primaries = random.sample(non_primaries,
                                  service._data.quorums.view_change.value)
    new_view = create_new_view_from_vc(vc, non_primaries)
    for vc_frm in non_primaries:
        external_bus.process_incoming(
            vc, generateName(vc_frm, service._data.inst_id))
        for ack, ack_frm in create_view_change_acks(vc, vc_frm, non_primaries):
            external_bus.process_incoming(
                ack, generateName(ack_frm, service._data.inst_id))

        # check that NewViewAccepted hasn't been sent if NewView is from non-primary
        external_bus.process_incoming(
            new_view, generateName(non_primary_name, service._data.inst_id))
    handler.assert_not_called()
    assert service._data.view_no == initial_view_no + 1
    assert service._data.waiting_for_new_view

    # check that NewViewAccepted has been sent if NewView is from primary
    external_bus.process_incoming(
        new_view, generateName(primary_name, service._data.inst_id))
    expected_finish_vc = NewViewAccepted(view_no=initial_view_no + 1,
                                         view_changes=new_view.viewChanges,
                                         checkpoint=new_view.checkpoint,
                                         batches=new_view.batches)
    handler.assert_called_with(expected_finish_vc)

    # check that shared data is updated
    new_data = copy_shared_data(service._data)
    check_service_changed_only_owned_fields_in_shared_data(
        ViewChangeService, old_data, new_data)
    assert service._data.view_no == initial_view_no + 1
    assert not service._data.waiting_for_new_view
    def _finish_view_change(self):
        # Update shared data
        self._data.waiting_for_new_view = False

        # send message to other services
        self._bus.send(
            NewViewAccepted(view_no=self._new_view.viewNo,
                            view_changes=self._new_view.viewChanges,
                            checkpoint=self._new_view.checkpoint,
                            batches=self._new_view.batches))
Beispiel #6
0
    def _finish_view_change(self):
        # Update shared data
        self._data.waiting_for_new_view = False
        self._data.prev_view_prepare_cert = self._new_view.batches[-1].pp_seq_no if self._new_view.batches else None

        # Cancel View Change timeout task
        self._resend_inst_change_timer.stop()

        # send message to other services
        self._bus.send(NewViewAccepted(view_no=self._new_view.viewNo,
                                       view_changes=self._new_view.viewChanges,
                                       checkpoint=self._new_view.checkpoint,
                                       batches=self._new_view.batches))
Beispiel #7
0
def test_do_nothing_on_new_view_accepted(internal_bus, orderer):
    orderer._data.preprepared = create_batches(view_no=0)
    orderer._data.prepared = create_batches(view_no=0)
    old_data = copy_shared_data(orderer._data)

    initial_view_no = 3
    new_view = create_new_view(initial_view_no=initial_view_no, stable_cp=200)
    internal_bus.send(NewViewAccepted(view_no=initial_view_no + 1,
                                      view_changes=new_view.viewChanges,
                                      checkpoint=new_view.checkpoint,
                                      batches=new_view.batches))

    new_data = copy_shared_data(orderer._data)
    assert old_data == new_data
def test_do_nothing_on_new_view_accepted(internal_bus, view_change_service):
    view_change_service._data.waiting_for_new_view = False
    view_change_service._data.view_no = 1
    view_change_service._data.primary_name = "Alpha"
    view_change_service._data.primaries = ["Alpha", "Beta"]
    old_data = copy_shared_data(view_change_service._data)

    new_view = create_new_view(initial_view_no=3, stable_cp=200)
    internal_bus.send(
        NewViewAccepted(view_no=4,
                        view_changes=new_view.viewChanges,
                        checkpoint=new_view.checkpoint,
                        batches=new_view.batches))

    new_data = copy_shared_data(view_change_service._data)
    assert old_data == new_data
Beispiel #9
0
    def _finish_view_change(self):
        logger.info("{} finished view change to view {}. Primaries: {}".format(
            self._data.name, self._data.view_no, self._data.primaries))
        # Update shared data
        self._data.waiting_for_new_view = False
        self._data.prev_view_prepare_cert = self._data.new_view.batches[-1].pp_seq_no \
            if self._data.new_view.batches else 0

        # Cancel View Change timeout task
        self._resend_inst_change_timer.stop()
        # send message to other services
        self._bus.send(
            NewViewAccepted(view_no=self._data.new_view.viewNo,
                            view_changes=self._data.new_view.viewChanges,
                            checkpoint=self._data.new_view.checkpoint,
                            batches=self._data.new_view.batches))
        self.last_completed_view_no = self._data.view_no
Beispiel #10
0
def test_view_change_finished_sends_new_view_checkpoint_applied(
        internal_bus, checkpoint_service):
    handler = Mock()
    internal_bus.subscribe(NewViewCheckpointsApplied, handler)

    initial_view_no = 3
    new_view = create_new_view(initial_view_no=initial_view_no, stable_cp=200)
    internal_bus.send(
        NewViewAccepted(view_no=initial_view_no + 1,
                        view_changes=new_view.viewChanges,
                        checkpoint=new_view.checkpoint,
                        batches=new_view.batches))
    expected_apply_new_view = NewViewCheckpointsApplied(
        view_no=initial_view_no + 1,
        view_changes=new_view.viewChanges,
        checkpoint=new_view.checkpoint,
        batches=new_view.batches)

    handler.assert_called_once_with(expected_apply_new_view)
    def _finish_view_change(self):
        # Update shared data
        self._data.waiting_for_new_view = False
        self._data.prev_view_prepare_cert = self._data.new_view.batches[-1].pp_seq_no \
            if self._data.new_view.batches else self._data.new_view.checkpoint.seqNoEnd
        if f.PRIMARY.nm in self._data.new_view:
            self._data.primary_name = generateName(self._data.new_view.primary,
                                                   self._data.inst_id)

        logger.info("{} finished view change to view {}. Master Primary: {}".format(self._data.name,
                                                                                    self._data.view_no,
                                                                                    self._data.primary_name))
        # Cancel View Change timeout task
        self._resend_inst_change_timer.stop()
        # send message to other services
        self._bus.send(NewViewAccepted(view_no=self._data.new_view.viewNo,
                                       view_changes=self._data.new_view.viewChanges,
                                       checkpoint=self._data.new_view.checkpoint,
                                       batches=self._data.new_view.batches))
        self.last_completed_view_no = self._data.view_no
def test_view_change_finished_is_sent_by_primary_once_view_change_certificate_is_reached(
        internal_bus, validators, primary, view_change_service_builder,
        initial_view_no):
    handler = Mock()
    internal_bus.subscribe(NewViewAccepted, handler)

    primary_name = primary(initial_view_no + 1)
    service = view_change_service_builder(primary_name)

    # start view change
    internal_bus.send(NeedViewChange())
    service._network.sent_messages.clear()
    old_data = copy_shared_data(service._data)

    # receive quorum of ViewChanges and ViewChangeAcks
    non_primaries = [item for item in validators if item != primary_name]
    non_primaries = random.sample(non_primaries,
                                  service._data.quorums.view_change.value)
    vc = create_view_change(initial_view_no)
    new_view = create_new_view_from_vc(vc, non_primaries)
    for vc_frm in non_primaries:
        service._network.process_incoming(vc, vc_frm)
        for ack, ack_frm in create_view_change_acks(vc, vc_frm, non_primaries):
            service._network.process_incoming(ack, ack_frm)

    # check that NewViewAccepted has been sent
    expected_finish_vc = NewViewAccepted(view_no=initial_view_no + 1,
                                         view_changes=new_view.viewChanges,
                                         checkpoint=new_view.checkpoint,
                                         batches=new_view.batches)
    handler.assert_called_with(expected_finish_vc)

    # check that shared data is updated
    new_data = copy_shared_data(service._data)
    check_service_changed_only_owned_fields_in_shared_data(
        ViewChangeService, old_data, new_data)
    assert service._data.view_no == initial_view_no + 1
    assert not service._data.waiting_for_new_view
def test_view_change_finished_sends_new_view_checkpoint_applied(internal_bus, checkpoint_service, is_master):
    # TODO: Need to decide on how we handle this case
    if not is_master:
        return

    handler = Mock()
    internal_bus.subscribe(NewViewCheckpointsApplied, handler)

    initial_view_no = 3
    checkpoint_service._data.checkpoints.clear()
    checkpoint_service._data.checkpoints.update(
        [Checkpoint(instId=0, viewNo=3, seqNoStart=0, seqNoEnd=200, digest=cp_digest(200))])
    new_view = create_new_view(initial_view_no=initial_view_no, stable_cp=200)
    internal_bus.send(NewViewAccepted(view_no=initial_view_no + 1,
                                      view_changes=new_view.viewChanges,
                                      checkpoint=new_view.checkpoint,
                                      batches=new_view.batches))
    expected_apply_new_view = NewViewCheckpointsApplied(view_no=initial_view_no + 1,
                                                        view_changes=new_view.viewChanges,
                                                        checkpoint=new_view.checkpoint,
                                                        batches=new_view.batches)

    handler.assert_called_once_with(expected_apply_new_view)