chaintopology: Use the DB to locate transactions and rebroadcast txs

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker
2018-04-09 18:48:39 +02:00
parent 86b6402e5c
commit 23984ecde4
4 changed files with 38 additions and 58 deletions

View File

@@ -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)

View File

@@ -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) {