#include <stdio.h>
#include <string.h>
#include <tashi-vertex/tashi-vertex.h>
TVContext* context = NULL;
TVEngine* engine = NULL;
void handle_message_recv(TVResult result, TVMessage message, void* data, void* user_data) {
switch (message) {
case TV_MESSAGE_NONE:
return; // shutting down
case TV_MESSAGE_EVENT: {
TVEvent* event = (TVEvent*)data;
size_t transactions = 0;
tv_event_get_transaction_count(event, &transactions);
if (transactions != 0) {
printf(" > Received EVENT with %zu transaction(s)\n", transactions);
}
tv_free(event);
break;
}
case TV_MESSAGE_SYNC_POINT: {
TVSyncPoint* sync_point = (TVSyncPoint*)data;
tv_free(sync_point);
printf(" > Received SYNC POINT\n");
break;
}
}
// listen for the next message
tv_message_recv(engine, handle_message_recv, NULL);
}
void handle_socket_bound(TVResult result, TVSocket* socket, void* user_data) {
TVOptions* options = NULL;
tv_options_new(&options);
TVKeySecret secret = /* ... */;
TVPeers* peers = /* ... */;
// start the consensus engine
// ownership of socket, options, and peers is transferred to the engine
tv_engine_start(context, &socket, &options, &secret, &peers, &engine);
// send a transaction
uint8_t* buffer = NULL;
size_t size = 5;
tv_transaction_allocate(size, &buffer);
memcpy(buffer, "hello", size);
tv_transaction_send(engine, buffer, size);
// start receiving messages
tv_message_recv(engine, handle_message_recv, NULL);
}
int main() {
tv_context_new(&context);
tv_socket_bind(context, "127.0.0.1:9000", handle_socket_bound, NULL);
// blocks until all async operations complete
tv_free(context);
return 0;
}
:: Configured network for 3 peers
:: Initialized runtime
:: Bound local socket
:: Started the consensus engine
> Received SYNC POINT
> Received EVENT with 1 transaction(s)
> Received EVENT with 1 transaction(s)
> Received EVENT with 1 transaction(s)