| … | … | @@ -0,0 +1,228 @@ |
| 1 | // Gate: after unannounce, NO node on a real testnet still holds our record. |
| 2 | // |
| 3 | // test_unannounce_live <host:port[,host:port...]> |
| 4 | // driven by test/run_unannounce_live.js, which stands up the testnet |
| 5 | // |
| 6 | // ⛔ THE MEASUREMENT IS "HOW MANY NODES STILL ANSWER WITH US", not "did |
| 7 | // unannounce return". A LOOKUP after the withdrawal reports one FoundPeer per |
| 8 | // answering node, tagged with the node that answered — so counting distinct |
| 9 | // answering nodes whose peer list holds our key is the quantity that matters, |
| 10 | // and nothing below it is evidence. |
| 11 | // |
| 12 | // It was written to settle docs/PARITY-hyperswarm.md A2, which claimed our |
| 13 | // commit-phase withdrawal reaches fewer nodes than JS's during-traversal one. |
| 14 | // It settled it the other way: see §7.5. The gate stays because "unannounce |
| 15 | // actually withdraws, from every node, on a real network" is worth holding on |
| 16 | // to whatever the answer to A2 turned out to be. |
| 17 | |
| 18 | #include <cstdio> |
| 19 | #include <cstdlib> |
| 20 | #include <cstring> |
| 21 | #include <set> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include <sodium.h> |
| 26 | |
| 27 | #include "dht_rpc.h" |
| 28 | #include "hyperdht.h" |
| 29 | |
| 30 | extern "C" { |
| 31 | #include <udx.h> |
| 32 | #include <uv.h> |
| 33 | } |
| 34 | |
| 35 | enum Phase { BOOTSTRAPPING, ANNOUNCING, LOOKUP_BEFORE, UNANNOUNCING, LOOKUP_AFTER, DONE }; |
| 36 | |
| 37 | struct Ctx { |
| 38 | uv_loop_t loop; |
| 39 | udx_t udx; |
| 40 | udx_socket_t socket; |
| 41 | uv_timer_t timer; |
| 42 | uv_timer_t deadline; |
| 43 | DhtRpc rpc; |
| 44 | HyperDht dht; |
| 45 | uint8_t topic[32]; |
| 46 | uint8_t our_pk[32]; |
| 47 | |
| 48 | Phase phase = BOOTSTRAPPING; |
| 49 | uint64_t phase_until = 0; |
| 50 | std::set<std::string> holders_before, holders_after; |
| 51 | std::set<std::string> *collecting = nullptr; |
| 52 | int phase_ms = 8000; |
| 53 | }; |
| 54 | |
| 55 | static std::string addr_key(const IPv4Address &a) { |
| 56 | char b[32]; |
| 57 | snprintf(b, sizeof(b), "%d.%d.%d.%d:%d", a.ip[0], a.ip[1], a.ip[2], a.ip[3], a.port); |
| 58 | return b; |
| 59 | } |
| 60 | |
| 61 | static void on_socket_recv(udx_socket_t *sock, ssize_t read_len, const uv_buf_t *buf, |
| 62 | const struct sockaddr *from) { |
| 63 | Ctx *ctx = static_cast<Ctx *>(sock->data); |
| 64 | if (!ctx || read_len <= 0) return; |
| 65 | const uint8_t *data = (const uint8_t *)buf->base; |
| 66 | if ((size_t)read_len >= 4 && (data[0] == DHT_REQUEST_ID || data[0] == DHT_RESPONSE_ID)) { |
| 67 | ctx->rpc.on_message(data, (size_t)read_len, (const struct sockaddr_in *)from); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static void on_tick(uv_timer_t *h) { |
| 72 | Ctx *ctx = static_cast<Ctx *>(h->data); |
| 73 | const uint64_t now = uv_now(&ctx->loop); |
| 74 | const int phase_ms = ctx->phase_ms; |
| 75 | ctx->rpc.tick(now); |
| 76 | ctx->dht.tick(now); |
| 77 | |
| 78 | switch (ctx->phase) { |
| 79 | case BOOTSTRAPPING: |
| 80 | if (!ctx->rpc.is_bootstrapped()) break; |
| 81 | printf(" bootstrapped; announcing the topic\n"); |
| 82 | ctx->dht.announce(ctx->topic); |
| 83 | ctx->phase = ANNOUNCING; |
| 84 | ctx->phase_until = now + phase_ms; |
| 85 | break; |
| 86 | |
| 87 | case ANNOUNCING: |
| 88 | if (now < ctx->phase_until) break; |
| 89 | printf(" looking the topic up BEFORE the unannounce\n"); |
| 90 | ctx->collecting = &ctx->holders_before; |
| 91 | ctx->dht.lookup(ctx->topic); |
| 92 | ctx->phase = LOOKUP_BEFORE; |
| 93 | ctx->phase_until = now + phase_ms; |
| 94 | break; |
| 95 | |
| 96 | case LOOKUP_BEFORE: |
| 97 | if (now < ctx->phase_until) break; |
| 98 | printf(" %zu node(s) hold the record; unannouncing\n", ctx->holders_before.size()); |
| 99 | ctx->collecting = nullptr; |
| 100 | ctx->dht.unannounce(ctx->topic); |
| 101 | ctx->phase = UNANNOUNCING; |
| 102 | ctx->phase_until = now + phase_ms + 4000; |
| 103 | break; |
| 104 | |
| 105 | case UNANNOUNCING: |
| 106 | if (now < ctx->phase_until) break; |
| 107 | printf(" looking the topic up AFTER the unannounce\n"); |
| 108 | ctx->collecting = &ctx->holders_after; |
| 109 | ctx->dht.lookup(ctx->topic); |
| 110 | ctx->phase = LOOKUP_AFTER; |
| 111 | ctx->phase_until = now + phase_ms + 4000; |
| 112 | break; |
| 113 | |
| 114 | case LOOKUP_AFTER: |
| 115 | if (now < ctx->phase_until) break; |
| 116 | ctx->phase = DONE; |
| 117 | uv_stop(&ctx->loop); |
| 118 | break; |
| 119 | |
| 120 | case DONE: |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static void on_deadline(uv_timer_t *h) { |
| 126 | uv_stop(&static_cast<Ctx *>(h->data)->loop); |
| 127 | } |
| 128 | |
| 129 | int main(int argc, char **argv) { |
| 130 | if (sodium_init() < 0) return 1; |
| 131 | if (argc < 2) { printf("usage: %s <host:port,...> [phase_ms]\n", argv[0]); return 2; } |
| 132 | // A bigger testnet needs longer for an announce to settle and for a lookup |
| 133 | // to walk it. Passed in rather than guessed, because a phase that ends early |
| 134 | // reports "no holders" and that is indistinguishable from a real absence. |
| 135 | const int phase_ms = (argc > 2) ? atoi(argv[2]) : 8000; |
| 136 | |
| 137 | std::vector<std::string> boots; |
| 138 | { |
| 139 | std::string all(argv[1]); |
| 140 | size_t p = 0; |
| 141 | while (p <= all.size()) { |
| 142 | size_t c = all.find(',', p); |
| 143 | if (c == std::string::npos) c = all.size(); |
| 144 | if (c > p) boots.push_back(all.substr(p, c - p)); |
| 145 | p = c + 1; |
| 146 | } |
| 147 | } |
| 148 | std::vector<const char *> bootv; |
| 149 | for (auto &b : boots) bootv.push_back(b.c_str()); |
| 150 | printf(" bootstrap: %s\n", argv[1]); |
| 151 | |
| 152 | Ctx *ctx = new Ctx(); |
| 153 | uv_loop_init(&ctx->loop); |
| 154 | udx_init(&ctx->loop, &ctx->udx, nullptr); |
| 155 | udx_socket_init(&ctx->udx, &ctx->socket, nullptr); |
| 156 | ctx->socket.data = ctx; |
| 157 | |
| 158 | struct sockaddr_in bind_addr; |
| 159 | uv_ip4_addr("0.0.0.0", 0, &bind_addr); |
| 160 | udx_socket_bind(&ctx->socket, (struct sockaddr *)&bind_addr, 0); |
| 161 | udx_socket_recv_start(&ctx->socket, on_socket_recv); |
| 162 | |
| 163 | ctx->rpc.init(&ctx->socket, &ctx->loop); |
| 164 | ctx->rpc.set_bootstrap(bootv.data(), (int)bootv.size()); |
| 165 | |
| 166 | struct sockaddr_in local_addr; |
| 167 | int alen = sizeof(local_addr); |
| 168 | udx_socket_getsockname(&ctx->socket, (struct sockaddr *)&local_addr, &alen); |
| 169 | IPv4Address local; |
| 170 | memcpy(local.ip, &local_addr.sin_addr.s_addr, 4); |
| 171 | local.port = ntohs(local_addr.sin_port); |
| 172 | ctx->rpc.set_local_address(local); |
| 173 | |
| 174 | Ed25519KeyPair kp; |
| 175 | crypto_sign_keypair(kp.public_key, kp.secret_key); |
| 176 | memcpy(ctx->our_pk, kp.public_key, 32); |
| 177 | ctx->dht.init(&ctx->rpc, kp); |
| 178 | ctx->phase_ms = phase_ms; |
| 179 | randombytes_buf(ctx->topic, 32); |
| 180 | |
| 181 | // One FoundPeer per answering node per peer entry. We only care about the |
| 182 | // entries that are US, and about WHICH node answered with them. |
| 183 | ctx->dht.on_peer_found = [ctx](uint32_t, const FoundPeer &fp) { |
| 184 | if (!ctx->collecting) return; |
| 185 | if (memcmp(fp.public_key, ctx->our_pk, 32) != 0) return; |
| 186 | ctx->collecting->insert(addr_key(fp.relay_node)); |
| 187 | }; |
| 188 | |
| 189 | ctx->rpc.bootstrap(); |
| 190 | |
| 191 | uv_timer_init(&ctx->loop, &ctx->timer); |
| 192 | ctx->timer.data = ctx; |
| 193 | uv_timer_start(&ctx->timer, on_tick, 0, 20); |
| 194 | |
| 195 | uv_timer_init(&ctx->loop, &ctx->deadline); |
| 196 | ctx->deadline.data = ctx; |
| 197 | uv_timer_start(&ctx->deadline, on_deadline, (uint64_t)phase_ms * 6 + 40000, 0); |
| 198 | |
| 199 | uv_run(&ctx->loop, UV_RUN_DEFAULT); |
| 200 | |
| 201 | int checks = 0, failures = 0; |
| 202 | auto check = [&](bool c, const char *what) { |
| 203 | checks++; |
| 204 | if (c) { printf(" PASS: %s\n", what); return; } |
| 205 | failures++; |
| 206 | printf(" FAIL: %s\n", what); |
| 207 | }; |
| 208 | |
| 209 | printf("\n"); |
| 210 | check(ctx->phase == DONE, "the whole sequence ran"); |
| 211 | // ⛔ BASELINE. Without holders before, "no holders after" means nothing — |
| 212 | // it is the same shape as a gate reporting silence from a swarm that never |
| 213 | // meshed. |
| 214 | check(!ctx->holders_before.empty(), |
| 215 | "at least one node held the record BEFORE the unannounce"); |
| 216 | check(ctx->holders_after.empty(), |
| 217 | "NO node still holds the record after the unannounce"); |
| 218 | |
| 219 | printf("\n holders before=%zu after=%zu\n", |
| 220 | ctx->holders_before.size(), ctx->holders_after.size()); |
| 221 | if (!ctx->holders_after.empty()) { |
| 222 | printf(" still holding:"); |
| 223 | for (const std::string &a : ctx->holders_after) printf(" %s", a.c_str()); |
| 224 | printf("\n"); |
| 225 | } |
| 226 | printf("\n%d/%d passed\n", checks - failures, checks); |
| 227 | return failures ? 1 : 0; |
| 228 | } |