Browse Source

topo: Remove in-memory txs from the block struct

The only use for these was to compute their txids so we could notify depth
in case of reorgs.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
ppa-0.6.1
Christian Decker 7 years ago
parent
commit
f27cd3e43f
  1. 22
      lightningd/chaintopology.c
  2. 3
      lightningd/chaintopology.h
  3. 8
      lightningd/watch.c
  4. 2
      lightningd/watch.h
  5. 22
      wallet/wallet.c
  6. 7
      wallet/wallet.h

22
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);

3
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;

8
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,

2
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,

22
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;
}

7
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 */

Loading…
Cancel
Save