Search in sources :

Example 6 with Lao

use of com.github.dedis.popstellar.model.objects.Lao 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 Lao

use of com.github.dedis.popstellar.model.objects.Lao in project popstellar by dedis.

the class SocialMediaActivity method onOptionsItemSelected.

@SuppressLint("NonConstantResourceId")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Retrieve the index of the lao within the list
    int i = item.getItemId();
    List<Lao> laos = mViewModel.getLAOs().getValue();
    if (laos != null && i >= 0 && i < laos.size()) {
        Lao lao = laos.get(i);
        mViewModel.setLaoId(lao.getId());
        mViewModel.setLaoName(lao.getName());
        return true;
    }
    return super.onOptionsItemSelected(item);
}
Also used : Lao(com.github.dedis.popstellar.model.objects.Lao) AndroidEntryPoint(dagger.hilt.android.AndroidEntryPoint) SuppressLint(android.annotation.SuppressLint) SuppressLint(android.annotation.SuppressLint)

Example 8 with Lao

use of com.github.dedis.popstellar.model.objects.Lao in project popstellar by dedis.

the class SocialMediaViewModel method sendChirp.

/**
 * Send a chirp to your own channel.
 *
 * <p>Publish a MessageGeneral containing AddChirp data.
 *
 * @param text the text written in the chirp
 * @param parentId the id of the chirp to which you replied
 * @param timestamp the time at which you sent the chirp
 */
public void sendChirp(String text, @Nullable MessageID parentId, long timestamp) {
    Log.d(TAG, "Sending a chirp");
    Lao lao = getCurrentLao();
    if (lao == null) {
        Log.e(TAG, LAO_FAILURE_MESSAGE);
        return;
    }
    AddChirp addChirp = new AddChirp(text, parentId, timestamp);
    try {
        PoPToken token = keyManager.getValidPoPToken(lao);
        Channel channel = lao.getChannel().subChannel(SOCIAL).subChannel(token.getPublicKey().getEncoded());
        Log.d(TAG, PUBLISH_MESSAGE);
        MessageGeneral msg = new MessageGeneral(token, addChirp, gson);
        Disposable disposable = networkManager.getMessageSender().publish(token, channel, addChirp).subscribe(() -> Log.d(TAG, "sent chirp with messageId: " + msg.getMessageId()), error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_sending_chirp));
        disposables.add(disposable);
    } catch (KeyException e) {
        ErrorUtils.logAndShow(getApplication(), TAG, e, R.string.error_retrieve_own_token);
    }
}
Also used : AddChirp(com.github.dedis.popstellar.model.network.method.message.data.socialmedia.AddChirp) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Channel(com.github.dedis.popstellar.model.objects.Channel) Lao(com.github.dedis.popstellar.model.objects.Lao) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException)

Example 9 with Lao

use of com.github.dedis.popstellar.model.objects.Lao in project popstellar by dedis.

the class SocialMediaViewModel method deleteChirp.

public void deleteChirp(MessageID chirpId, long timestamp) {
    Log.d(TAG, "Deleting the chirp with id: " + chirpId);
    Lao lao = getCurrentLao();
    if (lao == null) {
        Log.e(TAG, LAO_FAILURE_MESSAGE);
        return;
    }
    DeleteChirp deleteChirp = new DeleteChirp(chirpId, timestamp);
    try {
        PoPToken token = keyManager.getValidPoPToken(lao);
        Channel channel = lao.getChannel().subChannel(SOCIAL).subChannel(token.getPublicKey().getEncoded());
        Log.d(TAG, PUBLISH_MESSAGE);
        MessageGeneral msg = new MessageGeneral(token, deleteChirp, gson);
        Disposable disposable = networkManager.getMessageSender().publish(token, channel, deleteChirp).subscribe(() -> {
            Log.d(TAG, "Deleted chirp with messageId: " + msg.getMessageId());
            Toast.makeText(getApplication().getApplicationContext(), "Deleted chirp!", Toast.LENGTH_LONG).show();
        }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_delete_chirp));
        disposables.add(disposable);
    } catch (KeyException e) {
        ErrorUtils.logAndShow(getApplication(), TAG, e, R.string.error_retrieve_own_token);
    }
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Channel(com.github.dedis.popstellar.model.objects.Channel) Lao(com.github.dedis.popstellar.model.objects.Lao) DeleteChirp(com.github.dedis.popstellar.model.network.method.message.data.socialmedia.DeleteChirp) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException)

Example 10 with Lao

use of com.github.dedis.popstellar.model.objects.Lao 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)

Aggregations

Lao (com.github.dedis.popstellar.model.objects.Lao)46 Channel (com.github.dedis.popstellar.model.objects.Channel)31 LAORepository (com.github.dedis.popstellar.repository.LAORepository)24 StateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)18 UpdateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)18 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)16 Disposable (io.reactivex.disposables.Disposable)14 MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)12 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)11 Election (com.github.dedis.popstellar.model.objects.Election)10 RollCall (com.github.dedis.popstellar.model.objects.RollCall)10 CreateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao)9 PoPToken (com.github.dedis.popstellar.model.objects.security.PoPToken)9 CloseRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall)8 PublicKey (com.github.dedis.popstellar.model.objects.security.PublicKey)8 KeyException (com.github.dedis.popstellar.utility.error.keys.KeyException)8 CreateRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall)7 OpenRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall)6 ElectInstance (com.github.dedis.popstellar.model.objects.ElectInstance)6 LAOState (com.github.dedis.popstellar.repository.LAOState)6