diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index cc67f47da..7f08da950 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -28,18 +28,6 @@ static void next_topology_timer(struct chain_topology *topo) try_extend_tip, topo)); } -/* FIXME: Remove tx from block when peer done. */ -static void add_tx_to_block(struct block *b, - const struct bitcoin_tx *tx, const u32 txnum) -{ - size_t n = tal_count(b->txs); - - tal_resize(&b->txs, n+1); - tal_resize(&b->txnums, n+1); - b->txs[n] = tal_steal(b->txs, tx); - b->txnums[n] = txnum; -} - static bool we_broadcast(const struct chain_topology *topo, const struct bitcoin_txid *txid) { @@ -86,7 +74,6 @@ static void filter_block_txs(struct chain_topology *topo, struct block *b) bitcoin_txid(tx, &txid); if (watching_txid(topo, &txid) || we_broadcast(topo, &txid) || satoshi_owned != 0) { - add_tx_to_block(b, tx, i); wallet_transaction_add(topo->wallet, tx, b->height, i); } } @@ -442,7 +429,6 @@ static struct block *new_block(struct chain_topology *topo, b->hdr = blk->hdr; - b->txs = tal_arr(b, const struct bitcoin_tx *, 0); b->txnums = tal_arr(b, u32, 0); b->full_txs = tal_steal(b, blk->tx); @@ -452,7 +438,8 @@ static struct block *new_block(struct chain_topology *topo, static void remove_tip(struct chain_topology *topo) { struct block *b = topo->tip; - size_t i, n = tal_count(b->txs); + struct bitcoin_txid *txs; + size_t i, n; /* Move tip back one. */ topo->tip = b->prev; @@ -461,9 +448,12 @@ static void remove_tip(struct chain_topology *topo) b->height, type_to_string(tmpctx, struct bitcoin_blkid, &b->blkid)); + txs = wallet_transactions_by_height(b, topo->wallet, b->height); + n = tal_count(txs); + /* Notify that txs are kicked out. */ for (i = 0; i < n; i++) - txwatch_fire(topo, b->txs[i], 0); + txwatch_fire(topo, &txs[i], 0); wallet_block_remove(topo->wallet, b); block_map_del(&topo->block_map, b); diff --git a/lightningd/chaintopology.h b/lightningd/chaintopology.h index f0ac98fc5..d0030ff13 100644 --- a/lightningd/chaintopology.h +++ b/lightningd/chaintopology.h @@ -49,9 +49,6 @@ struct block { /* Key for hash table */ struct bitcoin_blkid blkid; - /* Transactions in this block we care about */ - const struct bitcoin_tx **txs; - /* And their associated index in the block */ u32 *txnums; diff --git a/lightningd/watch.c b/lightningd/watch.c index 32f0bfbf2..cebeefebb 100644 --- a/lightningd/watch.c +++ b/lightningd/watch.c @@ -229,17 +229,15 @@ static bool txw_fire(struct txwatch *txw, } void txwatch_fire(struct chain_topology *topo, - const struct bitcoin_tx *tx, + const struct bitcoin_txid *txid, unsigned int depth) { - struct bitcoin_txid txid; struct txwatch *txw; - bitcoin_txid(tx, &txid); - txw = txwatch_hash_get(&topo->txwatches, &txid); + txw = txwatch_hash_get(&topo->txwatches, txid); if (txw) - txw_fire(txw, &txid, depth); + txw_fire(txw, txid, depth); } void txowatch_fire(const struct txowatch *txow, diff --git a/lightningd/watch.h b/lightningd/watch.h index 3edcc8547..f7dc211b5 100644 --- a/lightningd/watch.h +++ b/lightningd/watch.h @@ -70,7 +70,7 @@ struct txwatch *find_txwatch(struct chain_topology *topo, const struct channel *channel); void txwatch_fire(struct chain_topology *topo, - const struct bitcoin_tx *tx, + const struct bitcoin_txid *txid, unsigned int depth); void txowatch_fire(const struct txowatch *txow, diff --git a/wallet/wallet.c b/wallet/wallet.c index c276fdca3..7d3d8131f 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -2246,3 +2246,25 @@ fail: sqlite3_finalize(stmt); return NULL; } + +struct bitcoin_txid *wallet_transactions_by_height(const tal_t *ctx, + struct wallet *w, + const u32 blockheight) +{ + sqlite3_stmt *stmt; + struct bitcoin_txid *txids = tal_arr(ctx, struct bitcoin_txid, 0); + int count = 0; + stmt = db_prepare( + w->db, "SELECT id FROM transactions WHERE blockheight=?"); + sqlite3_bind_int(stmt, 1, blockheight); + + while (sqlite3_step(stmt) == SQLITE_ROW) { + count++; + tal_resize(&txids, count); + sqlite3_column_sha256(stmt, 0, &txids[count-1].shad.sha); + } + sqlite3_finalize(stmt); + + return txids; +} + diff --git a/wallet/wallet.h b/wallet/wallet.h index 021856316..f60e9cdc5 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -827,4 +827,11 @@ u32 wallet_transaction_height(struct wallet *w, const struct bitcoin_txid *txid) struct txlocator *wallet_transaction_locate(const tal_t *ctx, struct wallet *w, const struct bitcoin_txid *txid); +/** + * Get transaction IDs for transactions that we are tracking. + */ +struct bitcoin_txid *wallet_transactions_by_height(const tal_t *ctx, + struct wallet *w, + const u32 blockheight); + #endif /* LIGHTNING_WALLET_WALLET_H */