Class ElectionContract

java.lang.Object
it.unibo.ds.chainvote.contract.ElectionContract
All Implemented Interfaces:
org.hyperledger.fabric.contract.ContractInterface

public final class ElectionContract extends Object implements org.hyperledger.fabric.contract.ContractInterface

A Hyperledger Fabric contract to manage elections.

The API Gateway client will receive the transaction returned values wrapped inside a Response json object .

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    castVote(org.hyperledger.fabric.contract.Context ctx, it.unibo.ds.chainvote.utils.Choice choice, String electionId)
    Cast a vote in an existing Election.
    it.unibo.ds.chainvote.elections.Election
    createElection(org.hyperledger.fabric.contract.Context ctx, String electionId, Map<String,Long> results)
    Create an Election.
    it.unibo.ds.chainvote.elections.Election
    deleteElection(org.hyperledger.fabric.contract.Context ctx, String electionId)
    Delete an Election.
    boolean
    electionExists(org.hyperledger.fabric.contract.Context ctx, String electionId)
    Check if an Election exists.
    getAllElection(org.hyperledger.fabric.contract.Context ctx)
    Return all the existing Elections.
    readElection(org.hyperledger.fabric.contract.Context ctx, String electionId)
    Return the ElectionFacade related to Election and ElectionInfo labeled with the given electionId.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.hyperledger.fabric.contract.ContractInterface

    afterTransaction, beforeTransaction, createContext, unknownTransaction
  • Constructor Details

    • ElectionContract

      public ElectionContract()
  • Method Details

    • createElection

      public it.unibo.ds.chainvote.elections.Election createElection(org.hyperledger.fabric.contract.Context ctx, String electionId, Map<String,Long> results)
      Create an Election. Expected JSON input in the following format:
       {
            "function": "ElectionContract:createElection",
            "Args": [
                "your_election_id",
                {["{"choice":"your_choiceN"}":n]*[,"{"choice":"your_choiceM"}":m]}
            ]
       }
       
      Constraints: n >= 0, m >= 0, String must be non-empty.
      Parameters:
      ctx - the Context.
      electionId - the id of the ElectionInfo and the Election.
      results - the initial results of the Election to create.
      Returns:
      the Election built.
      Throws:
      org.hyperledger.fabric.shim.ChaincodeException - with
      • ELECTION_ALREADY_EXISTS as payload if it has already been created an Election with the same electionId
      • ELECTION_INVALID_BUILD_ARGUMENT as payload if at least one of the given arguments is not valid
      • ELECTION_INFO_NOT_FOUND as payload if the ElectionInfo labeled by electionId hasn't been created.
    • readElection

      public ElectionFacade readElection(org.hyperledger.fabric.contract.Context ctx, String electionId)
      Return the ElectionFacade related to Election and ElectionInfo labeled with the given electionId. If the Election is still open, Map representing its results will be empty. Expected JSON input in the following format:
       {
            "function": "ElectionContract:readElection",
            "Args": [
                "your_election_id"
            ]
       }
       
      Constraints: String must be non-empty.
      Parameters:
      ctx - the Context.
      electionId - the id of the Election to retrieve open information (electionId and affluence).
      Returns:
      the ElectionFacade.
    • castVote

      public boolean castVote(org.hyperledger.fabric.contract.Context ctx, it.unibo.ds.chainvote.utils.Choice choice, String electionId)
      Cast a vote in an existing Election. Expected JSON input in the following format:
       {
            "function": "ElectionContract:castVote",
            "Args": [
                "{"choice":"your_choice"}",
                "your_election_id"
            ]
       }
       
      Expected transient map:
       {
            "userId":"your_user_id",
            "code":"your_code"
       }
       
      Constraints: String must be non-empty. Transient String values must be converted in base64 bytes.
      Parameters:
      ctx - the Context. A transient map is expected with the following key-value pairs: userId and code.
      choice - the Choice of the vote.
      electionId - the id of the Election where the vote is cast.
      Returns:
      the Boolean representing the successfulness of the operation.
      Throws:
      org.hyperledger.fabric.shim.ChaincodeException - with
      • ELECTION_NOT_FOUND as payload if the Election in which the vote should be cast doesn't exist
      • INVALID_CREDENTIALS_TO_CAST_VOTE as payload in case userId or code aren't valid
      • INVALID_BALLOT_BUILD_ARGUMENTS in case the arguments used to build the Ballot are not valid
      • INVALID_BALLOT_CAST_ARGUMENTS in case something went wrong with casting.
    • deleteElection

      public it.unibo.ds.chainvote.elections.Election deleteElection(org.hyperledger.fabric.contract.Context ctx, String electionId)
      Delete an Election. Expected JSON input in the following format:
       {
            "function": "ElectionContract:deleteElection",
            "Args": [
                "your_election_id"
            ]
       }
       
      Constraints: String must be non-empty.
      Parameters:
      ctx - the Context.
      electionId - the id of the Election to delete.
      Returns:
      the Election deleted.
      Throws:
      org.hyperledger.fabric.shim.ChaincodeException - with ELECTION_NOT_FOUND as payload if the Election labeled by electionId doesn't exist.
    • electionExists

      public boolean electionExists(org.hyperledger.fabric.contract.Context ctx, String electionId)
      Check if an Election exists. Expected JSON input in the following format:
       {
            "function": "ElectionContract:electionExists",
            "Args": [
                "your_election_id"
            ]
       }
       
      Parameters:
      ctx - the Context.
      electionId - the Election's id.
      Returns:
      a boolean representing if the Election exists.
    • getAllElection

      public List<ElectionFacade> getAllElection(org.hyperledger.fabric.contract.Context ctx)
      Return all the existing Elections. Expected JSON input in the following format:
       {
            "function": "ElectionContract:getAllElection",
            "Args": []
       }
       
      Parameters:
      ctx - the Context.
      Returns:
      the Elections serialized.
      Throws:
      org.hyperledger.fabric.shim.ChaincodeException - with CROSS_INVOCATION_FAILED as payload if something went wrong during cross channel invocation.