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 all commits
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
14 changes: 14 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 @@ -36,6 +37,7 @@ const struct chainparams networks[] = {
0x5a, 0x08, 0x9c, 0x68, 0xd6, 0x19, 0x00,
0x00, 0x00, 0x00, 0x00}}}},
.rpc_port = 8332,
.ln_port = 9735,
.cli = "bitcoin-cli",
.cli_args = NULL,
.cli_min_supported_version = 150000,
Expand Down Expand Up @@ -67,6 +69,7 @@ const struct chainparams networks[] = {
0x33, 0x2a, 0x1f, 0xc7, 0xb2, 0xb7, 0x3c,
0xf1, 0x88, 0x91, 0x0f}}}},
.rpc_port = 18443,
.ln_port = 19846,
.cli = "bitcoin-cli",
.cli_args = "-regtest",
.cli_min_supported_version = 150000,
Expand All @@ -92,6 +95,7 @@ const struct chainparams networks[] = {
0x2c, 0x42, 0x25, 0xe9, 0x73, 0x98, 0x81,
0x08, 0x00, 0x00, 0x00}}}},
.rpc_port = 38332,
.ln_port = 39735,
.cli = "bitcoin-cli",
.cli_args = "-signet",
.cli_min_supported_version = 150000,
Expand All @@ -115,6 +119,7 @@ const struct chainparams networks[] = {
0xe9, 0x0e, 0xad, 0x01, 0xea, 0x33, 0x09,
0x00, 0x00, 0x00, 0x00}}}},
.rpc_port = 18332,
.ln_port = 19735,
.cli = "bitcoin-cli",
.cli_args = "-testnet",
.cli_min_supported_version = 150000,
Expand All @@ -138,6 +143,7 @@ const struct chainparams networks[] = {
0x1e, 0xda, 0xba, 0x59, 0x40, 0xfd, 0x1f,
0xe3, 0x65, 0xa7, 0x12}}}},
.rpc_port = 9332,
.ln_port = 9735,
.cli = "litecoin-cli",
.cli_args = NULL,
.cli_min_supported_version = 150000,
Expand All @@ -162,6 +168,7 @@ const struct chainparams networks[] = {
0x13, 0xee, 0xfd, 0xd9, 0x51, 0x28, 0x4b,
0x5a, 0x62, 0x66, 0x49}}}},
.rpc_port = 19332,
.ln_port = 9735,
.cli = "litecoin-cli",
.cli_args = "-testnet",
.cli_min_supported_version = 150000,
Expand All @@ -186,6 +193,7 @@ const struct chainparams networks[] = {
0xfe, 0x14, 0x68, 0x01, 0x16, 0x23, 0x93,
0x36, 0x42, 0x86, 0xc6}}}},
.rpc_port = 19332,
.ln_port = 20735,
.cli = "elements-cli",
.cli_args = "-chain=liquid-regtest",
.dust_limit = {546},
Expand All @@ -209,6 +217,7 @@ const struct chainparams networks[] = {
0x68, 0x8d, 0x2c, 0x37, 0x92, 0x96, 0x88,
0x8a, 0x20, 0x60, 0x03}}}},
.rpc_port = 7041,
.ln_port = 9735,
.cli = "elements-cli",
.cli_args = "-chain=liquidv1",
.dust_limit = {546},
Expand Down Expand Up @@ -262,3 +271,8 @@ const char *chainparams_get_network_names(const tal_t *ctx)
return networks_string;
}

int chainparams_get_ln_port(const struct chainparams *params)
{
assert(params);
return params->ln_port;
}
15 changes: 15 additions & 0 deletions bitcoin/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ struct chainparams {
const char *bip70_name;
const struct bitcoin_blkid genesis_blockhash;
const int rpc_port;
/**
* BOLT 1:
*
* The default TCP port depends on the network used. The most common networks are:
*
* - Bitcoin mainet with port number 9735 or the corresponding hexadecimal `0x2607`;
* - Bitcoin testnet with port number 19735 (`0x4D17`);
* - Bitcoin signet with port number 39735 (`0xF87`).
*/
const int ln_port;
const char *cli;
const char *cli_args;
/* The min numeric version of cli supported */
Expand Down Expand Up @@ -64,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
12 changes: 7 additions & 5 deletions devtools/gossipwith.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
static bool stream_stdin = false;
static bool no_init = false;
static bool hex = false;
static bool explicit_network = false;
static int timeout_after = -1;
static u8 *features;

Expand All @@ -45,7 +46,7 @@ static struct io_plan *simple_close(struct io_conn *conn)
return NULL;
}

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

