Browse Source

txwatch: hand ld to callback, don't assume channel is non-NULL.

We're about to use the txwatch facility for UTXOs, where there's no channel,
so allow that the be NULL, and hand the struct lightningd which callers
want anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 7 years ago
committed by Christian Decker
parent
commit
05f12edf60
  1. 7
      lightningd/onchain_control.c
  2. 4
      lightningd/peer_control.c
  3. 29
      lightningd/watch.c
  4. 13
      lightningd/watch.h
  5. 5
      wallet/test/run-wallet.c

7
lightningd/onchain_control.c

@ -75,11 +75,12 @@ static void onchain_tx_depth(struct channel *channel,
/**
* Entrypoint for the txwatch callback, calls onchain_tx_depth.
*/
static enum watch_result onchain_tx_watched(struct channel *channel,
static enum watch_result onchain_tx_watched(struct lightningd *ld,
struct channel *channel,
const struct bitcoin_txid *txid,
unsigned int depth)
{
u32 blockheight = channel->peer->ld->topology->tip->height;
u32 blockheight = get_block_height(ld->topology);
if (depth == 0) {
log_unusual(channel->log, "Chain reorganization!");
channel_set_owner(channel, NULL, false);
@ -93,7 +94,7 @@ static enum watch_result onchain_tx_watched(struct channel *channel,
}
/* Store the channeltx so we can replay later */
wallet_channeltxs_add(channel->peer->ld->wallet, channel,
wallet_channeltxs_add(ld->wallet, channel,
WIRE_ONCHAIN_DEPTH, txid, 0, blockheight);
onchain_tx_depth(channel, txid, depth);

4
lightningd/peer_control.c

@ -508,12 +508,12 @@ send_error:
peer_start_openingd(peer, &cs, peer_fd, gossip_fd, error);
}
static enum watch_result funding_lockin_cb(struct channel *channel,
static enum watch_result funding_lockin_cb(struct lightningd *ld,
struct channel *channel,
const struct bitcoin_txid *txid,
unsigned int depth)
{
const char *txidstr;
struct lightningd *ld = channel->peer->ld;
txidstr = type_to_string(channel, struct bitcoin_txid, txid);
log_debug(channel->log, "Funding tx %s depth %u of %u",

29
lightningd/watch.c

@ -58,7 +58,7 @@ struct txowatch {
struct txwatch {
struct chain_topology *topo;
/* Channel who owns us. */
/* Channel who owns us (or NULL, for wallet usage). */
struct channel *channel;
/* Transaction to watch. */
@ -66,7 +66,8 @@ struct txwatch {
unsigned int depth;
/* A new depth (0 if kicked out, otherwise 1 = tip, etc.) */
enum watch_result (*cb)(struct channel *channel,
enum watch_result (*cb)(struct lightningd *ld,
struct channel *channel,
const struct bitcoin_txid *txid,
unsigned int depth);
};
@ -121,9 +122,10 @@ struct txwatch *watch_txid(const tal_t *ctx,
struct chain_topology *topo,
struct channel *channel,
const struct bitcoin_txid *txid,
enum watch_result (*cb)(struct channel *channel,
const struct bitcoin_txid *,
unsigned int depth))
enum watch_result (*cb)(struct lightningd *ld,
struct channel *channel,
const struct bitcoin_txid *,
unsigned int depth))
{
struct txwatch *w;
@ -168,9 +170,10 @@ struct txwatch *watch_tx(const tal_t *ctx,
struct chain_topology *topo,
struct channel *channel,
const struct bitcoin_tx *tx,
enum watch_result (*cb)(struct channel *channel,
const struct bitcoin_txid *,
unsigned int depth))
enum watch_result (*cb)(struct lightningd *ld,
struct channel *channel,
const struct bitcoin_txid *,
unsigned int depth))
{
struct bitcoin_txid txid;
@ -208,15 +211,21 @@ static bool txw_fire(struct txwatch *txw,
unsigned int depth)
{
enum watch_result r;
struct log *log;
if (depth == txw->depth)
return false;
log_debug(txw->channel->log,
if (txw->channel)
log = txw->channel->log;
else
log = txw->topo->log;
log_debug(log,
"Got depth change %u->%u for %s",
txw->depth, depth,
type_to_string(tmpctx, struct bitcoin_txid, &txw->txid));
txw->depth = depth;
r = txw->cb(txw->channel, txid, txw->depth);
r = txw->cb(txw->topo->bitcoind->ld, txw->channel, txid, txw->depth);
switch (r) {
case DELETE_WATCH:
tal_free(txw);

13
lightningd/watch.h

@ -12,6 +12,7 @@ struct bitcoin_tx;
struct block;
struct channel;
struct chain_topology;
struct lightningd;
struct txowatch;
struct txwatch;
@ -43,17 +44,19 @@ struct txwatch *watch_txid(const tal_t *ctx,
struct chain_topology *topo,
struct channel *channel,
const struct bitcoin_txid *txid,
enum watch_result (*cb)(struct channel *channel,
const struct bitcoin_txid *,
enum watch_result (*cb)(struct lightningd *ld,
struct channel *channel,
const struct bitcoin_txid *,
unsigned int depth));
struct txwatch *watch_tx(const tal_t *ctx,
struct chain_topology *topo,
struct channel *channel,
const struct bitcoin_tx *tx,
enum watch_result (*cb)(struct channel *channel,
const struct bitcoin_txid *,
unsigned int depth));
enum watch_result (*cb)(struct lightningd *ld,
struct channel *channel,
const struct bitcoin_txid *,
unsigned int depth));
struct txowatch *watch_txo(const tal_t *ctx,
struct chain_topology *topo,

5
wallet/test/run-wallet.c

@ -362,8 +362,9 @@ struct txwatch *watch_txid(const tal_t *ctx UNNEEDED,
struct chain_topology *topo UNNEEDED,
struct channel *channel UNNEEDED,
const struct bitcoin_txid *txid UNNEEDED,
enum watch_result (*cb)(struct channel *channel UNNEEDED,
const struct bitcoin_txid * UNNEEDED,
enum watch_result (*cb)(struct lightningd *ld UNNEEDED,
struct channel *channel UNNEEDED,
const struct bitcoin_txid * UNNEEDED,
unsigned int depth))
{ fprintf(stderr, "watch_txid called!\n"); abort(); }
/* Generated stub for watch_txo */

Loading…
Cancel
Save