* wallet_add_utxo - Register an UTXO which we (partially) own * * Add an UTXO to the set of outputs we care about. * * This can fail if we've already seen UTXO. */
| 115 | * This can fail if we've already seen UTXO. |
| 116 | */ |
| 117 | static bool wallet_add_utxo(struct wallet *w, struct utxo *utxo, |
| 118 | enum wallet_output_type type) |
| 119 | { |
| 120 | struct db_stmt *stmt; |
| 121 | |
| 122 | stmt = db_prepare_v2(w->db, SQL("SELECT * from outputs WHERE " |
| 123 | "prev_out_tx=? AND prev_out_index=?")); |
| 124 | db_bind_txid(stmt, 0, &utxo->outpoint.txid); |
| 125 | db_bind_int(stmt, 1, utxo->outpoint.n); |
| 126 | db_query_prepared(stmt); |
| 127 | |
| 128 | /* If we get a result, that means a clash. */ |
| 129 | if (db_step(stmt)) { |
| 130 | db_col_ignore(stmt, "*"); |
| 131 | tal_free(stmt); |
| 132 | return false; |
| 133 | } |
| 134 | tal_free(stmt); |
| 135 | |
| 136 | stmt = db_prepare_v2( |
| 137 | w->db, SQL("INSERT INTO outputs (" |
| 138 | " prev_out_tx" |
| 139 | ", prev_out_index" |
| 140 | ", value" |
| 141 | ", type" |
| 142 | ", status" |
| 143 | ", keyindex" |
| 144 | ", channel_id" |
| 145 | ", peer_id" |
| 146 | ", commitment_point" |
| 147 | ", option_anchor_outputs" |
| 148 | ", confirmation_height" |
| 149 | ", spend_height" |
| 150 | ", scriptpubkey" |
| 151 | ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")); |
| 152 | db_bind_txid(stmt, 0, &utxo->outpoint.txid); |
| 153 | db_bind_int(stmt, 1, utxo->outpoint.n); |
| 154 | db_bind_amount_sat(stmt, 2, &utxo->amount); |
| 155 | db_bind_int(stmt, 3, wallet_output_type_in_db(type)); |
| 156 | db_bind_int(stmt, 4, OUTPUT_STATE_AVAILABLE); |
| 157 | db_bind_int(stmt, 5, utxo->keyindex); |
| 158 | if (utxo->close_info) { |
| 159 | db_bind_u64(stmt, 6, utxo->close_info->channel_id); |
| 160 | db_bind_node_id(stmt, 7, &utxo->close_info->peer_id); |
| 161 | if (utxo->close_info->commitment_point) |
| 162 | db_bind_pubkey(stmt, 8, utxo->close_info->commitment_point); |
| 163 | else |
| 164 | db_bind_null(stmt, 8); |
| 165 | db_bind_int(stmt, 9, utxo->close_info->option_anchor_outputs); |
| 166 | } else { |
| 167 | db_bind_null(stmt, 6); |
| 168 | db_bind_null(stmt, 7); |
| 169 | db_bind_null(stmt, 8); |
| 170 | db_bind_null(stmt, 9); |
| 171 | } |
| 172 | |
| 173 | if (utxo->blockheight) { |
| 174 | db_bind_int(stmt, 10, *utxo->blockheight); |