All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.openchvote.printingauthority.writein.states.S200 Maven / Gradle / Ivy

/*
 * Copyright (C) 2024 Berner Fachhochschule https://e-voting.bfh.ch
 *
 *  - This program is free software: you can redistribute it and/or modify                           -
 *  - it under the terms of the GNU Affero General Public License as published by                    -
 *  - the Free Software Foundation, either version 3 of the License, or                              -
 *  - (at your option) any later version.                                                            -
 *  -                                                                                                -
 *  - This program is distributed in the hope that it will be useful,                                -
 *  - but WITHOUT ANY WARRANTY; without even the implied warranty of                                 -
 *  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                                   -
 *  - GNU General Public License for more details.                                                   -
 *  -                                                                                                -
 *  - You should have received a copy of the GNU Affero General Public License                       -
 *  - along with this program. If not, see .                           -
 */
package ch.openchvote.printingauthority.writein.states;

import ch.openchvote.algorithms.AlgorithmException;
import ch.openchvote.framework.annotations.state.Phase;
import ch.openchvote.framework.annotations.state.Notify;
import ch.openchvote.framework.communication.Message;
import ch.openchvote.framework.communication.Status;
import ch.openchvote.framework.party.State;
import ch.openchvote.printingauthority.PrintingAuthority;
import ch.openchvote.printingauthority.writein.EventContext;
import ch.openchvote.printingauthority.writein.states.error.A200;
import ch.openchvote.printingauthority.writein.tasks.T200;
import ch.openchvote.protocol.protocols.writein.content.message.MEP1;
import ch.openchvote.protocol.protocols.writein.content.mail.MPV1;
import ch.openchvote.protocol.phases.Preparation;
import ch.openchvote.utilities.set.IntSet;
import ch.openchvote.utilities.tuples.Pair;

@SuppressWarnings("MissingJavadoc")
@Phase(Preparation.class)
@Notify(Status.Type.READY)
public final class S200 extends State {

    public S200(PrintingAuthority printingAuthority, EventContext eventContext) {
        super(printingAuthority, eventContext);
        this.registerMessageHandler(MEP1.class, this::handleMEP1);
    }

    private void handleMEP1(Message message) {

        // decompose event context
        var eventId = this.eventContext.getEventId();
        var publicData = this.eventContext.getPublicData();
        var secretData = this.eventContext.getSecretData();
        var securityLevel = this.eventContext.getSecurityLevel();

        // select setup parameters
        var ES = publicData.get_ES().get();
        var j = ES.get_SP().getIndexOf(message.getSenderId());

        // select election parameters
        var EP = publicData.get_EP().get();
        var N_E = EP.get_N_E();
        var bold_d = EP.get_bold_d();

        // get and check message content
        var aux = new Pair<>(ES, EP);
        var MEP1 = this.party.getAndCheckContent(MEP1.class, message, aux, securityLevel);

        // update secret data
        secretData.setContent(j, MEP1);

        // check if all MEP1 messages are available
        if (T200.isReady(publicData, secretData)) {
            try {
                // perform task
                T200.run(publicData, secretData);

                // select event data
                var bold_ec = secretData.get_bold_ec().get();

                // send MPV1 to voters
                for (int i : IntSet.range(1, N_E)) {
                    var EC_i = bold_ec.getValue(i);
                    this.party.deliverMail(eventId, bold_d.getValue(i), new MPV1(EC_i));
                }

                // update state
                this.party.updateState(this.eventContext, FINAL.class);

            } catch (AlgorithmException exception) {
                // move to error state
                this.party.updateState(this.eventContext, A200.class);
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy