From 27835df8fd4500f2da431ce52d74f4277fe4b112 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 13 Aug 2018 12:32:18 +0930 Subject: [PATCH] wallet: add accessor getting close-info-needed unconfirmed UTXOs. These are not confirmed by the normal methods (wallet_can_spend is false!), so we'll deal with them manually. We use UTXO_FIELDS in wallet_add_utxo, too, for consistency. Signed-off-by: Rusty Russell --- wallet/wallet.c | 42 +++++++++++++++++++++++++++--------------- wallet/wallet.h | 10 ++++++++++ 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/wallet/wallet.c b/wallet/wallet.c index 214692b7e..b0cb57180 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -61,6 +61,11 @@ struct wallet *wallet_new(struct lightningd *ld, return wallet; } +#define UTXO_FIELDS \ + "prev_out_tx, prev_out_index, value, type, status, keyindex, " \ + "channel_id, peer_id, commitment_point, confirmation_height, " \ + "spend_height" + /* We actually use the db constraints to uniquify, so OK if this fails. */ bool wallet_add_utxo(struct wallet *w, struct utxo *utxo, enum wallet_output_type type) @@ -68,17 +73,8 @@ bool wallet_add_utxo(struct wallet *w, struct utxo *utxo, sqlite3_stmt *stmt; stmt = db_prepare(w->db, "INSERT INTO outputs (" - "prev_out_tx, " - "prev_out_index, " - "value, " - "type, " - "status, " - "keyindex, " - "channel_id, " - "peer_id, " - "commitment_point, " - "confirmation_height, " - "spend_height) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"); + UTXO_FIELDS + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"); sqlite3_bind_blob(stmt, 1, &utxo->txid, sizeof(utxo->txid), SQLITE_TRANSIENT); sqlite3_bind_int(stmt, 2, utxo->outnum); sqlite3_bind_int64(stmt, 3, utxo->amount); @@ -175,10 +171,6 @@ bool wallet_update_output_status(struct wallet *w, return sqlite3_changes(w->db->sql) > 0; } -#define UTXO_FIELDS \ - "prev_out_tx, prev_out_index, value, type, status, keyindex, " \ - "channel_id, peer_id, commitment_point, confirmation_height, " \ - "spend_height" struct utxo **wallet_get_utxos(const tal_t *ctx, struct wallet *w, const enum output_status state) { struct utxo **results; @@ -205,6 +197,26 @@ struct utxo **wallet_get_utxos(const tal_t *ctx, struct wallet *w, const enum ou return results; } +struct utxo **wallet_get_unconfirmed_closeinfo_utxos(const tal_t *ctx, struct wallet *w) +{ + struct utxo **results; + int i; + + sqlite3_stmt *stmt = db_prepare( + w->db, "SELECT " UTXO_FIELDS + " FROM outputs WHERE channel_id IS NOT NULL and confirmation_height IS NULL"); + + results = tal_arr(ctx, struct utxo*, 0); + for (i=0; sqlite3_step(stmt) == SQLITE_ROW; i++) { + tal_resize(&results, i+1); + results[i] = tal(results, struct utxo); + wallet_stmt2output(stmt, results[i]); + } + db_stmt_done(stmt); + + return results; +} + /** * unreserve_utxo - Mark a reserved UTXO as available again */ diff --git a/wallet/wallet.h b/wallet/wallet.h index 2fa32c48e..9e1a20940 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -249,6 +249,16 @@ bool wallet_update_output_status(struct wallet *w, struct utxo **wallet_get_utxos(const tal_t *ctx, struct wallet *w, const enum output_status state); + +/** + * wallet_get_unconfirmed_closeinfo_utxos - Retrieve any unconfirmed utxos w/ closeinfo + * + * Returns a `tal_arr` of `utxo` structs. Double indirection in order + * to be able to steal individual elements onto something else. + */ +struct utxo **wallet_get_unconfirmed_closeinfo_utxos(const tal_t *ctx, + struct wallet *w); + const struct utxo **wallet_select_coins(const tal_t *ctx, struct wallet *w, const u64 value, const u32 feerate_per_kw,