Package it.unibo.ds.chainvote.contract
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 -
Method Summary
Modifier and TypeMethodDescriptionbooleancastVote(org.hyperledger.fabric.contract.Context ctx, it.unibo.ds.chainvote.utils.Choice choice, String electionId) Cast a vote in an existingElection.it.unibo.ds.chainvote.elections.ElectioncreateElection(org.hyperledger.fabric.contract.Context ctx, String electionId, Map<String, Long> results) Create anElection.it.unibo.ds.chainvote.elections.ElectiondeleteElection(org.hyperledger.fabric.contract.Context ctx, String electionId) Delete anElection.booleanelectionExists(org.hyperledger.fabric.contract.Context ctx, String electionId) Check if anElectionexists.getAllElection(org.hyperledger.fabric.contract.Context ctx) Return all the existingElections.readElection(org.hyperledger.fabric.contract.Context ctx, String electionId) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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 anElection. 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- theContext.electionId- the id of theElectionInfoand theElection.results- the initial results of theElectionto create.- Returns:
- the
Electionbuilt. - Throws:
org.hyperledger.fabric.shim.ChaincodeException- with-
ELECTION_ALREADY_EXISTSas payload if it has already been created anElectionwith the sameelectionId -
ELECTION_INVALID_BUILD_ARGUMENTas payload if at least one of the given arguments is not valid -
ELECTION_INFO_NOT_FOUNDas payload if theElectionInfolabeled byelectionIdhasn't been created.
-
-
readElection
Return theElectionFacaderelated toElectionandElectionInfolabeled with the given electionId. If theElectionis still open,Maprepresenting 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- theContext.electionId- the id of theElectionto 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 existingElection. 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- theContext. A transient map is expected with the following key-value pairs:userIdandcode.choice- theChoiceof the vote.electionId- the id of theElectionwhere the vote is cast.- Returns:
- the
Booleanrepresenting the successfulness of the operation. - Throws:
org.hyperledger.fabric.shim.ChaincodeException- with-
ELECTION_NOT_FOUNDas payload if theElectionin which the vote should be cast doesn't exist -
INVALID_CREDENTIALS_TO_CAST_VOTEas payload in caseuserIdorcodearen't valid -
INVALID_BALLOT_BUILD_ARGUMENTSin case the arguments used to build theBallotare not valid -
INVALID_BALLOT_CAST_ARGUMENTSin case something went wrong with casting.
-
-
deleteElection
public it.unibo.ds.chainvote.elections.Election deleteElection(org.hyperledger.fabric.contract.Context ctx, String electionId) Delete anElection. Expected JSON input in the following format:{ "function": "ElectionContract:deleteElection", "Args": [ "your_election_id" ] }Constraints: String must be non-empty.- Parameters:
ctx- theContext.electionId- the id of theElectionto delete.- Returns:
- the
Electiondeleted. - Throws:
org.hyperledger.fabric.shim.ChaincodeException- withELECTION_NOT_FOUNDas payload if theElectionlabeled byelectionIddoesn't exist.
-
electionExists
Check if anElectionexists. Expected JSON input in the following format:{ "function": "ElectionContract:electionExists", "Args": [ "your_election_id" ] }- Parameters:
ctx- theContext.electionId- theElection's id.- Returns:
- a boolean representing if the
Electionexists.
-
getAllElection
Return all the existingElections. Expected JSON input in the following format:{ "function": "ElectionContract:getAllElection", "Args": [] }- Parameters:
ctx- theContext.- Returns:
- the
Elections serialized. - Throws:
org.hyperledger.fabric.shim.ChaincodeException- withCROSS_INVOCATION_FAILEDas payload if something went wrong during cross channel invocation.
-