input htlcs use failcode & failonion & we_filled, output htlcs use failmsg & failonion */
| 2509 | |
| 2510 | /* input htlcs use failcode & failonion & we_filled, output htlcs use failmsg & failonion */ |
| 2511 | void wallet_htlc_update(struct wallet *wallet, const u64 htlc_dbid, |
| 2512 | const enum htlc_state new_state, |
| 2513 | const struct preimage *payment_key, |
| 2514 | u64 max_commit_num, |
| 2515 | enum onion_wire badonion, |
| 2516 | const struct onionreply *failonion, |
| 2517 | const u8 *failmsg, |
| 2518 | bool *we_filled) |
| 2519 | { |
| 2520 | struct db_stmt *stmt; |
| 2521 | bool terminal = (new_state == RCVD_REMOVE_ACK_REVOCATION |
| 2522 | || new_state == SENT_REMOVE_ACK_REVOCATION); |
| 2523 | |
| 2524 | /* We should only use this for badonion codes */ |
| 2525 | assert(!badonion || (badonion & BADONION)); |
| 2526 | |
| 2527 | /* The database ID must be set by a previous call to |
| 2528 | * `wallet_htlc_save_*` */ |
| 2529 | assert(htlc_dbid); |
| 2530 | stmt = db_prepare_v2( |
| 2531 | wallet->db, SQL("UPDATE channel_htlcs SET hstate=?, payment_key=?, " |
| 2532 | "malformed_onion=?, failuremsg=?, localfailmsg=?, " |
| 2533 | "we_filled=?, max_commit_num=?" |
| 2534 | " WHERE id=?")); |
| 2535 | |
| 2536 | db_bind_int(stmt, 0, htlc_state_in_db(new_state)); |
| 2537 | db_bind_u64(stmt, 7, htlc_dbid); |
| 2538 | |
| 2539 | if (payment_key) |
| 2540 | db_bind_preimage(stmt, 1, payment_key); |
| 2541 | else |
| 2542 | db_bind_null(stmt, 1); |
| 2543 | |
| 2544 | db_bind_int(stmt, 2, badonion); |
| 2545 | |
| 2546 | if (failonion) |
| 2547 | db_bind_onionreply(stmt, 3, failonion); |
| 2548 | else |
| 2549 | db_bind_null(stmt, 3); |
| 2550 | |
| 2551 | db_bind_talarr(stmt, 4, failmsg); |
| 2552 | |
| 2553 | if (we_filled) |
| 2554 | db_bind_int(stmt, 5, *we_filled); |
| 2555 | else |
| 2556 | db_bind_null(stmt, 5); |
| 2557 | |
| 2558 | /* Set max_commit_num iff we're in final state. */ |
| 2559 | if (terminal) |
| 2560 | db_bind_u64(stmt, 6, max_commit_num); |
| 2561 | else |
| 2562 | db_bind_null(stmt, 6); |
| 2563 | |
| 2564 | db_exec_prepared_v2(take(stmt)); |
| 2565 | |
| 2566 | if (terminal) { |
| 2567 | /* If it's terminal, remove the data we needed for re-xmission. */ |
| 2568 | stmt = db_prepare_v2( |