Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc: use the standard port derivation in connect command #5242

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bitcoin/chainparams.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "config.h"
#include <assert.h>
#include <bitcoin/chainparams.h>
#include <ccan/array_size/array_size.h>
#include <ccan/tal/str/str.h>
Expand Down Expand Up @@ -269,3 +270,9 @@ const char *chainparams_get_network_names(const tal_t *ctx)
tal_append_fmt(&networks_string, ", %s", networks[i].network_name);
return networks_string;
}

int chainparams_get_ln_port(const struct chainparams *params)
{
assert(params);
return params->ln_port;
}
5 changes: 5 additions & 0 deletions bitcoin/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ const struct chainparams *chainparams_by_chainhash(const struct bitcoin_blkid *c
*/
const char *chainparams_get_network_names(const tal_t *ctx);

/**
* chainparams_get_ln_port - Return the lightning network default port by
* network if the chainparams is initialized, otherwise 9735 as mock port
*/
int chainparams_get_ln_port(const struct chainparams *params);
#endif /* LIGHTNING_BITCOIN_CHAINPARAMS_H */
15 changes: 14 additions & 1 deletion common/test/run-wireaddr.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
#include "config.h"
#include "../wireaddr.c"
#include <bitcoin/chainparams.h>
#include <common/amount.h>
#include <common/setup.h>
#include <stdio.h>

int simple_get_ln_port(const struct chainparams *params);

#define chainparams_get_ln_port simple_get_ln_port

#include "../wireaddr.c"

#define DEFAULT_PORT simple_get_ln_port(NULL)

int simple_get_ln_port(const struct chainparams *params UNNEEDED)
{
return 9735;
}

/* AUTOGENERATED MOCKS START */
/* Generated stub for amount_asset_is_main */
bool amount_asset_is_main(struct amount_asset *asset UNNEEDED)
Expand Down
5 changes: 3 additions & 2 deletions common/wireaddr.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "config.h"
#include <arpa/inet.h>
#include <assert.h>
#include <bitcoin/chainparams.h>
#include <ccan/mem/mem.h>
#include <ccan/tal/str/str.h>
#include <common/base32.h>
Expand Down Expand Up @@ -612,7 +613,7 @@ bool parse_wireaddr_internal(const char *arg, struct wireaddr_internal *addr,
* an onion address. */
if (strstarts(arg, "autotor:")) {
addr->itype = ADDR_INTERNAL_AUTOTOR;
addr->u.torservice.port = DEFAULT_PORT;
addr->u.torservice.port = chainparams_get_ln_port(chainparams);
/* Format is separated by slash. */
char **parts = tal_strsplit(tmpctx, arg, "/", STR_EMPTY_OK);

Expand Down Expand Up @@ -644,7 +645,7 @@ bool parse_wireaddr_internal(const char *arg, struct wireaddr_internal *addr,
if (strstarts(arg, "statictor:")) {
bool use_magic_blob = true;
addr->itype = ADDR_INTERNAL_STATICTOR;
addr->u.torservice.port = DEFAULT_PORT;
addr->u.torservice.port = chainparams_get_ln_port(chainparams);
memset(addr->u.torservice.blob, 0, sizeof(addr->u.torservice.blob));

/* Format is separated by slash. */
Expand Down
8 changes: 0 additions & 8 deletions common/wireaddr.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ struct sockaddr_in6;
struct sockaddr_in;
struct sockaddr_un;

/* BOLT #1:
*
* The default TCP port is 9735. This corresponds to hexadecimal
* `0x2607`: the Unicode code point for LIGHTNING.
*/
#define DEFAULT_PORT 9735


/* BOLT #7:
*
* The following `address descriptor` types are defined:
Expand Down
5 changes: 3 additions & 2 deletions connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* it.
*/
#include "config.h"
#include <bitcoin/chainparams.h>
#include <ccan/array_size/array_size.h>
#include <ccan/asort/asort.h>
#include <ccan/closefrom/closefrom.h>
Expand Down Expand Up @@ -1707,7 +1708,7 @@ static void add_seed_addrs(struct wireaddr_internal **addrs,

for (size_t i = 0; i < tal_count(hostnames); i++) {
status_peer_debug(id, "Resolving %s", hostnames[i]);
new_addrs = wireaddr_from_hostname(tmpctx, hostnames[i], DEFAULT_PORT,
new_addrs = wireaddr_from_hostname(tmpctx, hostnames[i], chainparams_get_ln_port(chainparams),
NULL, broken_reply, NULL);
if (new_addrs) {
for (size_t j = 0; j < tal_count(new_addrs); j++) {
Expand Down Expand Up @@ -1859,7 +1860,7 @@ static void try_connect_peer(struct daemon *daemon,
for (size_t i = 0; i < tal_count(hostnames); i++) {
wireaddr_from_unresolved(&unresolved,
hostnames[i],
DEFAULT_PORT);
chainparams_get_ln_port(chainparams));
tal_arr_expand(&addrs, unresolved);
}
} else if (daemon->use_dns) {
Expand Down
2 changes: 2 additions & 0 deletions connectd/test/run-netaddress.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <stdio.h>
#include <wire/wire.h>

#define DEFAULT_PORT 9735

/* AUTOGENERATED MOCKS START */
/* Generated stub for amount_asset_is_main */
bool amount_asset_is_main(struct amount_asset *asset UNNEEDED)
Expand Down
11 changes: 8 additions & 3 deletions devtools/gossipwith.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stdio.h>
#include <wire/peer_wire.h>

#define chainparams_get_ln_port simple_get_ln_port
#define io_write_ simple_write
#define io_read_ simple_read
#define io_close simple_close
Expand All @@ -45,7 +46,12 @@ static struct io_plan *simple_close(struct io_conn *conn)
return NULL;
}

#include "../connectd/handshake.c"
static int simple_get_ln_port(const struct chainparams *params UNNEEDED)
{
return 9735;
}

#include "../connectd/handshake.c"

/* This makes the handshake prototypes work. */
struct io_conn {
Expand Down Expand Up @@ -322,7 +328,7 @@ int main(int argc, char *argv[])
opt_usage_exit_fail("Invalid id %.*s",
(int)(at - argv[1]), argv[1]);

if (!parse_wireaddr_internal(at+1, &addr, DEFAULT_PORT, NULL,
if (!parse_wireaddr_internal(at+1, &addr, simple_get_ln_port(NULL), NULL,
true, false, true, &err_msg))
opt_usage_exit_fail("%s '%s'", err_msg, argv[1]);

Expand Down Expand Up @@ -376,4 +382,3 @@ int main(int argc, char *argv[])
handshake_success, argv+2);
exit(0);
}

4 changes: 2 additions & 2 deletions gossipd/gossipd.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ static void handle_remote_addr(struct daemon *daemon, const u8 *msg)
if (!fromwire_gossipd_remote_addr(msg, &remote_addr))
master_badmsg(WIRE_GOSSIPD_REMOTE_ADDR, msg);

/* current best guess is that we use DEFAULT_PORT on public internet */
remote_addr.port = DEFAULT_PORT;
/* current best guess is that we use default port on public internet */
remote_addr.port = chainparams_get_ln_port(chainparams);

switch (remote_addr.type) {
case ADDR_TYPE_IPV4:
Expand Down
1 change: 0 additions & 1 deletion gossipd/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,3 @@ $(GOSSIPD_TEST_PROGRAMS): $(GOSSIPD_TEST_COMMON_OBJS) $(BITCOIN_OBJS)
$(GOSSIPD_TEST_OBJS): $(GOSSIPD_HEADERS) $(GOSSIPD_SRC)

gossipd-tests: $(GOSSIPD_TEST_PROGRAMS:%=unittest/%)

2 changes: 1 addition & 1 deletion lightningd/connect_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ static struct command_result *json_connect(struct command *cmd,
/* Is there a port? */
if (!port) {
port = tal(cmd, u32);
*port = chainparams->ln_port;
*port = chainparams_get_ln_port(chainparams);
}
addr = tal(cmd, struct wireaddr_internal);
if (!parse_wireaddr_internal(name, addr, *port, false,
Expand Down
2 changes: 1 addition & 1 deletion lightningd/lightningd.c
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ int main(int argc, char *argv[])

/*~ Set the default portnum according to the used network
* similarly to what Bitcoin Core does to ports by default. */
ld->portnum = chainparams->ln_port;
ld->portnum = chainparams_get_ln_port(chainparams);

/*~ Initialize all the plugins we just registered, so they can
* do their thing and tell us about themselves (including
Expand Down
10 changes: 5 additions & 5 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ def test_remote_addr(node_factory, bitcoind):
# must not yet be send as we need the same `remote_addr` confirmed from a
# another peer we have a channel with.
# Note: In this state l2 stores remote_addr as reported by l1
assert not l2.daemon.is_in_log("Update our node_announcement for discovered address: 127.0.0.1:9735")
assert not l2.daemon.is_in_log("Update our node_announcement for discovered address: 127.0.0.1:19846")
l1.restart()
l2.rpc.connect(l1.info['id'], 'localhost', l1.port)
l2.daemon.wait_for_log("Peer says it sees our address as: 127.0.0.1:[0-9]{5}")

# Now l1 sees l2 but without announced addresses.
assert(len(l1.rpc.listnodes(l2.info['id'])['nodes'][0]['addresses']) == 0)
assert not l2.daemon.is_in_log("Update our node_announcement for discovered address: 127.0.0.1:9735")
assert not l2.daemon.is_in_log("Update our node_announcement for discovered address: 127.0.0.1:19846")

# connect second node. This will not yet trigger `node_annoucement` update,
# as we again do not have a channel at the time we connected.
Expand All @@ -118,20 +118,20 @@ def test_remote_addr(node_factory, bitcoind):
# fund channel and check we didn't send Update earlier already
l2.fundchannel(l3, wait_for_active=True)
bitcoind.generate_block(5)
assert not l2.daemon.is_in_log("Update our node_announcement for discovered address: 127.0.0.1:9735")
assert not l2.daemon.is_in_log("Update our node_announcement for discovered address: 127.0.0.1:19846")

# restart, reconnect and re-check for updated node_annoucement. This time
# l2 sees that two different peers with channel reported the same `remote_addr`.
l3.restart()
l2.rpc.connect(l3.info['id'], 'localhost', l3.port)
l2.daemon.wait_for_log("Peer says it sees our address as: 127.0.0.1:[0-9]{5}")
l2.daemon.wait_for_log("Update our node_announcement for discovered address: 127.0.0.1:9735")
l2.daemon.wait_for_log("Update our node_announcement for discovered address: 127.0.0.1:19846")
l1.daemon.wait_for_log(f"Received node_announcement for node {l2.info['id']}")

address = l1.rpc.listnodes(l2.info['id'])['nodes'][0]['addresses'][0]
assert address['type'] == "ipv4"
assert address['address'] == "127.0.0.1"
assert address['port'] == 9735
assert address['port'] == 19846


@pytest.mark.developer("needs DEVELOPER=1 for fast gossip and --dev-allow-localhost for local remote_addr")
Expand Down
4 changes: 2 additions & 2 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -838,11 +838,11 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid)

/* This can happen for peers last seen on Torv2! */
addrstr = db_col_strdup(tmpctx, stmt, "address");
if (!parse_wireaddr_internal(addrstr, &addr, DEFAULT_PORT,
if (!parse_wireaddr_internal(addrstr, &addr, chainparams_get_ln_port(chainparams),
false, false, true, true, NULL)) {
log_unusual(w->log, "Unparsable peer address %s: replacing",
addrstr);
parse_wireaddr_internal("127.0.0.1:1", &addr, DEFAULT_PORT,
parse_wireaddr_internal("127.0.0.1:1", &addr, chainparams_get_ln_port(chainparams),
false, false, true, true, NULL);
}

Expand Down
3 changes: 3 additions & 0 deletions wire/test/run-tlvstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ static const char *reason;
/* Generated stub for chainparams_by_chainhash */
const struct chainparams *chainparams_by_chainhash(const struct bitcoin_blkid *chain_hash UNNEEDED)
{ fprintf(stderr, "chainparams_by_chainhash called!\n"); abort(); }
/* Generate std for chainparams_get_ln_port */
int chainparams_get_ln_port(const struct chainparams *params UNNEEDED)
{ fprintf(stderr, "chainparams_get_ln_port called!\n"); abort(); }
/* Generated stub for fromwire_channel_id */
bool fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
struct channel_id *channel_id UNNEEDED)
Expand Down