/* This makes the handshake prototypes work. */
struct io_conn {
Expand Down Expand Up @@ -77,6 +78,7 @@ static char *opt_set_network(const char *arg, void *unused)
chainparams = chainparams_for_network(arg);
if (!chainparams)
return tal_fmt(NULL, "Unknown network name '%s'", arg);
explicit_network = true;
return NULL;
}

Expand Down Expand Up @@ -178,12 +180,12 @@ static struct io_plan *handshake_success(struct io_conn *conn,

if (!no_init) {
struct tlv_init_tlvs *tlvs = NULL;
if (chainparams) {
if (explicit_network) {
tlvs = tlv_init_tlvs_new(NULL);
tlvs->networks = tal_arr(tlvs, struct bitcoin_blkid, 1);
tlvs->networks[0] = chainparams->genesis_blockhash;
}
msg = towire_init(NULL, NULL, features, tlvs);
msg = towire_init(NULL, NULL, features, tlvs);

sync_crypto_write(peer_fd, cs, take(msg));
/* Ignore their init message. */
Expand Down Expand Up @@ -281,6 +283,7 @@ int main(int argc, char *argv[])

memset(&notsosecret, 0x42, sizeof(notsosecret));
features = tal_arr(conn, u8, 0);
chainparams = chainparams_for_network("bitcoin");

opt_register_noarg("--initial-sync", opt_set_bool, &initial_sync,
"Stream complete gossip history at start");
Expand Down Expand Up @@ -322,7 +325,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, chainparams_get_ln_port(chainparams), NULL,
true, false, true, &err_msg))
opt_usage_exit_fail("%s '%s'", err_msg, argv[1]);

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

6 changes: 5 additions & 1 deletion doc/lightning-connect.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ be of the form *id@host* or *id@host:port*. In this case, the *host* and

*host* is the peer's hostname or IP address.

If not specified, the *port* defaults to 9735.
If not specified, the *port* depends on the current network:
- bitcoin **mainnet**: 9735.
- bitcoin **testnet**: 19735.
- bitcoin **signet**: 39735.
- bitcoin **regtest**: 19846.

If *host* is not specified (or doesn't work), the connection will be attempted to an IP
belonging to *id* obtained through gossip with other already connected
Expand Down
3 changes: 2 additions & 1 deletion doc/lightningd-config.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ network.
Note that for simple setups, the implicit *autolisten* option does the
right thing: for the mainnet (bitcoin) network it will try to bind to
port 9735 on IPv4 and IPv6, and will announce it to peers if it seems
like a public address.
like a public address (and other default ports for other networks,
as described below).

Core Lightning also support IPv4/6 address discovery behind NAT routers.
If your node detects an new public address, it will update its announcement.
Expand Down
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/%)

3 changes: 2 additions & 1 deletion lightningd/connect_control.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "config.h"
#include <bitcoin/chainparams.h>
#include <ccan/err/err.h>
#include <ccan/tal/str/str.h>
#include <common/configdir.h>
Expand Down Expand Up @@ -140,7 +141,7 @@ static struct command_result *json_connect(struct command *cmd,
/* Is there a port? */
if (!port) {
port = tal(cmd, u32);
*port = DEFAULT_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 = DEFAULT_PORT + chainparams->rpc_port - 8332;
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
14 changes: 8 additions & 6 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
expected_peer_features, expected_node_features,
expected_channel_features,
check_coin_moves, first_channel_id, account_balance, basic_fee,
scriptpubkey_addr,
scriptpubkey_addr, default_ln_port,
EXPERIMENTAL_FEATURES, mine_funding_to_announce
)
from pyln.testing.utils import SLOW_MACHINE, VALGRIND, EXPERIMENTAL_DUAL_FUND, FUNDAMOUNT
Expand Down Expand Up @@ -97,18 +97,20 @@ def test_remote_addr(node_factory, bitcoind):
l1.daemon.wait_for_log(f"Received node_announcement for node {l2.info['id']}")
assert(len(l1.rpc.listnodes(l2.info['id'])['nodes'][0]['addresses']) == 0)

def_port = default_ln_port(l2.info["network"])

# when we restart l1 with a channel and reconnect, node_annoucement update
# 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:{}".format(def_port))
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:{}".format(def_port))

# 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 +120,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:{}".format(def_port))

# 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:{}".format(def_port))
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'] == def_port


@pytest.mark.developer("needs DEVELOPER=1 for fast gossip and --dev-allow-localhost for local remote_addr")
Expand Down
Loading