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

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

There is a newer version: 2.1
Show newest version
/*
 * 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.plain.states;

import ch.openchvote.algorithms.Algorithm;
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.plain.EventContext;
import ch.openchvote.printingauthority.plain.states.error.A1;
import ch.openchvote.printingauthority.plain.tasks.TP1;
import ch.openchvote.protocol.protocols.plain.content.message.MEP1;
import ch.openchvote.protocol.protocols.plain.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 (TP1.isReady(publicData, secretData)) {
            try {
                // perform task
                TP1.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 (Algorithm.Exception exception) {
                // move to error state
                this.party.updateState(this.eventContext, A1.class);
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy