ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
election.h File Reference

Deterministic host election algorithm. More...

Go to the source code of this file.

Functions

uint32_t consensus_election_compute_score (const participant_metrics_t *metrics)
 Compute deterministic score for a participant.
 
asciichat_error_t consensus_election_choose_hosts (const participant_metrics_t *metrics, int num_metrics, int *out_best_index, int *out_backup_index)
 Elect best and backup host from metrics.
 
asciichat_error_t consensus_election_verify (const participant_metrics_t *metrics, int num_metrics, const uint8_t announced_host_id[16], const uint8_t announced_backup_id[16], bool *out_valid)
 Verify election result matches expected scores.
 

Detailed Description

Deterministic host election algorithm.

Definition in file election.h.

Function Documentation

◆ consensus_election_choose_hosts()

asciichat_error_t consensus_election_choose_hosts ( const participant_metrics_t *  metrics,
int  num_metrics,
int *  out_best_index,
int *  out_backup_index 
)

Elect best and backup host from metrics.

Returns indices of best and second-best participants. Deterministic tie-breaking ensures identical result on all clients.

Definition at line 67 of file election.c.

68 {
69 if (!metrics || !out_best_index || !out_backup_index || num_metrics < 1) {
70 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid election parameters");
71 }
72
73 // Compute scores for all participants
74 uint32_t *scores = SAFE_MALLOC(sizeof(uint32_t) * num_metrics, uint32_t *);
75
76 for (int i = 0; i < num_metrics; i++) {
77 scores[i] = consensus_election_compute_score(&metrics[i]);
78 }
79
80 // Find best and second-best
81 find_best_two(scores, num_metrics, out_best_index, out_backup_index);
82
83 SAFE_FREE(scores);
84 return ASCIICHAT_OK;
85}
uint32_t consensus_election_compute_score(const participant_metrics_t *metrics)
Compute deterministic score for a participant.
Definition election.c:13
unsigned int uint32_t
Definition common.h:58
#define SAFE_FREE(ptr)
Definition common.h:376
#define SAFE_MALLOC(size, cast)
Definition common.h:264
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
@ ASCIICHAT_OK
Definition error_codes.h:51
@ ERROR_INVALID_PARAM

References ASCIICHAT_OK, consensus_election_compute_score(), ERROR_INVALID_PARAM, SAFE_FREE, SAFE_MALLOC, and SET_ERRNO.

Referenced by consensus_election_verify().

◆ consensus_election_compute_score()

uint32_t consensus_election_compute_score ( const participant_metrics_t *  metrics)

Compute deterministic score for a participant.

Higher score = better host candidate

score = (4 - nat_tier) * 1000 // NAT tier: 0=best, 4=worst

  • (upload_kbps / 10) // Bandwidth bonus
  • (500 - rtt_ms) // Latency bonus
  • stun_probe_success_pct // Network stability

Definition at line 13 of file election.c.

13 {
14 if (!metrics)
15 return 0;
16
17 // NAT tier: 0=LAN (best), 4=TURN (worst)
18 // (4 - tier) * 1000 makes LAN score 4000, TURN score 0
19 uint32_t nat_score = (4 - metrics->nat_tier) * 1000;
20
21 // Bandwidth: higher is better
22 // Divide by 10 to keep in reasonable scale (10Mbps = +1000)
23 uint32_t bw_score = metrics->upload_kbps / 10;
24
25 // Latency: lower is better
26 // (500ms - rtt_ns) means 0ns RTT = +500, 500ms = 0, high latency = negative (but capped at 0)
27 // 500ms = 500,000,000ns
28 uint32_t rtt_score = (metrics->rtt_ns < 500 * NS_PER_MS) ? (500 - (metrics->rtt_ns / NS_PER_MS)) : 0;
29
30 // STUN probe success: 0-100%
31 uint32_t probe_score = metrics->stun_probe_success_pct;
32
33 // Total score
34 uint32_t total = nat_score + bw_score + rtt_score + probe_score;
35 return total;
36}
#define NS_PER_MS
Definition time.h:147

References NS_PER_MS.

Referenced by consensus_election_choose_hosts().

◆ consensus_election_verify()

asciichat_error_t consensus_election_verify ( const participant_metrics_t *  metrics,
int  num_metrics,
const uint8_t  announced_host_id[16],
const uint8_t  announced_backup_id[16],
bool *  out_valid 
)

Verify election result matches expected scores.

Independent verification that leader's decision is correct. Used by all clients to validate election before accepting.

Definition at line 87 of file election.c.

89 {
90 if (!metrics || !announced_host_id || !announced_backup_id || !out_valid) {
91 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid verification parameters");
92 }
93
94 // Find which indices the announced IDs correspond to
95 int host_idx = -1, backup_idx = -1;
96
97 for (int i = 0; i < num_metrics; i++) {
98 if (memcmp(metrics[i].participant_id, announced_host_id, 16) == 0) {
99 host_idx = i;
100 }
101 if (memcmp(metrics[i].participant_id, announced_backup_id, 16) == 0) {
102 backup_idx = i;
103 }
104 }
105
106 // If we can't find announced IDs, result is invalid
107 if (host_idx < 0 || backup_idx < 0) {
108 *out_valid = false;
109 return ASCIICHAT_OK;
110 }
111
112 // Run our own election
113 int computed_host, computed_backup;
114 asciichat_error_t err = consensus_election_choose_hosts(metrics, num_metrics, &computed_host, &computed_backup);
115
116 if (err != ASCIICHAT_OK) {
117 return err;
118 }
119
120 // Check if computed result matches announced
121 *out_valid = (host_idx == computed_host && backup_idx == computed_backup);
122 return ASCIICHAT_OK;
123}
asciichat_error_t consensus_election_choose_hosts(const participant_metrics_t *metrics, int num_metrics, int *out_best_index, int *out_backup_index)
Elect best and backup host from metrics.
Definition election.c:67
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:49
uint8_t participant_id[16]

References ASCIICHAT_OK, consensus_election_choose_hosts(), ERROR_INVALID_PARAM, participant_id, and SET_ERRNO.