lightningd: populate min/max commit fields in db.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2021-10-13 14:15:30 +10:30 committed by Christian Decker
parent 36b66871a2
commit 8a85bf6880
4 changed files with 44 additions and 14 deletions

View File

@ -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) {

View File

@ -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 */

View File

@ -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));
}

View File

@ -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,