| 872 | } |
| 873 | |
| 874 | void migrate_invalid_last_tx_psbts(struct lightningd *ld, struct db *db) |
| 875 | { |
| 876 | struct db_stmt *stmt; |
| 877 | |
| 878 | /* We try all of them, but note that last_tx used to be a tx, |
| 879 | * and migrate_last_tx_to_psbt didn't convert channels which had |
| 880 | * already been closed, so we expect some failures. */ |
| 881 | stmt = db_prepare_v2(db, SQL("SELECT " |
| 882 | " id" |
| 883 | ", state" |
| 884 | ", last_tx" |
| 885 | " FROM channels")); |
| 886 | |
| 887 | db_query_prepared(stmt); |
| 888 | while (db_step(stmt)) { |
| 889 | struct db_stmt *update_stmt; |
| 890 | const u8 *bytes, *fixed; |
| 891 | enum channel_state state; |
| 892 | u64 id; |
| 893 | struct wally_psbt *psbt; |
| 894 | |
| 895 | state = db_col_int(stmt, "state"); |
| 896 | id = db_col_u64(stmt, "id"); |
| 897 | |
| 898 | /* Parses fine? */ |
| 899 | if (db_col_psbt(tmpctx, stmt, "last_tx")) |
| 900 | continue; |
| 901 | |
| 902 | /* Can we fix it? */ |
| 903 | bytes = db_col_arr(tmpctx, stmt, "last_tx", u8); |
| 904 | fixed = psbt_fixup(tmpctx, bytes); |
| 905 | if (!fixed) { |
| 906 | complain_unfixed(ld, state, id, bytes, "Could not fix"); |
| 907 | continue; |
| 908 | } |
| 909 | psbt = psbt_from_bytes(tmpctx, fixed, tal_bytelen(fixed)); |
| 910 | if (!psbt) { |
| 911 | complain_unfixed(ld, state, id, fixed, "Fix made invalid psbt"); |
| 912 | continue; |
| 913 | } |
| 914 | |
| 915 | log_broken(ld->log, "Forced database repair of psbt %s -> %s", |
| 916 | tal_hex(tmpctx, bytes), tal_hex(tmpctx, fixed)); |
| 917 | update_stmt = db_prepare_v2(db, SQL("UPDATE channels" |
| 918 | " SET last_tx = ?" |
| 919 | " WHERE id = ?;")); |
| 920 | db_bind_psbt(update_stmt, psbt); |
| 921 | db_bind_u64(update_stmt, id); |
| 922 | db_exec_prepared_v2(update_stmt); |
| 923 | tal_free(update_stmt); |
| 924 | } |
| 925 | tal_free(stmt); |
| 926 | } |
| 927 | /** |
| 928 | * We store the bolt11 string in several places with the `lightning:` prefix, so |
| 929 | * we update one by one by lowering and normalize the string in a canonical one. |
nothing calls this directly
no test coverage detected