diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index 11bb3d3a07df..77637065005b 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -55,7 +55,7 @@ static void filter_block_txs(struct chain_topology *topo, struct block *b) /* Now we see if any of those txs are interesting. */ const size_t num_txs = tal_count(b->full_txs); for (i = 0; i < num_txs; i++) { - const struct bitcoin_tx *tx = b->full_txs[i]; + struct bitcoin_tx *tx = b->full_txs[i]; struct bitcoin_txid txid; size_t j; bool is_coinbase = i == 0; @@ -63,12 +63,14 @@ static void filter_block_txs(struct chain_topology *topo, struct block *b) /* Tell them if it spends a txo we care about. */ for (j = 0; j < tx->wtx->num_inputs; j++) { struct bitcoin_outpoint out; - struct txowatch *txo; + struct txowatch_hash_iter it; + bitcoin_tx_input_get_txid(tx, j, &out.txid); out.n = tx->wtx->inputs[j].index; - txo = txowatch_hash_get(topo->txowatches, &out); - if (txo) { + for (struct txowatch *txo = txowatch_hash_getfirst(topo->txowatches, &out, &it); + txo; + txo = txowatch_hash_getnext(topo->txowatches, &out, &it)) { wallet_transaction_add(topo->ld->wallet, tx->wtx, b->height, i); txowatch_fire(txo, tx, j, b); @@ -104,7 +106,7 @@ static void filter_block_txs(struct chain_topology *topo, struct block *b) tx->wtx, b->height, i); } - txwatch_inform(topo, &txid, tx); + txwatch_inform(topo, &txid, take(tx)); } b->full_txs = tal_free(b->full_txs); b->txids = tal_free(b->txids); diff --git a/lightningd/watch.c b/lightningd/watch.c index 5cf9ffcb8dda..8ad4cd95cba9 100644 --- a/lightningd/watch.c +++ b/lightningd/watch.c @@ -30,6 +30,7 @@ * WE ASSUME NO MALLEABILITY! This requires segregated witness. */ #include "config.h" +#include #include #include #include @@ -235,12 +236,13 @@ void txwatch_fire(struct chain_topology *topo, const struct bitcoin_txid *txid, unsigned int depth) { - struct txwatch *txw; + struct txwatch_hash_iter it; - txw = txwatch_hash_get(topo->txwatches, txid); - - if (txw) + for (struct txwatch *txw = txwatch_hash_getfirst(topo->txwatches, txid, &it); + txw; + txw = txwatch_hash_getnext(topo->txwatches, txid, &it)) { txw_fire(txw, txid, depth); + } } void txowatch_fire(const struct txowatch *txow, @@ -295,12 +297,22 @@ void watch_topology_changed(struct chain_topology *topo) void txwatch_inform(const struct chain_topology *topo, const struct bitcoin_txid *txid, - const struct bitcoin_tx *tx_may_steal) + struct bitcoin_tx *tx TAKES) { - struct txwatch *txw; - - txw = txwatch_hash_get(topo->txwatches, txid); + struct txwatch_hash_iter it; + + for (struct txwatch *txw = txwatch_hash_getfirst(topo->txwatches, txid, &it); + txw; + txw = txwatch_hash_getnext(topo->txwatches, txid, &it)) { + if (txw->tx) + continue; + /* FIXME: YUCK! These don't have PSBTs attached */ + if (!tx->psbt) + tx->psbt = new_psbt(tx, tx->wtx); + txw->tx = clone_bitcoin_tx(txw, tx); + } - if (txw && !txw->tx) - txw->tx = tal_steal(txw, tx_may_steal); + /* If we don't clone above, handle take() now */ + if (taken(tx)) + tal_free(tx); } diff --git a/lightningd/watch.h b/lightningd/watch.h index 07a7b9ad137d..f5d73460de78 100644 --- a/lightningd/watch.h +++ b/lightningd/watch.h @@ -89,10 +89,9 @@ void txowatch_fire(const struct txowatch *txow, bool watching_txid(const struct chain_topology *topo, const struct bitcoin_txid *txid); -/* FIXME: Implement bitcoin_tx_dup() so we tx arg can be TAKEN */ void txwatch_inform(const struct chain_topology *topo, const struct bitcoin_txid *txid, - const struct bitcoin_tx *tx_may_steal); + struct bitcoin_tx *tx TAKES); void watch_topology_changed(struct chain_topology *topo); #endif /* LIGHTNING_LIGHTNINGD_WATCH_H */