Skip to content

Commit

Permalink
lightningd: add last_stable_connection field to db, channel.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Jan 31, 2024
1 parent 81a280d commit 0575f8a
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 9 deletions.
5 changes: 4 additions & 1 deletion lightningd/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ struct channel *new_unsaved_channel(struct peer *peer,

channel->lease_commit_sig = NULL;
channel->ignore_fee_limits = ld->config.ignore_fee_limits;
channel->last_stable_connection = 0;

/* No shachain yet */
channel->their_shachain.id = 0;
Expand Down Expand Up @@ -449,7 +450,8 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
struct amount_msat htlc_maximum_msat,
bool ignore_fee_limits,
/* NULL or stolen */
struct peer_update *peer_update STEALS)
struct peer_update *peer_update STEALS,
u64 last_stable_connection)
{
struct channel *channel = tal(peer->ld, struct channel);
struct amount_msat htlc_min, htlc_max;
Expand Down Expand Up @@ -600,6 +602,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
channel->close_blockheight = NULL;
channel->state_change_cause = reason;
channel->ignore_fee_limits = ignore_fee_limits;
channel->last_stable_connection = last_stable_connection;
/* Populate channel->channel_gossip */
channel_gossip_init(channel, take(peer_update));

Expand Down
6 changes: 5 additions & 1 deletion lightningd/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ struct channel {

/* Do we allow the peer to set any fee it wants? */
bool ignore_fee_limits;

/* Last time we had a stable connection, if any (0 = none) */
u64 last_stable_connection;
};

/* Is channel owned (and should be talking to peer) */
Expand Down Expand Up @@ -390,7 +393,8 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
struct amount_msat htlc_maximum_msat,
bool ignore_fee_limits,
/* NULL or stolen */
struct peer_update *peer_update STEALS);
struct peer_update *peer_update STEALS,
u64 last_stable_connection);

/* new_inflight - Create a new channel_inflight for a channel */
struct channel_inflight *new_inflight(struct channel *channel,
Expand Down
1 change: 1 addition & 0 deletions lightningd/closed_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct closed_channel {
const struct channel_type *type;
enum state_change state_change_cause;
bool leased;
u64 last_stable_connection;
};

#endif /* LIGHTNING_LIGHTNINGD_CLOSED_CHANNEL_H */
6 changes: 4 additions & 2 deletions lightningd/opening_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ wallet_commit_channel(struct lightningd *ld,
ld->config.htlc_minimum_msat,
ld->config.htlc_maximum_msat,
ld->config.ignore_fee_limits,
NULL);
NULL,
0);

/* Now we finally put it in the database. */
wallet_channel_insert(ld->wallet, channel);
Expand Down Expand Up @@ -1504,7 +1505,8 @@ static struct channel *stub_chan(struct command *cmd,
ld->config.htlc_minimum_msat,
ld->config.htlc_maximum_msat,
false,
NULL);
NULL,
0);

/* We don't want to gossip about this, ever. */
channel->channel_gossip = tal_free(channel->channel_gossip);
Expand Down
1 change: 1 addition & 0 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,7 @@ static struct migration dbmigrations[] = {
{SQL("ALTER TABLE channels ADD remote_cltv_expiry_delta INTEGER DEFAULT NULL;"), NULL},
{SQL("ALTER TABLE channels ADD remote_htlc_maximum_msat BIGINT DEFAULT NULL;"), NULL},
{SQL("ALTER TABLE channels ADD remote_htlc_minimum_msat BIGINT DEFAULT NULL;"), NULL},
{SQL("ALTER TABLE channels ADD last_stable_connection BIGINT DEFAULT 0;"), NULL},
};

/**
Expand Down
3 changes: 2 additions & 1 deletion wallet/test/run-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ struct channel *new_channel(struct peer *peer UNNEEDED, u64 dbid UNNEEDED,
struct amount_msat htlc_maximum_msat UNNEEDED,
bool ignore_fee_limits UNNEEDED,
/* NULL or stolen */
struct peer_update *peer_update STEALS UNNEEDED)
struct peer_update *peer_update STEALS UNNEEDED,
u64 last_stable_connection UNNEEDED)
{ fprintf(stderr, "new_channel called!\n"); abort(); }
/* Generated stub for new_coin_wallet_deposit */
struct chain_coin_mvt *new_coin_wallet_deposit(const tal_t *ctx UNNEEDED,
Expand Down
3 changes: 2 additions & 1 deletion wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,8 @@ static bool test_channel_inflight_crud(struct lightningd *ld, const tal_t *ctx)
AMOUNT_MSAT(0),
AMOUNT_MSAT(-1ULL),
false,
NULL);
NULL,
0);
db_begin_transaction(w->db);
CHECK(!wallet_err);
wallet_channel_insert(w, chan);
Expand Down
12 changes: 9 additions & 3 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1763,7 +1763,8 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm
htlc_minimum_msat,
htlc_maximum_msat,
ignore_fee_limits,
remote_update);
remote_update,
db_col_u64(stmt, "last_stable_connection"));

if (!wallet_channel_load_inflights(w, chan)) {
tal_free(chan);
Expand Down Expand Up @@ -1800,6 +1801,7 @@ static struct closed_channel *wallet_stmt2closed_channel(const tal_t *ctx,
cc->our_msat = db_col_amount_msat(stmt, "msatoshi_local");
cc->msat_to_us_min = db_col_amount_msat(stmt, "msatoshi_to_us_min");
cc->msat_to_us_max = db_col_amount_msat(stmt, "msatoshi_to_us_max");
cc->last_stable_connection = db_col_u64(stmt, "last_stable_connection");
/* last_tx is null for stub channels used for recovering funds through
* Static channel backups. */
if (!db_col_is_null(stmt, "last_tx"))
Expand Down Expand Up @@ -1845,6 +1847,7 @@ struct closed_channel **wallet_load_closed_channels(const tal_t *ctx,
", channel_type"
", state_change_reason"
", lease_commit_sig"
", last_stable_connection"
" FROM channels"
" LEFT JOIN peers p ON p.id = peer_id"
" WHERE state = ?;"));
Expand Down Expand Up @@ -1956,6 +1959,7 @@ static bool wallet_channels_load_active(struct wallet *w)
", remote_cltv_expiry_delta"
", remote_htlc_minimum_msat"
", remote_htlc_maximum_msat"
", last_stable_connection"
" FROM channels"
" WHERE state != ?;")); //? 0
db_bind_int(stmt, CLOSED);
Expand Down Expand Up @@ -2275,8 +2279,9 @@ void wallet_channel_save(struct wallet *w, struct channel *chan)
" remote_feerate_ppm=?," // 48
" remote_cltv_expiry_delta=?," // 49
" remote_htlc_minimum_msat=?," // 50
" remote_htlc_maximum_msat=?" // 51
" WHERE id=?")); // 52
" remote_htlc_maximum_msat=?,"
" last_stable_connection=?"
" WHERE id=?"));
db_bind_u64(stmt, chan->their_shachain.id);
if (chan->scid)
db_bind_short_channel_id(stmt, chan->scid);
Expand Down Expand Up @@ -2372,6 +2377,7 @@ void wallet_channel_save(struct wallet *w, struct channel *chan)
db_bind_null(stmt);
db_bind_null(stmt);
}
db_bind_u64(stmt, chan->last_stable_connection);
db_bind_u64(stmt, chan->dbid);
db_exec_prepared_v2(take(stmt));

Expand Down

0 comments on commit 0575f8a

Please sign in to comment.