Skip to content

Commit 68feb55

Browse files
committed
wallet: save last known address.
If we connected out, remember that address. We always remember the last address, but that may be an incoming address. This is explicitly the last outgoing address which worked. Signed-off-by: Rusty Russell <[email protected]>
1 parent 64af5db commit 68feb55

File tree

11 files changed

+77
-8
lines changed

11 files changed

+77
-8
lines changed

db/bindings.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,18 @@ struct secret *db_col_secret_arr(const tal_t *ctx,
601601
return db_col_arr(ctx, stmt, colname, struct secret);
602602
}
603603

604+
struct wireaddr *db_col_wireaddr(const tal_t *ctx,
605+
struct db_stmt *stmt,
606+
const char *colname)
607+
{
608+
struct wireaddr *waddr = tal(ctx, struct wireaddr);
609+
const u8 *wire = db_col_arr(tmpctx, stmt, colname, u8);
610+
size_t len = tal_bytelen(wire);
611+
if (!fromwire_wireaddr(&wire, &len, waddr))
612+
return tal_free(waddr);
613+
return waddr;
614+
}
615+
604616
void db_col_txid(struct db_stmt *stmt, const char *colname, struct bitcoin_txid *t)
605617
{
606618
db_col_sha256d(stmt, colname, &t->shad);

db/bindings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ struct bitcoin_tx *db_col_psbt_to_tx(const tal_t *ctx, struct db_stmt *stmt, con
103103

104104
struct onionreply *db_col_onionreply(const tal_t *ctx,
105105
struct db_stmt *stmt, const char *colname);
106+
struct wireaddr *db_col_wireaddr(const tal_t *ctx,
107+
struct db_stmt *stmt,
108+
const char *colname);
106109

107110
#define db_col_arr(ctx, stmt, colname, type) \
108111
((type *)db_col_arr_((ctx), (stmt), (colname), \

lightningd/opening_control.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,7 @@ static struct channel *stub_chan(struct command *cmd,
14961496
&nodeid,
14971497
&wint,
14981498
NULL,
1499+
NULL,
14991500
false);
15001501
}
15011502

lightningd/peer_control.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ void peer_set_dbid(struct peer *peer, u64 dbid)
9191
struct peer *new_peer(struct lightningd *ld, u64 dbid,
9292
const struct node_id *id,
9393
const struct wireaddr_internal *addr,
94+
const struct wireaddr *last_known_addr,
9495
const u8 *their_features,
9596
bool connected_incoming)
9697
{
@@ -102,6 +103,7 @@ struct peer *new_peer(struct lightningd *ld, u64 dbid,
102103
peer->id = *id;
103104
peer->uncommitted_channel = NULL;
104105
peer->addr = *addr;
106+
peer->last_known_addr = tal_dup_or_null(peer, struct wireaddr, last_known_addr);
105107
peer->connected_incoming = connected_incoming;
106108
peer->remote_addr = NULL;
107109
list_head_init(&peer->channels);
@@ -1673,6 +1675,7 @@ void peer_connected(struct lightningd *ld, const u8 *msg)
16731675
struct peer_connected_hook_payload *hook_payload;
16741676
u64 connectd_counter;
16751677
const char *cmd_id;
1678+
struct wireaddr *last_known_addr;
16761679

16771680
hook_payload = tal(NULL, struct peer_connected_hook_payload);
16781681
hook_payload->ld = ld;
@@ -1691,15 +1694,31 @@ void peer_connected(struct lightningd *ld, const u8 *msg)
16911694
* now it's reconnected, we've gotta force them out. */
16921695
peer_channels_cleanup(ld, &id);
16931696

1697+
/* If we connected, and it's a normal address */
1698+
if (!hook_payload->incoming
1699+
&& hook_payload->addr.itype == ADDR_INTERNAL_WIREADDR
1700+
&& !hook_payload->addr.u.wireaddr.is_websocket) {
1701+
last_known_addr = &hook_payload->addr.u.wireaddr.wireaddr;
1702+
} else {
1703+
last_known_addr = NULL;
1704+
}
1705+
16941706
/* If we're already dealing with this peer, hand off to correct
16951707
* subdaemon. Otherwise, we'll hand to openingd to wait there. */
16961708
peer = peer_by_id(ld, &id);
1697-
if (!peer)
1709+
if (!peer) {
1710+
/* If we connected to them, we know this is a good address. */
16981711
peer = new_peer(ld, 0, &id, &hook_payload->addr,
1712+
last_known_addr,
16991713
take(their_features), hook_payload->incoming);
1700-
else {
1714+
} else {
17011715
tal_free(peer->their_features);
17021716
peer->their_features = tal_steal(peer, their_features);
1717+
1718+
/* Update known address. */
1719+
tal_free(peer->last_known_addr);
1720+
peer->last_known_addr = tal_dup_or_null(peer, struct wireaddr,
1721+
last_known_addr);
17031722
}
17041723

17051724
/* We track this, because messages can race between connectd and us.

lightningd/peer_control.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ struct peer {
5353
struct wireaddr_internal addr;
5454
bool connected_incoming;
5555

56+
/* If we ever successfully connected out to an address, this is non-NULL */
57+
struct wireaddr *last_known_addr;
58+
5659
/* They send what they see as our address as remote_addr */
5760
struct wireaddr *remote_addr;
5861

@@ -71,6 +74,7 @@ struct peer *find_peer_by_dbid(struct lightningd *ld, u64 dbid);
7174
struct peer *new_peer(struct lightningd *ld, u64 dbid,
7275
const struct node_id *id,
7376
const struct wireaddr_internal *addr,
77+
const struct wireaddr *last_known_addr,
7478
const u8 *their_features TAKES,
7579
bool connected_incoming);
7680

plugins/bkpr/test/run-bkpr_db.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
9292
/* Generated stub for fromwire_u8_array */
9393
void fromwire_u8_array(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, u8 *arr UNNEEDED, size_t num UNNEEDED)
9494
{ fprintf(stderr, "fromwire_u8_array called!\n"); abort(); }
95+
/* Generated stub for fromwire_wireaddr */
96+
bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED)
97+
{ fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); }
9598
/* Generated stub for fromwire_wirestring */
9699
char *fromwire_wirestring(const tal_t *ctx UNNEEDED, const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
97100
{ fprintf(stderr, "fromwire_wirestring called!\n"); abort(); }

plugins/bkpr/test/run-recorder.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
9898
/* Generated stub for fromwire_u8_array */
9999
void fromwire_u8_array(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, u8 *arr UNNEEDED, size_t num UNNEEDED)
100100
{ fprintf(stderr, "fromwire_u8_array called!\n"); abort(); }
101+
/* Generated stub for fromwire_wireaddr */
102+
bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED)
103+
{ fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); }
101104
/* Generated stub for fromwire_wirestring */
102105
char *fromwire_wirestring(const tal_t *ctx UNNEEDED, const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
103106
{ fprintf(stderr, "fromwire_wirestring called!\n"); abort(); }

wallet/db.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,7 @@ static struct migration dbmigrations[] = {
10281028
" addrtype INTEGER)"), NULL},
10291029
{NULL, insert_addrtype_to_addresses},
10301030
{SQL("ALTER TABLE channel_funding_inflights ADD remote_funding BLOB DEFAULT NULL;"), NULL},
1031+
{SQL("ALTER TABLE peers ADD last_known_address BLOB DEFAULT NULL;"), NULL},
10311032
};
10321033

10331034
/**

wallet/test/run-db.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ struct logger *new_logger(const tal_t *ctx UNNEEDED, struct log_book *record UNN
239239
struct peer *new_peer(struct lightningd *ld UNNEEDED, u64 dbid UNNEEDED,
240240
const struct node_id *id UNNEEDED,
241241
const struct wireaddr_internal *addr UNNEEDED,
242+
const struct wireaddr *last_known_addr UNNEEDED,
242243
const u8 *their_features TAKES UNNEEDED,
243244
bool connected_incoming UNNEEDED)
244245
{ fprintf(stderr, "new_peer called!\n"); abort(); }

wallet/test/run-wallet.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,7 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx)
14411441

14421442
/* Add another utxo that's CSV-locked for 5 blocks */
14431443
assert(parse_wireaddr_internal(tmpctx, "localhost:1234", 0, false, &addr) == NULL);
1444-
channel.peer = new_peer(ld, 0, &id, &addr, NULL, false);
1444+
channel.peer = new_peer(ld, 0, &id, &addr, NULL, NULL, false);
14451445
channel.dbid = 1;
14461446
channel.type = channel_type_anchors_zero_fee_htlc(tmpctx);
14471447
memset(&u.outpoint, 3, sizeof(u.outpoint));
@@ -1767,7 +1767,7 @@ static bool test_channel_crud(struct lightningd *ld, const tal_t *ctx)
17671767
c1.first_blocknum = 1;
17681768
assert(parse_wireaddr_internal(tmpctx, "localhost:1234", 0, false, &addr) == NULL);
17691769
c1.final_key_idx = 1337;
1770-
p = new_peer(ld, 0, &id, &addr, NULL, false);
1770+
p = new_peer(ld, 0, &id, &addr, NULL, NULL, false);
17711771
c1.peer = p;
17721772
c1.dbid = wallet_get_channel_dbid(w);
17731773
c1.state = CHANNELD_NORMAL;
@@ -1935,7 +1935,7 @@ static bool test_channel_inflight_crud(struct lightningd *ld, const tal_t *ctx)
19351935
assert(parse_wireaddr_internal(tmpctx, "localhost:1234", 0, false, &addr) == NULL);
19361936

19371937
/* new channel! */
1938-
p = new_peer(ld, 0, &id, &addr, NULL, false);
1938+
p = new_peer(ld, 0, &id, &addr, NULL, NULL, false);
19391939

19401940
funding_sats = AMOUNT_SAT(4444444);
19411941
our_sats = AMOUNT_SAT(3333333);

wallet/wallet.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,10 +1011,11 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid)
10111011
struct peer *peer = NULL;
10121012
struct node_id id;
10131013
struct wireaddr_internal addr;
1014+
struct wireaddr *last_known_addr;
10141015
struct db_stmt *stmt;
10151016

10161017
stmt = db_prepare_v2(
1017-
w->db, SQL("SELECT id, node_id, address, feature_bits FROM peers WHERE id=?;"));
1018+
w->db, SQL("SELECT id, node_id, address, feature_bits, last_known_address FROM peers WHERE id=?;"));
10181019
db_bind_u64(stmt, dbid);
10191020
db_query_prepared(stmt);
10201021

