Search in sources :

Example 6 with LAORepository

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);
}
Also used : InvalidSignatureException(com.github.dedis.popstellar.utility.error.InvalidSignatureException) PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) InvalidMessageIdException(com.github.dedis.popstellar.utility.error.InvalidMessageIdException) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) PublicKeySignaturePair(com.github.dedis.popstellar.model.network.method.message.PublicKeySignaturePair)

Example 7 with LAORepository

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());
}
Also used : ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) InvalidMessageIdException(com.github.dedis.popstellar.utility.error.InvalidMessageIdException) Lao(com.github.dedis.popstellar.model.objects.Lao) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 8 with LAORepository

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());
}
Also used : ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) InvalidMessageIdException(com.github.dedis.popstellar.utility.error.InvalidMessageIdException) Lao(com.github.dedis.popstellar.model.objects.Lao)

Example 9 with LAORepository

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);
}
Also used : LAOState(com.github.dedis.popstellar.repository.LAOState) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Before(org.junit.Before)

Example 10 with LAORepository

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));
}
Also used : MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) ConsensusAccept(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusAccept) HashMap(java.util.HashMap) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) ConsensusPropose(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusPropose) ConsensusPromise(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusPromise) ConsensusPrepare(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusPrepare) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID) Test(org.junit.Test)

Aggregations

LAORepository (com.github.dedis.popstellar.repository.LAORepository)30 Channel (com.github.dedis.popstellar.model.objects.Channel)25 Lao (com.github.dedis.popstellar.model.objects.Lao)22 CreateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao)15 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)12 Before (org.junit.Before)11 Test (org.junit.Test)7 DataRegistryModule (com.github.dedis.popstellar.di.DataRegistryModule)6 JsonModule (com.github.dedis.popstellar.di.JsonModule)6 GenericMessage (com.github.dedis.popstellar.model.network.GenericMessage)6 Error (com.github.dedis.popstellar.model.network.answer.Error)6 ErrorCode (com.github.dedis.popstellar.model.network.answer.ErrorCode)6 Result (com.github.dedis.popstellar.model.network.answer.Result)6 ResultMessages (com.github.dedis.popstellar.model.network.answer.ResultMessages)6 Catchup (com.github.dedis.popstellar.model.network.method.Catchup)6 Publish (com.github.dedis.popstellar.model.network.method.Publish)6 Query (com.github.dedis.popstellar.model.network.method.Query)6 Subscribe (com.github.dedis.popstellar.model.network.method.Subscribe)6 Unsubscribe (com.github.dedis.popstellar.model.network.method.Unsubscribe)6 Data (com.github.dedis.popstellar.model.network.method.message.data.Data)6