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