use of com.github.dedis.popstellar.model.objects.Lao in project popstellar by dedis.
the class LaoDetailViewModel method sendConsensusElect.
/**
* Sends a ConsensusElect message.
*
* <p>Publish a GeneralMessage containing ConsensusElect data.
*
* @param creation the creation time of the consensus
* @param objId the id of the object the consensus refers to (e.g. election_id)
* @param type the type of object the consensus refers to (e.g. election)
* @param property the property the value refers to (e.g. "state")
* @param value the proposed new value for the property (e.g. "started")
*/
public void sendConsensusElect(long creation, String objId, String type, String property, Object value) {
Log.d(TAG, String.format("creating a new consensus for type: %s, property: %s, value: %s", type, property, value));
Lao lao = getCurrentLaoValue();
if (lao == null) {
Log.d(TAG, LAO_FAILURE_MESSAGE);
return;
}
Channel channel = lao.getChannel().subChannel("consensus");
ConsensusElect consensusElect = new ConsensusElect(creation, objId, type, property, value);
Log.d(TAG, PUBLISH_MESSAGE);
MessageGeneral msg = new MessageGeneral(keyManager.getMainKeyPair(), consensusElect, gson);
Disposable disposable = networkManager.getMessageSender().publish(channel, msg).subscribe(() -> Log.d(TAG, "created a consensus with message id : " + msg.getMessageId()), error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_start_election));
disposables.add(disposable);
}
use of com.github.dedis.popstellar.model.objects.Lao in project popstellar by dedis.
the class LaoDetailViewModel method openAddWitness.
private void openAddWitness() {
Lao lao = getCurrentLaoValue();
if (lao == null) {
Log.d(TAG, LAO_FAILURE_MESSAGE);
return;
}
witnesses = new HashSet<>(lao.getWitnesses());
mOpenAddWitness.setValue(new SingleEvent<>(HomeViewModel.HomeViewAction.SCAN));
}
use of com.github.dedis.popstellar.model.objects.Lao in project popstellar by dedis.
the class LAORepository method getElectionByChannel.
/**
* Retrieves the Election in a given channel
*
* @param channel the channel on which the election was created
* @return the election corresponding to this channel
*/
public Election getElectionByChannel(Channel channel) {
Log.d(TAG, "querying election for channel " + channel);
if (!channel.isElectionChannel())
throw new IllegalArgumentException("invalid channel for an election : " + channel);
Lao lao = getLaoByChannel(channel);
Optional<Election> electionOption = lao.getElection(channel.extractElectionId());
if (!electionOption.isPresent()) {
throw new IllegalArgumentException("the election should be present when receiving a result");
}
return electionOption.get();
}
use of com.github.dedis.popstellar.model.objects.Lao 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.Lao 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());
}
Aggregations