From 8a85bf68809601be2ed85ad8bd73e27bc5c265e9 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 13 Oct 2021 14:15:30 +1030 Subject: [PATCH] lightningd: populate min/max commit fields in db. Signed-off-by: Rusty Russell --- lightningd/peer_htlcs.c | 19 ++++++++++++++++--- wallet/test/run-wallet.c | 11 ++++++----- wallet/wallet.c | 26 ++++++++++++++++++++------ wallet/wallet.h | 2 ++ 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index 1f9891efd..e937b8105 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -63,6 +63,8 @@ static bool htlc_in_update_state(struct channel *channel, wallet_htlc_update(channel->peer->ld->wallet, hin->dbid, newstate, hin->preimage, + max_unsigned(channel->next_index[LOCAL], + channel->next_index[REMOTE]), hin->badonion, hin->failonion, NULL, hin->we_filled); @@ -80,7 +82,10 @@ static bool htlc_out_update_state(struct channel *channel, bool we_filled = false; wallet_htlc_update(channel->peer->ld->wallet, hout->dbid, newstate, - hout->preimage, 0, hout->failonion, + hout->preimage, + max_unsigned(channel->next_index[LOCAL], + channel->next_index[REMOTE]), + 0, hout->failonion, hout->failmsg, &we_filled); hout->hstate = newstate; @@ -174,6 +179,8 @@ static void failmsg_update_reply(struct subd *gossipd, wallet_htlc_update(gossipd->ld->wallet, cbdata->hin->dbid, cbdata->hin->hstate, cbdata->hin->preimage, + max_unsigned(cbdata->hin->key.channel->next_index[LOCAL], + cbdata->hin->key.channel->next_index[REMOTE]), cbdata->hin->badonion, cbdata->hin->failonion, NULL, &we_filled); @@ -1282,7 +1289,10 @@ static void fulfill_our_htlc_out(struct channel *channel, struct htlc_out *hout, htlc_out_check(hout, __func__); wallet_htlc_update(ld->wallet, hout->dbid, hout->hstate, - hout->preimage, 0, hout->failonion, + hout->preimage, + max_unsigned(channel->next_index[LOCAL], + channel->next_index[REMOTE]), + 0, hout->failonion, hout->failmsg, &we_filled); /* Update channel stats */ wallet_channel_stats_incr_out_fulfilled(ld->wallet, @@ -1453,7 +1463,10 @@ void onchain_failed_our_htlc(const struct channel *channel, bool we_filled = false; wallet_htlc_update(ld->wallet, hout->dbid, hout->hstate, - hout->preimage, 0, hout->failonion, + hout->preimage, + max_unsigned(channel->next_index[LOCAL], + channel->next_index[REMOTE]), + 0, hout->failonion, hout->failmsg, &we_filled); if (hout->am_origin) { diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 33e788b01..c4e603fad 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -1717,6 +1717,7 @@ static bool test_htlc_crud(struct lightningd *ld, const tal_t *ctx) chan->dbid = 1; chan->peer = peer; + chan->next_index[LOCAL] = chan->next_index[REMOTE] = 1; memset(&in, 0, sizeof(in)); memset(&out, 0, sizeof(out)); @@ -1743,17 +1744,17 @@ static bool test_htlc_crud(struct lightningd *ld, const tal_t *ctx) wallet_err = tal_free(wallet_err); /* Update */ - CHECK_MSG(transaction_wrap(w->db, wallet_htlc_update(w, in.dbid, RCVD_ADD_HTLC, NULL, 0, NULL, NULL, false)), + CHECK_MSG(transaction_wrap(w->db, wallet_htlc_update(w, in.dbid, RCVD_ADD_HTLC, NULL, 0, 0, NULL, NULL, false)), "Update HTLC with null payment_key failed"); CHECK_MSG( - transaction_wrap(w->db, wallet_htlc_update(w, in.dbid, SENT_REMOVE_HTLC, &payment_key, 0, NULL, NULL, false)), + transaction_wrap(w->db, wallet_htlc_update(w, in.dbid, SENT_REMOVE_HTLC, &payment_key, 0, 0, NULL, NULL, false)), "Update HTLC with payment_key failed"); onionreply = new_onionreply(tmpctx, tal_arrz(tmpctx, u8, 100)); CHECK_MSG( - transaction_wrap(w->db, wallet_htlc_update(w, in.dbid, SENT_REMOVE_HTLC, NULL, 0, onionreply, NULL, false)), + transaction_wrap(w->db, wallet_htlc_update(w, in.dbid, SENT_REMOVE_HTLC, NULL, 0, 0, onionreply, NULL, false)), "Update HTLC with failonion failed"); CHECK_MSG( - transaction_wrap(w->db, wallet_htlc_update(w, in.dbid, SENT_REMOVE_HTLC, NULL, WIRE_INVALID_ONION_VERSION, NULL, NULL, false)), + transaction_wrap(w->db, wallet_htlc_update(w, in.dbid, SENT_REMOVE_HTLC, NULL, 0, WIRE_INVALID_ONION_VERSION, NULL, NULL, false)), "Update HTLC with failcode failed"); CHECK_MSG(transaction_wrap(w->db, wallet_htlc_save_out(w, chan, &out)), @@ -1765,7 +1766,7 @@ static bool test_htlc_crud(struct lightningd *ld, const tal_t *ctx) CHECK(wallet_err); wallet_err = tal_free(wallet_err); CHECK_MSG( - transaction_wrap(w->db, wallet_htlc_update(w, out.dbid, SENT_ADD_ACK_REVOCATION, NULL, 0, NULL, tal_arrz(tmpctx, u8, 100), false)), + transaction_wrap(w->db, wallet_htlc_update(w, out.dbid, SENT_ADD_ACK_REVOCATION, NULL, 0, 0, NULL, tal_arrz(tmpctx, u8, 100), false)), "Update outgoing HTLC with failmsg failed"); /* Attempt to load them from the DB again */ diff --git a/wallet/wallet.c b/wallet/wallet.c index 227740077..e7530d1f6 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -2314,8 +2314,9 @@ void wallet_htlc_save_in(struct wallet *wallet, " hstate," " shared_secret," " routing_onion," - " received_time) VALUES " - "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")); + " received_time," + " min_commit_num) VALUES " + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")); db_bind_u64(stmt, 0, chan->dbid); db_bind_u64(stmt, 1, in->key.id); @@ -2339,6 +2340,8 @@ void wallet_htlc_save_in(struct wallet *wallet, sizeof(in->onion_routing_packet)); db_bind_timeabs(stmt, 10, in->received_time); + db_bind_u64(stmt, 11, min_unsigned(chan->next_index[LOCAL]-1, + chan->next_index[REMOTE]-1)); db_exec_prepared_v2(stmt); in->dbid = db_last_insert_id_v2(take(stmt)); @@ -2369,8 +2372,9 @@ void wallet_htlc_save_out(struct wallet *wallet, " routing_onion," " malformed_onion," " partid," - " groupid" - ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?);")); + " groupid," + " min_commit_num" + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?);")); db_bind_u64(stmt, 0, chan->dbid); db_bind_u64(stmt, 1, out->key.id); @@ -2400,6 +2404,8 @@ void wallet_htlc_save_out(struct wallet *wallet, db_bind_u64(stmt, 10, out->partid); db_bind_u64(stmt, 11, out->groupid); } + db_bind_u64(stmt, 12, min_u64(chan->next_index[LOCAL]-1, + chan->next_index[REMOTE]-1)); db_exec_prepared_v2(stmt); out->dbid = db_last_insert_id_v2(stmt); @@ -2410,6 +2416,7 @@ void wallet_htlc_save_out(struct wallet *wallet, void wallet_htlc_update(struct wallet *wallet, const u64 htlc_dbid, const enum htlc_state new_state, const struct preimage *payment_key, + u64 max_commit_num, enum onion_wire badonion, const struct onionreply *failonion, const u8 *failmsg, @@ -2426,11 +2433,11 @@ void wallet_htlc_update(struct wallet *wallet, const u64 htlc_dbid, stmt = db_prepare_v2( wallet->db, SQL("UPDATE channel_htlcs SET hstate=?, payment_key=?, " "malformed_onion=?, failuremsg=?, localfailmsg=?, " - "we_filled=?" + "we_filled=?, max_commit_num=?" " WHERE id=?")); db_bind_int(stmt, 0, htlc_state_in_db(new_state)); - db_bind_u64(stmt, 6, htlc_dbid); + db_bind_u64(stmt, 7, htlc_dbid); if (payment_key) db_bind_preimage(stmt, 1, payment_key); @@ -2451,6 +2458,13 @@ void wallet_htlc_update(struct wallet *wallet, const u64 htlc_dbid, else db_bind_null(stmt, 5); + /* Set max_commit_num iff we're in final state. */ + if (new_state == RCVD_REMOVE_ACK_REVOCATION + || new_state == SENT_REMOVE_ACK_REVOCATION) + db_bind_u64(stmt, 6, max_commit_num); + else + db_bind_null(stmt, 6); + db_exec_prepared_v2(take(stmt)); } diff --git a/wallet/wallet.h b/wallet/wallet.h index 7cf7eaaf1..9215e2824 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -713,6 +713,7 @@ void wallet_htlc_save_out(struct wallet *wallet, * @new_state: the state we should transition to * @payment_key: the `payment_key` which hashes to the `payment_hash`, * or NULL if unknown. + * @max_commit_num: maximum of local and remote commitment numbers. * @badonion: the current BADONION failure code, or 0. * @failonion: the current failure onion message (from peer), or NULL. * @failmsg: the current local failure message, or NULL. @@ -725,6 +726,7 @@ void wallet_htlc_save_out(struct wallet *wallet, void wallet_htlc_update(struct wallet *wallet, const u64 htlc_dbid, const enum htlc_state new_state, const struct preimage *payment_key, + u64 max_commit_num, enum onion_wire badonion, const struct onionreply *failonion, const u8 *failmsg,