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));
}
}
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);
}
}
}
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());
}
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);
}
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));
}
Aggregations