use of com.github.dedis.popstellar.model.objects.ConsensusNode 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.ConsensusNode 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.ConsensusNode in project popstellar by dedis.
the class ElectionStartFragment method onCreateView.
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ElectionStartFragmentBinding binding = ElectionStartFragmentBinding.inflate(inflater, container, false);
electionStart = binding.electionStart;
electionStatus = binding.electionStatus;
LaoDetailViewModel mLaoDetailViewModel = LaoDetailActivity.obtainViewModel(requireActivity());
Election election = mLaoDetailViewModel.getCurrentElection();
if (election == null) {
Log.e(TAG, "The current election of the LaoDetailViewModel is null");
return null;
}
String scheduledDate = dateFormat.format(new Date(election.getStartTimestampInMillis()));
String electionId = election.getId();
String instanceId = ElectInstance.generateConsensusId("election", electionId, "state");
binding.electionTitle.setText(getString(R.string.election_start_title, election.getName()));
electionStatus.setText(R.string.waiting_scheduled_time);
electionStart.setText(getString(R.string.election_scheduled, scheduledDate));
electionStart.setEnabled(false);
setupTimerUpdate(election);
setupButtonListeners(binding, mLaoDetailViewModel, electionId);
Lao lao = mLaoDetailViewModel.getCurrentLaoValue();
List<ConsensusNode> nodes = lao.getNodes();
ownNode = lao.getNode(mLaoDetailViewModel.getPublicKey());
if (ownNode == null) {
// Only possible if the user wasn't an acceptor, but shouldn't have access to this fragment
Log.e(TAG, "Couldn't find the Node with public key : " + mLaoDetailViewModel.getPublicKey());
throw new IllegalStateException("Only acceptors are allowed to access ElectionStartFragment");
}
NodesAcceptorAdapter adapter = new NodesAcceptorAdapter(nodes, ownNode, instanceId, getViewLifecycleOwner(), mLaoDetailViewModel);
GridView gridView = binding.nodesGrid;
gridView.setAdapter(adapter);
if (isElectionStartTimePassed(election)) {
updateStartAndStatus(nodes, election, instanceId);
}
mLaoDetailViewModel.getNodes().observe(getViewLifecycleOwner(), consensusNodes -> {
Log.d(TAG, "got an update for nodes : " + consensusNodes);
adapter.setList(consensusNodes);
if (isElectionStartTimePassed(election)) {
updateStartAndStatus(consensusNodes, election, instanceId);
}
});
binding.setLifecycleOwner(getViewLifecycleOwner());
return binding.getRoot();
}
use of com.github.dedis.popstellar.model.objects.ConsensusNode 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();
}
use of com.github.dedis.popstellar.model.objects.ConsensusNode 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