Search in sources :

Example 1 with StateLao

use of com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao 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 2 with StateLao

use of com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao in project popstellar by dedis.

the class LaoHandlerTest method testHandleStateLao.

@Test
public void testHandleStateLao() throws DataHandlingException {
    // Create the state LAO message
    StateLao stateLao = new StateLao(CREATE_LAO.getId(), CREATE_LAO.getName(), CREATE_LAO.getCreation(), Instant.now().getEpochSecond(), CREATE_LAO.getOrganizer(), createLaoMessage.getMessageId(), new HashSet<>(), new ArrayList<>());
    MessageGeneral message = new MessageGeneral(SENDER_KEY, stateLao, GSON);
    // Call the message handler
    messageHandler.handleMessage(laoRepository, messageSender, LAO_CHANNEL, message);
    // Check the LAO last modification time and ID was updated
    assertEquals((Long) stateLao.getLastModified(), laoRepository.getLaoByChannel(LAO_CHANNEL).getLastModified());
    assertEquals(stateLao.getModificationId(), laoRepository.getLaoByChannel(LAO_CHANNEL).getModificationId());
}
Also used : MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) Test(org.junit.Test)

Example 3 with StateLao

use of com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao in project popstellar by dedis.

the class LaoDetailViewModel method updateLaoName.

/**
 * Method to update the name of a Lao by sending an updateLao msg and a stateLao msg to the
 * backend
 */
public void updateLaoName() {
    Log.d(TAG, "Updating lao name to " + mLaoName.getValue());
    Lao lao = getCurrentLaoValue();
    Channel channel = lao.getChannel();
    KeyPair mainKey = keyManager.getMainKeyPair();
    long now = Instant.now().getEpochSecond();
    UpdateLao updateLao = new UpdateLao(mainKey.getPublicKey(), lao.getCreation(), mLaoName.getValue(), now, lao.getWitnesses());
    MessageGeneral msg = new MessageGeneral(mainKey, updateLao, gson);
    Disposable disposable = networkManager.getMessageSender().publish(channel, msg).subscribe(() -> {
        Log.d(TAG, "updated lao name");
        dispatchLaoUpdate("lao name", updateLao, lao, channel, msg);
    }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_update_lao));
    disposables.add(disposable);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Channel(com.github.dedis.popstellar.model.objects.Channel) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)

Example 4 with StateLao

use of com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao in project popstellar by dedis.

the class LaoDetailViewModel method updateLaoWitnesses.

/**
 * Method to update the list of witnesses of a Lao by sending an updateLao msg and a stateLao msg
 * to the backend
 */
public void updateLaoWitnesses() {
    Log.d(TAG, "Updating lao witnesses ");
    Lao lao = getCurrentLaoValue();
    if (lao == null) {
        Log.d(TAG, LAO_FAILURE_MESSAGE);
        return;
    }
    Channel channel = lao.getChannel();
    KeyPair mainKey = keyManager.getMainKeyPair();
    long now = Instant.now().getEpochSecond();
    UpdateLao updateLao = new UpdateLao(mainKey.getPublicKey(), lao.getCreation(), lao.getName(), now, witnesses);
    MessageGeneral msg = new MessageGeneral(mainKey, updateLao, gson);
    Disposable disposable = networkManager.getMessageSender().publish(channel, msg).subscribe(() -> {
        Log.d(TAG, "updated lao witnesses");
        dispatchLaoUpdate("lao state with new witnesses", updateLao, lao, channel, msg);
    }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_update_lao));
    disposables.add(disposable);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Channel(com.github.dedis.popstellar.model.objects.Channel) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)

Example 5 with StateLao

use of com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao in project popstellar by dedis.

the class LaoDetailViewModel method dispatchLaoUpdate.

/**
 * Helper method for updateLaoWitnesses and updateLaoName to send a stateLao message
 */
