From 23984ecde4e08699072bb6ac597c841191bd85d6 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 9 Apr 2018 18:48:39 +0200 Subject: [PATCH] chaintopology: Use the DB to locate transactions and rebroadcast txs Signed-off-by: Christian Decker --- lightningd/chaintopology.c | 57 +------------------------------------- lightningd/peer_control.c | 2 +- wallet/wallet.c | 30 +++++++++++++++++++- wallet/wallet.h | 7 +++++ 4 files changed, 38 insertions(+), 58 deletions(-) diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index 6ff3d941b..cc67f47da 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -93,39 +93,6 @@ static void filter_block_txs(struct chain_topology *topo, struct block *b) b->full_txs = tal_free(b->full_txs); } -static const struct bitcoin_tx *tx_in_block(const struct block *b, - const struct bitcoin_txid *txid) -{ - size_t i, n = tal_count(b->txs); - - for (i = 0; i < n; i++) { - struct bitcoin_txid this_txid; - bitcoin_txid(b->txs[i], &this_txid); - if (structeq(&this_txid, txid)) - return b->txs[i]; - } - return NULL; -} - -/* FIXME: Use hash table. */ -static struct block *block_for_tx(const struct chain_topology *topo, - const struct bitcoin_txid *txid, - const struct bitcoin_tx **tx) -{ - struct block *b; - const struct bitcoin_tx *dummy_tx; - - if (!tx) - tx = &dummy_tx; - - for (b = topo->tip; b; b = b->prev) { - *tx = tx_in_block(b, txid); - if (*tx) - return b; - } - return NULL; -} - size_t get_tx_depth(const struct chain_topology *topo, const struct bitcoin_txid *txid) { @@ -189,7 +156,7 @@ static void rebroadcast_txs(struct chain_topology *topo, struct command *cmd) /* Put any txs we want to broadcast in ->txs. */ txs->txs = tal_arr(txs, const char *, 0); list_for_each(&topo->outgoing_txs, otx, list) { - if (block_for_tx(topo, &otx->txid, NULL)) + if (wallet_transaction_height(topo->wallet, &otx->txid)) continue; tal_resize(&txs->txs, num_txs+1); @@ -625,28 +592,6 @@ u32 get_feerate(const struct chain_topology *topo, enum feerate feerate) return topo->feerate[feerate]; } -struct txlocator *locate_tx(const void *ctx, const struct chain_topology *topo, - const struct bitcoin_txid *txid) -{ - struct block *block = block_for_tx(topo, txid, NULL); - if (block == NULL) { - return NULL; - } - - struct txlocator *loc = talz(ctx, struct txlocator); - loc->blkheight = block->height; - size_t i, n = tal_count(block->txs); - for (i = 0; i < n; i++) { - struct bitcoin_txid this_txid; - bitcoin_txid(block->txs[i], &this_txid); - if (structeq(&this_txid, txid)){ - loc->index = block->txnums[i]; - return loc; - } - } - return tal_free(loc); -} - #if DEVELOPER static void json_dev_blockheight(struct command *cmd, const char *buffer UNUSED, const jsmntok_t *params UNUSED) diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 819a8bc8b..820b4d3be 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -516,7 +516,7 @@ static enum watch_result funding_lockin_cb(struct channel *channel, if (depth < channel->minimum_depth) return KEEP_WATCHING; - loc = locate_tx(channel, ld->topology, txid); + loc = wallet_transaction_locate(channel, ld->wallet, txid); /* If we restart, we could already have peer->scid from database */ if (!channel->scid) { diff --git a/wallet/wallet.c b/wallet/wallet.c index a6b573e4e..c276fdca3 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -2206,7 +2206,7 @@ u32 wallet_transaction_height(struct wallet *w, const struct bitcoin_txid *txid) { u32 blockheight; sqlite3_stmt *stmt = db_prepare( - w->db, "SELECT blockheight, txindex, rawtx FROM transactions WHERE id=?"); + w->db, "SELECT blockheight FROM transactions WHERE id=?"); sqlite3_bind_sha256(stmt, 1, &txid->shad.sha); if (sqlite3_step(stmt) != SQLITE_ROW) { @@ -2218,3 +2218,31 @@ u32 wallet_transaction_height(struct wallet *w, const struct bitcoin_txid *txid) sqlite3_finalize(stmt); return blockheight; } + +struct txlocator *wallet_transaction_locate(const tal_t *ctx, struct wallet *w, + const struct bitcoin_txid *txid) +{ + struct txlocator *loc; + sqlite3_stmt *stmt; + + stmt = db_prepare( + w->db, "SELECT blockheight, txindex FROM transactions WHERE id=?"); + sqlite3_bind_sha256(stmt, 1, &txid->shad.sha); + + if (sqlite3_step(stmt) != SQLITE_ROW) { + goto fail; + + } + if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) + goto fail; + + loc = tal(ctx, struct txlocator); + loc->blkheight = sqlite3_column_int(stmt, 0); + loc->index = sqlite3_column_int(stmt, 1); + sqlite3_finalize(stmt); + return loc; + +fail: + sqlite3_finalize(stmt); + return NULL; +} diff --git a/wallet/wallet.h b/wallet/wallet.h index 187844163..021856316 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -820,4 +820,11 @@ void wallet_transaction_add(struct wallet *w, const struct bitcoin_tx *tx, */ u32 wallet_transaction_height(struct wallet *w, const struct bitcoin_txid *txid); +/** + * Locate a transaction in the blockchain, returns NULL if the transaction is + * not tracked or is not yet confirmed. + */ +struct txlocator *wallet_transaction_locate(const tal_t *ctx, struct wallet *w, + const struct bitcoin_txid *txid); + #endif /* LIGHTNING_WALLET_WALLET_H */