| 3334 | } |
| 3335 | |
| 3336 | void wallet_payment_set_status(struct wallet *wallet, |
| 3337 | const struct sha256 *payment_hash, |
| 3338 | u64 partid, u64 groupid, |
| 3339 | const enum wallet_payment_status newstatus, |
| 3340 | const struct preimage *preimage) |
| 3341 | { |
| 3342 | struct db_stmt *stmt; |
| 3343 | struct wallet_payment *payment; |
| 3344 | u32 completed_at = 0; |
| 3345 | |
| 3346 | if (newstatus != PAYMENT_PENDING) |
| 3347 | completed_at = time_now().ts.tv_sec; |
| 3348 | |
| 3349 | /* We can only fail an unstored payment! */ |
| 3350 | payment = find_unstored_payment(wallet, payment_hash, partid); |
| 3351 | if (payment) { |
| 3352 | assert(newstatus == PAYMENT_FAILED); |
| 3353 | tal_free(payment); |
| 3354 | return; |
| 3355 | } |
| 3356 | |
| 3357 | stmt = db_prepare_v2(wallet->db, |
| 3358 | SQL("UPDATE payments SET status=?, completed_at=? " |
| 3359 | "WHERE payment_hash=? AND partid=? AND groupid=?")); |
| 3360 | |
| 3361 | db_bind_int(stmt, 0, wallet_payment_status_in_db(newstatus)); |
| 3362 | if (completed_at != 0) { |
| 3363 | db_bind_u64(stmt, 1, completed_at); |
| 3364 | } else { |
| 3365 | db_bind_null(stmt, 1); |
| 3366 | } |
| 3367 | db_bind_sha256(stmt, 2, payment_hash); |
| 3368 | db_bind_u64(stmt, 3, partid); |
| 3369 | db_bind_u64(stmt, 4, groupid); |
| 3370 | db_exec_prepared_v2(take(stmt)); |
| 3371 | |
| 3372 | if (preimage) { |
| 3373 | stmt = db_prepare_v2(wallet->db, |
| 3374 | SQL("UPDATE payments SET payment_preimage=? " |
| 3375 | "WHERE payment_hash=? AND partid=? AND groupid=?")); |
| 3376 | |
| 3377 | db_bind_preimage(stmt, 0, preimage); |
| 3378 | db_bind_sha256(stmt, 1, payment_hash); |
| 3379 | db_bind_u64(stmt, 2, partid); |
| 3380 | db_bind_u64(stmt, 3, groupid); |
| 3381 | db_exec_prepared_v2(take(stmt)); |
| 3382 | } |
| 3383 | if (newstatus != PAYMENT_PENDING) { |
| 3384 | stmt = |
| 3385 | db_prepare_v2(wallet->db, SQL("UPDATE payments" |
| 3386 | " SET path_secrets = NULL" |
| 3387 | " , route_nodes = NULL" |
| 3388 | " , route_channels = NULL" |
| 3389 | " WHERE payment_hash = ?" |
| 3390 | " AND partid = ? AND groupid=?;")); |
| 3391 | db_bind_sha256(stmt, 0, payment_hash); |
| 3392 | db_bind_u64(stmt, 1, partid); |
| 3393 | db_bind_u64(stmt, 2, groupid); |