Search in sources :

Example 1 with ConsensusNode

use of com.github.dedis.popstellar.model.objects.ConsensusNode 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 2 with ConsensusNode

use of com.github.dedis.popstellar.model.objects.ConsensusNode 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 3 with ConsensusNode

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

the class ElectionStartFragment method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ElectionStartFragmentBinding binding = ElectionStartFragmentBinding.inflate(inflater, container, false);
    electionStart = binding.electionStart;
    electionStatus = binding.electionStatus;
    LaoDetailViewModel mLaoDetailViewModel = LaoDetailActivity.obtainViewModel(requireActivity());
    Election election = mLaoDetailViewModel.getCurrentElection();
    if (election == null) {
        Log.e(TAG, "The current election of the LaoDetailViewModel is null");
        return null;
    }
    String scheduledDate = dateFormat.format(new Date(election.getStartTimestampInMillis()));
    String electionId = election.getId();
    String instanceId = ElectInstance.generateConsensusId("election", electionId, "state");
    binding.electionTitle.setText(getString(R.string.election_start_title, election.getName()));
    electionStatus.setText(R.string.waiting_scheduled_time);
    electionStart.setText(getString(R.string.election_scheduled, scheduledDate));
    electionStart.setEnabled(false);
    setupTimerUpdate(election);
    setupButtonListeners(binding, mLaoDetailViewModel, electionId);
    Lao lao = mLaoDetailViewModel.getCurrentLaoValue();
    List<ConsensusNode> nodes = lao.getNodes();
    ownNode = lao.getNode(mLaoDetailViewModel.getPublicKey());
    if (ownNode == null) {
        // Only possible if the user wasn't an acceptor, but shouldn't have access to this fragment
        Log.e(TAG, "Couldn't find the Node with public key : " + mLaoDetailViewModel.getPublicKey());
        throw new IllegalStateException("Only acceptors are allowed to access ElectionStartFragment");
    }
    NodesAcceptorAdapter adapter = new NodesAcceptorAdapter(nodes, ownNode, instanceId, getViewLifecycleOwner(), mLaoDetailViewModel);
    GridView gridView = binding.nodesGrid;
    gridView.setAdapter(adapter);
    if (isElectionStartTimePassed(election)) {
        updateStartAndStatus(nodes, election, instanceId);
    }
    mLaoDetailViewModel.getNodes().observe(getViewLifecycleOwner(), consensusNodes -> {
        Log.d(TAG, "got an update for nodes : " + consensusNodes);
        adapter.setList(consensusNodes);
        if (isElectionStartTimePassed(election)) {
            updateStartAndStatus(consensusNodes, election, instanceId);
        }
    });
    binding.setLifecycleOwner(getViewLifecycleOwner());
    return binding.getRoot();
}
Also used : LaoDetailViewModel(com.github.dedis.popstellar.ui.detail.LaoDetailViewModel) ElectionStartFragmentBinding(com.github.dedis.popstellar.databinding.ElectionStartFragmentBinding) Lao(com.github.dedis.popstellar.model.objects.Lao) ConsensusNode(com.github.dedis.popstellar.model.objects.ConsensusNode) GridView(android.widget.GridView) Election(com.github.dedis.popstellar.model.objects.Election) Date(java.util.Date)

Example 4 with ConsensusNode

use of com.github.dedis.popstellar.model.objects.ConsensusNode 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)

Example 5 with ConsensusNode

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

the class ConsensusHandlerTest method handleConsensusElectTest.

// handle an elect from node2
// This should add an attempt from node2 to start a consensus (in this case for starting an
// election)
private void handleConsensusElectTest() throws DataHandlingException {
    messageHandler.handleMessage(laoRepository, messageSender, CONSENSUS_CHANNEL, electMsg);
    Optional<ElectInstance> electInstanceOpt = lao.getElectInstance(electMsg.getMessageId());
    assertTrue(electInstanceOpt.isPresent());
    ElectInstance electInstance = electInstanceOpt.get();
    assertEquals(electMsg.getMessageId(), electInstance.getMessageId());
    assertEquals(NODE_2, electInstance.getProposer());
    assertEquals(CONSENSUS_CHANNEL, electInstance.getChannel());
    assertEquals(CREATION_TIME, electInstance.getCreation());
    assertEquals(VALUE, electInstance.getValue());
    assertEquals(KEY, electInstance.getKey());
    assertTrue(electInstance.getAcceptorsToMessageId().isEmpty());
    assertEquals(Sets.newSet(ORGANIZER, NODE_2, NODE_3), electInstance.getNodes());
    Map<MessageID, ElectInstance> messageIdToElectInstance = lao.getMessageIdToElectInstance();
    assertEquals(1, messageIdToElectInstance.size());
    assertEquals(electInstance, messageIdToElectInstance.get(electInstance.getMessageId()));
    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);
    Optional<ElectInstance> organizerElectInstance = organizer.getLastElectInstance(INSTANCE_ID);
    Optional<ElectInstance> node2ElectInstance = node2.getLastElectInstance(INSTANCE_ID);
    Optional<ElectInstance> node3ElectInstance = node3.getLastElectInstance(INSTANCE_ID);
    assertEquals(Optional.empty(), organizerElectInstance);
    assertTrue(node2ElectInstance.isPresent());
    assertEquals(electInstance, node2ElectInstance.get());
    assertEquals(Optional.empty(), node3ElectInstance);
}
Also used : ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) ConsensusNode(com.github.dedis.popstellar.model.objects.ConsensusNode) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Aggregations

ConsensusNode (com.github.dedis.popstellar.model.objects.ConsensusNode)5 ElectInstance (com.github.dedis.popstellar.model.objects.ElectInstance)4 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)2 MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)2 LaoDetailViewModel (com.github.dedis.popstellar.ui.detail.LaoDetailViewModel)2 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 BaseAdapter (android.widget.BaseAdapter)1 GridView (android.widget.GridView)1 DataBindingUtil (androidx.databinding.DataBindingUtil)1 LifecycleOwner (androidx.lifecycle.LifecycleOwner)1 ConsensusNodeLayoutBinding (com.github.dedis.popstellar.databinding.ConsensusNodeLayoutBinding)1 ElectionStartFragmentBinding (com.github.dedis.popstellar.databinding.ElectionStartFragmentBinding)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 State (com.github.dedis.popstellar.model.objects.ElectInstance.State)1 Election (com.github.dedis.popstellar.model.objects.Election)1 Lao (com.github.dedis.popstellar.model.objects.Lao)1 PublicKey (com.github.dedis.popstellar.model.objects.security.PublicKey)1