Search in sources :

Example 1 with MessageID

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

the class ElectionHandler method handleElectionSetup.

/**
 * Process an ElectionSetup message.
 *
 * @param context the HandlerContext of the message
 * @param electionSetup the message that was received
 */
public static void handleElectionSetup(HandlerContext context, ElectionSetup electionSetup) {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    MessageID messageId = context.getMessageId();
    if (channel.isLaoChannel()) {
        Lao lao = laoRepository.getLaoByChannel(channel);
        Log.d(TAG, "handleElectionSetup: channel " + channel + " name " + electionSetup.getName());
        Election election = new Election(lao.getId(), electionSetup.getCreation(), electionSetup.getName());
        election.setChannel(channel.subChannel(election.getId()));
        election.setElectionQuestions(electionSetup.getQuestions());
        election.setStart(electionSetup.getStartTime());
        election.setEnd(electionSetup.getEndTime());
        election.setEventState(OPENED);
        // Once the election is created, we subscribe to the election channel
        context.getMessageSender().subscribe(election.getChannel()).subscribe();
        Log.d(TAG, "election id " + election.getId());
        lao.updateElection(election.getId(), election);
        lao.updateWitnessMessage(messageId, electionSetupWitnessMessage(messageId, election));
    }
}
Also used : Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Lao(com.github.dedis.popstellar.model.objects.Lao) Election(com.github.dedis.popstellar.model.objects.Election) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 2 with MessageID

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

the class ElectionHandler method handleCastVote.

/**
 * Process a CastVote message.
 *
 * @param context the HandlerContext of the message
 * @param castVote the message that was received
 */
public static void handleCastVote(HandlerContext context, CastVote castVote) {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    MessageID messageId = context.getMessageId();
    PublicKey senderPk = context.getSenderPk();
    Log.d(TAG, "handleCastVote: channel " + channel);
    Lao lao = laoRepository.getLaoByChannel(channel);
    Election election = laoRepository.getElectionByChannel(channel);
    // Verify the vote was created before the end of the election or the election is not closed yet
    if (election.getEndTimestamp() >= castVote.getCreation() || election.getState() != CLOSED) {
        // Retrieve previous cast vote message stored for the given sender
        Optional<MessageID> previousMessageIdOption = election.getMessageMap().entrySet().stream().filter(entry -> senderPk.equals(entry.getValue())).map(Map.Entry::getKey).findFirst();
        // Retrieve the creation time of the previous cast vote, if doesn't exist replace with min
        // value
        long previousMessageCreation = previousMessageIdOption.map(s -> laoRepository.getMessageById().get(s)).map(MessageGeneral::getData).map(CastVote.class::cast).map(CastVote::getCreation).orElse(Long.MIN_VALUE);
        // Verify the current cast vote message is the last one received
        if (previousMessageCreation <= castVote.getCreation()) {
            election.putVotesBySender(senderPk, castVote.getVotes());
            election.putSenderByMessageId(senderPk, messageId);
            lao.updateElection(election.getId(), election);
        }
    }
}
Also used : PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) CastVote(com.github.dedis.popstellar.model.network.method.message.data.election.CastVote) Lao(com.github.dedis.popstellar.model.objects.Lao) Election(com.github.dedis.popstellar.model.objects.Election) Map(java.util.Map) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 3 with MessageID

use of com.github.dedis.popstellar.model.objects.security.MessageID 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 4 with MessageID

use of com.github.dedis.popstellar.model.objects.security.MessageID 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 5 with MessageID

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

the class ConsensusHandlerTest method handleConsensusDoNothingOnBackendMessageTest.

@Test
public void handleConsensusDoNothingOnBackendMessageTest() throws DataHandlingException {
    LAORepository mockLAORepository = mock(LAORepository.class);
    Map<MessageID, MessageGeneral> messageById = new HashMap<>();
    when(mockLAORepository.getMessageById()).thenReturn(messageById);
    ConsensusPrepare prepare = new ConsensusPrepare(INSTANCE_ID, messageId, CREATION_TIME, 3);
    ConsensusPromise promise = new ConsensusPromise(INSTANCE_ID, messageId, CREATION_TIME, 3, true, 2);
    ConsensusPropose propose = new ConsensusPropose(INSTANCE_ID, messageId, CREATION_TIME, 3, true, Collections.emptyList());
    ConsensusAccept accept = new ConsensusAccept(INSTANCE_ID, messageId, CREATION_TIME, 3, true);
    messageHandler.handleMessage(mockLAORepository, messageSender, CONSENSUS_CHANNEL, getMsg(ORGANIZER_KEY, prepare));
    messageHandler.handleMessage(mockLAORepository, messageSender, CONSENSUS_CHANNEL, getMsg(ORGANIZER_KEY, promise));
    messageHandler.handleMessage(mockLAORepository, messageSender, CONSENSUS_CHANNEL, getMsg(ORGANIZER_KEY, propose));
    messageHandler.handleMessage(mockLAORepository, messageSender, CONSENSUS_CHANNEL, getMsg(ORGANIZER_KEY, accept));
    // The handlers for prepare/promise/propose/accept should do nothing (call or update nothing)
    // because theses messages should only be handle in the backend server.
    verify(mockLAORepository, never()).getLaoByChannel(any(Channel.class));
    verify(mockLAORepository, never()).updateNodes(any(Channel.class));
}
Also used : MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) ConsensusAccept(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusAccept) HashMap(java.util.HashMap) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) ConsensusPropose(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusPropose) ConsensusPromise(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusPromise) ConsensusPrepare(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusPrepare) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID) Test(org.junit.Test)

Aggregations

MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)23 Channel (com.github.dedis.popstellar.model.objects.Channel)10 Lao (com.github.dedis.popstellar.model.objects.Lao)10 LAORepository (com.github.dedis.popstellar.repository.LAORepository)10 PublicKey (com.github.dedis.popstellar.model.objects.security.PublicKey)8 Test (org.junit.Test)7 Base64DataUtils.generateMessageID (com.github.dedis.popstellar.testutils.Base64DataUtils.generateMessageID)6 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)4 ElectInstance (com.github.dedis.popstellar.model.objects.ElectInstance)4 ConsensusElectAccept (com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElectAccept)3 CloseRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall)3 CreateRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall)3 OpenRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall)3 AddChirp (com.github.dedis.popstellar.model.network.method.message.data.socialmedia.AddChirp)3 DeleteChirp (com.github.dedis.popstellar.model.network.method.message.data.socialmedia.DeleteChirp)3 Chirp (com.github.dedis.popstellar.model.objects.Chirp)3 RollCall (com.github.dedis.popstellar.model.objects.RollCall)3 StateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)2 UpdateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)2 ConsensusNode (com.github.dedis.popstellar.model.objects.ConsensusNode)2