From 8810ce72416e03862c6f55da83bc4ee4b090700a Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 15 Jun 2021 06:37:39 +0930 Subject: [PATCH] gossipd/test: remove routing tests. We're about to remove routing from gossipd. Signed-off-by: Rusty Russell --- gossipd/test/run-bench-find_route.c | 279 ------------------------- gossipd/test/run-find_route-specific.c | 256 ----------------------- gossipd/test/run-find_route.c | 276 ------------------------ gossipd/test/run-overlong.c | 196 ----------------- 4 files changed, 1007 deletions(-) delete mode 100644 gossipd/test/run-bench-find_route.c delete mode 100644 gossipd/test/run-find_route-specific.c delete mode 100644 gossipd/test/run-find_route.c delete mode 100644 gossipd/test/run-overlong.c diff --git a/gossipd/test/run-bench-find_route.c b/gossipd/test/run-bench-find_route.c deleted file mode 100644 index 80b1533c9..000000000 --- a/gossipd/test/run-bench-find_route.c +++ /dev/null @@ -1,279 +0,0 @@ -#include "config.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "../routing.c" -#include "../gossip_store.c" - -void status_fmt(enum log_level level UNUSED, - const struct node_id *node_id, - const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - vprintf(fmt, ap); - printf("\n"); - va_end(ap); -} - -/* AUTOGENERATED MOCKS START */ -/* Generated stub for cupdate_different */ -bool cupdate_different(struct gossip_store *gs UNNEEDED, - const struct half_chan *hc UNNEEDED, - const u8 *cupdate UNNEEDED) -{ fprintf(stderr, "cupdate_different called!\n"); abort(); } -/* Generated stub for fmt_wireaddr_without_port */ -char *fmt_wireaddr_without_port(const tal_t *ctx UNNEEDED, const struct wireaddr *a UNNEEDED) -{ fprintf(stderr, "fmt_wireaddr_without_port called!\n"); abort(); } -/* Generated stub for fromwire_wireaddr_array */ -struct wireaddr *fromwire_wireaddr_array(const tal_t *ctx UNNEEDED, const u8 *ser UNNEEDED) -{ fprintf(stderr, "fromwire_wireaddr_array called!\n"); abort(); } -/* Generated stub for json_add_member */ -void json_add_member(struct json_stream *js UNNEEDED, - const char *fieldname UNNEEDED, - bool quote UNNEEDED, - const char *fmt UNNEEDED, ...) -{ fprintf(stderr, "json_add_member called!\n"); abort(); } -/* Generated stub for json_member_direct */ -char *json_member_direct(struct json_stream *js UNNEEDED, - const char *fieldname UNNEEDED, size_t extra UNNEEDED) -{ fprintf(stderr, "json_member_direct called!\n"); abort(); } -/* Generated stub for json_object_end */ -void json_object_end(struct json_stream *js UNNEEDED) -{ fprintf(stderr, "json_object_end called!\n"); abort(); } -/* Generated stub for json_object_start */ -void json_object_start(struct json_stream *ks UNNEEDED, const char *fieldname UNNEEDED) -{ fprintf(stderr, "json_object_start called!\n"); abort(); } -/* Generated stub for memleak_add_helper_ */ -void memleak_add_helper_(const tal_t *p UNNEEDED, void (*cb)(struct htable *memtable UNNEEDED, - const tal_t *)){ } -/* Generated stub for nannounce_different */ -bool nannounce_different(struct gossip_store *gs UNNEEDED, - const struct node *node UNNEEDED, - const u8 *nannounce UNNEEDED) -{ fprintf(stderr, "nannounce_different called!\n"); abort(); } -/* Generated stub for notleak_ */ -void *notleak_(const void *ptr UNNEEDED, bool plus_children UNNEEDED) -{ fprintf(stderr, "notleak_ called!\n"); abort(); } -/* Generated stub for peer_supplied_good_gossip */ -void peer_supplied_good_gossip(struct peer *peer UNNEEDED, size_t amount UNNEEDED) -{ fprintf(stderr, "peer_supplied_good_gossip called!\n"); abort(); } -/* Generated stub for private_channel_announcement */ -const u8 *private_channel_announcement(const tal_t *ctx UNNEEDED, - const struct short_channel_id *scid UNNEEDED, - const struct node_id *local_node_id UNNEEDED, - const struct node_id *remote_node_id UNNEEDED, - const u8 *features UNNEEDED) -{ fprintf(stderr, "private_channel_announcement called!\n"); abort(); } -/* Generated stub for sanitize_error */ -char *sanitize_error(const tal_t *ctx UNNEEDED, const u8 *errmsg UNNEEDED, - struct channel_id *channel_id UNNEEDED) -{ fprintf(stderr, "sanitize_error called!\n"); abort(); } -/* Generated stub for status_failed */ -void status_failed(enum status_failreason code UNNEEDED, - const char *fmt UNNEEDED, ...) -{ fprintf(stderr, "status_failed called!\n"); abort(); } -/* Generated stub for towire_warningfmt */ -u8 *towire_warningfmt(const tal_t *ctx UNNEEDED, - const struct channel_id *channel UNNEEDED, - const char *fmt UNNEEDED, ...) -{ fprintf(stderr, "towire_warningfmt called!\n"); abort(); } -/* AUTOGENERATED MOCKS END */ - -#if DEVELOPER -/* Generated stub for memleak_remove_htable */ -void memleak_remove_htable(struct htable *memtable UNNEEDED, const struct htable *ht UNNEEDED) -{ fprintf(stderr, "memleak_remove_htable called!\n"); abort(); } -/* Generated stub for memleak_remove_intmap_ */ -void memleak_remove_intmap_(struct htable *memtable UNNEEDED, const struct intmap *m UNNEEDED) -{ fprintf(stderr, "memleak_remove_intmap_ called!\n"); abort(); } -#endif - -/* NOOP for new_reltimer_ */ -struct oneshot *new_reltimer_(struct timers *timers UNNEEDED, - const tal_t *ctx UNNEEDED, - struct timerel expire UNNEEDED, - void (*cb)(void *) UNNEEDED, void *arg UNNEEDED) -{ - return NULL; -} - -/* Updates existing route if required. */ -static void add_connection(struct routing_state *rstate, - const struct node_id *nodes, - u32 from, u32 to, - u32 base_fee, s32 proportional_fee, - u32 delay) -{ - struct short_channel_id scid; - struct half_chan *c; - struct chan *chan; - int idx = node_id_idx(&nodes[from], &nodes[to]); - - /* Encode src and dst in scid. */ - memcpy((char *)&scid + idx * sizeof(from), &from, sizeof(from)); - memcpy((char *)&scid + (!idx) * sizeof(to), &to, sizeof(to)); - - chan = get_channel(rstate, &scid); - if (!chan) { - chan = new_chan(rstate, &scid, &nodes[from], &nodes[to], - AMOUNT_SAT(1000000), NULL); - } - - c = &chan->half[idx]; - c->base_fee = base_fee; - c->proportional_fee = proportional_fee; - c->delay = delay; - c->channel_flags = node_id_idx(&nodes[from], &nodes[to]); - /* This must be non-zero, otherwise we consider it disabled! */ - c->bcast.index = 1; - c->htlc_maximum = AMOUNT_MSAT(-1ULL); - c->htlc_minimum = AMOUNT_MSAT(0); -} - -static struct node_id nodeid(size_t n) -{ - struct node_id id; - struct pubkey k; - struct secret s; - - memset(&s, 0xFF, sizeof(s)); - memcpy(&s, &n, sizeof(n)); - pubkey_from_secret(&s, &k); - node_id_from_pubkey(&id, &k); - return id; -} - -static void populate_random_node(struct routing_state *rstate, - const struct node_id *nodes, - u32 n) -{ - /* Create 2 random channels. */ - if (n < 1) - return; - - for (size_t i = 0; i < 2; i++) { - u32 randnode = pseudorand(n); - - add_connection(rstate, nodes, n, randnode, - pseudorand(1000), - pseudorand(1000), - pseudorand(144)); - add_connection(rstate, nodes, randnode, n, - pseudorand(1000), - pseudorand(1000), - pseudorand(144)); - } -} - -static void run(const char *name) -{ - int status; - - switch (fork()) { - case 0: - execlp(name, name, NULL); - exit(127); - case -1: - err(1, "forking %s", name); - default: - wait(&status); - if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) - errx(1, "%s failed", name); - } -} - -int main(int argc, char *argv[]) -{ - struct routing_state *rstate; - size_t num_nodes = 100, num_runs = 1; - struct timemono start, end; - size_t route_lengths[ROUTING_MAX_HOPS+1]; - struct node_id me; - struct node_id *nodes; - bool perfme = false; - const double riskfactor = 0.01 / BLOCKS_PER_YEAR / 10000; - struct siphash_seed base_seed; - - common_setup(argv[0]); - - me = nodeid(0); - rstate = new_routing_state(tmpctx, &me, NULL, NULL, NULL, - false, false); - opt_register_noarg("--perfme", opt_set_bool, &perfme, - "Run perfme-start and perfme-stop around benchmark"); - - opt_parse(&argc, argv, opt_log_stderr_exit); - - if (argc > 1) - num_nodes = atoi(argv[1]); - if (argc > 2) - num_runs = atoi(argv[2]); - if (argc > 3) - opt_usage_and_exit("[num_nodes [num_runs]]"); - - printf("Creating nodes...\n"); - nodes = tal_arr(rstate, struct node_id, num_nodes); - for (size_t i = 0; i < num_nodes; i++) - nodes[i] = nodeid(i); - - printf("Populating nodes...\n"); - memset(&base_seed, 0, sizeof(base_seed)); - for (size_t i = 0; i < num_nodes; i++) - populate_random_node(rstate, nodes, i); - - if (perfme) - run("perfme-start"); - - printf("Starting...\n"); - memset(route_lengths, 0, sizeof(route_lengths)); - start = time_mono(); - for (size_t i = 0; i < num_runs; i++) { - const struct node_id *from = &nodes[pseudorand(num_nodes)]; - const struct node_id *to = &nodes[pseudorand(num_nodes)]; - struct amount_msat fee; - struct chan **route; - size_t num_hops; - - route = find_route(tmpctx, rstate, from, to, - (struct amount_msat){pseudorand(100000)}, - riskfactor, - 0.75, &base_seed, - ROUTING_MAX_HOPS, - &fee); - num_hops = tal_count(route); - assert(num_hops < ARRAY_SIZE(route_lengths)); - route_lengths[num_hops]++; - tal_free(route); - } - end = time_mono(); - - if (perfme) - run("perfme-stop"); - - printf("%zu (%zu succeeded) routes in %zu nodes in %"PRIu64" msec (%"PRIu64" nanoseconds per route)\n", - num_runs, num_runs - route_lengths[0], num_nodes, - time_to_msec(timemono_between(end, start)), - time_to_nsec(time_divide(timemono_between(end, start), num_runs))); - for (size_t i = 0; i < ARRAY_SIZE(route_lengths); i++) - if (route_lengths[i]) - printf(" Length %zu: %zu\n", i, route_lengths[i]); - - common_shutdown(); - opt_free_table(); - return 0; -} diff --git a/gossipd/test/run-find_route-specific.c b/gossipd/test/run-find_route-specific.c deleted file mode 100644 index 8431e25a5..000000000 --- a/gossipd/test/run-find_route-specific.c +++ /dev/null @@ -1,256 +0,0 @@ -/* We can't seem to route the following: - * - * Expect route 03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf -> 0230ad0e74ea03976b28fda587bb75bdd357a1938af4424156a18265167f5e40ae -> 02ea622d5c8d6143f15ed3ce1d501dd0d3d09d3b1c83a44d0034949f8a9ab60f06 - * - * getchannels: - * {'channels': [{'active': True, 'short_id': '6990x2x1/1', 'fee_per_kw': 10, 'delay': 5, 'message_flags': 0, 'channel_flags': 1, 'destination': '0230ad0e74ea03976b28fda587bb75bdd357a1938af4424156a18265167f5e40ae', 'source': '02ea622d5c8d6143f15ed3ce1d501dd0d3d09d3b1c83a44d0034949f8a9ab60f06', 'last_update': 1504064344}, {'active': True, 'short_id': '6989x2x1/0', 'fee_per_kw': 10, 'delay': 5, 'message_flags': 0, 'channel_flags': 0, 'destination': '03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf', 'source': '0230ad0e74ea03976b28fda587bb75bdd357a1938af4424156a18265167f5e40ae', 'last_update': 1504064344}, {'active': True, 'short_id': '6990x2x1/0', 'fee_per_kw': 10, 'delay': 5, 'message_flags': 0, 'channel_flags': 0, 'destination': '02ea622d5c8d6143f15ed3ce1d501dd0d3d09d3b1c83a44d0034949f8a9ab60f06', 'source': '0230ad0e74ea03976b28fda587bb75bdd357a1938af4424156a18265167f5e40ae', 'last_update': 1504064344}, {'active': True, 'short_id': '6989x2x1/1', 'fee_per_kw': 10, 'delay': 5, 'message_flags': 0, 'channel_flags': 1, 'destination': '0230ad0e74ea03976b28fda587bb75bdd357a1938af4424156a18265167f5e40ae', 'source': '03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf', 'last_update': 1504064344}]} - */ -#include -#include -#include - -#include -#define status_fmt(level, node_id, fmt, ...) \ - do { (void)node_id; printf((fmt) ,##__VA_ARGS__); printf("\n"); } while(0) - -#include "../routing.c" -#include "../gossip_store.c" - -/* AUTOGENERATED MOCKS START */ -/* Generated stub for cupdate_different */ -bool cupdate_different(struct gossip_store *gs UNNEEDED, - const struct half_chan *hc UNNEEDED, - const u8 *cupdate UNNEEDED) -{ fprintf(stderr, "cupdate_different called!\n"); abort(); } -/* Generated stub for fmt_wireaddr_without_port */ -char *fmt_wireaddr_without_port(const tal_t *ctx UNNEEDED, const struct wireaddr *a UNNEEDED) -{ fprintf(stderr, "fmt_wireaddr_without_port called!\n"); abort(); } -/* Generated stub for fromwire_wireaddr_array */ -struct wireaddr *fromwire_wireaddr_array(const tal_t *ctx UNNEEDED, const u8 *ser UNNEEDED) -{ fprintf(stderr, "fromwire_wireaddr_array called!\n"); abort(); } -/* Generated stub for json_add_member */ -void json_add_member(struct json_stream *js UNNEEDED, - const char *fieldname UNNEEDED, - bool quote UNNEEDED, - const char *fmt UNNEEDED, ...) -{ fprintf(stderr, "json_add_member called!\n"); abort(); } -/* Generated stub for json_member_direct */ -char *json_member_direct(struct json_stream *js UNNEEDED, - const char *fieldname UNNEEDED, size_t extra UNNEEDED) -{ fprintf(stderr, "json_member_direct called!\n"); abort(); } -/* Generated stub for json_object_end */ -void json_object_end(struct json_stream *js UNNEEDED) -{ fprintf(stderr, "json_object_end called!\n"); abort(); } -/* Generated stub for json_object_start */ -void json_object_start(struct json_stream *ks UNNEEDED, const char *fieldname UNNEEDED) -{ fprintf(stderr, "json_object_start called!\n"); abort(); } -/* Generated stub for memleak_add_helper_ */ -void memleak_add_helper_(const tal_t *p UNNEEDED, void (*cb)(struct htable *memtable UNNEEDED, - const tal_t *)){ } -/* Generated stub for nannounce_different */ -bool nannounce_different(struct gossip_store *gs UNNEEDED, - const struct node *node UNNEEDED, - const u8 *nannounce UNNEEDED) -{ fprintf(stderr, "nannounce_different called!\n"); abort(); } -/* Generated stub for notleak_ */ -void *notleak_(const void *ptr UNNEEDED, bool plus_children UNNEEDED) -{ fprintf(stderr, "notleak_ called!\n"); abort(); } -/* Generated stub for peer_supplied_good_gossip */ -void peer_supplied_good_gossip(struct peer *peer UNNEEDED, size_t amount UNNEEDED) -{ fprintf(stderr, "peer_supplied_good_gossip called!\n"); abort(); } -/* Generated stub for private_channel_announcement */ -const u8 *private_channel_announcement(const tal_t *ctx UNNEEDED, - const struct short_channel_id *scid UNNEEDED, - const struct node_id *local_node_id UNNEEDED, - const struct node_id *remote_node_id UNNEEDED, - const u8 *features UNNEEDED) -{ fprintf(stderr, "private_channel_announcement called!\n"); abort(); } -/* Generated stub for sanitize_error */ -char *sanitize_error(const tal_t *ctx UNNEEDED, const u8 *errmsg UNNEEDED, - struct channel_id *channel_id UNNEEDED) -{ fprintf(stderr, "sanitize_error called!\n"); abort(); } -/* Generated stub for status_failed */ -void status_failed(enum status_failreason code UNNEEDED, - const char *fmt UNNEEDED, ...) -{ fprintf(stderr, "status_failed called!\n"); abort(); } -/* Generated stub for towire_warningfmt */ -u8 *towire_warningfmt(const tal_t *ctx UNNEEDED, - const struct channel_id *channel UNNEEDED, - const char *fmt UNNEEDED, ...) -{ fprintf(stderr, "towire_warningfmt called!\n"); abort(); } -/* AUTOGENERATED MOCKS END */ - -#if DEVELOPER -/* Generated stub for memleak_remove_htable */ -void memleak_remove_htable(struct htable *memtable UNNEEDED, const struct htable *ht UNNEEDED) -{ fprintf(stderr, "memleak_remove_htable called!\n"); abort(); } -/* Generated stub for memleak_remove_intmap_ */ -void memleak_remove_intmap_(struct htable *memtable UNNEEDED, const struct intmap *m UNNEEDED) -{ fprintf(stderr, "memleak_remove_intmap_ called!\n"); abort(); } -#endif - -/* NOOP for new_reltimer_ */ -struct oneshot *new_reltimer_(struct timers *timers UNNEEDED, - const tal_t *ctx UNNEEDED, - struct timerel expire UNNEEDED, - void (*cb)(void *) UNNEEDED, void *arg UNNEEDED) -{ - return NULL; -} - -const void *trc; - -static struct half_chan * -get_or_make_connection(struct routing_state *rstate, - const struct node_id *from_id, - const struct node_id *to_id, - const char *shortid, - struct amount_sat satoshis) -{ - struct short_channel_id scid; - struct chan *chan; - const int idx = node_id_idx(from_id, to_id); - - if (!short_channel_id_from_str(shortid, strlen(shortid), &scid)) - abort(); - chan = get_channel(rstate, &scid); - if (!chan) - chan = new_chan(rstate, &scid, from_id, to_id, satoshis, NULL); - - /* Make sure it's seen as initialized (index non-zero). */ - chan->half[idx].bcast.index = 1; - chan->half[idx].htlc_minimum = AMOUNT_MSAT(0); - if (!amount_sat_to_msat(&chan->half[idx].htlc_maximum, satoshis)) - abort(); - - return &chan->half[idx]; -} - -static bool channel_is_between(const struct chan *chan, - const struct node_id *a, const struct node_id *b) -{ - if (node_id_eq(&chan->nodes[0]->id, a) - && node_id_eq(&chan->nodes[1]->id, b)) - return true; - - if (node_id_eq(&chan->nodes[0]->id, b) - && node_id_eq(&chan->nodes[1]->id, a)) - return true; - - return false; -} - -int main(int argc, char *argv[]) -{ - struct half_chan *nc; - struct routing_state *rstate; - struct node_id a, b, c, d; - struct amount_msat fee; - struct chan **route; - const double riskfactor = 1.0 / BLOCKS_PER_YEAR / 10000; - - common_setup(argv[0]); - - node_id_from_hexstr("03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf", - strlen("03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf"), - &a); - node_id_from_hexstr("0230ad0e74ea03976b28fda587bb75bdd357a1938af4424156a18265167f5e40ae", - strlen("0230ad0e74ea03976b28fda587bb75bdd357a1938af4424156a18265167f5e40ae"), - &b); - node_id_from_hexstr("02ea622d5c8d6143f15ed3ce1d501dd0d3d09d3b1c83a44d0034949f8a9ab60f06", - strlen("02ea622d5c8d6143f15ed3ce1d501dd0d3d09d3b1c83a44d0034949f8a9ab60f06"), - &c); - node_id_from_hexstr("02cca6c5c966fcf61d121e3a70e03a1cd9eeeea024b26ea666ce974d43b242e636", - strlen("02cca6c5c966fcf61d121e3a70e03a1cd9eeeea024b26ea666ce974d43b242e636"), - &d); - - rstate = new_routing_state(tmpctx, &a, NULL, NULL, NULL, false, false); - - /* [{'active': True, 'short_id': '6990:2:1/1', 'fee_per_kw': 10, 'delay': 5, 'message_flags': 0, 'channel_flags': 1, 'destination': '0230ad0e74ea03976b28fda587bb75bdd357a1938af4424156a18265167f5e40ae', 'source': '02ea622d5c8d6143f15ed3ce1d501dd0d3d09d3b1c83a44d0034949f8a9ab60f06', 'last_update': 1504064344}, */ - - nc = get_or_make_connection(rstate, &c, &b, "6990x2x1", AMOUNT_SAT(1000)); - nc->base_fee = 0; - nc->proportional_fee = 10; - nc->delay = 5; - nc->channel_flags = 1; - nc->message_flags = 0; - nc->bcast.timestamp = 1504064344; - - /* {'active': True, 'short_id': '6989:2:1/0', 'fee_per_kw': 10, 'delay': 5, 'message_flags': 0, 'channel_flags': 0, 'destination': '03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf', 'source': '0230ad0e74ea03976b28fda587bb75bdd357a1938af4424156a18265167f5e40ae', 'last_update': 1504064344}, */ - nc = get_or_make_connection(rstate, &b, &a, "6989x2x1", AMOUNT_SAT(1000)); - nc->base_fee = 0; - nc->proportional_fee = 10; - nc->delay = 5; - nc->channel_flags = 0; - nc->message_flags = 0; - nc->bcast.timestamp = 1504064344; - - /* {'active': True, 'short_id': '6990:2:1/0', 'fee_per_kw': 10, 'delay': 5, 'message_flags': 0, 'channel_flags': 0, 'destination': '02ea622d5c8d6143f15ed3ce1d501dd0d3d09d3b1c83a44d0034949f8a9ab60f06', 'source': '0230ad0e74ea03976b28fda587bb75bdd357a1938af4424156a18265167f5e40ae', 'last_update': 1504064344}, */ - nc = get_or_make_connection(rstate, &b, &c, "6990x2x1", AMOUNT_SAT(1000)); - nc->base_fee = 0; - nc->proportional_fee = 10; - nc->delay = 5; - nc->channel_flags = 0; - nc->message_flags = 0; - nc->bcast.timestamp = 1504064344; - nc->htlc_minimum = AMOUNT_MSAT(100); - - /* {'active': True, 'short_id': '6989:2:1/1', 'fee_per_kw': 10, 'delay': 5, 'message_flags': 0, 'channel_flags': 1, 'destination': '0230ad0e74ea03976b28fda587bb75bdd357a1938af4424156a18265167f5e40ae', 'source': '03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf', 'last_update': 1504064344}]} */ - nc = get_or_make_connection(rstate, &a, &b, "6989x2x1", AMOUNT_SAT(1000)); - nc->base_fee = 0; - nc->proportional_fee = 10; - nc->delay = 5; - nc->channel_flags = 1; - nc->message_flags = 0; - nc->bcast.timestamp = 1504064344; - - route = find_route(tmpctx, rstate, &a, &c, AMOUNT_MSAT(100000), riskfactor, 0.0, NULL, - ROUTING_MAX_HOPS, &fee); - assert(route); - assert(tal_count(route) == 2); - assert(channel_is_between(route[0], &a, &b)); - assert(channel_is_between(route[1], &b, &c)); - - - /* We should not be able to find a route that exceeds our own capacity */ - route = find_route(tmpctx, rstate, &a, &c, AMOUNT_MSAT(1000001), riskfactor, 0.0, NULL, - ROUTING_MAX_HOPS, &fee); - assert(!route); - - /* Now test with a query that exceeds the channel capacity after adding - * some fees */ - route = find_route(tmpctx, rstate, &a, &c, AMOUNT_MSAT(999999), riskfactor, 0.0, NULL, - ROUTING_MAX_HOPS, &fee); - assert(!route); - - /* This should fail to return a route because it is smaller than these - * htlc_minimum_msat on the last channel. */ - route = find_route(tmpctx, rstate, &a, &c, AMOUNT_MSAT(1), riskfactor, 0.0, NULL, - ROUTING_MAX_HOPS, &fee); - assert(!route); - - /* {'active': True, 'short_id': '6990:2:1/0', 'fee_per_kw': 10, 'delay': 5, 'message_flags': 1, 'htlc_maximum_msat': 500000, 'htlc_minimum_msat': 100, 'channel_flags': 0, 'destination': '02cca6c5c966fcf61d121e3a70e03a1cd9eeeea024b26ea666ce974d43b242e636', 'source': '03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf', 'last_update': 1504064344}, */ - nc = get_or_make_connection(rstate, &a, &d, "6991x2x1", AMOUNT_SAT(1000)); - nc->base_fee = 0; - nc->proportional_fee = 0; - nc->delay = 5; - nc->channel_flags = 0; - nc->message_flags = 1; - nc->bcast.timestamp = 1504064344; - nc->htlc_minimum = AMOUNT_MSAT(100); - nc->htlc_maximum = AMOUNT_MSAT(500000); /* half capacity */ - - /* This should route correctly at the max_msat level */ - route = find_route(tmpctx, rstate, &a, &d, AMOUNT_MSAT(500000), riskfactor, 0.0, NULL, - ROUTING_MAX_HOPS, &fee); - assert(route); - - /* This should fail to return a route because it's larger than the - * htlc_maximum_msat on the last channel. */ - route = find_route(tmpctx, rstate, &a, &d, AMOUNT_MSAT(500001), riskfactor, 0.0, NULL, - ROUTING_MAX_HOPS, &fee); - assert(!route); - - common_shutdown(); - return 0; -} diff --git a/gossipd/test/run-find_route.c b/gossipd/test/run-find_route.c deleted file mode 100644 index 1e63f6700..000000000 --- a/gossipd/test/run-find_route.c +++ /dev/null @@ -1,276 +0,0 @@ -#include "../routing.c" -#include "../gossip_store.c" -#include -#include -#include - -void status_fmt(enum log_level level UNUSED, - const struct node_id *node_id, - const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - vprintf(fmt, ap); - printf("\n"); - va_end(ap); -} - -/* AUTOGENERATED MOCKS START */ -/* Generated stub for cupdate_different */ -bool cupdate_different(struct gossip_store *gs UNNEEDED, - const struct half_chan *hc UNNEEDED, - const u8 *cupdate UNNEEDED) -{ fprintf(stderr, "cupdate_different called!\n"); abort(); } -/* Generated stub for fmt_wireaddr_without_port */ -char *fmt_wireaddr_without_port(const tal_t *ctx UNNEEDED, const struct wireaddr *a UNNEEDED) -{ fprintf(stderr, "fmt_wireaddr_without_port called!\n"); abort(); } -/* Generated stub for fromwire_wireaddr_array */ -struct wireaddr *fromwire_wireaddr_array(const tal_t *ctx UNNEEDED, const u8 *ser UNNEEDED) -{ fprintf(stderr, "fromwire_wireaddr_array called!\n"); abort(); } -/* Generated stub for json_add_member */ -void json_add_member(struct json_stream *js UNNEEDED, - const char *fieldname UNNEEDED, - bool quote UNNEEDED, - const char *fmt UNNEEDED, ...) -{ fprintf(stderr, "json_add_member called!\n"); abort(); } -/* Generated stub for json_member_direct */ -char *json_member_direct(struct json_stream *js UNNEEDED, - const char *fieldname UNNEEDED, size_t extra UNNEEDED) -{ fprintf(stderr, "json_member_direct called!\n"); abort(); } -/* Generated stub for json_object_end */ -void json_object_end(struct json_stream *js UNNEEDED) -{ fprintf(stderr, "json_object_end called!\n"); abort(); } -/* Generated stub for json_object_start */ -void json_object_start(struct json_stream *ks UNNEEDED, const char *fieldname UNNEEDED) -{ fprintf(stderr, "json_object_start called!\n"); abort(); } -/* Generated stub for memleak_add_helper_ */ -void memleak_add_helper_(const tal_t *p UNNEEDED, void (*cb)(struct htable *memtable UNNEEDED, - const tal_t *)){ } -/* Generated stub for nannounce_different */ -bool nannounce_different(struct gossip_store *gs UNNEEDED, - const struct node *node UNNEEDED, - const u8 *nannounce UNNEEDED) -{ fprintf(stderr, "nannounce_different called!\n"); abort(); } -/* Generated stub for notleak_ */ -void *notleak_(const void *ptr UNNEEDED, bool plus_children UNNEEDED) -{ fprintf(stderr, "notleak_ called!\n"); abort(); } -/* Generated stub for peer_supplied_good_gossip */ -void peer_supplied_good_gossip(struct peer *peer UNNEEDED, size_t amount UNNEEDED) -{ fprintf(stderr, "peer_supplied_good_gossip called!\n"); abort(); } -/* Generated stub for private_channel_announcement */ -const u8 *private_channel_announcement(const tal_t *ctx UNNEEDED, - const struct short_channel_id *scid UNNEEDED, - const struct node_id *local_node_id UNNEEDED, - const struct node_id *remote_node_id UNNEEDED, - const u8 *features UNNEEDED) -{ fprintf(stderr, "private_channel_announcement called!\n"); abort(); } -/* Generated stub for sanitize_error */ -char *sanitize_error(const tal_t *ctx UNNEEDED, const u8 *errmsg UNNEEDED, - struct channel_id *channel_id UNNEEDED) -{ fprintf(stderr, "sanitize_error called!\n"); abort(); } -/* Generated stub for status_failed */ -void status_failed(enum status_failreason code UNNEEDED, - const char *fmt UNNEEDED, ...) -{ fprintf(stderr, "status_failed called!\n"); abort(); } -/* Generated stub for towire_warningfmt */ -u8 *towire_warningfmt(const tal_t *ctx UNNEEDED, - const struct channel_id *channel UNNEEDED, - const char *fmt UNNEEDED, ...) -{ fprintf(stderr, "towire_warningfmt called!\n"); abort(); } -/* AUTOGENERATED MOCKS END */ - -#if DEVELOPER -/* Generated stub for memleak_remove_htable */ -void memleak_remove_htable(struct htable *memtable UNNEEDED, const struct htable *ht UNNEEDED) -{ fprintf(stderr, "memleak_remove_htable called!\n"); abort(); } -/* Generated stub for memleak_remove_intmap_ */ -void memleak_remove_intmap_(struct htable *memtable UNNEEDED, const struct intmap *m UNNEEDED) -{ fprintf(stderr, "memleak_remove_intmap_ called!\n"); abort(); } -#endif - -/* NOOP for new_reltimer_ */ -struct oneshot *new_reltimer_(struct timers *timers UNNEEDED, - const tal_t *ctx UNNEEDED, - struct timerel expire UNNEEDED, - void (*cb)(void *) UNNEEDED, void *arg UNNEEDED) -{ - return NULL; -} - -static void node_id_from_privkey(const struct privkey *p, struct node_id *id) -{ - struct pubkey k; - pubkey_from_privkey(p, &k); - node_id_from_pubkey(id, &k); -} - -/* Updates existing route if required. */ -static void add_connection(struct routing_state *rstate, - const struct node_id *from, - const struct node_id *to, - u32 base_fee, s32 proportional_fee, - u32 delay) -{ - struct short_channel_id scid; - struct half_chan *c; - struct chan *chan; - struct amount_sat satoshis = AMOUNT_SAT(100000); - - /* Make a unique scid. */ - memcpy(&scid, from, sizeof(scid) / 2); - memcpy((char *)&scid + sizeof(scid) / 2, to, sizeof(scid) / 2); - - chan = get_channel(rstate, &scid); - if (!chan) - chan = new_chan(rstate, &scid, from, to, satoshis, NULL); - - c = &chan->half[node_id_idx(from, to)]; - /* Make sure it's seen as initialized (index non-zero). */ - c->bcast.index = 1; - c->base_fee = base_fee; - c->proportional_fee = proportional_fee; - c->delay = delay; - c->channel_flags = node_id_idx(from, to); - c->htlc_minimum = AMOUNT_MSAT(0); - c->htlc_maximum = AMOUNT_MSAT(100000 * 1000); -} - -/* Returns chan connecting from and to: *idx set to refer - * to connection with src=from, dst=to */ -static struct chan *find_channel(struct routing_state *rstate UNUSED, - const struct node *from, - const struct node *to, - int *idx) -{ - struct chan_map_iter i; - struct chan *c; - - *idx = node_id_idx(&from->id, &to->id); - - for (c = first_chan(to, &i); c; c = next_chan(to, &i)) { - if (c->nodes[*idx] == from) - return c; - } - return NULL; -} - -static struct half_chan *get_connection(struct routing_state *rstate, - const struct node_id *from_id, - const struct node_id *to_id) -{ - int idx; - struct node *from, *to; - struct chan *c; - - from = get_node(rstate, from_id); - to = get_node(rstate, to_id); - if (!from || ! to) - return NULL; - - c = find_channel(rstate, from, to, &idx); - if (!c) - return NULL; - return &c->half[idx]; -} - -static bool channel_is_between(const struct chan *chan, - const struct node_id *a, const struct node_id *b) -{ - if (node_id_eq(&chan->nodes[0]->id, a) - && node_id_eq(&chan->nodes[1]->id, b)) - return true; - - if (node_id_eq(&chan->nodes[0]->id, b) - && node_id_eq(&chan->nodes[1]->id, a)) - return true; - - return false; -} - -int main(int argc, char *argv[]) -{ - struct routing_state *rstate; - struct node_id a, b, c, d; - struct privkey tmp; - struct amount_msat fee; - struct chan **route; - const double riskfactor = 1.0 / BLOCKS_PER_YEAR / 10000; - - common_setup(argv[0]); - - memset(&tmp, 'a', sizeof(tmp)); - node_id_from_privkey(&tmp, &a); - rstate = new_routing_state(tmpctx, &a, NULL, NULL, NULL, false, false); - - new_node(rstate, &a); - - memset(&tmp, 'b', sizeof(tmp)); - node_id_from_privkey(&tmp, &b); - new_node(rstate, &b); - - /* A<->B */ - add_connection(rstate, &a, &b, 1, 1, 1); - - route = find_route(tmpctx, rstate, &a, &b, AMOUNT_MSAT(1000), riskfactor, 0.0, NULL, - ROUTING_MAX_HOPS, &fee); - assert(route); - assert(tal_count(route) == 1); - assert(amount_msat_eq(fee, AMOUNT_MSAT(0))); - - /* A<->B<->C */ - memset(&tmp, 'c', sizeof(tmp)); - node_id_from_privkey(&tmp, &c); - new_node(rstate, &c); - - status_debug("A = %s", type_to_string(tmpctx, struct node_id, &a)); - status_debug("B = %s", type_to_string(tmpctx, struct node_id, &b)); - status_debug("C = %s", type_to_string(tmpctx, struct node_id, &c)); - add_connection(rstate, &b, &c, 1, 1, 1); - - route = find_route(tmpctx, rstate, &a, &c, AMOUNT_MSAT(1000), riskfactor, 0.0, NULL, - ROUTING_MAX_HOPS, &fee); - assert(route); - assert(tal_count(route) == 2); - assert(amount_msat_eq(fee, AMOUNT_MSAT(1))); - - /* A<->D<->C: Lower base, higher percentage. */ - memset(&tmp, 'd', sizeof(tmp)); - node_id_from_privkey(&tmp, &d); - new_node(rstate, &d); - status_debug("D = %s", type_to_string(tmpctx, struct node_id, &d)); - - add_connection(rstate, &a, &d, 0, 2, 1); - add_connection(rstate, &d, &c, 0, 2, 1); - - /* Will go via D for small amounts. */ - route = find_route(tmpctx, rstate, &a, &c, AMOUNT_MSAT(1000), riskfactor, 0.0, NULL, - ROUTING_MAX_HOPS, &fee); - assert(route); - assert(tal_count(route) == 2); - assert(channel_is_between(route[0], &a, &d)); - assert(channel_is_between(route[1], &d, &c)); - assert(amount_msat_eq(fee, AMOUNT_MSAT(0))); - - /* Will go via B for large amounts. */ - route = find_route(tmpctx, rstate, &a, &c, AMOUNT_MSAT(3000000), riskfactor, 0.0, NULL, - ROUTING_MAX_HOPS, &fee); - assert(route); - assert(tal_count(route) == 2); - assert(channel_is_between(route[0], &a, &b)); - assert(channel_is_between(route[1], &b, &c)); - assert(amount_msat_eq(fee, AMOUNT_MSAT(1 + 3))); - - /* Make B->C inactive, force it back via D */ - get_connection(rstate, &b, &c)->channel_flags |= ROUTING_FLAGS_DISABLED; - route = find_route(tmpctx, rstate, &a, &c, AMOUNT_MSAT(3000000), riskfactor, 0.0, NULL, - ROUTING_MAX_HOPS, &fee); - assert(route); - assert(tal_count(route) == 2); - assert(channel_is_between(route[0], &a, &d)); - assert(channel_is_between(route[1], &d, &c)); - assert(amount_msat_eq(fee, AMOUNT_MSAT(0 + 6))); - - common_shutdown(); - return 0; -} diff --git a/gossipd/test/run-overlong.c b/gossipd/test/run-overlong.c deleted file mode 100644 index b643cd755..000000000 --- a/gossipd/test/run-overlong.c +++ /dev/null @@ -1,196 +0,0 @@ -#include "../routing.c" -#include "../gossip_store.c" -#include -#include -#include - -void status_fmt(enum log_level level UNUSED, - const struct node_id *node_id, - const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - vprintf(fmt, ap); - printf("\n"); - va_end(ap); -} - -/* AUTOGENERATED MOCKS START */ -/* Generated stub for cupdate_different */ -bool cupdate_different(struct gossip_store *gs UNNEEDED, - const struct half_chan *hc UNNEEDED, - const u8 *cupdate UNNEEDED) -{ fprintf(stderr, "cupdate_different called!\n"); abort(); } -/* Generated stub for fmt_wireaddr_without_port */ -char *fmt_wireaddr_without_port(const tal_t *ctx UNNEEDED, const struct wireaddr *a UNNEEDED) -{ fprintf(stderr, "fmt_wireaddr_without_port called!\n"); abort(); } -/* Generated stub for fromwire_wireaddr_array */ -struct wireaddr *fromwire_wireaddr_array(const tal_t *ctx UNNEEDED, const u8 *ser UNNEEDED) -{ fprintf(stderr, "fromwire_wireaddr_array called!\n"); abort(); } -/* Generated stub for json_add_member */ -void json_add_member(struct json_stream *js UNNEEDED, - const char *fieldname UNNEEDED, - bool quote UNNEEDED, - const char *fmt UNNEEDED, ...) -{ fprintf(stderr, "json_add_member called!\n"); abort(); } -/* Generated stub for json_member_direct */ -char *json_member_direct(struct json_stream *js UNNEEDED, - const char *fieldname UNNEEDED, size_t extra UNNEEDED) -{ fprintf(stderr, "json_member_direct called!\n"); abort(); } -/* Generated stub for json_object_end */ -void json_object_end(struct json_stream *js UNNEEDED) -{ fprintf(stderr, "json_object_end called!\n"); abort(); } -/* Generated stub for json_object_start */ -void json_object_start(struct json_stream *ks UNNEEDED, const char *fieldname UNNEEDED) -{ fprintf(stderr, "json_object_start called!\n"); abort(); } -/* Generated stub for memleak_add_helper_ */ -void memleak_add_helper_(const tal_t *p UNNEEDED, void (*cb)(struct htable *memtable UNNEEDED, - const tal_t *)){ } -/* Generated stub for nannounce_different */ -bool nannounce_different(struct gossip_store *gs UNNEEDED, - const struct node *node UNNEEDED, - const u8 *nannounce UNNEEDED) -{ fprintf(stderr, "nannounce_different called!\n"); abort(); } -/* Generated stub for notleak_ */ -void *notleak_(const void *ptr UNNEEDED, bool plus_children UNNEEDED) -{ fprintf(stderr, "notleak_ called!\n"); abort(); } -/* Generated stub for peer_supplied_good_gossip */ -void peer_supplied_good_gossip(struct peer *peer UNNEEDED, size_t amount UNNEEDED) -{ fprintf(stderr, "peer_supplied_good_gossip called!\n"); abort(); } -/* Generated stub for private_channel_announcement */ -const u8 *private_channel_announcement(const tal_t *ctx UNNEEDED, - const struct short_channel_id *scid UNNEEDED, - const struct node_id *local_node_id UNNEEDED, - const struct node_id *remote_node_id UNNEEDED, - const u8 *features UNNEEDED) -{ fprintf(stderr, "private_channel_announcement called!\n"); abort(); } -/* Generated stub for sanitize_error */ -char *sanitize_error(const tal_t *ctx UNNEEDED, const u8 *errmsg UNNEEDED, - struct channel_id *channel_id UNNEEDED) -{ fprintf(stderr, "sanitize_error called!\n"); abort(); } -/* Generated stub for status_failed */ -void status_failed(enum status_failreason code UNNEEDED, - const char *fmt UNNEEDED, ...) -{ fprintf(stderr, "status_failed called!\n"); abort(); } -/* Generated stub for towire_warningfmt */ -u8 *towire_warningfmt(const tal_t *ctx UNNEEDED, - const struct channel_id *channel UNNEEDED, - const char *fmt UNNEEDED, ...) -{ fprintf(stderr, "towire_warningfmt called!\n"); abort(); } -/* AUTOGENERATED MOCKS END */ - -#if DEVELOPER -/* Generated stub for memleak_remove_htable */ -void memleak_remove_htable(struct htable *memtable UNNEEDED, const struct htable *ht UNNEEDED) -{ fprintf(stderr, "memleak_remove_htable called!\n"); abort(); } -/* Generated stub for memleak_remove_intmap_ */ -void memleak_remove_intmap_(struct htable *memtable UNNEEDED, const struct intmap *m UNNEEDED) -{ fprintf(stderr, "memleak_remove_intmap_ called!\n"); abort(); } -#endif - -/* NOOP for new_reltimer_ */ -struct oneshot *new_reltimer_(struct timers *timers UNNEEDED, - const tal_t *ctx UNNEEDED, - struct timerel expire UNNEEDED, - void (*cb)(void *) UNNEEDED, void *arg UNNEEDED) -{ - return NULL; -} - -static void node_id_from_privkey(const struct privkey *p, struct node_id *id) -{ - struct pubkey k; - pubkey_from_privkey(p, &k); - node_id_from_pubkey(id, &k); -} - -#define NUM_NODES (ROUTING_MAX_HOPS + 1) - -/* We create an arrangement of nodes, each node N connected to N+1 and - * to node 1. The cost for each N to N+1 route is 1, for N to 1 is - * 2^N. That means it's always cheapest to go the longer route */ -int main(int argc, char *argv[]) -{ - struct routing_state *rstate; - struct node_id ids[NUM_NODES]; - struct chan **route; - struct amount_msat last_fee; - - common_setup(argv[0]); - - for (size_t i = 0; i < NUM_NODES; i++) { - struct privkey tmp; - memset(&tmp, i+1, sizeof(tmp)); - node_id_from_privkey(&tmp, &ids[i]); - } - /* We are node 0 */ - rstate = new_routing_state(tmpctx, &ids[0], NULL, NULL, NULL, - false, false); - - for (size_t i = 0; i < NUM_NODES; i++) { - struct chan *chan; - struct half_chan *hc; - struct short_channel_id scid; - - new_node(rstate, &ids[i]); - - if (i == 0) - continue; - if (!mk_short_channel_id(&scid, i, i-1, 0)) - abort(); - chan = new_chan(rstate, &scid, &ids[i], &ids[i-1], - AMOUNT_SAT(1000000), NULL); - - hc = &chan->half[node_id_idx(&ids[i-1], &ids[i])]; - hc->bcast.index = 1; - hc->base_fee = 1; - hc->proportional_fee = 0; - hc->delay = 0; - hc->channel_flags = node_id_idx(&ids[i-1], &ids[i]); - hc->htlc_minimum = AMOUNT_MSAT(0); - hc->htlc_maximum = AMOUNT_MSAT(1000000 * 1000); - SUPERVERBOSE("Joining %s to %s, fee %u", - type_to_string(tmpctx, struct node_id, &ids[i-1]), - type_to_string(tmpctx, struct node_id, &ids[i]), - (int)hc->base_fee); - - if (i <= 2) - continue; - if (!mk_short_channel_id(&scid, i, 1, 0)) - abort(); - chan = new_chan(rstate, &scid, &ids[i], &ids[1], - AMOUNT_SAT(1000000), NULL); - hc = &chan->half[node_id_idx(&ids[1], &ids[i])]; - hc->bcast.index = 1; - hc->base_fee = 1 << i; - hc->proportional_fee = 0; - hc->delay = 0; - hc->channel_flags = node_id_idx(&ids[1], &ids[i]); - hc->htlc_minimum = AMOUNT_MSAT(0); - hc->htlc_maximum = AMOUNT_MSAT(1000000 * 1000); - SUPERVERBOSE("Joining %s to %s, fee %u", - type_to_string(tmpctx, struct node_id, &ids[1]), - type_to_string(tmpctx, struct node_id, &ids[i]), - (int)hc->base_fee); - } - - for (size_t i = ROUTING_MAX_HOPS; i > 1; i--) { - struct amount_msat fee; - SUPERVERBOSE("%s -> %s:", - type_to_string(tmpctx, struct node_id, &ids[0]), - type_to_string(tmpctx, struct node_id, &ids[NUM_NODES-1])); - - route = find_route(tmpctx, rstate, &ids[0], &ids[NUM_NODES-1], - AMOUNT_MSAT(1000), 0, 0.0, NULL, - i, &fee); - assert(route); - assert(tal_count(route) == i); - if (i != ROUTING_MAX_HOPS) - assert(amount_msat_greater(fee, last_fee)); - last_fee = fee; - } - - common_shutdown(); - return 0; -}