diff --git a/daemon/bitcoind.c b/daemon/bitcoind.c index 228a09c76..d55922355 100644 --- a/daemon/bitcoind.c +++ b/daemon/bitcoind.c @@ -167,7 +167,7 @@ static void process_transactions(struct bitcoin_cli *bcli) bool valid; void (*cb)(struct lightningd_state *dstate, const struct sha256_double *txid, - int confirmations) = bcli->cb; + int confirmations, bool is_coinbase) = bcli->cb; tokens = json_parse_input(bcli->output, bcli->output_bytes, &valid); if (!tokens) @@ -183,12 +183,13 @@ 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; - long int conf; - char *end; + const jsmntok_t *txidtok, *conftok, *blkindxtok; + unsigned int conf; + bool is_coinbase; txidtok = json_get_member(bcli->output, t, "txid"); conftok = json_get_member(bcli->output, t, "confirmations"); + blkindxtok = json_get_member(bcli->output, t, "blockindex"); if (!txidtok || !conftok) fatal("listtransactions: no %s field!", txidtok ? "confirmations" : "txid"); @@ -199,27 +200,39 @@ static void process_transactions(struct bitcoin_cli *bcli) (int)(txidtok->end - txidtok->start), bcli->output + txidtok->start); } - conf = strtol(bcli->output + conftok->start, &end, 10); - if (end != bcli->output + conftok->end) + if (!json_tok_number(bcli->output, conftok, &conf)) fatal("listtransactions: bad confirmations '%.*s'", (int)(conftok->end - conftok->start), bcli->output + conftok->start); + /* This can happen with zero conf. */ + blkindxtok = json_get_member(bcli->output, t, "blockindex"); + if (!blkindxtok) + is_coinbase = false; + else { + unsigned int blkidx; + if (!json_tok_number(bcli->output, blkindxtok, &blkidx)) + fatal("listtransactions: bad blockindex '%.*s'", + (int)(blkindxtok->end - blkindxtok->start), + bcli->output + blkindxtok->start); + is_coinbase = (blkidx == 0); + } /* FIXME: log txid */ log_debug(bcli->dstate->base_log, - "txid %02x%02x%02x%02x..., conf %li", + "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); + conf, is_coinbase); - cb(bcli->dstate, &txid, conf); + cb(bcli->dstate, &txid, conf, is_coinbase); } } void bitcoind_poll_transactions(struct lightningd_state *dstate, void (*cb)(struct lightningd_state *dstate, const struct sha256_double *txid, - int confirmations)) + int confirmations, + bool is_coinbase)) { /* FIXME: Iterate and detect duplicates. */ start_bitcoin_cli(dstate, process_transactions, cb, NULL, diff --git a/daemon/bitcoind.h b/daemon/bitcoind.h index 2b2a9e5bb..47ddd6abe 100644 --- a/daemon/bitcoind.h +++ b/daemon/bitcoind.h @@ -3,6 +3,7 @@ #include "config.h" #include #include +#include struct sha256_double; struct lightningd_state; @@ -16,7 +17,8 @@ void bitcoind_watch_addr(struct lightningd_state *dstate, void bitcoind_poll_transactions(struct lightningd_state *dstate, void (*cb)(struct lightningd_state *dstate, const struct sha256_double *txid, - int confirmations)); + int confirmations, + bool is_coinbase)); void bitcoind_txid_lookup_(struct lightningd_state *dstate, const struct sha256_double *txid, diff --git a/daemon/watch.c b/daemon/watch.c index c4496effe..69c42fa0a 100644 --- a/daemon/watch.c +++ b/daemon/watch.c @@ -187,7 +187,9 @@ static void tx_watched_inputs(struct lightningd_state *dstate, static void watched_transaction(struct lightningd_state *dstate, const struct sha256_double *txid, - int confirmations) + int confirmations, + bool is_coinbase) + { struct txwatch *txw; @@ -206,7 +208,8 @@ static void watched_transaction(struct lightningd_state *dstate, insert_txwatch(dstate, dstate, NULL, txid, NULL, NULL); /* Maybe it spent an output we're watching? */ - bitcoind_txid_lookup(dstate, txid, tx_watched_inputs, NULL); + if (!is_coinbase) + bitcoind_txid_lookup(dstate, txid, tx_watched_inputs, NULL); } static struct timeout watch_timeout;