Search in sources :

Example 1 with ElectInstance

use of com.github.dedis.popstellar.model.objects.ElectInstance in project popstellar by dedis.

the class ConsensusHandler method handleElectAccept.

public static void handleElectAccept(HandlerContext context, ConsensusElectAccept consensusElectAccept) throws DataHandlingException {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    MessageID messageId = context.getMessageId();
    PublicKey senderPk = context.getSenderPk();
    Lao lao = laoRepository.getLaoByChannel(channel);
    Optional<ElectInstance> electInstanceOpt = lao.getElectInstance(consensusElectAccept.getMessageId());
    if (!electInstanceOpt.isPresent()) {
        Log.w(TAG, "elect_accept for invalid messageId : " + consensusElectAccept.getMessageId());
        throw new InvalidMessageIdException(consensusElectAccept, consensusElectAccept.getMessageId());
    }
    ElectInstance electInstance = electInstanceOpt.get();
    electInstance.addElectAccept(senderPk, messageId, consensusElectAccept);
    lao.updateElectInstance(electInstance);
    laoRepository.updateNodes(lao.getChannel());
}
Also used : ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) InvalidMessageIdException(com.github.dedis.popstellar.utility.error.InvalidMessageIdException) Lao(com.github.dedis.popstellar.model.objects.Lao) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 2 with ElectInstance

use of com.github.dedis.popstellar.model.objects.ElectInstance in project popstellar by dedis.

the class ConsensusHandler method handleConsensusFailure.

public static void handleConsensusFailure(HandlerContext context, ConsensusFailure failure) throws InvalidMessageIdException {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    Lao lao = laoRepository.getLaoByChannel(channel);
    Optional<ElectInstance> electInstanceOpt = lao.getElectInstance(failure.getMessageId());
    if (!electInstanceOpt.isPresent()) {
        Log.w(TAG, "Failure for invalid messageId : " + failure.getMessageId());
        throw new InvalidMessageIdException(failure, failure.getMessageId());
    }
    ElectInstance electInstance = electInstanceOpt.get();
    electInstance.setState(ElectInstance.State.FAILED);
    lao.updateElectInstance(electInstance);
    laoRepository.updateNodes(lao.getChannel());
}
Also used : ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) InvalidMessageIdException(com.github.dedis.popstellar.utility.error.InvalidMessageIdException) Lao(com.github.dedis.popstellar.model.objects.Lao)

Example 3 with ElectInstance

use of com.github.dedis.popstellar.model.objects.ElectInstance in project popstellar by dedis.

the class ConsensusHandlerTest method handleConsensusElectAcceptTest.

// handle an electAccept from node3 for the elect of node2
// This test need be run after the elect message was handled, else the messageId would be invalid
private void handleConsensusElectAcceptTest() throws DataHandlingException {
    ConsensusElectAccept electAccept = new ConsensusElectAccept(INSTANCE_ID, messageId, true);
    MessageGeneral electAcceptMsg = getMsg(NODE_3_KEY, electAccept);
    messageHandler.handleMessage(laoRepository, messageSender, CONSENSUS_CHANNEL, electAcceptMsg);
    Optional<ElectInstance> electInstanceOpt = lao.getElectInstance(electMsg.getMessageId());
    assertTrue(electInstanceOpt.isPresent());
    ElectInstance electInstance = electInstanceOpt.get();
    Map<PublicKey, MessageID> acceptorsToMessageId = electInstance.getAcceptorsToMessageId();
    assertEquals(1, acceptorsToMessageId.size());
    assertEquals(electAcceptMsg.getMessageId(), acceptorsToMessageId.get(NODE_3));
    assertEquals(3, lao.getNodes().size());
    ConsensusNode organizer = lao.getNode(ORGANIZER);
    ConsensusNode node2 = lao.getNode(NODE_2);
    ConsensusNode node3 = lao.getNode(NODE_3);
    assertNotNull(organizer);
    assertNotNull(node2);
    assertNotNull(node3);
    Set<MessageID> organizerAcceptedMsg = organizer.getAcceptedMessageIds();
    Set<MessageID> node2AcceptedMsg = node2.getAcceptedMessageIds();
    Set<MessageID> node3AcceptedMsg = node3.getAcceptedMessageIds();
    assertTrue(organizerAcceptedMsg.isEmpty());
    assertTrue(node2AcceptedMsg.isEmpty());
    assertEquals(Sets.newSet(electMsg.getMessageId()), node3AcceptedMsg);
}
Also used : ConsensusElectAccept(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElectAccept) ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) ConsensusNode(com.github.dedis.popstellar.model.objects.ConsensusNode) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 4 with ElectInstance

use of com.github.dedis.popstellar.model.objects.ElectInstance in project popstellar by dedis.

the class ConsensusHandlerTest method handleConsensusFailure.

