| 4693 | } |
| 4694 | |
| 4695 | bool wallet_forward_delete(struct wallet *w, |
| 4696 | const struct short_channel_id *chan_in, |
| 4697 | const u64 *htlc_id, |
| 4698 | enum forward_status state) |
| 4699 | { |
| 4700 | struct db_stmt *stmt; |
| 4701 | bool changed; |
| 4702 | |
| 4703 | /* When deleting settled ones, we have to add to deleted_forward_fees! */ |
| 4704 | if (state == FORWARD_SETTLED) { |
| 4705 | /* Of course, it might not be settled: don't add if they're wrong! */ |
| 4706 | if (htlc_id) { |
| 4707 | stmt = db_prepare_v2(w->db, SQL("SELECT" |
| 4708 | " CAST(COALESCE(SUM(in_msatoshi - out_msatoshi), 0) AS BIGINT)" |
| 4709 | " FROM forwards " |
| 4710 | " WHERE in_channel_scid = ?" |
| 4711 | " AND in_htlc_id = ?" |
| 4712 | " AND state = ?;")); |
| 4713 | db_bind_scid(stmt, 0, chan_in); |
| 4714 | db_bind_u64(stmt, 1, *htlc_id); |
| 4715 | db_bind_int(stmt, 2, wallet_forward_status_in_db(FORWARD_SETTLED)); |
| 4716 | } else { |
| 4717 | stmt = db_prepare_v2(w->db, SQL("SELECT" |
| 4718 | " CAST(COALESCE(SUM(in_msatoshi - out_msatoshi), 0) AS BIGINT)" |
| 4719 | " FROM forwards " |
| 4720 | " WHERE in_channel_scid = ?" |
| 4721 | " AND in_htlc_id IS NULL" |
| 4722 | " AND state = ?;")); |
| 4723 | db_bind_scid(stmt, 0, chan_in); |
| 4724 | db_bind_int(stmt, 1, wallet_forward_status_in_db(FORWARD_SETTLED)); |
| 4725 | } |
| 4726 | db_query_prepared(stmt); |
| 4727 | |
| 4728 | if (db_step(stmt)) { |
| 4729 | struct amount_msat deleted; |
| 4730 | |
| 4731 | db_col_amount_msat(stmt, "CAST(COALESCE(SUM(in_msatoshi - out_msatoshi), 0) AS BIGINT)", &deleted); |
| 4732 | deleted.millisatoshis += /* Raw: db access */ |
| 4733 | db_get_intvar(w->db, "deleted_forward_fees", 0); |
| 4734 | db_set_intvar(w->db, "deleted_forward_fees", |
| 4735 | deleted.millisatoshis); /* Raw: db access */ |
| 4736 | } |
| 4737 | tal_free(stmt); |
| 4738 | } |
| 4739 | |
| 4740 | if (htlc_id) { |
| 4741 | stmt = db_prepare_v2(w->db, |
| 4742 | SQL("DELETE FROM forwards" |
| 4743 | " WHERE in_channel_scid = ?" |
| 4744 | " AND in_htlc_id = ?" |
| 4745 | " AND state = ?")); |
| 4746 | db_bind_scid(stmt, 0, chan_in); |
| 4747 | db_bind_u64(stmt, 1, *htlc_id); |
| 4748 | db_bind_int(stmt, 2, wallet_forward_status_in_db(state)); |
| 4749 | } else { |
| 4750 | stmt = db_prepare_v2(w->db, |
| 4751 | SQL("DELETE FROM forwards" |
| 4752 | " WHERE in_channel_scid = ?" |
no test coverage detected