@@ -1025,6 +1026,7 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid)
10251026
db_col_ignore(stmt, "address");
10261027
db_col_ignore(stmt, "id");
10271028
db_col_ignore(stmt, "feature_bits");
1029+
db_col_ignore(stmt, "last_known_address");
10281030
goto done;
10291031
}
10301032

@@ -1041,8 +1043,18 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid)
10411043
assert(!err);
10421044
}
10431045

1044-
/* FIXME: save incoming in db! */
1045-
peer = new_peer(w->ld, db_col_u64(stmt, "id"), &id, &addr, db_col_arr(stmt, stmt, "feature_bits", u8), false);
1046+
if (db_col_is_null(stmt, "last_known_address")) {
1047+
last_known_addr = NULL;
1048+
} else {
1049+
last_known_addr = db_col_wireaddr(tmpctx, stmt, "last_known_address");
1050+
if (!last_known_addr) {
1051+
log_broken(w->log, "Unparsable lastknown address %s: ignoring",
1052+
tal_hex(tmpctx, db_col_arr(tmpctx, stmt, "last_known_address", u8)));
1053+
}
1054+
}
1055+
1056+
peer = new_peer(w->ld, db_col_u64(stmt, "id"), &id, &addr, last_known_addr,
1057+
db_col_arr(stmt, stmt, "feature_bits", u8), false);
10461058

10471059
done:
10481060
tal_free(stmt);
@@ -2583,6 +2595,16 @@ static void wallet_peer_save(struct wallet *w, struct peer *peer)
25832595
db_exec_prepared_v2(stmt);
25842596
peer_set_dbid(peer, db_last_insert_id_v2(take(stmt)));
25852597
}
2598+
2599+
if (peer->last_known_addr) {
2600+
u8 *wire = tal_arr(tmpctx, u8, 0);
2601+
towire_wireaddr(&wire, peer->last_known_addr);
2602+
stmt = db_prepare_v2(w->db,
2603+
SQL("UPDATE peers SET last_known_address = ? WHERE id = ?;"));
2604+
db_bind_talarr(stmt, wire);
2605+
db_bind_u64(stmt, peer->dbid);
2606+
db_exec_prepared_v2(take(stmt));
2607+
}
25862608
}
25872609

25882610
bool channel_exists_by_id(struct wallet *w, u64 dbid) {

0 commit comments

Comments
 (0)