Voter¶
-
typedef struct cardano_voter_t cardano_voter_t¶
A voter is any participant with an eligible role who either has a direct stake or has delegated their stake, and they exercise their rights by casting votes on governance actions.
The weight or influence of their vote is determined by the amount of their active stake or the stake that’s been delegated to them.
Various roles in the Cardano ecosystem can participate in voting. This includes constitutional committee members, DReps (Delegation Representatives), and SPOs (Stake Pool Operators).
-
cardano_error_t cardano_voter_new(cardano_voter_type_t voter_type, cardano_credential_t *credential, cardano_voter_t **voter)¶
Creates and initializes a new instance of a voter.
This function creates and initializes a new instance of a cardano_voter_t object based on the specified voter type and credentials. It returns an error code to indicate the success or failure of the operation.
Usage Example:
cardano_credential_t* credential = cardano_credential_new(...); cardano_voter_t* voter = NULL; // Attempt to create a new voter cardano_error_t result = cardano_voter_new(CARDANO_VOTER_TYPE_CONSTITUTIONAL_COMMITTEE_KEY_HASH, credential, &voter); if (result == CARDANO_SUCCESS) { // Use the voter // Once done, ensure to clean up and release the voter cardano_voter_unref(&voter); } // Clean up the credential object once done cardano_credential_unref(&credential);- Parameters:¶
- cardano_voter_type_t voter_type¶
[in] The type of the voter to create.
- cardano_credential_t *credential¶
[in] A pointer to the cardano_credential_t object containing the credentials of the voter. The object must not be NULL.
- cardano_voter_t **voter¶
[out] On successful initialization, this will point to a newly created cardano_voter_t object. This object represents a “strong reference” to the voter, meaning that it is fully initialized and ready for use. The caller is responsible for managing the lifecycle of this object. Specifically, once the voter is no longer needed, the caller must release it by calling cardano_voter_unref.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the voter was successfully created, or an appropriate error code indicating the failure reason.
-
cardano_error_t cardano_voter_from_cbor(cardano_cbor_reader_t *reader, cardano_voter_t **voter)¶
Creates a voter from a CBOR reader.
This function parses CBOR data using a provided cardano_cbor_reader_t and constructs a cardano_voter_t object. It assumes that the CBOR reader is set up correctly and that the CBOR data corresponds to the structure expected for a voter.
Usage Example:
cardano_cbor_reader_t* reader = cardano_cbor_reader_new(cbor_data, data_size); cardano_voter_t* voter = NULL; cardano_error_t result = cardano_voter_from_cbor(reader, &voter); if (result == CARDANO_SUCCESS) { // Use the voter // Once done, ensure to clean up and release the voter cardano_voter_unref(&voter); } else { const char* error = cardano_cbor_reader_get_last_error(reader); printf("Failed to decode voter: %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_voter_t object by calling cardano_voter_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 voter data.
- cardano_voter_t **voter¶
[out] A pointer to a pointer of cardano_voter_t that will be set to the address of the newly created voter object upon successful decoding.
- Returns:¶
A cardano_error_t value indicating the outcome of the operation. Returns CARDANO_SUCCESS if the voter was successfully created, or an appropriate error code if an error occurred.
-
cardano_error_t cardano_voter_to_cbor(const cardano_voter_t *voter, cardano_cbor_writer_t *writer)¶
Serializes a voter into CBOR format using a CBOR writer.
This function serializes the given cardano_voter_t object using a cardano_cbor_writer_t.
Usage Example:
cardano_voter_t* voter = ...; cardano_cbor_writer_t* writer = cardano_cbor_writer_new(); if (writer) { cardano_error_t result = cardano_voter_to_cbor(voter, 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_voter_unref(&voter);- Parameters:¶
- const cardano_voter_t *voter¶
[in] A constant pointer to the cardano_voter_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
voterorwriteris NULL, returns CARDANO_ERROR_POINTER_IS_NULL.
-
cardano_error_t cardano_voter_to_cip116_json(const cardano_voter_t *voter, cardano_json_writer_t *writer)¶
Serializes a voter object to CIP-116 JSON based on the provided schema.
This function serializes the voter object according to its type:
CC: { “tag”: “cc_credential”, “credential”: { … } }
DRep: { “tag”: “drep_credential”, “credential”: { … } }
SPO: { “tag”: “spo_keyhash”, “pubkey_hash”: “<hex_hash>” }
- Parameters:¶
- const cardano_voter_t *voter¶
[in] Pointer to a valid cardano_voter_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
voterorwriteris NULL. CARDANO_ERROR_INVALID_ARGUMENT If the voter type is unknown. Other Any error propagated from nested writers.
-
cardano_error_t cardano_voter_set_type(cardano_voter_t *voter, cardano_voter_type_t type)¶
Sets the type for a voter.
This function assigns a type to the specified cardano_voter_t object.
Usage Example:
cardano_voter_t* voter = cardano_voter_new(...); cardano_voter_type_t voter_type = CARDANO_VOTER_TYPE_DREP_KEY_HASH; cardano_error_t result = cardano_voter_set_type(voter, voter_type); if (result == CARDANO_SUCCESS) { // The voter type has been set successfully } else { printf("Failed to set the voter type.\n"); } // Clean up the voter object once done cardano_voter_unref(&voter);- Parameters:¶
- cardano_voter_t *voter¶
[inout] A pointer to the cardano_voter_t object for which the type is to be set.
- cardano_voter_type_t type¶
[in] The voter type to set, as defined by cardano_voter_type_t.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the voter type was successfully set, or an appropriate error code indicating the failure reason, such as CARDANO_ERROR_POINTER_IS_NULL if any of the input pointers are NULL.
-
cardano_error_t cardano_voter_get_type(const cardano_voter_t *voter, cardano_voter_type_t *type)¶
Retrieves the type of the voter.
This function retrieves the type of a given cardano_voter_t object and stores it in the provided output parameter. The voter type is defined in the cardano_voter_type_t enumeration.
Usage Example:
cardano_voter_t* voter = cardano_voter_new(...); cardano_voter_type_t type; cardano_error_t result = cardano_voter_get_type(voter, &type); if (result == CARDANO_SUCCESS) { switch(type) { case CARDANO_VOTER_TYPE_CONSTITUTIONAL_COMMITTEE_KEY_HASH: printf("Voter is a constitutional committee member identified by key hash.\n"); break; case CARDANO_VOTER_TYPE_CONSTITUTIONAL_COMMITTEE_SCRIPT_HASH: printf("Voter is a constitutional committee member identified by script hash.\n"); break; case CARDANO_VOTER_TYPE_DREP_KEY_HASH: printf("Voter is a DRep identified by key hash.\n"); break; case CARDANO_VOTER_TYPE_DREP_SCRIPT_HASH: printf("Voter is a DRep identified by script hash.\n"); break; case CARDANO_VOTER_TYPE_STAKE_POOL_KEY_HASH: printf("Voter is a Stake Pool Operator identified by key hash.\n"); break; } } // Clean up the voter object once done cardano_voter_unref(&voter);- Parameters:¶
- const cardano_voter_t *voter¶
[in] A constant pointer to the cardano_voter_t object from which the type is to be retrieved. The object must not be NULL.
- cardano_voter_type_t *type¶
[out] Pointer to a variable where the voter type will be stored. This variable will be set to the value from the cardano_voter_type_t enumeration.
- Returns:¶
CARDANO_SUCCESS if the type was successfully retrieved, or an appropriate error code if the input is NULL or any other error occurs.
-
cardano_credential_t *cardano_voter_get_credential(cardano_voter_t *voter)¶
Retrieves the credential associated with a voter.
This function returns the credential of a cardano_voter_t object. It returns a new reference to a cardano_credential_t object representing the voter’s credential. It is the caller’s responsibility to release it by calling cardano_credential_unref when it is no longer needed.
Usage Example:
cardano_voter_t* original_voter = cardano_voter_new(...); cardano_credential_t* credential = cardano_voter_get_credential(original_voter); if (credential) { // Use the credential // Once done, ensure to clean up and release the credential cardano_credential_unref(&credential); } // Release the original voter after use cardano_voter_unref(&original_voter);- Parameters:¶
- cardano_voter_t *voter¶
[in] A constant pointer to the cardano_voter_t object from which the credential is to be retrieved.
- Returns:¶
A pointer to a new cardano_credential_t object containing the voter’s credential. If the input voter is NULL, returns NULL. The caller is responsible for managing the lifecycle of this object, including releasing it with cardano_credential_unref.
-
cardano_error_t cardano_voter_set_credential(cardano_voter_t *voter, cardano_credential_t *credential)¶
Sets the credential for a voter.
This function associates a credential with a cardano_voter_t object. The function increases the reference count of the credential, meaning the original credential must still be managed by the caller.
Usage Example:
cardano_voter_t* voter = cardano_voter_new(...); cardano_credential_t* credential = cardano_credential_new(...); cardano_error_t result = cardano_voter_set_credential(voter, credential); if (result == CARDANO_SUCCESS) { // Credential has been successfully set to the voter } // Clean up cardano_credential_unref(&credential); // Decrease the reference since it was created separately cardano_voter_unref(&voter); // Release the voter after use- Parameters:¶
- cardano_voter_t *voter¶
[in] A pointer to the cardano_voter_t object to which the credential will be set.
- cardano_credential_t *credential¶
[in] A pointer to the cardano_credential_t object that will be associated with the voter.
- Returns:¶
CARDANO_SUCCESS if the credential was successfully set, or an appropriate error code indicating the failure reason, such as CARDANO_ERROR_POINTER_IS_NULL if any of the input pointers are NULL.
-
bool cardano_voter_equals(const cardano_voter_t *lhs, const cardano_voter_t *rhs)¶
Checks if two voter objects are equal.
This function compares two cardano_voter_t objects for equality. It checks if the contents of the two voter objects are identical.
Usage Example:
cardano_voter_t* voter1 = ...; // Assume voter1 is initialized cardano_voter_t* voter2 = ...; // Assume voter2 is initialized if (cardano_voter_equals(voter1, voter2)) { printf("voter1 is equal to voter2\n"); } else { printf("voter1 is not equal to voter2\n"); } // Clean up the voter objects once done cardano_voter_unref(&voter1); cardano_voter_unref(&voter2);- Parameters:¶
- const cardano_voter_t *lhs¶
[in] A constant pointer to the first cardano_voter_t object to be compared.
- const cardano_voter_t *rhs¶
[in] A constant pointer to the second cardano_voter_t object to be compared.
- Returns:¶
trueif the two voter objects are equal (have the same contents),falseotherwise.
-
void cardano_voter_unref(cardano_voter_t **voter)¶
Decrements the reference count of a voter object.
This function is responsible for managing the lifecycle of a cardano_voter_t object by decreasing its reference count. When the reference count reaches zero, the voter is finalized; its associated resources are released, and its memory is deallocated.
Usage Example:
cardano_voter_t* voter = cardano_voter_new(); // Perform operations with the voter... cardano_voter_unref(&voter); // At this point, voter is NULL and cannot be used.Note
After calling cardano_voter_unref, the pointer to the cardano_voter_t object will be set to NULL to prevent its reuse.
- Parameters:¶
- cardano_voter_t **voter¶
[inout] A pointer to the pointer of the voter 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_voter_ref(cardano_voter_t *voter)¶
Increases the reference count of the cardano_voter_t object.
This function is used to manually increment the reference count of a voter 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_voter_unref.
Usage Example:
// Assuming voter is a previously created voter object cardano_voter_ref(voter); // Now voter can be safely used elsewhere without worrying about premature deallocationNote
Always ensure that for every call to cardano_voter_ref there is a corresponding call to cardano_voter_unref to prevent memory leaks.
- Parameters:¶
- cardano_voter_t *voter¶
A pointer to the voter object whose reference count is to be incremented.
-
size_t cardano_voter_refcount(const cardano_voter_t *voter)¶
Retrieves the current reference count of the cardano_voter_t object.
This function returns the number of active references to a voter object. It’s useful for debugging purposes or managing the lifecycle of the object in complex scenarios.
Usage Example:
// Assuming voter is a previously created voter object size_t ref_count = cardano_voter_refcount(voter); 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_voter_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_voter_t *voter¶
A pointer to the voter object whose reference count is queried. The object must not be NULL.
- Returns:¶
The number of active references to the specified voter object. If the object is properly managed (i.e., every cardano_voter_ref call is matched with a cardano_voter_unref call), this count should reach zero right before the object is deallocated.
-
void cardano_voter_set_last_error(cardano_voter_t *voter, const char *message)¶
Sets the last error message for a given voter object.
Records an error message in the voter’s last_error buffer, overwriting any existing message. This is useful for storing devoterive 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_voter_t *voter¶
[in] A pointer to the cardano_voter_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 voter’s last_error is set to an empty string, indicating no error.
-
const char *cardano_voter_get_last_error(const cardano_voter_t *voter)¶
Retrieves the last error message recorded for a specific voter.
This function returns a pointer to the null-terminated string containing the last error message set by cardano_voter_set_last_error for the given voter. 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_voter_set_last_error for the same voter, or until the voter is deallocated.
- Parameters:¶
- const cardano_voter_t *voter¶
[in] A pointer to the cardano_voter_t instance whose last error message is to be retrieved. If the voter is NULL, the function returns a generic error message indicating the null voter.
- Returns:¶
A pointer to a null-terminated string containing the last error message for the specified voter. If the voter is NULL, “Object is NULL.” is returned to indicate the error.