Skip to content

Commit

Permalink
Persist feature bits across restarts
Browse files Browse the repository at this point in the history
  • Loading branch information
instagibbs committed Jun 5, 2023
1 parent 1e457bd commit 0aecbdf
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions lightningd/opening_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,7 @@ static struct channel *stub_chan(struct command *cmd,
0,
&nodeid,
&wint,
NULL,
false);
}

Expand Down
6 changes: 5 additions & 1 deletion lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ void peer_set_dbid(struct peer *peer, u64 dbid)
struct peer *new_peer(struct lightningd *ld, u64 dbid,
const struct node_id *id,
const struct wireaddr_internal *addr,
const u8 *their_features,
bool connected_incoming)
{
/* We are owned by our channels, and freed manually by destroy_channel */
Expand All @@ -112,6 +113,9 @@ struct peer *new_peer(struct lightningd *ld, u64 dbid,
peer->connected = PEER_DISCONNECTED;
peer->last_connect_attempt.ts.tv_sec
= peer->last_connect_attempt.ts.tv_nsec = 0;
if (their_features)
peer_update_features(peer, their_features);

#if DEVELOPER
peer->ignore_htlcs = false;
#endif
Expand Down Expand Up @@ -1403,7 +1407,7 @@ void peer_connected(struct lightningd *ld, const u8 *msg)
peer = peer_by_id(ld, &id);
if (!peer)
peer = new_peer(ld, 0, &id, &hook_payload->addr,
hook_payload->incoming);
NULL, hook_payload->incoming);

/* We track this, because messages can race between connectd and us.
* For example, we could tell it to attach a subd, but it's actually
Expand Down
1 change: 1 addition & 0 deletions lightningd/peer_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ struct peer *find_peer_by_dbid(struct lightningd *ld, u64 dbid);
struct peer *new_peer(struct lightningd *ld, u64 dbid,
const struct node_id *id,
const struct wireaddr_internal *addr,
const u8 *their_features,
bool connected_incoming);

/* Last one out deletes peer. Also removes from db. */
Expand Down
1 change: 1 addition & 0 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,7 @@ static struct migration dbmigrations[] = {
{NULL, migrate_invalid_last_tx_psbts},
{SQL("ALTER TABLE channels ADD channel_type BLOB DEFAULT NULL;"), NULL},
{NULL, migrate_fill_in_channel_type},
{SQL("ALTER TABLE peers ADD feature_bits BLOB DEFAULT NULL;"), NULL},
};

/**
Expand Down
6 changes: 3 additions & 3 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx)

/* Add another utxo that's CSV-locked for 5 blocks */
assert(parse_wireaddr_internal(tmpctx, "localhost:1234", 0, false, &addr) == NULL);
channel.peer = new_peer(ld, 0, &id, &addr, false);
channel.peer = new_peer(ld, 0, &id, &addr, NULL, false);
channel.dbid = 1;
channel.type = channel_type_anchor_outputs(tmpctx);
memset(&u.outpoint, 3, sizeof(u.outpoint));
Expand Down Expand Up @@ -1502,7 +1502,7 @@ static bool test_channel_crud(struct lightningd *ld, const tal_t *ctx)
c1.first_blocknum = 1;
assert(parse_wireaddr_internal(tmpctx, "localhost:1234", 0, false, &addr) == NULL);
c1.final_key_idx = 1337;
p = new_peer(ld, 0, &id, &addr, false);
p = new_peer(ld, 0, &id, &addr, NULL, false);
c1.peer = p;
c1.dbid = wallet_get_channel_dbid(w);
c1.state = CHANNELD_NORMAL;
Expand Down Expand Up @@ -1665,7 +1665,7 @@ static bool test_channel_inflight_crud(struct lightningd *ld, const tal_t *ctx)
assert(parse_wireaddr_internal(tmpctx, "localhost:1234", 0, false, &addr) == NULL);

/* new channel! */
p = new_peer(ld, 0, &id, &addr, false);
p = new_peer(ld, 0, &id, &addr, NULL, false);

funding_sats = AMOUNT_SAT(4444444);
our_sats = AMOUNT_SAT(3333333);
Expand Down
13 changes: 8 additions & 5 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid)
struct db_stmt *stmt;

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

Expand All @@ -869,6 +869,7 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid)
if (db_col_is_null(stmt, "node_id")) {
db_col_ignore(stmt, "address");
db_col_ignore(stmt, "id");
db_col_ignore(stmt, "feature_bits");
goto done;
}

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

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

done:
tal_free(stmt);
Expand Down Expand Up @@ -2271,21 +2272,23 @@ static void wallet_peer_save(struct wallet *w, struct peer *peer)
peer_set_dbid(peer, db_col_u64(stmt, "id"));
tal_free(stmt);

/* Since we're at it update the wireaddr */
/* Since we're at it update the wireaddr, feature bits */
stmt = db_prepare_v2(
w->db, SQL("UPDATE peers SET address = ? WHERE id = ?"));
w->db, SQL("UPDATE peers SET address = ? feature_bits = ? WHERE id = ?"));
db_bind_text(stmt, 0, addr);
db_bind_u64(stmt, 1, peer->dbid);
db_bind_talarr(stmt, 2, peer->their_features);
db_exec_prepared_v2(take(stmt));

} else {
/* Unknown peer, create it from scratch */
tal_free(stmt);
stmt = db_prepare_v2(w->db,
SQL("INSERT INTO peers (node_id, address) VALUES (?, ?);")
SQL("INSERT INTO peers (node_id, address, feature_bits) VALUES (?, ?, ?);")
);
db_bind_node_id(stmt, 0, &peer->id);
db_bind_text(stmt, 1,addr);
db_bind_talarr(stmt, 2, peer->their_features);
db_exec_prepared_v2(stmt);
peer_set_dbid(peer, db_last_insert_id_v2(take(stmt)));
}
Expand Down

0 comments on commit 0aecbdf

Please sign in to comment.