mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 05:12:45 +01:00
chaintopology: Use the DB to locate transactions and rebroadcast txs
Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
parent
86b6402e5c
commit
23984ecde4
@ -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)
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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 */
|
||||
|
Loading…
Reference in New Issue
Block a user