diff --git a/daemon/bitcoind.c b/daemon/bitcoind.c index 9cd8d14af..bdb165414 100644 --- a/daemon/bitcoind.c +++ b/daemon/bitcoind.c @@ -172,7 +172,8 @@ static void process_transactions(struct bitcoin_cli *bcli) bool valid; void (*cb)(struct lightningd_state *dstate, const struct sha256_double *txid, - int confirmations, bool is_coinbase) = bcli->cb; + int confirmations, bool is_coinbase, + const struct sha256_double *blkhash) = bcli->cb; if (!bcli->output) fatal("bitcoind: '%s' '%s' failed", @@ -191,8 +192,8 @@ static void process_transactions(struct bitcoin_cli *bcli) end = json_next(tokens); for (t = tokens + 1; t < end; t = json_next(t)) { - struct sha256_double txid; - const jsmntok_t *txidtok, *conftok, *blkindxtok; + struct sha256_double txid, blkhash = { 0 }; + const jsmntok_t *txidtok, *conftok, *blkindxtok, *blktok; unsigned int conf; bool is_coinbase; @@ -225,15 +226,27 @@ static void process_transactions(struct bitcoin_cli *bcli) (int)(blkindxtok->end - blkindxtok->start), bcli->output + blkindxtok->start); is_coinbase = (blkidx == 0); + + blktok = json_get_member(bcli->output, t, "blockhash"); + if (!blktok) + fatal("listtransactions: no blockhash field!"); + + if (!hex_decode(bcli->output + blktok->start, + blktok->end - blktok->start, + &blkhash, sizeof(blkhash))) { + fatal("listtransactions: bad blockhash '%.*s'", + (int)(blktok->end - blktok->start), + bcli->output + blktok->start); + } } - /* FIXME: log txid */ + /* FIXME: log txid, blkid */ log_debug(bcli->dstate->base_log, "txid %02x%02x%02x%02x..., conf %u, coinbase %u", txid.sha.u.u8[0], txid.sha.u.u8[1], txid.sha.u.u8[2], txid.sha.u.u8[3], conf, is_coinbase); - cb(bcli->dstate, &txid, conf, is_coinbase); + cb(bcli->dstate, &txid, conf, is_coinbase, &blkhash); } } @@ -241,7 +254,8 @@ void bitcoind_poll_transactions(struct lightningd_state *dstate, void (*cb)(struct lightningd_state *dstate, const struct sha256_double *txid, int confirmations, - bool is_coinbase)) + bool is_coinbase, + const struct sha256_double *blkhash)) { /* FIXME: Iterate and detect duplicates. */ start_bitcoin_cli(dstate, process_transactions, cb, NULL, diff --git a/daemon/bitcoind.h b/daemon/bitcoind.h index 47ddd6abe..4de4536c5 100644 --- a/daemon/bitcoind.h +++ b/daemon/bitcoind.h @@ -18,7 +18,8 @@ void bitcoind_poll_transactions(struct lightningd_state *dstate, void (*cb)(struct lightningd_state *dstate, const struct sha256_double *txid, int confirmations, - bool is_coinbase)); + bool is_coinbase, + const struct sha256_double *blkhash)); void bitcoind_txid_lookup_(struct lightningd_state *dstate, const struct sha256_double *txid, diff --git a/daemon/peer.c b/daemon/peer.c index c4e8e413e..d78539451 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -558,6 +558,7 @@ struct anchor_watch { }; static void anchor_depthchange(struct peer *peer, int depth, + const struct sha256_double *blkhash, struct anchor_watch *w) { /* Still waiting for it to reach depth? */ diff --git a/daemon/watch.c b/daemon/watch.c index ed0e45512..43e2fb504 100644 --- a/daemon/watch.c +++ b/daemon/watch.c @@ -103,7 +103,9 @@ static void destroy_txwatch(struct txwatch *w) static struct txwatch *insert_txwatch(const tal_t *ctx, struct peer *peer, const struct sha256_double *txid, - void (*cb)(struct peer *, int, void *), + void (*cb)(struct peer *, int, + const struct sha256_double *, + void *), void *cbdata) { struct txwatch *w; @@ -150,7 +152,9 @@ void add_anchor_watch_(const tal_t *ctx, struct peer *peer, const struct sha256_double *txid, unsigned int out, - void (*anchor_cb)(struct peer *peer, int depth, void *), + void (*anchor_cb)(struct peer *peer, int depth, + const struct sha256_double *blkhash, + void *), void (*spend_cb)(struct peer *peer, const struct bitcoin_tx *, void *), void *cbdata) @@ -177,7 +181,9 @@ void add_anchor_watch_(const tal_t *ctx, void add_commit_tx_watch_(const tal_t *ctx, struct peer *peer, const struct sha256_double *txid, - void (*cb)(struct peer *peer, int depth, void *), + void (*cb)(struct peer *peer, int depth, + const struct sha256_double *blkhash, + void *), void *cbdata) { insert_txwatch(ctx, peer, txid, cb, cbdata); @@ -186,7 +192,8 @@ void add_commit_tx_watch_(const tal_t *ctx, * watch anything else. */ } -static void cb_no_arg(struct peer *peer, int depth, void *vcb) +static void cb_no_arg(struct peer *peer, int depth, + const struct sha256_double *blkhash, void *vcb) { void (*cb)(struct peer *peer, int depth) = vcb; cb(peer, depth); @@ -226,7 +233,8 @@ static void tx_watched_inputs(struct lightningd_state *dstate, static void watched_transaction(struct lightningd_state *dstate, const struct sha256_double *txid, int confirmations, - bool is_coinbase) + bool is_coinbase, + const struct sha256_double *blkhash) { struct txwatch *txw; @@ -237,7 +245,8 @@ static void watched_transaction(struct lightningd_state *dstate, if (confirmations != txw->depth) { txw->depth = confirmations; if (txw->cb) - txw->cb(txw->peer, txw->depth, txw->cbdata); + txw->cb(txw->peer, txw->depth, blkhash, + txw->cbdata); } return; } diff --git a/daemon/watch.h b/daemon/watch.h index 516826989..37ff98b6e 100644 --- a/daemon/watch.h +++ b/daemon/watch.h @@ -49,8 +49,10 @@ struct txwatch { struct sha256_double txid; int depth; - /* A new depth (-1 if conflicted) */ - void (*cb)(struct peer *peer, int depth, void *cbdata); + /* A new depth (-1 if conflicted), blkhash valid if > 0 */ + void (*cb)(struct peer *peer, int depth, + const struct sha256_double *blkhash, + void *cbdata); void *cbdata; }; @@ -65,7 +67,9 @@ void add_anchor_watch_(const tal_t *ctx, struct peer *peer, const struct sha256_double *txid, unsigned int out, - void (*anchor_cb)(struct peer *peer, int depth, void *), + void (*anchor_cb)(struct peer *peer, int depth, + const struct sha256_double *blkhash, + void *), void (*spend_cb)(struct peer *peer, const struct bitcoin_tx *, void *), void *cbdata); @@ -75,7 +79,8 @@ void add_anchor_watch_(const tal_t *ctx, typesafe_cb_preargs(void, void *, \ (anchor_cb), (cbdata), \ struct peer *, \ - int depth), \ + int depth, \ + const struct sha256_double *), \ typesafe_cb_preargs(void, void *, \ (spend_cb), (cbdata), \ struct peer *, \ @@ -85,7 +90,9 @@ void add_anchor_watch_(const tal_t *ctx, void add_commit_tx_watch_(const tal_t *ctx, struct peer *peer, const struct sha256_double *txid, - void (*cb)(struct peer *peer, int depth, void *), + void (*cb)(struct peer *peer, int depth, + const struct sha256_double *blkhash, + void *), void *cbdata); #define add_commit_tx_watch(ctx, peer, txid, cb, cbdata) \ @@ -93,7 +100,8 @@ void add_commit_tx_watch_(const tal_t *ctx, typesafe_cb_preargs(void, void *, \ (cb), (cbdata), \ struct peer *, \ - int depth), \ + int, \ + const struct sha256_double *), \ (cbdata)) void add_close_tx_watch(const tal_t *ctx,