daemon: bitcoind callback gives the blockhash the tx was included in.

This is required for transactions which use OP_CSV to lock outputs for
a given amount of time: we need to know the mediantime of the block
they were included into.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-01-22 06:45:28 +10:30
parent b70c18a40e
commit 6afe3f718d
5 changed files with 52 additions and 19 deletions

View File

@ -172,7 +172,8 @@ 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, bool is_coinbase) = bcli->cb; int confirmations, bool is_coinbase,
const struct sha256_double *blkhash) = bcli->cb;
if (!bcli->output) if (!bcli->output)
fatal("bitcoind: '%s' '%s' failed", fatal("bitcoind: '%s' '%s' failed",
@ -191,8 +192,8 @@ 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, blkhash = { 0 };
const jsmntok_t *txidtok, *conftok, *blkindxtok; const jsmntok_t *txidtok, *conftok, *blkindxtok, *blktok;
unsigned int conf; unsigned int conf;
bool is_coinbase; bool is_coinbase;
@ -225,15 +226,27 @@ static void process_transactions(struct bitcoin_cli *bcli)
(int)(blkindxtok->end - blkindxtok->start), (int)(blkindxtok->end - blkindxtok->start),
bcli->output + blkindxtok->start); bcli->output + blkindxtok->start);
is_coinbase = (blkidx == 0); 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, log_debug(bcli->dstate->base_log,
"txid %02x%02x%02x%02x..., conf %u, coinbase %u", "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, is_coinbase); 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, void (*cb)(struct lightningd_state *dstate,
const struct sha256_double *txid, const struct sha256_double *txid,
int confirmations, int confirmations,
bool is_coinbase)) bool is_coinbase,
const struct sha256_double *blkhash))
{ {
/* 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,

View File

@ -18,7 +18,8 @@ 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)); bool is_coinbase,
const struct sha256_double *blkhash));
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,

View File

@ -558,6 +558,7 @@ struct anchor_watch {
}; };
static void anchor_depthchange(struct peer *peer, int depth, static void anchor_depthchange(struct peer *peer, int depth,
const struct sha256_double *blkhash,
struct anchor_watch *w) struct anchor_watch *w)
{ {
/* Still waiting for it to reach depth? */ /* Still waiting for it to reach depth? */

View File

@ -103,7 +103,9 @@ static void destroy_txwatch(struct txwatch *w)
static struct txwatch *insert_txwatch(const tal_t *ctx, static struct txwatch *insert_txwatch(const tal_t *ctx,
struct peer *peer, struct peer *peer,
const struct sha256_double *txid, const struct sha256_double *txid,
void (*cb)(struct peer *, int, void *), void (*cb)(struct peer *, int,
const struct sha256_double *,
void *),
void *cbdata) void *cbdata)
{ {
struct txwatch *w; struct txwatch *w;
@ -150,7 +152,9 @@ void add_anchor_watch_(const tal_t *ctx,
struct peer *peer, struct peer *peer,
const struct sha256_double *txid, const struct sha256_double *txid,
unsigned int out, 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, void (*spend_cb)(struct peer *peer,
const struct bitcoin_tx *, void *), const struct bitcoin_tx *, void *),
void *cbdata) void *cbdata)
@ -177,7 +181,9 @@ void add_anchor_watch_(const tal_t *ctx,
void add_commit_tx_watch_(const tal_t *ctx, void add_commit_tx_watch_(const tal_t *ctx,
struct peer *peer, struct peer *peer,
const struct sha256_double *txid, 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) void *cbdata)
{ {
insert_txwatch(ctx, peer, txid, cb, cbdata); insert_txwatch(ctx, peer, txid, cb, cbdata);
@ -186,7 +192,8 @@ void add_commit_tx_watch_(const tal_t *ctx,
* watch anything else. */ * 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; void (*cb)(struct peer *peer, int depth) = vcb;
cb(peer, depth); cb(peer, depth);
@ -226,7 +233,8 @@ 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) bool is_coinbase,
const struct sha256_double *blkhash)
{ {
struct txwatch *txw; struct txwatch *txw;
@ -237,7 +245,8 @@ static void watched_transaction(struct lightningd_state *dstate,
if (confirmations != txw->depth) { if (confirmations != txw->depth) {
txw->depth = confirmations; txw->depth = confirmations;
if (txw->cb) if (txw->cb)
txw->cb(txw->peer, txw->depth, txw->cbdata); txw->cb(txw->peer, txw->depth, blkhash,
txw->cbdata);
} }
return; return;
} }

View File

@ -49,8 +49,10 @@ struct txwatch {
struct sha256_double txid; struct sha256_double txid;
int depth; int depth;
/* A new depth (-1 if conflicted) */ /* A new depth (-1 if conflicted), blkhash valid if > 0 */
void (*cb)(struct peer *peer, int depth, void *cbdata); void (*cb)(struct peer *peer, int depth,
const struct sha256_double *blkhash,
void *cbdata);
void *cbdata; void *cbdata;
}; };
@ -65,7 +67,9 @@ void add_anchor_watch_(const tal_t *ctx,
struct peer *peer, struct peer *peer,
const struct sha256_double *txid, const struct sha256_double *txid,
unsigned int out, 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, void (*spend_cb)(struct peer *peer,
const struct bitcoin_tx *, void *), const struct bitcoin_tx *, void *),
void *cbdata); void *cbdata);
@ -75,7 +79,8 @@ void add_anchor_watch_(const tal_t *ctx,
typesafe_cb_preargs(void, void *, \ typesafe_cb_preargs(void, void *, \
(anchor_cb), (cbdata), \ (anchor_cb), (cbdata), \
struct peer *, \ struct peer *, \
int depth), \ int depth, \
const struct sha256_double *), \
typesafe_cb_preargs(void, void *, \ typesafe_cb_preargs(void, void *, \
(spend_cb), (cbdata), \ (spend_cb), (cbdata), \
struct peer *, \ struct peer *, \
@ -85,7 +90,9 @@ void add_anchor_watch_(const tal_t *ctx,
void add_commit_tx_watch_(const tal_t *ctx, void add_commit_tx_watch_(const tal_t *ctx,
struct peer *peer, struct peer *peer,
const struct sha256_double *txid, 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); void *cbdata);
#define add_commit_tx_watch(ctx, peer, txid, cb, 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 *, \ typesafe_cb_preargs(void, void *, \
(cb), (cbdata), \ (cb), (cbdata), \
struct peer *, \ struct peer *, \
int depth), \ int, \
const struct sha256_double *), \
(cbdata)) (cbdata))
void add_close_tx_watch(const tal_t *ctx, void add_close_tx_watch(const tal_t *ctx,