Skip to content

Commit fee7cbe

Browse files
instagibbsrustyrussell
authored andcommitted
Persist feature bits across restarts
[ Whitespace fix and remove gratuitous tal_free(peer->their_features) -- RR ]
1 parent 1e457bd commit fee7cbe

File tree

7 files changed

+54
-23
lines changed

7 files changed

+54
-23
lines changed

lightningd/opening_control.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,7 @@ static struct channel *stub_chan(struct command *cmd,
13511351
0,
13521352
&nodeid,
13531353
&wint,
1354+
NULL,
13541355
false);
13551356
}
13561357

lightningd/peer_control.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,6 @@ static void destroy_peer(struct peer *peer)
7676
peer_dbid_map_del(peer->ld->peers_by_dbid, peer);
7777
}
7878

79-
static void peer_update_features(struct peer *peer,
80-
const u8 *their_features TAKES)
81-
{
82-
tal_free(peer->their_features);
83-
peer->their_features = tal_dup_talarr(peer, u8, their_features);
84-
}
85-
8679
void peer_set_dbid(struct peer *peer, u64 dbid)
8780
{
8881
assert(!peer->dbid);
@@ -94,6 +87,7 @@ void peer_set_dbid(struct peer *peer, u64 dbid)
9487
struct peer *new_peer(struct lightningd *ld, u64 dbid,
9588
const struct node_id *id,
9689
const struct wireaddr_internal *addr,
90+
const u8 *their_features,
9791
bool connected_incoming)
9892
{
9993
/* We are owned by our channels, and freed manually by destroy_channel */
@@ -106,12 +100,16 @@ struct peer *new_peer(struct lightningd *ld, u64 dbid,
106100
peer->addr = *addr;
107101
peer->connected_incoming = connected_incoming;
108102
peer->remote_addr = NULL;
109-
peer->their_features = NULL;
110103
list_head_init(&peer->channels);
111104
peer->direction = node_id_idx(&peer->ld->id, &peer->id);
112105
peer->connected = PEER_DISCONNECTED;
113106
peer->last_connect_attempt.ts.tv_sec
114107
= peer->last_connect_attempt.ts.tv_nsec = 0;
108+
if (their_features)
109+
peer->their_features = tal_dup_talarr(peer, u8, their_features);
110+
else
111+
peer->their_features = NULL;
112+
115113
#if DEVELOPER
116114
peer->ignore_htlcs = false;
117115
#endif
@@ -1403,7 +1401,11 @@ void peer_connected(struct lightningd *ld, const u8 *msg)
14031401
peer = peer_by_id(ld, &id);
14041402
if (!peer)
14051403
peer = new_peer(ld, 0, &id, &hook_payload->addr,
1406-
hook_payload->incoming);
1404+
their_features, hook_payload->incoming);
1405+
else {
1406+
tal_free(peer->their_features);
1407+
peer->their_features = tal_dup_talarr(peer, u8, their_features);
1408+
}
14071409

14081410
/* We track this, because messages can race between connectd and us.
14091411
* For example, we could tell it to attach a subd, but it's actually
@@ -1423,7 +1425,6 @@ void peer_connected(struct lightningd *ld, const u8 *msg)
14231425
if (peer->remote_addr)
14241426
tal_free(peer->remote_addr);
14251427
peer->remote_addr = NULL;
1426-
peer_update_features(peer, their_features);
14271428
hook_payload->peer_id = id;
14281429

14291430
/* If there's a connect command, use its id as basis for hook id */
@@ -1942,9 +1943,6 @@ static void json_add_peer(struct lightningd *ld,
19421943
num_channels++;
19431944
json_add_num(response, "num_channels", num_channels);
19441945

1945-
/* If it's not connected, features are unreliable: we don't
1946-
* store them in the database, and they would only reflect
1947-
* their features *last* time they connected. */
19481946
if (p->connected == PEER_CONNECTED) {
19491947
json_array_start(response, "netaddr");
19501948
json_add_string(response, NULL,
@@ -1956,8 +1954,11 @@ static void json_add_peer(struct lightningd *ld,
19561954
if (p->remote_addr)
19571955
json_add_string(response, "remote_addr",
19581956
fmt_wireaddr(response, p->remote_addr));
1959-
json_add_hex_talarr(response, "features", p->their_features);
19601957
}
1958+
1959+
/* Note: If !PEER_CONNECTED, peer may use different features on reconnect */
1960+
json_add_hex_talarr(response, "features", p->their_features);
1961+
19611962
if (deprecated_apis) {
19621963
json_array_start(response, "channels");
19631964
json_add_uncommitted_channel(response, p->uncommitted_channel, NULL);

lightningd/peer_control.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ struct peer *find_peer_by_dbid(struct lightningd *ld, u64 dbid);
7373
struct peer *new_peer(struct lightningd *ld, u64 dbid,
7474
const struct node_id *id,
7575
const struct wireaddr_internal *addr,
76+
const u8 *their_features,
7677
bool connected_incoming);
7778

7879
/* Last one out deletes peer. Also removes from db. */

tests/test_connection.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4393,6 +4393,30 @@ def test_peer_disconnected_reflected_in_channel_state(node_factory):
43934393
wait_for(lambda: only_one(l1.rpc.listpeerchannels(l2.info['id'])['channels'])['peer_connected'] is False)
43944394

43954395

4396+
def test_peer_disconnected_has_featurebits(node_factory):
4397+
"""
4398+
Make sure that if a node is restarted, it still remembers feature
4399+
bits from a peer it has a channel with but isn't connected to
4400+
"""
4401+
l1, l2 = node_factory.line_graph(2)
4402+
4403+
expected_features = expected_peer_features()
4404+
4405+
l1_features = only_one(l2.rpc.listpeers()['peers'])['features']
4406+
l2_features = only_one(l1.rpc.listpeers()['peers'])['features']
4407+
assert l1_features == expected_features
4408+
assert l2_features == expected_features
4409+
4410+
l1.stop()
4411+
l2.stop()
4412+
4413+
# Ensure we persisted feature bits and return them even when disconnected
4414+
l1.start()
4415+
4416+
wait_for(lambda: only_one(l1.rpc.listpeers(l2.info['id'])['peers'])['connected'] is False)
4417+
wait_for(lambda: only_one(l1.rpc.listpeers()['peers'])['features'] == expected_features)
4418+
4419+
43964420
@pytest.mark.developer("needs dev-no-reconnect")
43974421
def test_reconnect_no_additional_transient_failure(node_factory, bitcoind):
43984422
l1, l2 = node_factory.line_graph(2, opts=[{'may_reconnect': True},

wallet/db.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,7 @@ static struct migration dbmigrations[] = {
949949
{NULL, migrate_invalid_last_tx_psbts},
950950
{SQL("ALTER TABLE channels ADD channel_type BLOB DEFAULT NULL;"), NULL},
951951
{NULL, migrate_fill_in_channel_type},
952+
{SQL("ALTER TABLE peers ADD feature_bits BLOB DEFAULT NULL;"), NULL},
952953
};
953954

954955
/**

wallet/test/run-wallet.c

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

11731173
/* Add another utxo that's CSV-locked for 5 blocks */
11741174
assert(parse_wireaddr_internal(tmpctx, "localhost:1234", 0, false, &addr) == NULL);
1175-
channel.peer = new_peer(ld, 0, &id, &addr, false);
1175+
channel.peer = new_peer(ld, 0, &id, &addr, NULL, false);
11761176
channel.dbid = 1;
11771177
channel.type = channel_type_anchor_outputs(tmpctx);
11781178
memset(&u.outpoint, 3, sizeof(u.outpoint));
@@ -1502,7 +1502,7 @@ static bool test_channel_crud(struct lightningd *ld, const tal_t *ctx)
15021502
c1.first_blocknum = 1;
15031503
assert(parse_wireaddr_internal(tmpctx, "localhost:1234", 0, false, &addr) == NULL);
15041504
c1.final_key_idx = 1337;
1505-
p = new_peer(ld, 0, &id, &addr, false);
1505+
p = new_peer(ld, 0, &id, &addr, NULL, false);
15061506
c1.peer = p;
15071507
c1.dbid = wallet_get_channel_dbid(w);
15081508
c1.state = CHANNELD_NORMAL;
@@ -1665,7 +1665,7 @@ static bool test_channel_inflight_crud(struct lightningd *ld, const tal_t *ctx)
16651665
assert(parse_wireaddr_internal(tmpctx, "localhost:1234", 0, false, &addr) == NULL);
16661666

16671667
/* new channel! */
1668-
p = new_peer(ld, 0, &id, &addr, false);
1668+
p = new_peer(ld, 0, &id, &addr, NULL, false);
16691669

16701670
funding_sats = AMOUNT_SAT(4444444);
16711671
our_sats = AMOUNT_SAT(3333333);

wallet/wallet.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid)
859859
struct db_stmt *stmt;
860860

861861
stmt = db_prepare_v2(
862-
w->db, SQL("SELECT id, node_id, address FROM peers WHERE id=?;"));
862+
w->db, SQL("SELECT id, node_id, address, feature_bits FROM peers WHERE id=?;"));
863863
db_bind_u64(stmt, 0, dbid);
864864
db_query_prepared(stmt);
865865

@@ -869,6 +869,7 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid)
869869
if (db_col_is_null(stmt, "node_id")) {
870870
db_col_ignore(stmt, "address");
871871
db_col_ignore(stmt, "id");
872+
db_col_ignore(stmt, "feature_bits");
872873
goto done;
873874
}
874875

@@ -886,7 +887,7 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid)
886887
}
887888

888889
/* FIXME: save incoming in db! */
889-
peer = new_peer(w->ld, db_col_u64(stmt, "id"), &id, &addr, false);
890+
peer = new_peer(w->ld, db_col_u64(stmt, "id"), &id, &addr, db_col_arr(stmt, stmt, "feature_bits", u8), false);
890891

891892
done:
892893
tal_free(stmt);
@@ -2271,21 +2272,23 @@ static void wallet_peer_save(struct wallet *w, struct peer *peer)
22712272
peer_set_dbid(peer, db_col_u64(stmt, "id"));
22722273
tal_free(stmt);
22732274

2274-
/* Since we're at it update the wireaddr */
2275+
/* Since we're at it update the wireaddr, feature bits */
22752276
stmt = db_prepare_v2(
2276-
w->db, SQL("UPDATE peers SET address = ? WHERE id = ?"));
2277+
w->db, SQL("UPDATE peers SET address = ?, feature_bits = ? WHERE id = ?"));
22772278
db_bind_text(stmt, 0, addr);
2278-
db_bind_u64(stmt, 1, peer->dbid);
2279+
db_bind_talarr(stmt, 1, peer->their_features);
2280+
db_bind_u64(stmt, 2, peer->dbid);
22792281
db_exec_prepared_v2(take(stmt));
22802282

22812283
} else {
22822284
/* Unknown peer, create it from scratch */
22832285
tal_free(stmt);
22842286
stmt = db_prepare_v2(w->db,
2285-
SQL("INSERT INTO peers (node_id, address) VALUES (?, ?);")
2287+
SQL("INSERT INTO peers (node_id, address, feature_bits) VALUES (?, ?, ?);")
22862288
);
22872289
db_bind_node_id(stmt, 0, &peer->id);
22882290
db_bind_text(stmt, 1,addr);
2291+
db_bind_talarr(stmt, 2, peer->their_features);
22892292
db_exec_prepared_v2(stmt);
22902293
peer_set_dbid(peer, db_last_insert_id_v2(take(stmt)));
22912294
}

0 commit comments

Comments
 (0)