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

ch.openchvote.electionauthority.plain.states.S220 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.electionauthority.plain.states;

import ch.openchvote.algorithms.Algorithm;
import ch.openchvote.electionauthority.ElectionAuthority;
import ch.openchvote.electionauthority.plain.EventContext;
import ch.openchvote.electionauthority.plain.states.error.A5;
import ch.openchvote.electionauthority.plain.states.error.A6;
import ch.openchvote.electionauthority.plain.states.error.B5;
import ch.openchvote.electionauthority.plain.tasks.TE5;
import ch.openchvote.electionauthority.plain.tasks.TE6;
import ch.openchvote.framework.annotations.state.Phase;
import ch.openchvote.framework.communication.Message;
import ch.openchvote.framework.exceptions.TaskException;
import ch.openchvote.framework.party.State;
import ch.openchvote.protocol.protocols.plain.content.message.MEE2;
import ch.openchvote.protocol.phases.Preparation;
import ch.openchvote.utilities.tuples.Pair;

@SuppressWarnings("MissingJavadoc")
@Phase(Preparation.class)
public final class S220 extends State {

    public S220(ElectionAuthority electionAuthority, EventContext eventContext) {
        super(electionAuthority, eventContext);
        this.registerMessageHandler(MEE2.class, this::handleMEE2);
    }

    private void handleMEE2(Message message) {

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

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

        // select election parameters
        var EP = publicData.get_EP().get();

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

        // update public data
        publicData.setContent(k, MEE2);

        try {
            // perform task
            TE5.run(k, publicData);

            // run self activation
            var stateId = this.getId();
            this.party.selfActivate(eventId, stateId, State::handleSelfActivation);

        } catch (Algorithm.Exception exception) {
            // move to error state
            this.party.updateState(this.eventContext, A5.class);
        } catch (TaskException exception) {
            // move to error state
            this.party.updateState(this.eventContext, B5.class);
        }
    }

    @SuppressWarnings("unused")
    @Override
    public void handleSelfActivation() {

        // decompose event context
        var publicData = this.eventContext.getPublicData();

        // check if all MEE2 messages are available
        if (TE6.isReady(publicData)) {
            try {
                // perform task
                TE6.run(publicData);

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

            } catch (Algorithm.Exception exception) {
                // move to error state
                this.party.updateState(this.eventContext, A6.class);
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy