Relay¶
-
typedef struct cardano_relay_t cardano_relay_t¶
A relay is a type of node that acts as intermediaries between core nodes (which produce blocks) and the wider internet.
They help in passing along transactions and blocks, ensuring that data is propagated throughout the network.
-
cardano_error_t cardano_relay_new_single_host_addr(cardano_single_host_addr_relay_t *single_host_addr_relay, cardano_relay_t **relay)¶
Creates a new relay object that points to a single host address.
This function allocates and initializes a new instance of a cardano_relay_t, which is configured to point to a single host via its IP address and a specified port. This type of relay is used for direct, single-host communication in the Cardano network.
Usage Example:
cardano_single_host_addr_relay_t* single_host_addr_relay = ...; // Assume single_host_addr_relay is initialized cardano_relay_t* relay = NULL; cardano_error_t result = cardano_relay_new_single_host_addr(single_host_addr_relay, &relay); if (result == CARDANO_SUCCESS) { // Use the relay // Once done, ensure to clean up and release the relay cardano_relay_unref(&relay); } cardano_single_host_addr_relay_unref(&single_host_addr_relay);- Parameters:¶
- cardano_single_host_addr_relay_t *single_host_addr_relay¶
[in] A pointer to an initialized cardano_single_host_addr_relay_t object that specifies the IP address and port of the host.
- cardano_relay_t **relay¶
[out] On successful initialization, this will point to the newly created cardano_relay_t object. This object represents a “strong reference” to the relay, meaning that it is fully initialized and ready for use. The caller is responsible for managing the lifecycle of this object. Specifically, once the relay is no longer needed, the caller must release it by calling cardano_relay_unref.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the relay was successfully created, or an appropriate error code indicating the failure reason.
-
cardano_error_t cardano_relay_new_single_host_name(cardano_single_host_name_relay_t *single_host_name, cardano_relay_t **relay)¶
Creates a new relay object that points to a single host name.
This function allocates and initializes a new instance of cardano_relay_t, configured to point to a single host via its DNS name and a specified port. This relay is useful for Cardano network communication where DNS resolution is preferred over direct IP address connections.
Usage Example:
cardano_single_host_name_relay_t* single_host_name = ...; // Assume single_host_name is initialized cardano_relay_t* relay = NULL; cardano_error_t result = cardano_relay_new_single_host_name(single_host_name, &relay); if (result == CARDANO_SUCCESS) { // Use the relay // Once done, ensure to clean up and release the relay cardano_relay_unref(&relay); } cardano_single_host_name_relay_unref(&single_host_name);- Parameters:¶
- cardano_single_host_name_relay_t *single_host_name¶
[in] A pointer to an initialized cardano_single_host_name_relay_t object that specifies the DNS name and port of the host.
- cardano_relay_t **relay¶
[out] On successful initialization, this will point to the newly created cardano_relay_t object. This object represents a “strong reference” to the relay, indicating that it is fully initialized and ready for use. The caller is responsible for managing the lifecycle of this object. Specifically, once the relay is no longer needed, the caller must release it by calling cardano_relay_unref.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the relay was successfully created, or an appropriate error code indicating the failure reason.
-
cardano_error_t cardano_relay_new_multi_host_name(cardano_multi_host_name_relay_t *multi_host_name_relay, cardano_relay_t **relay)¶
Creates a new relay object that points to multiple host names.
This function allocates and initializes a new instance of cardano_relay_t, configured to point to multiple hosts via a DNS SRV record. This type of relay allows for the resolution of multiple addresses and is useful in environments where high availability and load balancing are required.
Usage Example:
cardano_multi_host_name_relay_t* multi_host_name_relay = ...; // Assume multi_host_name_relay is initialized cardano_relay_t* relay = NULL; cardano_error_t result = cardano_relay_new_multi_host_name(multi_host_name_relay, &relay); if (result == CARDANO_SUCCESS) { // Use the relay // Once done, ensure to clean up and release the relay cardano_relay_unref(&relay); } cardano_multi_host_name_relay_unref(&multi_host_name_relay);- Parameters:¶
- cardano_multi_host_name_relay_t *multi_host_name_relay¶
[in] A pointer to an initialized cardano_multi_host_name_relay_t object that specifies the DNS SRV record.
- cardano_relay_t **relay¶
[out] On successful initialization, this will point to the newly created cardano_relay_t object. This object represents a “strong reference” to the relay, indicating that it is fully initialized and ready for use. The caller is responsible for managing the lifecycle of this object. Specifically, once the relay is no longer needed, the caller must release it by calling cardano_relay_unref.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the relay was successfully created, or an appropriate error code indicating the failure reason.
-
cardano_error_t cardano_relay_from_cbor(cardano_cbor_reader_t *reader, cardano_relay_t **relay)¶
Creates a cardano_relay_t from a CBOR reader.
This function parses CBOR data using a provided cardano_cbor_reader_t and constructs a cardano_relay_t object. It assumes that the CBOR reader is set up correctly and that the CBOR data corresponds to the structure expected for a relay.
Usage Example:
cardano_cbor_reader_t* reader = cardano_cbor_reader_new(cbor_data, data_size); cardano_relay_t* relay = NULL; cardano_error_t result = cardano_relay_from_cbor(reader, &relay); if (result == CARDANO_SUCCESS) { // Use the relay // Once done, ensure to clean up and release the relay cardano_relay_unref(&relay); } else { const char* error = cardano_cbor_reader_get_last_error(reader); printf("Failed to decode relay: %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_relay_t object by calling cardano_relay_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 data.
- cardano_relay_t **relay¶
[out] A pointer to a pointer of cardano_relay_t that will be set to the address of the newly created relay object upon successful decoding.
- Returns:¶
A cardano_error_t value indicating the outcome of the operation. Returns CARDANO_SUCCESS if the object was successfully created, or an appropriate error code if an error occurred.
-
cardano_error_t cardano_relay_to_cbor(const cardano_relay_t *relay, cardano_cbor_writer_t *writer)¶
Serializes protocol version into CBOR format using a CBOR writer.
This function serializes the given cardano_relay_t object using a cardano_cbor_writer_t.
Usage Example:
cardano_relay_t* relay = ...; cardano_cbor_writer_t* writer = cardano_cbor_writer_new(); if (writer) { cardano_error_t result = cardano_relay_to_cbor(relay, 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_relay_unref(&relay);- Parameters:¶
- const cardano_relay_t *relay¶
[in] A constant pointer to the cardano_relay_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
relayorwriteris NULL, returns CARDANO_ERROR_POINTER_IS_NULL.
-
cardano_error_t cardano_relay_to_cip116_json(const cardano_relay_t *relay, cardano_json_writer_t *writer)¶
Serializes a relay to CIP-116 JSON.
This function detects the specific type of the relay (single host address, single host name, or multi host name) and serializes it accordingly.
- Parameters:¶
- const cardano_relay_t *relay¶
[in] Pointer to a valid cardano_relay_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
relayorwriteris NULL. CARDANO_ERROR_INVALID_ARGUMENT If the relay type is unknown. Other Any error propagated from nested writers.
-
cardano_error_t cardano_relay_get_type(const cardano_relay_t *relay, cardano_relay_type_t *type)¶
Retrieves the type of a relay.
This function provides the type of a cardano_relay_t object, which indicates the specific configuration of the relay (e.g., single host address, single host name, or multi host name).
Usage Example:
const cardano_relay_t* relay = ...; // Assume relay is already initialized cardano_relay_type_t type; cardano_error_t result = cardano_relay_type(relay, &type); if (result == CARDANO_SUCCESS) { // Process depending on the type switch (type) { case CARDANO_RELAY_TYPE_SINGLE_HOST_ADDRESS: // Handle single host address relay break; case CARDANO_RELAY_TYPE_SINGLE_HOST_NAME: // Handle single host name relay break; case CARDANO_RELAY_TYPE_MULTI_HOST_NAME: // Handle multi host name relay break; } } else { printf("Failed to retrieve relay type.\n"); } cardano_relay_unref(&relay);- Parameters:¶
- const cardano_relay_t *relay¶
[in] A constant pointer to an initialized cardano_relay_t object.
- cardano_relay_type_t *type¶
[out] A pointer to a cardano_relay_type_t variable where the type of the relay will be stored upon successful execution.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the type was successfully retrieved, or an appropriate error code indicating the failure reason, such as CARDANO_ERROR_POINTER_IS_NULL if the input pointers are NULL.
-
cardano_error_t cardano_relay_to_single_host_addr(cardano_relay_t *relay, cardano_single_host_addr_relay_t **single_host_addr)¶
Converts a relay to a single host address relay if possible and provides a new reference.
This function attempts to convert a generic cardano_relay_t object to a more specific cardano_single_host_addr_relay_t object. This operation will only succeed if the relay is of the single host address type. On success, it returns a new reference to the single host address relay, which must be managed separately from the original relay.
Usage Example:
cardano_relay_t* relay = ...; // Assume relay is already initialized cardano_single_host_addr_relay_t* single_host_addr; cardano_error_t result = cardano_relay_to_single_host_addr(relay, &single_host_addr); if (result == CARDANO_SUCCESS) { // Use the single_host_addr // When done, release the newly created single_host_addr cardano_single_host_addr_relay_unref(single_host_addr); } else { printf("Failed to convert relay to single host address.\n"); } // Original relay must also be unref when it is no longer needed cardano_relay_unref(&relay);Note
The caller is responsible for managing the lifecycle of both the original and the newly converted relay objects. Both must be released with appropriate unref calls when no longer needed.
- Parameters:¶
- cardano_relay_t *relay¶
[in] A pointer to an initialized cardano_relay_t object.
- cardano_single_host_addr_relay_t **single_host_addr¶
[out] A pointer to a pointer of cardano_single_host_addr_relay_t which will point to the single host address relay representation if the conversion is successful. This parameter is set only if the operation succeeds and will hold a new reference.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the relay was successfully converted, or an appropriate error code indicating the failure reason. Common failure reasons include incorrect relay type or NULL pointer inputs.
-
cardano_error_t cardano_relay_to_single_host_name(cardano_relay_t *relay, cardano_single_host_name_relay_t **single_host_name)¶
Converts a relay to a single host name relay if possible and provides a new reference.
This function attempts to convert a generic cardano_relay_t object to a more specific cardano_single_host_name_relay_t object. This operation will only succeed if the relay is of the single host name type. On success, it returns a new reference to the single host name relay, which must be managed separately from the original relay.
Usage Example:
cardano_relay_t* relay = ...; // Assume relay is already initialized cardano_single_host_name_relay_t* single_host_name; cardano_error_t result = cardano_relay_to_single_host_name(relay, &single_host_name); if (result == CARDANO_SUCCESS) { // Use the single_host_name // When done, release the newly created single_host_name cardano_single_host_name_relay_unref(single_host_name); } else { printf("Failed to convert relay to single host name.\n"); } // Original relay must also be unref when it is no longer needed cardano_relay_unref(&relay);Note
The caller is responsible for managing the lifecycle of both the original and the newly converted relay objects. Both must be released with appropriate unref calls when no longer needed.
- Parameters:¶
- cardano_relay_t *relay¶
[in] A pointer to an initialized cardano_relay_t object.
- cardano_single_host_name_relay_t **single_host_name¶
[out] A pointer to a pointer of cardano_single_host_name_relay_t which will point to the single host name relay representation if the conversion is successful. This parameter is set only if the operation succeeds and will hold a new reference.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the relay was successfully converted, or an appropriate error code indicating the failure reason. Common failure reasons include incorrect relay type or NULL pointer inputs.
-
cardano_error_t cardano_relay_to_multi_host_name(cardano_relay_t *relay, cardano_multi_host_name_relay_t **multi_host_name)¶
Converts a relay to a multi host name relay if possible and provides a new reference.
This function attempts to convert a generic cardano_relay_t object to a more specific cardano_multi_host_name_relay_t object. This operation will only succeed if the relay is of the multi host name type. On success, it returns a new reference to the multi host name relay, which must be managed separately from the original relay.
Usage Example:
cardano_relay_t* relay = ...; // Assume relay is already initialized cardano_multi_host_name_relay_t* multi_host_name; cardano_error_t result = cardano_relay_to_multi_host_name(relay, &multi_host_name); if (result == CARDANO_SUCCESS) { // Use the multi_host_name // When done, release the newly created multi_host_name cardano_multi_host_name_relay_unref(multi_host_name); } else { printf("Failed to convert relay to multi host name.\n"); } // Original relay must also be unref when it is no longer needed cardano_relay_unref(&relay);Note
The caller is responsible for managing the lifecycle of both the original and the newly converted relay objects. Both must be released with appropriate unref calls when no longer needed.
- Parameters:¶
- cardano_relay_t *relay¶
[in] A pointer to an initialized cardano_relay_t object.
- cardano_multi_host_name_relay_t **multi_host_name¶
[out] A pointer to a pointer of cardano_multi_host_name_relay_t which will point to the multi host name relay representation if the conversion is successful. This parameter is set only if the operation succeeds and will hold a new reference.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the relay was successfully converted, or an appropriate error code indicating the failure reason. Common failure reasons include incorrect relay type or NULL pointer inputs.
-
void cardano_relay_unref(cardano_relay_t **relay)¶
Decrements the reference count of a cardano_relay_t object.
This function is responsible for managing the lifecycle of a cardano_relay_t object by decreasing its reference count. When the reference count reaches zero, the relay is finalized; its associated resources are released, and its memory is deallocated.
Usage Example:
cardano_relay_t* relay = cardano_relay_new(major, minor); // Perform operations with the relay... cardano_relay_unref(&relay); // At this point, relay is NULL and cannot be used.Note
After calling cardano_relay_unref, the pointer to the cardano_relay_t object will be set to NULL to prevent its reuse.
- Parameters:¶
- cardano_relay_t **relay¶
[inout] A pointer to the pointer of the relay 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_relay_ref(cardano_relay_t *relay)¶
Increases the reference count of the cardano_relay_t object.
This function is used to manually increment the reference count of an cardano_relay_t 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_relay_unref.
Usage Example:
// Assuming relay is a previously created relay object cardano_relay_ref(relay); // Now relay can be safely used elsewhere without worrying about premature deallocationNote
Always ensure that for every call to cardano_relay_ref there is a corresponding call to cardano_relay_unref to prevent memory leaks.
- Parameters:¶
- cardano_relay_t *relay¶
A pointer to the cardano_relay_t object whose reference count is to be incremented.
-
size_t cardano_relay_refcount(const cardano_relay_t *relay)¶
Retrieves the current reference count of the cardano_relay_t object.
This function returns the number of active references to an cardano_relay_t object. It’s useful for debugging purposes or managing the lifecycle of the object in complex scenarios.
Usage Example:
// Assuming relay is a previously created relay object size_t ref_count = cardano_relay_refcount(relay); 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_relay_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_relay_t *relay¶
A pointer to the cardano_relay_t object whose reference count is queried. The object must not be NULL.
- Returns:¶
The number of active references to the specified cardano_relay_t object. If the object is properly managed (i.e., every cardano_relay_ref call is matched with a cardano_relay_unref call), this count should reach zero right before the object is deallocated.
-
void cardano_relay_set_last_error(cardano_relay_t *relay, const char *message)¶
Sets the last error message for a given cardano_relay_t object.
Records an error message in the relay’s last_error buffer, overwriting any existing message. This is useful for storing descriptive 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_relay_t *relay¶
[in] A pointer to the cardano_relay_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 relay’s last_error is set to an empty string, indicating no error.
-
const char *cardano_relay_get_last_error(const cardano_relay_t *relay)¶
Retrieves the last error message recorded for a specific relay.
This function returns a pointer to the null-terminated string containing the last error message set by cardano_relay_set_last_error for the given relay. 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_relay_set_last_error for the same relay, or until the relay is deallocated.
- Parameters:¶
- const cardano_relay_t *relay¶
[in] A pointer to the cardano_relay_t instance whose last error message is to be retrieved. If the relay is NULL, the function returns a generic error message indicating the null relay.
- Returns:¶
A pointer to a null-terminated string containing the last error message for the specified relay. If the relay is NULL, “Object is NULL.” is returned to indicate the error.