Handling events
Responding to the library's events.

Event handling

Events will be passed to the user using the callback passed to init the Vertices library, from vertex_t:
1
typedef struct
2
{
3
provider_info_t *provider;
4
ret_code_t
5
(*vertices_evt_handler)(vtc_evt_t *evt);
6
} vertex_t;
Copied!
You can see here that the handler vertices_evt_handler takes a pointer to vtc_evt_t:
1
/// Events contains a type and a bufid. When implementing the event handler, the `bufid`
2
/// should be used to retrieve the data to be processed. Data type depends on event type.
3
typedef struct
4
{
5
vtc_evt_type_t type; ///< \see vtc_evt_type_t
6
size_t bufid; ///< internal buffer ID, used to identify a pending transaction
7
} vtc_evt_t;
Copied!
Two things are passed to the user: the event type and a bufid which is an identifier to the internal transaction being processed. In order to get the event's transaction, the user must use the function available:
1
ret_code_t
2
vertices_event_tx_get(size_t bufid, signed_transaction_t **tx);
Copied!
This function will set a local pointer to the signed_transaction_t generating the event.
Here are the different events needing the user attention:
1
/// Asynchronous operations can be handled using Vertices events types
2
typedef enum
3
{
4
VTC_EVT_TX_READY_TO_SIGN = 0, ///< transaction's payload must be signed: the user must provide the signing function
5
VTC_EVT_TX_SENDING, ///< transaction is being sent to the blockchain API. When the user got the event, the transaction has probably been already sent.
6
VTC_EVT_TX_SUCCESS, ///< transaction has been successfully sent and executed, after that event, the buffer is freed.
7
} vtc_evt_type_t;
Copied!
Let's go through the handling of those events.

Signing

VTC_EVT_TX_READY_TO_SIGN
This is the most critical event to handle: signing the payload. The payload is divided into two parts: the header and the body. Only the body part has to be signed, but with a subtility: we need to add the "TX" string before the body.
The first step is to get a pointer to the payload using vertices_event_tx_get:
1
typedef struct
2
{
3
unsigned char payload[TX_PAYLOAD_MAX_LENGTH]; ///< Full payload, comprised of the header + body. The body part is the signed part (prepended with "TX")
4
size_t
5
payload_header_length; ///< Header size. Do not use for signing. Full \c payload size is: \c payload_header_length + \c payload_body_length
6
size_t
7
payload_body_length; ///< Body size. Length of data to be signed. if not 0, indicates that TX is pending.
8
unsigned char signature[SIGNATURE_LENGTH];
9
unsigned char id[TRANSACTION_HASH_STR_MAX_LENGTH
10
]; ///< Unique Identifier of the transaction. It can be used to find the transaction in the ledger
11
} signed_transaction_t;
Copied!
We now have the payload with payload_header_length and payload_body_length. In order to have the byte array to sign, we need to init a new byte array starting with "TX" and then copy the full body into that byte array.
Now that we have a buffer ready to be signed, sign it using the Ed25519 algorithm. A private key will be necessary. Put the generated signature into the signed_transaction_t, signature field.
Once done and to send the transaction, we need to emit the event VTC_EVT_TX_SENDING using:
1
evt->type = VTC_EVT_TX_SENDING;
2
err_code = vertices_event_schedule(evt);
Copied!

Sending

VTC_EVT_TX_SENDING
The transaction is being sent. When the user gets that event in the event handler, the event has already been processed by the library, meaning the transaction has been sent.
We can use that event to have the full payload of the packed transaction (message pack).

Transaction success

VTC_EVT_TX_SUCCESS
Nice! The transaction has passed. I guess you can print a hoora message.

Event processing

Last but not least, for the Vertices SDK to process the event, we need to give the library some CPU time by calling as many times as needed the event processing routine:
1
/// Call this function to have the Vertices SDK process pending events. Whenever calling a Vertices
2
/// function, make sure to process all the events.
3
/// To make sure all the events are processed, this function should be called from an infinite loop or OS thread/task.
4
/// This function will call the user-defined callback passed when using \see vertices_new
5
/// \param queue_size
6
/// \return \c VTC_SUCCESS if event has correctly been processed
7
ret_code_t
8
vertices_event_process(size_t *queue_size);
Copied!
queue_size can give the user an indication about the queued events waiting to be processed but it is not mandatory.
Depending on whether you are using an RTOS or not, you might want to call this function from an indefinite loop.
Here is the link to our implementation of the given step.
You can use the repository as a template to get you started. 🚀
If you have more questions, don't hesitate to open an issue on the Github repository.
Last modified 3mo ago