diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index d28360c8f..d037d35cc 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -254,6 +254,13 @@ struct json_result *new_json_result(const tal_t *ctx UNNEEDED) /* Generated stub for null_response */ struct json_result *null_response(const tal_t *ctx UNNEEDED) { fprintf(stderr, "null_response called!\n"); abort(); } +/* Generated stub for outpointfilter_add */ +void outpointfilter_add(struct outpointfilter *of UNNEEDED, + const struct bitcoin_txid *txid UNNEEDED, const u32 outnum UNNEEDED) +{ fprintf(stderr, "outpointfilter_add called!\n"); abort(); } +/* Generated stub for outpointfilter_new */ +struct outpointfilter *outpointfilter_new(tal_t *ctx UNNEEDED) +{ fprintf(stderr, "outpointfilter_new called!\n"); abort(); } /* Generated stub for peer_accept_channel */ u8 *peer_accept_channel(struct lightningd *ld UNNEEDED, const struct pubkey *peer_id UNNEEDED, diff --git a/wallet/wallet.c b/wallet/wallet.c index 69d562d11..027715181 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -16,6 +16,17 @@ #define DIRECTION_INCOMING 0 #define DIRECTION_OUTGOING 1 +static void outpointfilters_init(struct wallet *w) +{ + struct utxo **utxos = wallet_get_utxos(NULL, w, output_state_any); + + w->owned_outpoints = outpointfilter_new(w); + for (size_t i = 0; i < tal_count(utxos); i++) + outpointfilter_add(w->owned_outpoints, &utxos[i]->txid, utxos[i]->outnum); + + tal_free(utxos); +} + struct wallet *wallet_new(struct lightningd *ld, struct log *log, struct timers *timers) { @@ -26,6 +37,10 @@ struct wallet *wallet_new(struct lightningd *ld, wallet->bip32_base = NULL; wallet->invoices = invoices_new(wallet, wallet->db, log, timers); list_head_init(&wallet->unstored_payments); + + db_begin_transaction(wallet->db); + outpointfilters_init(wallet); + db_commit_transaction(wallet->db); return wallet; } @@ -1016,6 +1031,8 @@ int wallet_extract_owned_outputs(struct wallet *w, const struct bitcoin_tx *tx, tal_free(utxo); return -1; } + outpointfilter_add(w->owned_outpoints, &utxo->txid, utxo->outnum); + *total_satoshi += utxo->amount; tal_free(utxo); num_utxos++; diff --git a/wallet/wallet.h b/wallet/wallet.h index 514bcd2f4..eda20ca88 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -32,6 +32,10 @@ struct wallet { struct invoices *invoices; struct list_head unstored_payments; u64 max_channel_dbid; + + /* Filter matching all outpoints corresponding to our owned outputs, + * including all spent ones */ + struct outpointfilter *owned_outpoints; }; /* Possible states for tracked outputs in the database. Not sure yet