diff --git a/lightningd/onchain_control.c b/lightningd/onchain_control.c index 175c1dcdc..16ca5527b 100644 --- a/lightningd/onchain_control.c +++ b/lightningd/onchain_control.c @@ -9,7 +9,6 @@ #include #include #include -#include #include /* We dump all the known preimages when onchaind starts up. */ @@ -53,12 +52,25 @@ static void handle_onchain_init_reply(struct channel *channel, const u8 *msg UNU channel_set_state(channel, FUNDING_SPEND_SEEN, ONCHAIN); } +/** + * Notify onchaind about the depth change of the watched tx. + */ +static void onchain_tx_depth(struct channel *channel, + const struct bitcoin_txid *txid, + unsigned int depth) +{ + u8 *msg; + msg = towire_onchain_depth(channel, txid, depth); + subd_send_msg(channel->owner, take(msg)); +} + +/** + * Entrypoint for the txwatch callback, calls onchain_tx_depth. + */ static enum watch_result onchain_tx_watched(struct channel *channel, const struct bitcoin_txid *txid, unsigned int depth) { - u8 *msg; - if (depth == 0) { log_unusual(channel->log, "Chain reorganization!"); channel_set_owner(channel, NULL); @@ -71,26 +83,37 @@ static enum watch_result onchain_tx_watched(struct channel *channel, return KEEP_WATCHING; } - msg = towire_onchain_depth(channel, txid, depth); - subd_send_msg(channel->owner, take(msg)); + onchain_tx_depth(channel, txid, depth); return KEEP_WATCHING; } static void watch_tx_and_outputs(struct channel *channel, const struct bitcoin_tx *tx); -static enum watch_result onchain_txo_watched(struct channel *channel, - const struct bitcoin_tx *tx, - size_t input_num, - const struct block *block) +/** + * Notify onchaind that an output was spent and register new watches. + */ +static void onchain_txo_spent(struct channel *channel, const struct bitcoin_tx *tx, size_t input_num, u32 blockheight) { u8 *msg; watch_tx_and_outputs(channel, tx); - msg = towire_onchain_spent(channel, tx, input_num, block->height); + msg = towire_onchain_spent(channel, tx, input_num, blockheight); subd_send_msg(channel->owner, take(msg)); +} + +/** + * Entrypoint for the txowatch callback, stores tx and calls onchain_txo_spent. + */ +static enum watch_result onchain_txo_watched(struct channel *channel, + const struct bitcoin_tx *tx, + size_t input_num, + const struct block *block) +{ + onchain_txo_spent(channel, tx, input_num, block->height); + /* We don't need to keep watching: If this output is double-spent * (reorg), we'll get a zero depth cb to onchain_tx_watched, and * restart onchaind. */ @@ -335,10 +358,9 @@ static void onchain_error(struct channel *channel, /* With a reorg, this can get called multiple times; each time we'll kill * onchaind (like any other owner), and restart */ -enum watch_result funding_spent(struct channel *channel, - const struct bitcoin_tx *tx, - size_t input_num UNUSED, - const struct block *block) +enum watch_result onchaind_funding_spent(struct channel *channel, + const struct bitcoin_tx *tx, + u32 blockheight) { u8 *msg; struct bitcoin_txid our_last_txid; @@ -408,7 +430,7 @@ enum watch_result funding_spent(struct channel *channel, &channel->channel_info.theirbase.htlc, &channel->channel_info.theirbase.delayed_payment, tx, - block->height, + blockheight, /* FIXME: config for 'reasonable depth' */ 3, channel->last_htlc_sigs, diff --git a/lightningd/onchain_control.h b/lightningd/onchain_control.h index 8b1b20bca..b7366ec5a 100644 --- a/lightningd/onchain_control.h +++ b/lightningd/onchain_control.h @@ -7,9 +7,8 @@ struct channel; struct bitcoin_tx; struct block; -enum watch_result funding_spent(struct channel *channel, - const struct bitcoin_tx *tx, - size_t input_num, - const struct block *block); +enum watch_result onchaind_funding_spent(struct channel *channel, + const struct bitcoin_tx *tx, + u32 blockheight); #endif /* LIGHTNING_LIGHTNINGD_ONCHAIN_CONTROL_H */ diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index fd8ea36ab..ae74e54e9 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -692,6 +692,20 @@ static enum watch_result funding_lockin_cb(struct channel *channel, return DELETE_WATCH; } +static enum watch_result funding_spent(struct channel *channel, + const struct bitcoin_tx *tx, + size_t inputnum UNUSED, + const struct block *block) +{ + struct bitcoin_txid txid; + bitcoin_txid(tx, &txid); + + wallet_channeltxs_add(channel->peer->ld->wallet, channel, + WIRE_ONCHAIN_INIT, &txid, 0, block->height); + + return onchaind_funding_spent(channel, tx, block->height); +} + void channel_watch_funding(struct lightningd *ld, struct channel *channel) { /* FIXME: Remove arg from cb? */ diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 469f38f17..6f4514832 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -90,12 +90,6 @@ bool fromwire_gossip_getpeers_reply(const tal_t *ctx UNNEEDED, const void *p UNN /* Generated stub for fromwire_gossip_peer_connected */ bool fromwire_gossip_peer_connected(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct pubkey *id UNNEEDED, struct wireaddr *addr UNNEEDED, struct crypto_state *crypto_state UNNEEDED, u64 *gossip_index UNNEEDED, u8 **gfeatures UNNEEDED, u8 **lfeatures UNNEEDED) { fprintf(stderr, "fromwire_gossip_peer_connected called!\n"); abort(); } -/* Generated stub for funding_spent */ -enum watch_result funding_spent(struct channel *channel UNNEEDED, - const struct bitcoin_tx *tx UNNEEDED, - size_t input_num UNNEEDED, - const struct block *block UNNEEDED) -{ fprintf(stderr, "funding_spent called!\n"); abort(); } /* Generated stub for get_feerate */ u32 get_feerate(const struct chain_topology *topo UNNEEDED, enum feerate feerate UNNEEDED) { fprintf(stderr, "get_feerate called!\n"); abort(); } @@ -296,6 +290,11 @@ struct json_result *new_json_result(const tal_t *ctx UNNEEDED) /* Generated stub for null_response */ struct json_result *null_response(const tal_t *ctx UNNEEDED) { fprintf(stderr, "null_response called!\n"); abort(); } +/* Generated stub for onchaind_funding_spent */ +enum watch_result onchaind_funding_spent(struct channel *channel UNNEEDED, + const struct bitcoin_tx *tx UNNEEDED, + u32 blockheight UNNEEDED) +{ fprintf(stderr, "onchaind_funding_spent called!\n"); abort(); } /* Generated stub for outpointfilter_add */ void outpointfilter_add(struct outpointfilter *of UNNEEDED, const struct bitcoin_txid *txid UNNEEDED, const u32 outnum UNNEEDED)