use of com.github.dedis.popstellar.model.objects.ElectInstance 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.ElectInstance in project popstellar by dedis.
the class ConsensusHandler method handleConsensusFailure.
public static void handleConsensusFailure(HandlerContext context, ConsensusFailure failure) throws InvalidMessageIdException {
LAORepository laoRepository = context.getLaoRepository();
Channel channel = context.getChannel();
Lao lao = laoRepository.getLaoByChannel(channel);
Optional<ElectInstance> electInstanceOpt = lao.getElectInstance(failure.getMessageId());
if (!electInstanceOpt.isPresent()) {
Log.w(TAG, "Failure for invalid messageId : " + failure.getMessageId());
throw new InvalidMessageIdException(failure, failure.getMessageId());
}
ElectInstance electInstance = electInstanceOpt.get();
electInstance.setState(ElectInstance.State.FAILED);
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 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.ElectInstance 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));
}
use of com.github.dedis.popstellar.model.objects.ElectInstance 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();
}
Aggregations