use of com.github.dedis.popstellar.utility.error.InvalidMessageIdException in project popstellar by dedis.
the class LaoHandler method handleStateLao.
/**
* Process a StateLao message.
*
* @param context the HandlerContext of the message
* @param stateLao the message that was received
*/
public static void handleStateLao(HandlerContext context, StateLao stateLao) throws DataHandlingException {
LAORepository laoRepository = context.getLaoRepository();
Channel channel = context.getChannel();
Log.d(TAG, "Receive State Lao Broadcast msg=" + stateLao);
Lao lao = laoRepository.getLaoByChannel(channel);
Log.d(TAG, "Receive State Lao Broadcast " + stateLao.getName());
if (!laoRepository.getMessageById().containsKey(stateLao.getModificationId())) {
Log.d(TAG, "Can't find modification id : " + stateLao.getModificationId());
// queue it if we haven't received the update message yet
throw new InvalidMessageIdException(stateLao, stateLao.getModificationId());
}
Log.d(TAG, "Verifying signatures");
for (PublicKeySignaturePair pair : stateLao.getModificationSignatures()) {
if (!pair.getWitness().verify(pair.getSignature(), stateLao.getModificationId())) {
throw new InvalidSignatureException(stateLao, pair.getSignature());
}
}
Log.d(TAG, "Success to verify state lao signatures");
// TODO: verify if lao/state_lao is consistent with the lao/update message
lao.setId(stateLao.getId());
lao.setWitnesses(stateLao.getWitnesses());
lao.setName(stateLao.getName());
lao.setLastModified(stateLao.getLastModified());
lao.setModificationId(stateLao.getModificationId());
PublicKey publicKey = context.getKeyManager().getMainPublicKey();
if (lao.getOrganizer().equals(publicKey) || lao.getWitnesses().contains(publicKey)) {
context.getMessageSender().subscribe(lao.getChannel().subChannel("consensus")).subscribe();
}
// Now we're going to remove all pending updates which came prior to this state lao
long targetTime = stateLao.getLastModified();
lao.getPendingUpdates().removeIf(pendingUpdate -> pendingUpdate.getModificationTime() <= targetTime);
laoRepository.updateNodes(channel);
}
use of com.github.dedis.popstellar.utility.error.InvalidMessageIdException 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.utility.error.InvalidMessageIdException 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.utility.error.InvalidMessageIdException 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.utility.error.InvalidMessageIdException in project popstellar by dedis.
the class ChirpHandler method handleDeleteChirp.
/**
* process a DeleteChirp message.
*
* @param context the HandlerContext of the message
* @param deleteChirp the data of the message that was received
*/
public static void handleDeleteChirp(HandlerContext context, DeleteChirp deleteChirp) throws DataHandlingException {
LAORepository laoRepository = context.getLaoRepository();
Channel channel = context.getChannel();
Lao lao = laoRepository.getLaoByChannel(channel);
Optional<Chirp> chirpOptional = lao.getChirp(deleteChirp.getChirpId());
Chirp chirp;
if (!chirpOptional.isPresent()) {
throw new InvalidMessageIdException(deleteChirp, deleteChirp.getChirpId());
}
chirp = chirpOptional.get();
if (chirp.getIsDeleted()) {
Log.d(TAG, "The chirp is already deleted");
} else {
chirp.setIsDeleted(true);
chirp.setText("");
}
}
Aggregations