Search in sources :

Example 16 with MessageGeneral

use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.

the class LaoDetailViewModel method sendConsensusElect.

/**
 * Sends a ConsensusElect message.
 *
 * <p>Publish a GeneralMessage containing ConsensusElect data.
 *
 * @param creation the creation time of the consensus
 * @param objId the id of the object the consensus refers to (e.g. election_id)
 * @param type the type of object the consensus refers to (e.g. election)
 * @param property the property the value refers to (e.g. "state")
 * @param value the proposed new value for the property (e.g. "started")
 */
public void sendConsensusElect(long creation, String objId, String type, String property, Object value) {
    Log.d(TAG, String.format("creating a new consensus for type: %s, property: %s, value: %s", type, property, value));
    Lao lao = getCurrentLaoValue();
    if (lao == null) {
        Log.d(TAG, LAO_FAILURE_MESSAGE);
        return;
    }
    Channel channel = lao.getChannel().subChannel("consensus");
    ConsensusElect consensusElect = new ConsensusElect(creation, objId, type, property, value);
    Log.d(TAG, PUBLISH_MESSAGE);
    MessageGeneral msg = new MessageGeneral(keyManager.getMainKeyPair(), consensusElect, gson);
    Disposable disposable = networkManager.getMessageSender().publish(channel, msg).subscribe(() -> Log.d(TAG, "created a consensus with message id : " + msg.getMessageId()), error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_start_election));
    disposables.add(disposable);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) ConsensusElect(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElect) 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 17 with MessageGeneral

use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.

the class JsonMessageGeneralSerializer method deserialize.

@Override
public MessageGeneral deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonMessageData jsonObject = context.deserialize(json, JsonMessageData.class);
    JsonElement dataElement = JsonParser.parseString(new String(jsonObject.data.getData(), StandardCharsets.UTF_8));
    Data data = context.deserialize(dataElement, Data.class);
    return new MessageGeneral(jsonObject.sender, jsonObject.data, data, jsonObject.signature, jsonObject.messageID, jsonObject.witnessSignatures);
}
Also used : MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) JsonElement(com.google.gson.JsonElement) Data(com.github.dedis.popstellar.model.network.method.message.data.Data) Base64URLData(com.github.dedis.popstellar.model.objects.security.Base64URLData)

Example 18 with MessageGeneral

use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.

the class JsonResultSerializer method deserialize.

@Override
public Result deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject root = json.getAsJsonObject();
    int id = root.get("id").getAsInt();
    JsonElement resultElement = root.get(RESULT);
    if (resultElement.isJsonPrimitive()) {
        return new Result(id);
    } else {
        Type listType = new TypeToken<ArrayList<MessageGeneral>>() {
        }.getType();
        List<MessageGeneral> messages = context.deserialize(resultElement.getAsJsonArray(), listType);
        return new ResultMessages(id, messages);
    }
}
Also used : Type(java.lang.reflect.Type) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) ResultMessages(com.github.dedis.popstellar.model.network.answer.ResultMessages) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Result(com.github.dedis.popstellar.model.network.answer.Result)

Example 19 with MessageGeneral

use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.

the class ElectionStartFragmentTest method startButtonSendElectMessageTest.

@Test
public void startButtonSendElectMessageTest() {
    setupViewModel(PAST_TIME);
    long minCreation = Instant.now().getEpochSecond();
    electionStartButton().perform(ViewActions.click());
    ArgumentCaptor<MessageGeneral> captor = ArgumentCaptor.forClass(MessageGeneral.class);
    Mockito.verify(messageSender).publish(eq(consensusChannel), captor.capture());
    MessageGeneral msgGeneral = captor.getValue();
    long maxCreation = Instant.now().getEpochSecond();
    assertEquals(mainKeyPair.getPublicKey(), msgGeneral.getSender());
    ConsensusElect elect = (ConsensusElect) msgGeneral.getData();
    assertEquals(KEY, elect.getKey());
    assertEquals(INSTANCE_ID, elect.getInstanceId());
    assertEquals("started", elect.getValue());
    assertTrue(minCreation <= elect.getCreation() && elect.getCreation() <= maxCreation);
}
Also used : ConsensusElect(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElect) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) HiltAndroidTest(dagger.hilt.android.testing.HiltAndroidTest) Test(org.junit.Test)

