Browse Source

bitcoind: tell bitcoind_poll_transactions's callback if tx is a coinbase.

In this case, the inputs aren't valid transactions, so don't try to
find them.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
0dbbd81430
  1. 33
      daemon/bitcoind.c
  2. 4
      daemon/bitcoind.h
  3. 7
      daemon/watch.c

33
daemon/bitcoind.c

@ -167,7 +167,7 @@ static void process_transactions(struct bitcoin_cli *bcli)
bool valid; bool valid;
void (*cb)(struct lightningd_state *dstate, void (*cb)(struct lightningd_state *dstate,
const struct sha256_double *txid, 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); tokens = json_parse_input(bcli->output, bcli->output_bytes, &valid);
if (!tokens) if (!tokens)
@ -183,12 +183,13 @@ static void process_transactions(struct bitcoin_cli *bcli)
end = json_next(tokens); end = json_next(tokens);
for (t = tokens + 1; t < end; t = json_next(t)) { for (t = tokens + 1; t < end; t = json_next(t)) {
struct sha256_double txid; struct sha256_double txid;
const jsmntok_t *txidtok, *conftok; const jsmntok_t *txidtok, *conftok, *blkindxtok;
long int conf; unsigned int conf;
char *end; bool is_coinbase;
txidtok = json_get_member(bcli->output, t, "txid"); txidtok = json_get_member(bcli->output, t, "txid");
conftok = json_get_member(bcli->output, t, "confirmations"); conftok = json_get_member(bcli->output, t, "confirmations");
blkindxtok = json_get_member(bcli->output, t, "blockindex");
if (!txidtok || !conftok) if (!txidtok || !conftok)
fatal("listtransactions: no %s field!", fatal("listtransactions: no %s field!",
txidtok ? "confirmations" : "txid"); txidtok ? "confirmations" : "txid");
@ -199,27 +200,39 @@ static void process_transactions(struct bitcoin_cli *bcli)
(int)(txidtok->end - txidtok->start), (int)(txidtok->end - txidtok->start),
bcli->output + txidtok->start); bcli->output + txidtok->start);
} }
conf = strtol(bcli->output + conftok->start, &end, 10); if (!json_tok_number(bcli->output, conftok, &conf))
if (end != bcli->output + conftok->end)
fatal("listtransactions: bad confirmations '%.*s'", fatal("listtransactions: bad confirmations '%.*s'",
(int)(conftok->end - conftok->start), (int)(conftok->end - conftok->start),
bcli->output + 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 */ /* FIXME: log txid */
log_debug(bcli->dstate->base_log, 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[0], txid.sha.u.u8[1],
txid.sha.u.u8[2], txid.sha.u.u8[3], 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 bitcoind_poll_transactions(struct lightningd_state *dstate,
void (*cb)(struct lightningd_state *dstate, void (*cb)(struct lightningd_state *dstate,
const struct sha256_double *txid, const struct sha256_double *txid,
int confirmations)) int confirmations,
bool is_coinbase))
{ {
/* FIXME: Iterate and detect duplicates. */ /* FIXME: Iterate and detect duplicates. */
start_bitcoin_cli(dstate, process_transactions, cb, NULL, start_bitcoin_cli(dstate, process_transactions, cb, NULL,

4
daemon/bitcoind.h

@ -3,6 +3,7 @@
#include "config.h" #include "config.h"
#include <ccan/short_types/short_types.h> #include <ccan/short_types/short_types.h>
#include <ccan/typesafe_cb/typesafe_cb.h> #include <ccan/typesafe_cb/typesafe_cb.h>
#include <stdbool.h>
struct sha256_double; struct sha256_double;
struct lightningd_state; struct lightningd_state;
@ -16,7 +17,8 @@ void bitcoind_watch_addr(struct lightningd_state *dstate,
void bitcoind_poll_transactions(struct lightningd_state *dstate, void bitcoind_poll_transactions(struct lightningd_state *dstate,
void (*cb)(struct lightningd_state *dstate, void (*cb)(struct lightningd_state *dstate,
const struct sha256_double *txid, const struct sha256_double *txid,
int confirmations)); int confirmations,
bool is_coinbase));
void bitcoind_txid_lookup_(struct lightningd_state *dstate, void bitcoind_txid_lookup_(struct lightningd_state *dstate,
const struct sha256_double *txid, const struct sha256_double *txid,

7
daemon/watch.c

@ -187,7 +187,9 @@ static void tx_watched_inputs(struct lightningd_state *dstate,
static void watched_transaction(struct lightningd_state *dstate, static void watched_transaction(struct lightningd_state *dstate,
const struct sha256_double *txid, const struct sha256_double *txid,
int confirmations) int confirmations,
bool is_coinbase)
{ {
struct txwatch *txw; struct txwatch *txw;
@ -206,7 +208,8 @@ static void watched_transaction(struct lightningd_state *dstate,
insert_txwatch(dstate, dstate, NULL, txid, NULL, NULL); insert_txwatch(dstate, dstate, NULL, txid, NULL, NULL);
/* Maybe it spent an output we're watching? */ /* 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; static struct timeout watch_timeout;

Loading…
Cancel
Save