Skip to content

Commit

Permalink
lightnind: remove the DEFAULT_PORT global definition.
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Jun 20, 2022
1 parent 7ec6353 commit b7b78b6
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 22 deletions.
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
12 changes: 9 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,13 @@ 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"
#include "../common/wireaddr.h"

/* This makes the handshake prototypes work. */
struct io_conn {
Expand Down Expand Up @@ -322,7 +329,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 +383,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
2 changes: 1 addition & 1 deletion wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid)
db_col_node_id(stmt, "node_id", &id);

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))
goto done;

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

0 comments on commit b7b78b6

Please sign in to comment.