Search in sources :

Example 6 with ElectInstance

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

the class ConsensusHandler method handleLearn.

public static void handleLearn(HandlerContext context, ConsensusLearn consensusLearn) throws DataHandlingException {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    Lao lao = laoRepository.getLaoByChannel(channel);
    Optional<ElectInstance> electInstanceOpt = lao.getElectInstance(consensusLearn.getMessageId());
    if (!electInstanceOpt.isPresent()) {
        Log.w(TAG, "learn for invalid messageId : " + consensusLearn.getMessageId());
        throw new InvalidMessageIdException(consensusLearn, consensusLearn.getMessageId());
    }
    ElectInstance electInstance = electInstanceOpt.get();
    if (consensusLearn.getLearnValue().isDecision()) {
        electInstance.setState(ElectInstance.State.ACCEPTED);
    }
    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 7 with ElectInstance

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

the class ConsensusHandler method handleElect.

/**
 * Process an Elect message.
 *
 * @param context the HandlerContext of the message
 * @param consensusElect the data of the message that was received
 */
public static void handleElect(HandlerContext context, ConsensusElect consensusElect) {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    MessageID messageId = context.getMessageId();
    PublicKey senderPk = context.getSenderPk();
    Lao lao = laoRepository.getLaoByChannel(channel);
    Set<PublicKey> nodes = new HashSet<>(lao.getWitnesses());
    nodes.add(lao.getOrganizer());
    ElectInstance electInstance = new ElectInstance(messageId, channel, senderPk, nodes, consensusElect);
    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) Lao(com.github.dedis.popstellar.model.objects.Lao) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID) HashSet(java.util.HashSet)

Example 8 with ElectInstance

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

the class ConsensusHandlerTest method handleConsensusLearnTest.

// handle a learn 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 handleConsensusLearnTest() throws DataHandlingException {
    ConsensusLearn learn = new ConsensusLearn(INSTANCE_ID, messageId, CREATION_TIME, true, Collections.emptyList());
    MessageGeneral learnMsg = getMsg(NODE_3_KEY, learn);
    messageHandler.handleMessage(laoRepository, messageSender, CONSENSUS_CHANNEL, learnMsg);
    Optional<ElectInstance> electInstanceOpt = lao.getElectInstance(electMsg.getMessageId());
    assertTrue(electInstanceOpt.isPresent());
    ElectInstance electInstance = electInstanceOpt.get();
    assertEquals(ACCEPTED, electInstance.getState());
}
Also used : ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) ConsensusLearn(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusLearn)

Example 9 with ElectInstance

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

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