| 272 | } |
| 273 | |
| 274 | static void insert_chain_fees_diff(struct command *cmd, |
| 275 | struct bkpr *bkpr, |
| 276 | const char *acct_name, |
| 277 | struct bitcoin_txid *txid, |
| 278 | struct amount_msat amount, |
| 279 | u64 timestamp) |
| 280 | { |
| 281 | struct onchain_fee *of, **ofs; |
| 282 | u32 max_update_count = 0; |
| 283 | struct amount_msat current_amt, credit, debit; |
| 284 | |
| 285 | current_amt = AMOUNT_MSAT(0); |
| 286 | ofs = account_get_chain_fees(tmpctx, bkpr, acct_name); |
| 287 | |
| 288 | for (size_t i = 0; i < tal_count(ofs); i++) { |
| 289 | if (!bitcoin_txid_eq(&ofs[i]->txid, txid)) |
| 290 | continue; |
| 291 | if (!amount_msat_accumulate(¤t_amt, ofs[i]->credit)) |
| 292 | plugin_err(cmd->plugin, "Overflow when adding onchain fees"); |
| 293 | |
| 294 | if (!amount_msat_deduct(¤t_amt, ofs[i]->debit)) |
| 295 | plugin_err(cmd->plugin, "Underflow when subtracting onchain fees"); |
| 296 | if (ofs[i]->update_count > max_update_count) |
| 297 | max_update_count = ofs[i]->update_count; |
| 298 | } |
| 299 | |
| 300 | /* If they're already equal, no need to update */ |
| 301 | if (amount_msat_eq(current_amt, amount)) |
| 302 | return; |
| 303 | |
| 304 | if (!amount_msat_sub(&credit, amount, current_amt)) { |
| 305 | credit = AMOUNT_MSAT(0); |
| 306 | if (!amount_msat_sub(&debit, current_amt, amount)) |
| 307 | plugin_err(cmd->plugin, "shouldn't happen, unable to subtract"); |
| 308 | } else |
| 309 | debit = AMOUNT_MSAT(0); |
| 310 | |
| 311 | of = new_onchain_fee(bkpr->onchain_fees, |
| 312 | acct_name, txid, credit, debit, timestamp, |
| 313 | max_update_count+1); |
| 314 | onchain_fee_datastore_add(cmd, of); |
| 315 | } |
| 316 | |
| 317 | /* Sort these as ORDER BY txid, account_name */ |
| 318 | static int compare_onchain_fee_txid_account(struct onchain_fee *const *a, |
no test coverage detected