Voting Procedure¶
-
typedef struct cardano_voting_procedure_t cardano_voting_procedure_t¶
A voting procedure is a pair of:
a vote.
an anchor, it links the vote to arbitrary off-chain JSON payload of metadata.
-
cardano_error_t cardano_voting_procedure_new(cardano_vote_t vote, cardano_anchor_t *anchor, cardano_voting_procedure_t **voting_procedure)¶
Creates and initializes a new voting procedure.
This function allocates and initializes a new instance of a cardano_voting_procedure_t object using the provided cardano_voter_t and cardano_anchor_t objects.
Usage Example:
cardano_vote_t vote = CARDANO_VOTE_YES; cardano_anchor_t* anchor = cardano_anchor_new(...); cardano_voting_procedure_t* voting_procedure = NULL; cardano_error_t result = cardano_voting_procedure_new(vote, anchor, &voting_procedure); if (result == CARDANO_SUCCESS) { // Use the voting procedure // Once done, ensure to clean up and release the voting procedure cardano_voting_procedure_unref(&voting_procedure); } // Clean up other resources cardano_anchor_unref(&anchor);- Parameters:¶
- cardano_vote_t vote¶
[in] A pointer to the cardano_voter_t object that represents the voter initiating the procedure.
- cardano_anchor_t *anchor¶
[in] A pointer to the cardano_anchor_t object that defines the starting point or condition for the voting procedure.
- cardano_voting_procedure_t **voting_procedure¶
[out] On successful initialization, this will point to a newly created cardano_voting_procedure_t object. This object represents a “strong reference” to the voting procedure, meaning that it is fully initialized and ready for use. The caller is responsible for managing the lifecycle of this object. Specifically, once the voting procedure is no longer needed, the caller must release it by calling cardano_voting_procedure_unref.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the voting procedure was successfully created, or an appropriate error code indicating the failure reason.
-
cardano_error_t cardano_voting_procedure_from_cbor(cardano_cbor_reader_t *reader, cardano_voting_procedure_t **voting_procedure)¶
Creates a voting_procedure from a CBOR reader.
This function parses CBOR data using a provided cardano_cbor_reader_t and constructs a cardano_voting_procedure_t object. It assumes that the CBOR reader is set up correctly and that the CBOR data corresponds to the structure expected for a voting_procedure.
Usage Example:
cardano_cbor_reader_t* reader = cardano_cbor_reader_new(cbor_data, data_size); cardano_voting_procedure_t* voting_procedure = NULL; cardano_error_t result = cardano_voting_procedure_from_cbor(reader, &voting_procedure); if (result == CARDANO_SUCCESS) { // Use the voting_procedure // Once done, ensure to clean up and release the voting_procedure cardano_voting_procedure_unref(&voting_procedure); } else { const char* error = cardano_cbor_reader_get_last_error(reader); printf("Failed to decode voting_procedure: %s\n", error); } cardano_cbor_reader_unref(&reader); // Cleanup the CBOR readerNote
If the function fails, the last error can be retrieved by calling cardano_cbor_reader_get_last_error with the reader. The caller is responsible for freeing the created cardano_voting_procedure_t object by calling cardano_voting_procedure_unref when it is no longer needed.
- Parameters:¶
- cardano_cbor_reader_t *reader¶
[in] A pointer to an initialized cardano_cbor_reader_t that is ready to read the CBOR-encoded voting_procedure data.
- cardano_voting_procedure_t **voting_procedure¶
[out] A pointer to a pointer of cardano_voting_procedure_t that will be set to the address of the newly created voting_procedure object upon successful decoding.
- Returns:¶
A cardano_error_t value indicating the outcome of the operation. Returns CARDANO_SUCCESS if the voting_procedure was successfully created, or an appropriate error code if an error occurred.
-
cardano_error_t cardano_voting_procedure_to_cbor(const cardano_voting_procedure_t *voting_procedure, cardano_cbor_writer_t *writer)¶
Serializes a voting_procedure into CBOR format using a CBOR writer.
This function serializes the given cardano_voting_procedure_t object using a cardano_cbor_writer_t.
Usage Example:
cardano_voting_procedure_t* voting_procedure = ...; cardano_cbor_writer_t* writer = cardano_cbor_writer_new(); if (writer) { cardano_error_t result = cardano_voting_procedure_to_cbor(voting_procedure, writer); if (result == CARDANO_SUCCESS) { // Use the writer's buffer containing the serialized data } else { const char* error_message = cardano_cbor_writer_get_last_error(writer); printf("Serialization failed: %s\n", error_message); } cardano_cbor_writer_unref(&writer); } cardano_voting_procedure_unref(&voting_procedure);- Parameters:¶
- const cardano_voting_procedure_t *voting_procedure¶
[in] A constant pointer to the cardano_voting_procedure_t object that is to be serialized.
- cardano_cbor_writer_t *writer¶
[out] A pointer to a cardano_cbor_writer_t where the CBOR serialized data will be written. The writer must already be initialized and ready to accept the data.
- Returns:¶
Returns CARDANO_SUCCESS if the serialization is successful. If the
voting_procedureorwriteris NULL, returns CARDANO_ERROR_POINTER_IS_NULL.
-
cardano_error_t cardano_voting_procedure_to_cip116_json(const cardano_voting_procedure_t *procedure, cardano_json_writer_t *writer)¶
Serializes a voting procedure to CIP-116 JSON.
The function writes the full JSON object, including the surrounding braces. Keys are written in the order: “vote”, then “anchor” (if present).
- Parameters:¶
- const cardano_voting_procedure_t *procedure¶
[in] Pointer to a valid cardano_voting_procedure_t.
- cardano_json_writer_t *writer¶
[in] Pointer to a valid cardano_json_writer_t.
- Returns:¶
CARDANO_SUCCESS On success. CARDANO_ERROR_POINTER_IS_NULL If
procedureorwriteris NULL. CARDANO_ERROR_INVALID_ARGUMENT If the vote type is invalid. Other Any error propagated from nested writers.
-
cardano_vote_t cardano_voting_procedure_get_vote(const cardano_voting_procedure_t *voting_procedure)¶
Retrieves the vote type from a voting procedure.
This function retrieves the vote type from the given cardano_voting_procedure_t object. The vote type is represented by the cardano_vote_t enumeration which includes options such as CARDANO_VOTE_NO, CARDANO_VOTE_YES, and CARDANO_VOTE_ABSTAIN.
Usage Example:
cardano_voting_procedure_t* voting_procedure = ...; // Assume voting_procedure is already initialized cardano_vote_t vote = cardano_voting_procedure_get_vote(voting_procedure); switch (vote) { case CARDANO_VOTE_YES: printf("The vote is YES.\n"); break; case CARDANO_VOTE_NO: printf("The vote is NO.\n"); break; case CARDANO_VOTE_ABSTAIN: printf("The vote is ABSTAIN.\n"); break; }- Parameters:¶
- const cardano_voting_procedure_t *voting_procedure¶
[in] A constant pointer to the cardano_voting_procedure_t object from which the vote type is to be retrieved.
- Returns:¶
The vote type as cardano_vote_t. If the input is NULL, the behavior is undefined.
-
cardano_error_t cardano_voting_procedure_set_vote(cardano_voting_procedure_t *voting_procedure, cardano_vote_t vote)¶
Sets the vote type in a voting procedure.
This function sets the vote type for the specified cardano_voting_procedure_t object. The vote type is defined by the cardano_vote_t enumeration, which includes options such as CARDANO_VOTE_NO, CARDANO_VOTE_YES, and CARDANO_VOTE_ABSTAIN.
Usage Example:
cardano_voting_procedure_t* voting_procedure = ...; // Assume voting_procedure is already initialized cardano_error_t result = cardano_voting_procedure_set_vote(voting_procedure, CARDANO_VOTE_YES); if (result == CARDANO_SUCCESS) { printf("Vote set successfully.\n"); } else { printf("Failed to set the vote.\n"); }- Parameters:¶
- cardano_voting_procedure_t *voting_procedure¶
[inout] A pointer to the cardano_voting_procedure_t object in which the vote type will be set. This object must be previously initialized and cannot be NULL.
- cardano_vote_t vote¶
[in] The vote type as cardano_vote_t to be set in the voting procedure.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the vote type was successfully set, or an appropriate error code indicating the failure reason.
-
cardano_anchor_t *cardano_voting_procedure_get_anchor(cardano_voting_procedure_t *voting_procedure)¶
Retrieves the anchor from a voting procedure.
This function returns the anchor associated with a given cardano_voting_procedure_t object. The anchor is an optional field and may not be set, in which case the function will return NULL.
Usage Example:
cardano_voting_procedure_t* voting_procedure = ...; // Assume voting_procedure is already initialized cardano_anchor_t* anchor = cardano_voting_procedure_get_anchor(voting_procedure); if (anchor) { printf("Anchor retrieved successfully.\n"); } else { printf("No anchor is set for this voting procedure.\n"); }- Parameters:¶
- cardano_voting_procedure_t *voting_procedure¶
[in] A constant pointer to the cardano_voting_procedure_t object from which the anchor is to be retrieved. This object must be previously initialized and cannot be NULL.
- Returns:¶
A pointer to the cardano_anchor_t object if it exists, otherwise NULL if the anchor is not set or if the input voting_procedure is NULL.
-
cardano_error_t cardano_voting_procedure_set_anchor(cardano_voting_procedure_t *voting_procedure, cardano_anchor_t *anchor)¶
Sets or unsets the anchor in a voting procedure.
This function assigns an anchor to a cardano_voting_procedure_t object. The anchor can be optionally unset by passing NULL as the anchor parameter.
Usage Example:
cardano_voting_procedure_t* voting_procedure = ...; // Assume voting_procedure is already initialized cardano_anchor_t* anchor = ...; // Assume anchor is initialized or NULL to unset cardano_error_t result = cardano_voting_procedure_set_anchor(voting_procedure, anchor); if (result == CARDANO_SUCCESS) { printf("Anchor set/unset successfully.\n"); } else { printf("Failed to set/unset the anchor.\n"); }- Parameters:¶
- cardano_voting_procedure_t *voting_procedure¶
[in] A pointer to the cardano_voting_procedure_t object to which the anchor will be set. This object must be previously initialized and cannot be NULL.
- cardano_anchor_t *anchor¶
[in] A pointer to the cardano_anchor_t object representing the anchor to be set. If this parameter is NULL, the anchor in the voting procedure will be unset.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the anchor was successfully set or unset, or an appropriate error code indicating the failure reason, such as CARDANO_ERROR_POINTER_IS_NULL if the voting_procedure pointer is NULL.
-
void cardano_voting_procedure_unref(cardano_voting_procedure_t **voting_procedure)¶
Decrements the reference count of a voting_procedure object.
This function is responsible for managing the lifecycle of a cardano_voting_procedure_t object by decreasing its reference count. When the reference count reaches zero, the voting_procedure is finalized; its associated resources are released, and its memory is deallocated.
Usage Example:
cardano_voting_procedure_t* voting_procedure = cardano_voting_procedure_new(...); // Perform operations with the voting_procedure... cardano_voting_procedure_unref(&voting_procedure); // At this point, voting_procedure is NULL and cannot be used.Note
After calling cardano_voting_procedure_unref, the pointer to the cardano_voting_procedure_t object will be set to NULL to prevent its reuse.
- Parameters:¶
- cardano_voting_procedure_t **voting_procedure¶
[inout] A pointer to the pointer of the voting_procedure object. This double indirection allows the function to set the caller’s pointer to NULL, avoiding dangling pointer issues after the object has been freed.
-
void cardano_voting_procedure_ref(cardano_voting_procedure_t *voting_procedure)¶
Increases the reference count of the cardano_voting_procedure_t object.
This function is used to manually increment the reference count of a voting_procedure object, indicating that another part of the code has taken ownership of it. This ensures the object remains allocated and valid until all owners have released their reference by calling cardano_voting_procedure_unref.
Usage Example:
// Assuming voting_procedure is a previously created voting_procedure object cardano_voting_procedure_ref(voting_procedure); // Now voting_procedure can be safely used elsewhere without worrying about premature deallocationNote
Always ensure that for every call to cardano_voting_procedure_ref there is a corresponding call to cardano_voting_procedure_unref to prevent memory leaks.
- Parameters:¶
- cardano_voting_procedure_t *voting_procedure¶
A pointer to the voting_procedure object whose reference count is to be incremented.
-
size_t cardano_voting_procedure_refcount(const cardano_voting_procedure_t *voting_procedure)¶
Retrieves the current reference count of the cardano_voting_procedure_t object.
This function returns the number of active references to a voting_procedure object. It’s useful for debugging purposes or managing the lifecycle of the object in complex scenarios.
Usage Example:
// Assuming voting_procedure is a previously created voting_procedure object size_t ref_count = cardano_voting_procedure_refcount(voting_procedure); printf("Reference count: %zu\n", ref_count);Warning
This function does not account for transitive references. A transitive reference occurs when an object holds a reference to another object, rather than directly to the cardano_voting_procedure_t. As such, the reported count may not fully represent the total number of conceptual references in cases where such transitive relationships exist.
- Parameters:¶
- const cardano_voting_procedure_t *voting_procedure¶
A pointer to the voting_procedure object whose reference count is queried. The object must not be NULL.
- Returns:¶
The number of active references to the specified voting_procedure object. If the object is properly managed (i.e., every cardano_voting_procedure_ref call is matched with a cardano_voting_procedure_unref call), this count should reach zero right before the object is deallocated.
-
void cardano_voting_procedure_set_last_error(cardano_voting_procedure_t *voting_procedure, const char *message)¶
Sets the last error message for a given voting_procedure object.
Records an error message in the voting_procedure’s last_error buffer, overwriting any existing message. This is useful for storing devoting_procedureive error information that can be later retrieved. The message is truncated if it exceeds the buffer’s capacity.
Note
The error message is limited to 1023 characters, including the null terminator, due to the fixed size of the last_error buffer.
- Parameters:¶
- cardano_voting_procedure_t *voting_procedure¶
[in] A pointer to the cardano_voting_procedure_t instance whose last error message is to be set. If
NULL, the function does nothing.- const char *message¶
[in] A null-terminated string containing the error message. If
NULL, the voting_procedure’s last_error is set to an empty string, indicating no error.
-
const char *cardano_voting_procedure_get_last_error(const cardano_voting_procedure_t *voting_procedure)¶
Retrieves the last error message recorded for a specific voting_procedure.
This function returns a pointer to the null-terminated string containing the last error message set by cardano_voting_procedure_set_last_error for the given voting_procedure. If no error message has been set, or if the last_error buffer was explicitly cleared, an empty string is returned, indicating no error.
Note
The returned string points to internal storage within the object and must not be modified by the caller. The string remains valid until the next call to cardano_voting_procedure_set_last_error for the same voting_procedure, or until the voting_procedure is deallocated.
- Parameters:¶
- const cardano_voting_procedure_t *voting_procedure¶
[in] A pointer to the cardano_voting_procedure_t instance whose last error message is to be retrieved. If the voting_procedure is NULL, the function returns a generic error message indicating the null voting_procedure.
- Returns:¶
A pointer to a null-terminated string containing the last error message for the specified voting_procedure. If the voting_procedure is NULL, “Object is NULL.” is returned to indicate the error.