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
-
Method Summary
Modifier and TypeMethodDescriptionboolean
castVote
(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.Election
createElection
(org.hyperledger.fabric.contract.Context ctx, String electionId, Map<String, Long> results) Create anElection
.it.unibo.ds.chainvote.elections.Election
deleteElection
(org.hyperledger.fabric.contract.Context ctx, String electionId) Delete anElection
.boolean
electionExists
(org.hyperledger.fabric.contract.Context ctx, String electionId) Check if anElection
exists.getAllElection
(org.hyperledger.fabric.contract.Context ctx) Return all the existingElection
s.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, 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 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 theElectionInfo
and theElection
.results
- the initial results of theElection
to create.- Returns:
- the
Election
built. - Throws:
org.hyperledger.fabric.shim.ChaincodeException
- with-
ELECTION_ALREADY_EXISTS
as payload if it has already been created anElection
with the sameelectionId
-
ELECTION_INVALID_BUILD_ARGUMENT
as payload if at least one of the given arguments is not valid -
ELECTION_INFO_NOT_FOUND
as payload if theElectionInfo
labeled byelectionId
hasn't been created.
-
-
readElection
Return theElectionFacade
related toElection
andElectionInfo
labeled with the given electionId. If theElection
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
- theContext
.electionId
- the id of theElection
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 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:userId
andcode
.choice
- theChoice
of the vote.electionId
- the id of theElection
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 theElection
in which the vote should be cast doesn't exist -
INVALID_CREDENTIALS_TO_CAST_VOTE
as payload in caseuserId
orcode
aren't valid -
INVALID_BALLOT_BUILD_ARGUMENTS
in case the arguments used to build theBallot
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 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 theElection
to delete.- Returns:
- the
Election
deleted. - Throws:
org.hyperledger.fabric.shim.ChaincodeException
- withELECTION_NOT_FOUND
as payload if theElection
labeled byelectionId
doesn't exist.
-
electionExists
Check if anElection
exists. 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
Election
exists.
-
getAllElection
Return all the existingElection
s. Expected JSON input in the following format:{ "function": "ElectionContract:getAllElection", "Args": [] }
- Parameters:
ctx
- theContext
.- Returns:
- the
Election
s serialized. - Throws:
org.hyperledger.fabric.shim.ChaincodeException
- withCROSS_INVOCATION_FAILED
as payload if something went wrong during cross channel invocation.
-