|
| 1 | +#include "nanorq.h" |
| 2 | +#include <assert.h> |
| 3 | +#include <stdbool.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +#define BLOCK_SIZE_SYMBOLS 100 |
| 9 | +#define SYMBOL_SIZE 256 |
| 10 | +#define NUM_NODES 5 |
| 11 | + |
| 12 | +typedef struct { |
| 13 | + int id; |
| 14 | + nanorq *decoder; |
| 15 | + struct ioctx *dec_io; |
| 16 | + uint8_t *decoded_payload; |
| 17 | + bool has_decoded; |
| 18 | + uint32_t rx_count; |
| 19 | +} p2p_node; |
| 20 | + |
| 21 | +p2p_node *node_new(int id, uint64_t common, uint32_t specific) { |
| 22 | + p2p_node *n = calloc(1, sizeof(p2p_node)); |
| 23 | + n->id = id; |
| 24 | + n->decoder = nanorq_decoder_new(common, specific); |
| 25 | + assert(n->decoder != NULL); |
| 26 | + nanorq_set_max_esi(n->decoder, 1000000); |
| 27 | + n->decoded_payload = calloc(1, BLOCK_SIZE_SYMBOLS * SYMBOL_SIZE); |
| 28 | + n->dec_io = |
| 29 | + ioctx_from_mem(n->decoded_payload, BLOCK_SIZE_SYMBOLS * SYMBOL_SIZE); |
| 30 | + return n; |
| 31 | +} |
| 32 | + |
| 33 | +void node_receive(p2p_node *n, uint8_t *coefs, uint8_t *payload, uint32_t tag) { |
| 34 | + if (n->has_decoded) |
| 35 | + return; /* ignore if synced */ |
| 36 | + |
| 37 | + n->rx_count++; |
| 38 | + |
| 39 | + /* try adding the symbol */ |
| 40 | + int res = nanorq_decoder_add_recoded_symbol(n->decoder, payload, tag, coefs, |
| 41 | + n->dec_io); |
| 42 | + if (res == NANORQ_SYM_ADDED || res == NANORQ_SYM_DUP) { |
| 43 | + size_t missing = nanorq_num_missing(n->decoder, 0); |
| 44 | + size_t repair = nanorq_num_repair(n->decoder, 0); |
| 45 | + if (repair >= missing) { |
| 46 | + if (nanorq_repair_block(n->decoder, n->dec_io, 0)) { |
| 47 | + n->has_decoded = true; |
| 48 | + printf("[Node %d] BLOCK SYNCED! (Received %d packets)\n", n->id, |
| 49 | + n->rx_count); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +int main() { |
| 56 | + uint32_t payload_len = BLOCK_SIZE_SYMBOLS * SYMBOL_SIZE; |
| 57 | + uint8_t *block_data = malloc(payload_len); |
| 58 | + for (uint32_t i = 0; i < payload_len; i++) |
| 59 | + block_data[i] = rand() % 256; |
| 60 | + |
| 61 | + struct ioctx *src_io = ioctx_from_mem(block_data, payload_len); |
| 62 | + |
| 63 | + /* miner who mined the block */ |
| 64 | + nanorq *miner = |
| 65 | + nanorq_encoder_new_ex(payload_len, SYMBOL_SIZE, BLOCK_SIZE_SYMBOLS, 1, 1); |
| 66 | + printf("[Miner] Mined new block! Size: %d KB. Encoding with TSNC...\n\n", |
| 67 | + payload_len / 1024); |
| 68 | + |
| 69 | + uint64_t common = nanorq_oti_common(miner); |
| 70 | + uint32_t specific = nanorq_oti_scheme_specific(miner); |
| 71 | + |
| 72 | + /* p2p network topology */ |
| 73 | + p2p_node *nodes[NUM_NODES]; |
| 74 | + for (int i = 0; i < NUM_NODES; i++) |
| 75 | + nodes[i] = node_new(i, common, specific); |
| 76 | + |
| 77 | + uint8_t *coef_buf = malloc(BLOCK_SIZE_SYMBOLS); |
| 78 | + uint8_t *payload_buf = malloc(SYMBOL_SIZE); |
| 79 | + |
| 80 | + int rounds = 0; |
| 81 | + bool all_synced = false; |
| 82 | + |
| 83 | + /* simulate network ticks */ |
| 84 | + uint32_t tag_idx = 0; |
| 85 | + while (!all_synced && rounds < 500) { |
| 86 | + rounds++; |
| 87 | + uint32_t current_tag = nanorq_tag(0, tag_idx++); |
| 88 | + |
| 89 | + /* miner streams to node 0 */ |
| 90 | + nanorq_generate_recoded_symbol(miner, src_io, 0, rounds, coef_buf, |
| 91 | + payload_buf); |
| 92 | + node_receive(nodes[0], coef_buf, payload_buf, current_tag); |
| 93 | + |
| 94 | + /* gossip recoded packets to next node */ |
| 95 | + for (int i = 0; i < NUM_NODES - 1; i++) { |
| 96 | + p2p_node *sender = nodes[i]; |
| 97 | + p2p_node *receiver = nodes[i + 1]; |
| 98 | + |
| 99 | + /* generate recoded packet if sender has buffered packets */ |
| 100 | + if (sender->rx_count > 0 && !receiver->has_decoded) { |
| 101 | + /* simulate 10% packet loss */ |
| 102 | + if ((rand() % 100) > 10) { |
| 103 | + uint32_t recoded_tag = nanorq_tag(0, tag_idx++); |
| 104 | + if (sender->has_decoded) { |
| 105 | + nanorq_generate_recoded_symbol(sender->decoder, sender->dec_io, 0, |
| 106 | + tag_idx, coef_buf, payload_buf); |
| 107 | + } else { |
| 108 | + nanorq_generate_recoded_symbol(sender->decoder, NULL, 0, 0, |
| 109 | + coef_buf, payload_buf); |
| 110 | + } |
| 111 | + node_receive(receiver, coef_buf, payload_buf, recoded_tag); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /* check completion */ |
| 117 | + all_synced = true; |
| 118 | + for (int i = 0; i < NUM_NODES; i++) { |
| 119 | + if (!nodes[i]->has_decoded) |
| 120 | + all_synced = false; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + printf("\n[Network] Global consensus reached in %d ticks!\n", rounds); |
| 125 | + |
| 126 | + /* verification */ |
| 127 | + int failed = 0; |
| 128 | + for (int i = 0; i < NUM_NODES; i++) { |
| 129 | + if (memcmp(block_data, nodes[i]->decoded_payload, payload_len) != 0) { |
| 130 | + failed++; |
| 131 | + printf("[Verification] Node %d FAILED! Differences:\n", i); |
| 132 | + for (uint32_t sym = 0; sym < BLOCK_SIZE_SYMBOLS; sym++) { |
| 133 | + if (memcmp(block_data + sym * SYMBOL_SIZE, |
| 134 | + nodes[i]->decoded_payload + sym * SYMBOL_SIZE, |
| 135 | + SYMBOL_SIZE) != 0) { |
| 136 | + printf(" Symbol %d mismatch:\n", sym); |
| 137 | + printf(" Src: "); |
| 138 | + for (int k = 0; k < 16; k++) |
| 139 | + printf("%02x ", block_data[sym * SYMBOL_SIZE + k]); |
| 140 | + printf("\n Dec: "); |
| 141 | + for (int k = 0; k < 16; k++) |
| 142 | + printf("%02x ", nodes[i]->decoded_payload[sym * SYMBOL_SIZE + k]); |
| 143 | + printf("\n"); |
| 144 | + break; /* only print first mismatch per node */ |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + nanorq_free(nodes[i]->decoder); |
| 150 | + nodes[i]->dec_io->destroy(nodes[i]->dec_io); |
| 151 | + free(nodes[i]->decoded_payload); |
| 152 | + free(nodes[i]); |
| 153 | + } |
| 154 | + |
| 155 | + if (failed == 0) { |
| 156 | + printf("[Verification] SUCCESS: All nodes correctly decoded!\n"); |
| 157 | + } else { |
| 158 | + printf("[Verification] FAILED on %d nodes!\n", failed); |
| 159 | + } |
| 160 | + |
| 161 | + free(coef_buf); |
| 162 | + free(payload_buf); |
| 163 | + free(block_data); |
| 164 | + src_io->destroy(src_io); |
| 165 | + nanorq_free(miner); |
| 166 | + |
| 167 | + return failed ? 1 : 0; |
| 168 | +} |
0 commit comments