use of com.github.dedis.popstellar.repository.LAORepository 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.repository.LAORepository 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.repository.LAORepository 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.repository.LAORepository in project popstellar by dedis.
the class ChirpHandlerTest method setup.
@Before
public void setup() throws GeneralSecurityException, DataHandlingException, IOException {
lenient().when(keyManager.getMainKeyPair()).thenReturn(SENDER_KEY);
lenient().when(keyManager.getMainPublicKey()).thenReturn(SENDER);
when(messageSender.subscribe(any())).then(args -> Completable.complete());
laoRepository = new LAORepository();
messageHandler = new MessageHandler(DataRegistryModule.provideDataRegistry(), keyManager);
laoRepository.getLaoById().put(LAO.getId(), new LAOState(LAO));
MessageGeneral createLaoMessage = new MessageGeneral(SENDER_KEY, CREATE_LAO, GSON);
messageHandler.handleMessage(laoRepository, messageSender, LAO.getChannel(), createLaoMessage);
}
use of com.github.dedis.popstellar.repository.LAORepository in project popstellar by dedis.
the class ConsensusHandlerTest method handleConsensusDoNothingOnBackendMessageTest.
@Test
public void handleConsensusDoNothingOnBackendMessageTest() throws DataHandlingException {
LAORepository mockLAORepository = mock(LAORepository.class);
Map<MessageID, MessageGeneral> messageById = new HashMap<>();
when(mockLAORepository.getMessageById()).thenReturn(messageById);
ConsensusPrepare prepare = new ConsensusPrepare(INSTANCE_ID, messageId, CREATION_TIME, 3);
ConsensusPromise promise = new ConsensusPromise(INSTANCE_ID, messageId, CREATION_TIME, 3, true, 2);
ConsensusPropose propose = new ConsensusPropose(INSTANCE_ID, messageId, CREATION_TIME, 3, true, Collections.emptyList());
ConsensusAccept accept = new ConsensusAccept(INSTANCE_ID, messageId, CREATION_TIME, 3, true);
messageHandler.handleMessage(mockLAORepository, messageSender, CONSENSUS_CHANNEL, getMsg(ORGANIZER_KEY, prepare));
messageHandler.handleMessage(mockLAORepository, messageSender, CONSENSUS_CHANNEL, getMsg(ORGANIZER_KEY, promise));
messageHandler.handleMessage(mockLAORepository, messageSender, CONSENSUS_CHANNEL, getMsg(ORGANIZER_KEY, propose));
messageHandler.handleMessage(mockLAORepository, messageSender, CONSENSUS_CHANNEL, getMsg(ORGANIZER_KEY, accept));
// The handlers for prepare/promise/propose/accept should do nothing (call or update nothing)
// because theses messages should only be handle in the backend server.
verify(mockLAORepository, never()).getLaoByChannel(any(Channel.class));
verify(mockLAORepository, never()).updateNodes(any(Channel.class));
}
Aggregations