use of com.github.dedis.popstellar.repository.LAORepository in project popstellar by dedis.
the class LAONetworkManagerTest method errorsAreDispatchedCorrectly.
@Test
public void errorsAreDispatchedCorrectly() {
TestSchedulerProvider schedulerProvider = new TestSchedulerProvider();
TestScheduler testScheduler = schedulerProvider.getTestScheduler();
LAONetworkManager networkManager = new LAONetworkManager(laoRepository, handler, connection, JsonModule.provideGson(DataRegistryModule.provideDataRegistry()), schedulerProvider);
ErrorCode error = new ErrorCode(3, "error");
// Setup mock answer
Answer<?> answer = args -> {
// Retrieve subscribe object
Query query = args.getArgument(0);
// Return a negative result
messages.onNext(new Error(query.getRequestId(), error));
return null;
};
doAnswer(answer).when(connection).sendMessage(any());
Disposable disposable = networkManager.subscribe(CHANNEL).subscribe(() -> {
throw new IllegalAccessException("The subscription should have failed.");
}, err -> assertTrue(err instanceof JsonRPCErrorException));
testScheduler.advanceTimeBy(5, TimeUnit.SECONDS);
disposable.dispose();
networkManager.dispose();
verify(connection).sendMessage(any(Subscribe.class));
verify(connection, atLeastOnce()).observeMessage();
verify(connection).observeConnectionEvents();
verify(connection).close();
verifyNoMoreInteractions(connection);
}
use of com.github.dedis.popstellar.repository.LAORepository in project popstellar by dedis.
the class LAONetworkManagerTest method subscribeSendsTheRightMessages.
@Test
public void subscribeSendsTheRightMessages() {
TestSchedulerProvider schedulerProvider = new TestSchedulerProvider();
TestScheduler testScheduler = schedulerProvider.getTestScheduler();
LAONetworkManager networkManager = new LAONetworkManager(laoRepository, handler, connection, JsonModule.provideGson(DataRegistryModule.provideDataRegistry()), schedulerProvider);
Answer<?> answer = args -> {
// Retrieve subscribe object
Subscribe subscribe = args.getArgument(0);
// Make sure the channel is correct
assertEquals(CHANNEL, subscribe.getChannel());
// Return a positive result
messages.onNext(new Result(subscribe.getRequestId()));
return null;
};
doAnswer(answer).when(connection).sendMessage(any(Subscribe.class));
// Actual test
Disposable disposable = networkManager.subscribe(CHANNEL).subscribe(() -> verify(connection, never()).sendMessage(any(Catchup.class)));
testScheduler.advanceTimeBy(5, TimeUnit.SECONDS);
disposable.dispose();
networkManager.dispose();
verify(connection).sendMessage(any(Subscribe.class));
verify(connection).sendMessage(any(Catchup.class));
verify(connection, atLeastOnce()).observeMessage();
verify(connection).observeConnectionEvents();
verify(connection).close();
verifyNoMoreInteractions(connection);
}
use of com.github.dedis.popstellar.repository.LAORepository 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.repository.LAORepository 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());
}
use of com.github.dedis.popstellar.repository.LAORepository in project popstellar by dedis.
the class LaoHandler method handleCreateLao.
/**
* Process a CreateLao message.
*
* @param context the HandlerContext of the message
* @param createLao the message that was received
*/
public static void handleCreateLao(HandlerContext context, CreateLao createLao) {
LAORepository laoRepository = context.getLaoRepository();
Channel channel = context.getChannel();
Log.d(TAG, "handleCreateLao: channel " + channel + ", msg=" + createLao);
Lao lao = laoRepository.getLaoByChannel(channel);
lao.setName(createLao.getName());
lao.setCreation(createLao.getCreation());
lao.setLastModified(createLao.getCreation());
lao.setOrganizer(createLao.getOrganizer());
lao.setId(createLao.getId());
lao.setWitnesses(new HashSet<>(createLao.getWitnesses()));
PublicKey publicKey = context.getKeyManager().getMainPublicKey();
if (lao.getOrganizer().equals(publicKey) || lao.getWitnesses().contains(publicKey)) {
context.getMessageSender().subscribe(lao.getChannel().subChannel("consensus")).subscribe();
}
laoRepository.updateNodes(channel);
}
Aggregations