6#include <ascii-chat/network/consensus/topology.h>
7#include <ascii-chat/common.h>
8#include <ascii-chat/asciichat_errno.h>
24static int find_uuid_position(
const uint8_t uuids[64][16],
int count,
const uint8_t target[16]) {
25 for (
int i = 0; i < count; i++) {
26 if (memcmp(uuids[i], target, 16) == 0) {
34static int qsort_uuid_compare(
const void *a,
const void *b) {
35 return memcmp(a, b, 16);
40 if (!participant_ids || !my_id || !out_topology || num_participants <= 0 || num_participants > 64) {
41 return SET_ERRNO(ERROR_INVALID_PARAM,
"Invalid topology creation parameters");
45 memset(topology, 0,
sizeof(*topology));
48 memcpy(topology->
participant_ids, participant_ids, num_participants * 16);
51 qsort(topology->
participant_ids, num_participants, 16, qsort_uuid_compare);
57 return SET_ERRNO(ERROR_INVALID_PARAM,
"My UUID not found in participant list");
60 *out_topology = topology;
83 if (!topology || !out_id) {
84 return SET_ERRNO(ERROR_INVALID_PARAM,
"Invalid parameters");
93 if (!topology || !out_id) {
94 return SET_ERRNO(ERROR_INVALID_PARAM,
"Invalid parameters");
103 if (!topology || !out_id) {
104 return SET_ERRNO(ERROR_INVALID_PARAM,
"Invalid parameters");
113 if (!topology || !out_ids || !out_count) {
114 return SET_ERRNO(ERROR_INVALID_PARAM,
"Invalid parameters");
Ring topology for consensus participants.
uint8_t participant_ids[64][16]
Participant UUIDs in sorted order.
int my_position
My index in the sorted list.
int num_participants
Total number of participants.
int consensus_topology_get_position(const consensus_topology_t *topology)
asciichat_error_t consensus_topology_get_prev(const consensus_topology_t *topology, uint8_t out_id[16])
void consensus_topology_destroy(consensus_topology_t *topology)
asciichat_error_t consensus_topology_get_leader(const consensus_topology_t *topology, uint8_t out_id[16])
bool consensus_topology_am_leader(const consensus_topology_t *topology)
asciichat_error_t consensus_topology_get_all(const consensus_topology_t *topology, uint8_t out_ids[64][16], int *out_count)
asciichat_error_t consensus_topology_get_next(const consensus_topology_t *topology, uint8_t out_id[16])
asciichat_error_t consensus_topology_create(const uint8_t participant_ids[64][16], int num_participants, const uint8_t my_id[16], consensus_topology_t **out_topology)
struct consensus_topology consensus_topology_t
Ring topology for consensus participants.