private void dispatchLaoUpdate(String desc, UpdateLao updateLao, Lao lao, Channel channel, MessageGeneral msg) {
    StateLao stateLao = new StateLao(updateLao.getId(), updateLao.getName(), lao.getCreation(), updateLao.getLastModified(), lao.getOrganizer(), msg.getMessageId(), updateLao.getWitnesses(), new ArrayList<>());
    disposables.add(networkManager.getMessageSender().publish(keyManager.getMainKeyPair(), channel, stateLao).subscribe(() -> Log.d(TAG, "updated " + desc), error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_state_lao)));
}
Also used : PackageManager(android.content.pm.PackageManager) HomeViewModel(com.github.dedis.popstellar.ui.home.HomeViewModel) NonNull(androidx.annotation.NonNull) ElectionEnd(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionEnd) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) LiveDataReactiveStreams(androidx.lifecycle.LiveDataReactiveStreams) ConsensusNode(com.github.dedis.popstellar.model.objects.ConsensusNode) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) Manifest(android.Manifest) GeneralSecurityException(java.security.GeneralSecurityException) Lao(com.github.dedis.popstellar.model.objects.Lao) WitnessMessage(com.github.dedis.popstellar.model.objects.WitnessMessage) Gson(com.google.gson.Gson) Schedulers(io.reactivex.schedulers.Schedulers) Transformations(androidx.lifecycle.Transformations) ContextCompat(androidx.core.content.ContextCompat) Log(android.util.Log) CloseRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall) ConsensusElect(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElect) KeyManager(com.github.dedis.popstellar.utility.security.KeyManager) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Set(java.util.Set) ElectionVote(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionVote) KeyGenerationException(com.github.dedis.popstellar.utility.error.keys.KeyGenerationException) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ElectionSetup(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionSetup) HiltViewModel(dagger.hilt.android.lifecycle.HiltViewModel) Objects(java.util.Objects) ScanningAction(com.github.dedis.popstellar.ui.qrcode.ScanningAction) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Stream(java.util.stream.Stream) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException) Application(android.app.Application) RollCall(com.github.dedis.popstellar.model.objects.RollCall) Optional(java.util.Optional) SingleEvent(com.github.dedis.popstellar.SingleEvent) Wallet(com.github.dedis.popstellar.model.objects.Wallet) CameraPermissionViewModel(com.github.dedis.popstellar.ui.qrcode.CameraPermissionViewModel) MutableLiveData(androidx.lifecycle.MutableLiveData) R(com.github.dedis.popstellar.R) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall) EventType(com.github.dedis.popstellar.model.objects.event.EventType) EventState(com.github.dedis.popstellar.model.objects.event.EventState) ErrorUtils(com.github.dedis.popstellar.utility.error.ErrorUtils) Signature(com.github.dedis.popstellar.model.objects.security.Signature) ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) LAORepository(com.github.dedis.popstellar.repository.LAORepository) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) UninitializedWalletException(com.github.dedis.popstellar.utility.error.keys.UninitializedWalletException) KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) Toast(android.widget.Toast) AndroidViewModel(androidx.lifecycle.AndroidViewModel) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) WitnessMessageSignature(com.github.dedis.popstellar.model.network.method.message.data.message.WitnessMessageSignature) LiveData(androidx.lifecycle.LiveData) Channel(com.github.dedis.popstellar.model.objects.Channel) BackpressureStrategy(io.reactivex.BackpressureStrategy) Election(com.github.dedis.popstellar.model.objects.Election) CastVote(com.github.dedis.popstellar.model.network.method.message.data.election.CastVote) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) ConsensusElectAccept(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElectAccept) Barcode(com.google.android.gms.vision.barcode.Barcode) QRCodeScanningViewModel(com.github.dedis.popstellar.ui.qrcode.QRCodeScanningViewModel) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID) VisibleForTesting(androidx.annotation.VisibleForTesting) GlobalNetworkManager(com.github.dedis.popstellar.repository.remote.GlobalNetworkManager) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)

Aggregations

StateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)6 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)4 UpdateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)4 Channel (com.github.dedis.popstellar.model.objects.Channel)4 Lao (com.github.dedis.popstellar.model.objects.Lao)4 KeyPair (com.github.dedis.popstellar.model.objects.security.KeyPair)3 CreateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao)2 WitnessMessageSignature (com.github.dedis.popstellar.model.network.method.message.data.message.WitnessMessageSignature)2 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)2 Disposable (io.reactivex.disposables.Disposable)2 Manifest (android.Manifest)1 Application (android.app.Application)1 PackageManager (android.content.pm.PackageManager)1 Log (android.util.Log)1 Toast (android.widget.Toast)1 NonNull (androidx.annotation.NonNull)1 VisibleForTesting (androidx.annotation.VisibleForTesting)1 ContextCompat (androidx.core.content.ContextCompat)1 AndroidViewModel (androidx.lifecycle.AndroidViewModel)1 LiveData (androidx.lifecycle.LiveData)1