Example 20 with MessageGeneral

use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.

the class ElectionStartFragmentTest method displayWithUpdatesIsCorrect.

@Test
public void displayWithUpdatesIsCorrect() throws DataHandlingException {
    setupViewModel(PAST_TIME);
    // Election start time has passed, should display that it's ready and start button enabled
    displayAssertions(STATUS_READY, START_START, true);
    DataInteraction grid = nodesGrid();
    nodeAssertions(grid, ownPos, "Waiting\n" + publicKey, false);
    nodeAssertions(grid, node2Pos, "Waiting\n" + node2, false);
    nodeAssertions(grid, node3Pos, "Waiting\n" + node3, false);
    // Nodes 3 try to start
    MessageGeneral elect3Msg = createMsg(node3KeyPair, elect);
    messageHandler.handleMessage(laoRepository, messageSender, consensusChannel, elect3Msg);
    nodeAssertions(grid, node3Pos, "Approve Start by\n" + node3, true);
    // We try to start (it should disable the start button)
    MessageGeneral elect1Msg = createMsg(mainKeyPair, elect);
    messageHandler.handleMessage(laoRepository, messageSender, consensusChannel, elect1Msg);
    displayAssertions(STATUS_READY, START_START, false);
    nodeAssertions(grid, ownPos, "Approve Start by\n" + publicKey, true);
    // We accepted node 3 (it should disable button for node3)
    ConsensusElectAccept electAccept3 = new ConsensusElectAccept(INSTANCE_ID, elect3Msg.getMessageId(), true);
    MessageGeneral accept3Msg = createMsg(mainKeyPair, electAccept3);
    messageHandler.handleMessage(laoRepository, messageSender, consensusChannel, accept3Msg);
    nodeAssertions(grid, node3Pos, "Approve Start by\n" + node3, false);
    // Receive a learn message => node3 was accepted and has started the election
    ConsensusLearn learn3 = new ConsensusLearn(INSTANCE_ID, elect3Msg.getMessageId(), PAST_TIME, true, Collections.emptyList());
    MessageGeneral learn3Msg = createMsg(node3KeyPair, learn3);
    messageHandler.handleMessage(laoRepository, messageSender, consensusChannel, learn3Msg);
    displayAssertions(STATUS_STARTED, START_STARTED, false);
    nodeAssertions(grid, node3Pos, "Started by\n" + node3, false);
}
Also used : ConsensusElectAccept(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElectAccept) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) ConsensusLearn(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusLearn) DataInteraction(androidx.test.espresso.DataInteraction) HiltAndroidTest(dagger.hilt.android.testing.HiltAndroidTest) Test(org.junit.Test)

Aggregations

MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)33 Test (org.junit.Test)18 Lao (com.github.dedis.popstellar.model.objects.Lao)10 Channel (com.github.dedis.popstellar.model.objects.Channel)8 LAORepository (com.github.dedis.popstellar.repository.LAORepository)8 Disposable (io.reactivex.disposables.Disposable)7 StateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)6 UpdateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)6 RollCall (com.github.dedis.popstellar.model.objects.RollCall)6 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)6 Before (org.junit.Before)6 ConsensusElectAccept (com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElectAccept)5 CreateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao)5 Election (com.github.dedis.popstellar.model.objects.Election)5 WitnessMessage (com.github.dedis.popstellar.model.objects.WitnessMessage)5 MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)5 LAOState (com.github.dedis.popstellar.repository.LAOState)5 CloseRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall)4 CreateRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall)4 OpenRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall)4