connectd: prefer non-TOR connections and filter wireaddr hint duplicate

This does two things:
 - It moves non-tor addresses upfront so it prefers normal connection
   which are less laggy and more reliable.
 - It prevents connectd from trying the same wire_addr twice when the
   addr_hint was given and gossip also added the same address.

Changelog-Changed: connectd: try non-TOR connections first
Changelog-Fixed: connectd: do not try address hint twice
This commit is contained in:
Michael Schmoock
2021-08-20 12:50:28 +02:00
committed by Rusty Russell
parent f1e0fe506d
commit f31c5b7662

View File

@@ -1459,9 +1459,18 @@ static void add_seed_addrs(struct wireaddr_internal **addrs,
}
}
static bool wireaddr_int_equals_wireaddr(struct wireaddr_internal *addr_a,
struct wireaddr *addr_b)
{
if (!addr_a || !addr_b)
return false;
return wireaddr_eq(&addr_a->u.wireaddr, addr_b);
}
/*~ This asks gossipd for any addresses advertized by the node. */
static void add_gossip_addrs(struct wireaddr_internal **addrs,
const struct node_id *id)
const struct node_id *id,
struct wireaddr_internal *addrhint)
{
u8 *msg;
struct wireaddr *normal_addrs;
@@ -1483,6 +1492,24 @@ static void add_gossip_addrs(struct wireaddr_internal **addrs,
/* Wrap each one in a wireaddr_internal and add to addrs. */
for (size_t i = 0; i < tal_count(normal_addrs); i++) {
/* add TOR addresses in a second loop */
if (normal_addrs[i].type == ADDR_TYPE_TOR_V2 ||
normal_addrs[i].type == ADDR_TYPE_TOR_V3)
continue;
if (wireaddr_int_equals_wireaddr(addrhint, &normal_addrs[i]))
continue;
struct wireaddr_internal addr;
addr.itype = ADDR_INTERNAL_WIREADDR;
addr.u.wireaddr = normal_addrs[i];
tal_arr_expand(addrs, addr);
}
/* so connectd prefers direct connections if possible. */
for (size_t i = 0; i < tal_count(normal_addrs); i++) {
if (normal_addrs[i].type != ADDR_TYPE_TOR_V2 &&
normal_addrs[i].type != ADDR_TYPE_TOR_V3)
continue;
if (wireaddr_int_equals_wireaddr(addrhint, &normal_addrs[i]))
continue;
struct wireaddr_internal addr;
addr.itype = ADDR_INTERNAL_WIREADDR;
addr.u.wireaddr = normal_addrs[i];
@@ -1524,10 +1551,11 @@ static void try_connect_peer(struct daemon *daemon,
addrs = tal_arr(tmpctx, struct wireaddr_internal, 0);
/* They can supply an optional address for the connect RPC */
/* We add this first so its tried first by connectd */
if (addrhint)
tal_arr_expand(&addrs, *addrhint);
add_gossip_addrs(&addrs, id);
add_gossip_addrs(&addrs, id, addrhint);
if (tal_count(addrs) == 0) {
/* Don't resolve via DNS seed if we're supposed to use proxy. */