diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index 353a2a291..87689f8c4 100644 --- a/lightningd/chaintopology.c +++ b/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); diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index 520b506d3..09bc5c63a 100644 --- a/lightningd/lightningd.c +++ b/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); diff --git a/lightningd/lightningd.h b/lightningd/lightningd.h index 6135a1d92..ba53ed767 100644 --- a/lightningd/lightningd.h +++ b/lightningd/lightningd.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -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; diff --git a/lightningd/test/run-find_my_path.c b/lightningd/test/run-find_my_path.c index 559478eb9..7a6a297be 100644 --- a/lightningd/test/run-find_my_path.c +++ b/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(); } diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 124df8c54..5e2027019 100644 --- a/wallet/walletrpc.c +++ b/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));