2016-01-21 21:11:49 +01:00
|
|
|
/* Code to talk to bitcoind to watch for various events.
|
|
|
|
*
|
|
|
|
* Here's what we want to know:
|
|
|
|
*
|
|
|
|
* - An anchor tx:
|
|
|
|
* - Reached given depth
|
|
|
|
* - Times out.
|
|
|
|
* - Is unspent after reaching given depth.
|
|
|
|
*
|
|
|
|
* - Our own commitment tx:
|
|
|
|
* - Reached a given depth.
|
|
|
|
*
|
|
|
|
* - HTLC spend tx:
|
|
|
|
* - Reached a given depth.
|
|
|
|
*
|
|
|
|
* - Anchor tx output:
|
|
|
|
* - Is spent by their current tx.
|
|
|
|
* - Is spent by a revoked tx.
|
|
|
|
*
|
|
|
|
* - Commitment tx HTLC outputs:
|
|
|
|
* - HTLC timed out
|
|
|
|
* - HTLC spent
|
|
|
|
*
|
|
|
|
* We do this by adding the P2SH address to the wallet, and then querying
|
|
|
|
* that using listtransactions.
|
|
|
|
*
|
|
|
|
* WE ASSUME NO MALLEABILITY! This requires segregated witness.
|
|
|
|
*/
|
|
|
|
#include "bitcoin/script.h"
|
|
|
|
#include "bitcoin/tx.h"
|
|
|
|
#include "bitcoind.h"
|
2016-04-24 12:18:35 +02:00
|
|
|
#include "chaintopology.h"
|
2016-01-21 21:11:49 +01:00
|
|
|
#include "lightningd.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "peer.h"
|
|
|
|
#include "timeout.h"
|
|
|
|
#include "watch.h"
|
|
|
|
#include <ccan/hash/hash.h>
|
2016-04-24 12:20:35 +02:00
|
|
|
#include <ccan/ptrint/ptrint.h>
|
2016-01-21 21:11:49 +01:00
|
|
|
#include <ccan/structeq/structeq.h>
|
|
|
|
|
|
|
|
const struct txwatch_output *txowatch_keyof(const struct txowatch *w)
|
|
|
|
{
|
|
|
|
return &w->out;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t txo_hash(const struct txwatch_output *out)
|
|
|
|
{
|
|
|
|
return hash(&out->txid, 1, out->index);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool txowatch_eq(const struct txowatch *w, const struct txwatch_output *out)
|
|
|
|
{
|
|
|
|
return structeq(&w->out.txid, &out->txid)
|
|
|
|
&& w->out.index == out->index;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_txowatch(struct txowatch *w)
|
|
|
|
{
|
2016-01-21 21:11:49 +01:00
|
|
|
txowatch_hash_del(&w->peer->dstate->txowatches, w);
|
2016-01-21 21:11:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct sha256_double *txwatch_keyof(const struct txwatch *w)
|
|
|
|
{
|
|
|
|
return &w->txid;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t txid_hash(const struct sha256_double *txid)
|
|
|
|
{
|
|
|
|
return hash(txid->sha.u.u8, sizeof(txid->sha.u.u8), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool txwatch_eq(const struct txwatch *w, const struct sha256_double *txid)
|
|
|
|
{
|
|
|
|
return structeq(&w->txid, txid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_txwatch(struct txwatch *w)
|
|
|
|
{
|
2016-01-21 21:11:49 +01:00
|
|
|
txwatch_hash_del(&w->dstate->txwatches, w);
|
2016-01-21 21:11:49 +01:00
|
|
|
}
|
|
|
|
|
2016-04-24 12:09:21 +02:00
|
|
|
struct txwatch *watch_txid_(const tal_t *ctx,
|
|
|
|
struct peer *peer,
|
|
|
|
const struct sha256_double *txid,
|
|
|
|
void (*cb)(struct peer *peer, int depth,
|
|
|
|
const struct sha256_double *txid,
|
|
|
|
void *arg),
|
|
|
|
void *cb_arg)
|
2016-01-21 21:11:49 +01:00
|
|
|
{
|
|
|
|
struct txwatch *w;
|
|
|
|
|
2016-04-24 12:09:21 +02:00
|
|
|
assert(!txwatch_hash_get(&peer->dstate->txwatches, txid));
|
2016-01-21 21:11:49 +01:00
|
|
|
|
|
|
|
w = tal(ctx, struct txwatch);
|
2016-04-24 12:09:21 +02:00
|
|
|
w->depth = -1;
|
2016-01-21 21:11:49 +01:00
|
|
|
w->txid = *txid;
|
2016-01-21 21:15:28 +01:00
|
|
|
w->dstate = peer->dstate;
|
2016-01-21 21:11:49 +01:00
|
|
|
w->peer = peer;
|
|
|
|
w->cb = cb;
|
2016-04-24 12:09:21 +02:00
|
|
|
w->cbdata = cb_arg;
|
2016-01-21 21:11:49 +01:00
|
|
|
|
2016-01-21 21:11:49 +01:00
|
|
|
txwatch_hash_add(&w->dstate->txwatches, w);
|
2016-01-21 21:11:49 +01:00
|
|
|
tal_add_destructor(w, destroy_txwatch);
|
|
|
|
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2016-04-24 12:09:21 +02:00
|
|
|
struct txwatch *watch_tx_(const tal_t *ctx,
|
|
|
|
struct peer *peer,
|
|
|
|
const struct bitcoin_tx *tx,
|
|
|
|
void (*cb)(struct peer *peer, int depth,
|
|
|
|
const struct sha256_double *txid,
|
|
|
|
void *arg),
|
|
|
|
void *cb_arg)
|
|
|
|
{
|
|
|
|
struct sha256_double txid;
|
|
|
|
|
2016-04-24 12:31:52 +02:00
|
|
|
bitcoin_txid(tx, &txid);
|
2016-04-24 12:09:21 +02:00
|
|
|
return watch_txid(ctx, peer, &txid, cb, cb_arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct txowatch *watch_txo_(const tal_t *ctx,
|
|
|
|
struct peer *peer,
|
|
|
|
const struct sha256_double *txid,
|
|
|
|
unsigned int output,
|
|
|
|
void (*cb)(struct peer *peer,
|
|
|
|
const struct bitcoin_tx *tx,
|
|
|
|
void *),
|
|
|
|
void *cbdata)
|
|
|
|
{
|
|
|
|
struct txowatch *w = tal(ctx, struct txowatch);
|
|
|
|
|
|
|
|
w->out.txid = *txid;
|
|
|
|
w->out.index = output;
|
|
|
|
w->peer = peer;
|
|
|
|
w->cb = cb;
|
|
|
|
w->cbdata = cbdata;
|
|
|
|
|
|
|
|
txowatch_hash_add(&w->peer->dstate->txowatches, w);
|
|
|
|
tal_add_destructor(w, destroy_txowatch);
|
|
|
|
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-24 12:18:35 +02:00
|
|
|
void txwatch_fire(struct lightningd_state *dstate,
|
|
|
|
struct txwatch *txw,
|
2016-04-24 12:20:35 +02:00
|
|
|
unsigned int depth)
|
2016-01-21 21:15:28 +01:00
|
|
|
{
|
2016-04-24 12:18:35 +02:00
|
|
|
if (depth != txw->depth) {
|
|
|
|
log_debug(txw->peer->log,
|
|
|
|
"Got depth change %u for %02x%02x%02x...\n",
|
|
|
|
txw->depth,
|
|
|
|
txw->txid.sha.u.u8[0],
|
|
|
|
txw->txid.sha.u.u8[1],
|
|
|
|
txw->txid.sha.u.u8[2]);
|
|
|
|
txw->depth = depth;
|
2016-04-24 12:20:35 +02:00
|
|
|
txw->cb(txw->peer, txw->depth, &txw->txid, txw->cbdata);
|
2016-04-24 12:18:35 +02:00
|
|
|
}
|
2016-01-21 21:15:28 +01:00
|
|
|
}
|
|
|
|
|
2016-04-24 12:18:35 +02:00
|
|
|
void txowatch_fire(struct lightningd_state *dstate,
|
|
|
|
const struct txowatch *txow,
|
|
|
|
const struct bitcoin_tx *tx)
|
2016-01-21 21:15:28 +01:00
|
|
|
{
|
|
|
|
struct sha256_double txid;
|
|
|
|
|
2016-04-24 12:09:21 +02:00
|
|
|
bitcoin_txid(tx, &txid);
|
2016-04-24 12:18:35 +02:00
|
|
|
log_debug(txow->peer->log,
|
|
|
|
"Got UTXO spend for %02x%02x%02x:%u: %02x%02x%02x%02x...\n",
|
|
|
|
txow->out.txid.sha.u.u8[0],
|
|
|
|
txow->out.txid.sha.u.u8[1],
|
|
|
|
txow->out.txid.sha.u.u8[2],
|
|
|
|
txow->out.index,
|
2016-04-24 12:09:21 +02:00
|
|
|
txid.sha.u.u8[0],
|
|
|
|
txid.sha.u.u8[1],
|
2016-04-24 12:18:35 +02:00
|
|
|
txid.sha.u.u8[2],
|
|
|
|
txid.sha.u.u8[3]);
|
|
|
|
txow->cb(txow->peer, tx, txow->cbdata);
|
2016-01-21 21:11:49 +01:00
|
|
|
}
|
|
|
|
|
2016-04-24 12:18:35 +02:00
|
|
|
void watch_topology_changed(struct lightningd_state *dstate)
|
2016-01-21 21:11:49 +01:00
|
|
|
{
|
2016-04-24 12:18:35 +02:00
|
|
|
struct txwatch_hash_iter i;
|
|
|
|
struct txwatch *w;
|
|
|
|
bool needs_rerun;
|
|
|
|
|
|
|
|
again:
|
|
|
|
/* Iterating a htable during deletes is safe, but might skip entries. */
|
|
|
|
needs_rerun = false;
|
|
|
|
for (w = txwatch_hash_first(&dstate->txwatches, &i);
|
|
|
|
w;
|
|
|
|
w = txwatch_hash_next(&dstate->txwatches, &i)) {
|
|
|
|
size_t depth;
|
|
|
|
|
|
|
|
/* Don't fire if we haven't seen it at all. */
|
|
|
|
if (w->depth == -1)
|
|
|
|
continue;
|
|
|
|
|
2016-04-24 12:20:35 +02:00
|
|
|
depth = get_tx_depth(dstate, w);
|
2016-04-24 12:18:35 +02:00
|
|
|
if (depth != w->depth) {
|
|
|
|
w->depth = depth;
|
2016-04-24 12:20:35 +02:00
|
|
|
w->cb(w->peer, w->depth, &w->txid, w->cbdata);
|
2016-04-24 12:18:35 +02:00
|
|
|
needs_rerun = true;
|
2016-01-21 21:11:49 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-24 12:18:35 +02:00
|
|
|
if (needs_rerun)
|
|
|
|
goto again;
|
2016-01-21 21:11:49 +01:00
|
|
|
}
|