Skip to content

Commit

Permalink
daemon: bitcoind callback gives the blockhash the tx was included in.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rustyrussell committed Jan 21, 2016
1 parent b70c18a commit 6afe3f7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 19 deletions.
26 changes: 20 additions & 6 deletions daemon/bitcoind.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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;

Expand Down Expand Up @@ -225,23 +226,36 @@ 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);
}
}

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,
Expand Down
3 changes: 2 additions & 1 deletion daemon/bitcoind.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions daemon/peer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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? */
Expand Down
21 changes: 15 additions & 6 deletions daemon/watch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
20 changes: 14 additions & 6 deletions daemon/watch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand All @@ -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);
Expand All @@ -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 *, \
Expand All @@ -85,15 +90,18 @@ 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) \
add_commit_tx_watch_((ctx), (peer), (txid), \
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,
Expand Down

0 comments on commit 6afe3f7

Please sign in to comment.