| 5822 | } |
| 5823 | |
| 5824 | bool wallet_forward_delete(struct wallet *w, |
| 5825 | struct short_channel_id chan_in, |
| 5826 | const u64 *htlc_id, |
| 5827 | enum forward_status state) |
| 5828 | { |
| 5829 | struct db_stmt *stmt; |
| 5830 | bool changed; |
| 5831 | |
| 5832 | /* When deleting settled ones, we have to add to deleted_forward_fees! */ |
| 5833 | if (state == FORWARD_SETTLED) { |
| 5834 | /* Of course, it might not be settled: don't add if they're wrong! */ |
| 5835 | if (htlc_id) { |
| 5836 | stmt = db_prepare_v2(w->db, SQL("SELECT" |
| 5837 | " CAST(COALESCE(SUM(in_msatoshi - out_msatoshi), 0) AS BIGINT)" |
| 5838 | " FROM forwards " |
| 5839 | " WHERE in_channel_scid = ?" |
| 5840 | " AND in_htlc_id = ?" |
| 5841 | " AND state = ?;")); |
| 5842 | db_bind_short_channel_id(stmt, chan_in); |
| 5843 | db_bind_u64(stmt, *htlc_id); |
| 5844 | db_bind_int(stmt, wallet_forward_status_in_db(FORWARD_SETTLED)); |
| 5845 | } else { |
| 5846 | stmt = db_prepare_v2(w->db, SQL("SELECT" |
| 5847 | " CAST(COALESCE(SUM(in_msatoshi - out_msatoshi), 0) AS BIGINT)" |
| 5848 | " FROM forwards " |
| 5849 | " WHERE in_channel_scid = ?" |
| 5850 | " AND in_htlc_id IS NULL" |
| 5851 | " AND state = ?;")); |
| 5852 | db_bind_short_channel_id(stmt, chan_in); |
| 5853 | db_bind_int(stmt, wallet_forward_status_in_db(FORWARD_SETTLED)); |
| 5854 | } |
| 5855 | db_query_prepared(stmt); |
| 5856 | |
| 5857 | if (db_step(stmt)) { |
| 5858 | struct amount_msat deleted; |
| 5859 | |
| 5860 | deleted = db_col_amount_msat(stmt, "CAST(COALESCE(SUM(in_msatoshi - out_msatoshi), 0) AS BIGINT)"); |
| 5861 | deleted.millisatoshis += /* Raw: db access */ |
| 5862 | db_get_intvar(w->db, "deleted_forward_fees", 0); |
| 5863 | db_set_intvar(w->db, "deleted_forward_fees", |
| 5864 | deleted.millisatoshis); /* Raw: db access */ |
| 5865 | } |
| 5866 | tal_free(stmt); |
| 5867 | } |
| 5868 | |
| 5869 | if (htlc_id) { |
| 5870 | stmt = db_prepare_v2(w->db, |
| 5871 | SQL("DELETE FROM forwards" |
| 5872 | " WHERE in_channel_scid = ?" |
| 5873 | " AND in_htlc_id = ?" |
| 5874 | " AND state = ?")); |
| 5875 | db_bind_short_channel_id(stmt, chan_in); |
| 5876 | db_bind_u64(stmt, *htlc_id); |
| 5877 | db_bind_int(stmt, wallet_forward_status_in_db(state)); |
| 5878 | } else { |
| 5879 | stmt = db_prepare_v2(w->db, |
| 5880 | SQL("DELETE FROM forwards" |
| 5881 | " WHERE in_channel_scid = ?" |
no test coverage detected