@Test
public void handleConsensusFailure() throws DataHandlingException {
    // handle an elect from node2 then handle a failure for this elect
    // the state of the node2 for this instanceId should be FAILED
    ConsensusFailure failure = new ConsensusFailure(INSTANCE_ID, messageId, CREATION_TIME);
    MessageGeneral failureMsg = getMsg(ORGANIZER_KEY, failure);
    messageHandler.handleMessage(laoRepository, messageSender, CONSENSUS_CHANNEL, electMsg);
    messageHandler.handleMessage(laoRepository, messageSender, CONSENSUS_CHANNEL, failureMsg);
    Optional<ElectInstance> electInstanceOpt = lao.getElectInstance(electMsg.getMessageId());
    assertTrue(electInstanceOpt.isPresent());
    ElectInstance electInstance = electInstanceOpt.get();
    assertEquals(FAILED, electInstance.getState());
    ConsensusNode node2 = lao.getNode(NODE_2);
    assertNotNull(node2);
    assertEquals(FAILED, node2.getState(INSTANCE_ID));
}
Also used : ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) ConsensusFailure(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusFailure) ConsensusNode(com.github.dedis.popstellar.model.objects.ConsensusNode) Test(org.junit.Test)

Example 5 with ElectInstance

use of com.github.dedis.popstellar.model.objects.ElectInstance in project popstellar by dedis.

the class NodesAcceptorAdapter method getView.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ConsensusNodeLayoutBinding binding;
    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        binding = ConsensusNodeLayoutBinding.inflate(inflater);
    } else {
        binding = DataBindingUtil.getBinding(convertView);
    }
    if (binding == null) {
        throw new IllegalStateException("Binding could not be find in the view");
    }
    ConsensusNode node = getItem(position);
    Optional<ElectInstance> lastElectInstance = node.getLastElectInstance(instanceId);
    State state = node.getState(instanceId);
    boolean alreadyAccepted = lastElectInstance.map(ElectInstance::getMessageId).map(ownNode.getAcceptedMessageIds()::contains).orElse(false);
    String text = "";
    switch(state) {
        case FAILED:
            text = "Start Failed\n";
            break;
        case STARTING:
            text = "Approve Start by\n";
            break;
        case WAITING:
            text = "Waiting\n";
            break;
        case ACCEPTED:
            text = "Started by\n";
    }
    text += node.getPublicKey().getEncoded();
    binding.nodeButton.setText(text);
    binding.nodeButton.setEnabled(state == State.STARTING && !alreadyAccepted);
    lastElectInstance.ifPresent(electInstance -> binding.nodeButton.setOnClickListener(clicked -> laoDetailViewModel.sendConsensusElectAccept(electInstance, true)));
    binding.setLifecycleOwner(lifecycleOwner);
    binding.executePendingBindings();
    return binding.getRoot();
}
Also used : ConsensusNodeLayoutBinding(com.github.dedis.popstellar.databinding.ConsensusNodeLayoutBinding) LayoutInflater(android.view.LayoutInflater) ConsensusNode(com.github.dedis.popstellar.model.objects.ConsensusNode) ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) ViewGroup(android.view.ViewGroup) LaoDetailViewModel(com.github.dedis.popstellar.ui.detail.LaoDetailViewModel) List(java.util.List) BaseAdapter(android.widget.BaseAdapter) State(com.github.dedis.popstellar.model.objects.ElectInstance.State) LifecycleOwner(androidx.lifecycle.LifecycleOwner) View(android.view.View) Optional(java.util.Optional) DataBindingUtil(androidx.databinding.DataBindingUtil) ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) State(com.github.dedis.popstellar.model.objects.ElectInstance.State) LayoutInflater(android.view.LayoutInflater) ConsensusNode(com.github.dedis.popstellar.model.objects.ConsensusNode) ConsensusNodeLayoutBinding(com.github.dedis.popstellar.databinding.ConsensusNodeLayoutBinding)

Aggregations

ElectInstance (com.github.dedis.popstellar.model.objects.ElectInstance)9 Channel (com.github.dedis.popstellar.model.objects.Channel)4 ConsensusNode (com.github.dedis.popstellar.model.objects.ConsensusNode)4 Lao (com.github.dedis.popstellar.model.objects.Lao)4 MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)4 LAORepository (com.github.dedis.popstellar.repository.LAORepository)4 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)3 PublicKey (com.github.dedis.popstellar.model.objects.security.PublicKey)3 InvalidMessageIdException (com.github.dedis.popstellar.utility.error.InvalidMessageIdException)3 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 BaseAdapter (android.widget.BaseAdapter)1 DataBindingUtil (androidx.databinding.DataBindingUtil)1 LifecycleOwner (androidx.lifecycle.LifecycleOwner)1 ConsensusNodeLayoutBinding (com.github.dedis.popstellar.databinding.ConsensusNodeLayoutBinding)1 ConsensusElectAccept (com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElectAccept)1 ConsensusFailure (com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusFailure)1 ConsensusLearn (com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusLearn)1 State (com.github.dedis.popstellar.model.objects.ElectInstance.State)1