Browse Source

topology: Add transaction filtering to connect_block

The filter is being populated while initializing the daemon and by
adding new keys as they are being generated. The filter is then used
in connect_block to identify transactions of interest.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
ppa-0.6.1
Christian Decker 7 years ago
parent
commit
c29923a623
  1. 6
      lightningd/chaintopology.c
  2. 34
      lightningd/lightningd.c
  3. 4
      lightningd/lightningd.h
  4. 9
      lightningd/test/run-find_my_path.c
  5. 2
      wallet/walletrpc.c

6
lightningd/chaintopology.c

@ -88,8 +88,10 @@ static void connect_block(struct chain_topology *topo,
}
satoshi_owned = 0;
wallet_extract_owned_outputs(topo->bitcoind->ld->wallet, tx,
&satoshi_owned);
if (txfilter_match(topo->bitcoind->ld->owned_txfilter, tx)) {
wallet_extract_owned_outputs(topo->bitcoind->ld->wallet,
tx, &satoshi_owned);
}
/* We did spends first, in case that tells us to watch tx. */
bitcoin_txid(tx, &txid);

34
lightningd/lightningd.c

@ -213,6 +213,20 @@ struct chainparams *get_chainparams(const struct lightningd *ld)
ld->topology->bitcoind->chainparams);
}
static void init_txfilter(struct wallet *w, struct txfilter *filter)
{
struct ext_key ext;
u64 bip32_max_index;
bip32_max_index = db_get_intvar(w->db, "bip32_max_index", 0);
for (u64 i = 0; i <= bip32_max_index; i++) {
if (bip32_key_from_parent(w->bip32_base, i, BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) {
abort();
}
txfilter_add_derkey(filter, ext.pub_key);
}
}
int main(int argc, char *argv[])
{
struct log_book *log_book;
@ -253,6 +267,7 @@ int main(int argc, char *argv[])
/* Initialize wallet, now that we are in the correct directory */
ld->wallet = wallet_new(ld, ld->log);
ld->owned_txfilter = txfilter_new(ld);
/* Set up HSM. */
hsm_init(ld, newdir);
@ -260,17 +275,13 @@ int main(int argc, char *argv[])
/* Now we know our ID, we can set our color/alias if not already. */
setup_color_and_alias(ld);
/* Initialize block topology (does its own transaction) */
setup_topology(ld->topology,
&ld->timers,
ld->config.poll_time,
/* FIXME: Load from peers. */
0);
/* Everything is within a transaction. */
db_begin_transaction(ld->wallet->db);
/* Load invoices from the database */
/* Initialize the transaction filter with our pubkeys. */
init_txfilter(ld->wallet, ld->owned_txfilter);
/* Load invoices from the database */
if (!wallet_invoices_load(ld->wallet, ld->invoices)) {
fatal("Could not load invoices from the database");
}
@ -298,6 +309,13 @@ int main(int argc, char *argv[])
db_commit_transaction(ld->wallet->db);
/* Initialize block topology (does its own transaction) */
setup_topology(ld->topology,
&ld->timers,
ld->config.poll_time,
/* FIXME: Load from peers. */
0);
/* Create RPC socket (if any) */
setup_jsonrpc(ld, ld->rpc_filename);

4
lightningd/lightningd.h

@ -7,6 +7,7 @@
#include <ccan/time/time.h>
#include <ccan/timer/timer.h>
#include <lightningd/htlc_end.h>
#include <lightningd/txfilter.h>
#include <stdio.h>
#include <wallet/wallet.h>
@ -131,6 +132,9 @@ struct lightningd {
/* Any outstanding "pay" commands. */
struct list_head pay_commands;
/* Transaction filter matching what we're interested in */
struct txfilter *owned_txfilter;
#if DEVELOPER
/* If we want to debug a subdaemon. */
const char *dev_debug_subdaemon;

9
lightningd/test/run-find_my_path.c

@ -13,6 +13,9 @@ void db_begin_transaction_(struct db *db UNNEEDED, const char *location UNNEEDED
/* Generated stub for db_commit_transaction */
void db_commit_transaction(struct db *db UNNEEDED)
{ fprintf(stderr, "db_commit_transaction called!\n"); abort(); }
/* Generated stub for db_get_intvar */
s64 db_get_intvar(struct db *db UNNEEDED, char *varname UNNEEDED, s64 defval UNNEEDED)
{ fprintf(stderr, "db_get_intvar called!\n"); abort(); }
/* Generated stub for debug_poll */
int debug_poll(struct pollfd *fds UNNEEDED, nfds_t nfds UNNEEDED, int timeout UNNEEDED)
{ fprintf(stderr, "debug_poll called!\n"); abort(); }
@ -75,6 +78,12 @@ void subd_shutdown(struct subd *subd UNNEEDED, unsigned int seconds UNNEEDED)
/* Generated stub for timer_expired */
void timer_expired(tal_t *ctx UNNEEDED, struct timer *timer UNNEEDED)
{ fprintf(stderr, "timer_expired called!\n"); abort(); }
/* Generated stub for txfilter_add_derkey */
void txfilter_add_derkey(struct txfilter *filter UNNEEDED, u8 derkey[33])
{ fprintf(stderr, "txfilter_add_derkey called!\n"); abort(); }
/* Generated stub for txfilter_new */
struct txfilter *txfilter_new(const tal_t *ctx UNNEEDED)
{ fprintf(stderr, "txfilter_new called!\n"); abort(); }
/* Generated stub for version */
const char *version(void)
{ fprintf(stderr, "version called!\n"); abort(); }

2
wallet/walletrpc.c

@ -222,6 +222,8 @@ static void json_newaddr(struct command *cmd,
return;
}
txfilter_add_derkey(cmd->ld->owned_txfilter, ext.pub_key);
redeemscript = bitcoin_redeem_p2sh_p2wpkh(cmd, &pubkey);
sha256(&h, redeemscript, tal_count(redeemscript));
ripemd160(&p2sh, h.u.u8, sizeof(h));

Loading…
Cancel
Save