Skip to content

Commit

Permalink
txwatch: remove unused callback arg, hide struct definitions.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Feb 20, 2018
1 parent ae8fb96 commit 719290a
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 170 deletions.
13 changes: 5 additions & 8 deletions lightningd/onchain_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ static void handle_onchain_init_reply(struct channel *channel, const u8 *msg)

static enum watch_result onchain_tx_watched(struct channel *channel,
const struct bitcoin_tx *tx,
unsigned int depth,
void *unused)
unsigned int depth)
{
u8 *msg;
struct bitcoin_txid txid;
Expand Down Expand Up @@ -100,8 +99,7 @@ static void watch_tx_and_outputs(struct channel *channel,
static enum watch_result onchain_txo_watched(struct channel *channel,
const struct bitcoin_tx *tx,
size_t input_num,
const struct block *block,
void *unused)
const struct block *block)
{
u8 *msg;

Expand All @@ -128,11 +126,11 @@ static void watch_tx_and_outputs(struct channel *channel,

/* Make txwatch a parent of txo watches, so we can unwatch together. */
txw = watch_tx(channel->owner, ld->topology, channel, tx,
onchain_tx_watched, NULL);
onchain_tx_watched);

for (size_t i = 0; i < tal_count(tx->output); i++)
watch_txo(txw, ld->topology, channel, &txid, i,
onchain_txo_watched, NULL);
onchain_txo_watched);
}

static void handle_onchain_broadcast_tx(struct channel *channel, const u8 *msg)
Expand Down Expand Up @@ -352,8 +350,7 @@ static void onchain_error(struct channel *channel,
enum watch_result funding_spent(struct channel *channel,
const struct bitcoin_tx *tx,
size_t input_num,
const struct block *block,
void *unused)
const struct block *block)
{
u8 *msg, *scriptpubkey;
struct bitcoin_txid our_last_txid;
Expand Down
3 changes: 1 addition & 2 deletions lightningd/onchain_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ struct block;
enum watch_result funding_spent(struct channel *channel,
const struct bitcoin_tx *tx,
size_t input_num,
const struct block *block,
void *unused);
const struct block *block);

#endif /* LIGHTNING_LIGHTNINGD_ONCHAIN_CONTROL_H */
14 changes: 6 additions & 8 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,7 @@ void peer_sent_nongossip(struct lightningd *ld,

static enum watch_result funding_announce_cb(struct channel *channel,
const struct bitcoin_tx *tx,
unsigned int depth,
void *unused)
unsigned int depth)
{
if (depth < ANNOUNCE_MIN_DEPTH) {
return KEEP_WATCHING;
Expand All @@ -502,8 +501,7 @@ static enum watch_result funding_announce_cb(struct channel *channel,

static enum watch_result funding_lockin_cb(struct channel *channel,
const struct bitcoin_tx *tx,
unsigned int depth,
void *unused)
unsigned int depth)
{
struct bitcoin_txid txid;
const char *txidstr;
Expand Down Expand Up @@ -560,21 +558,21 @@ static enum watch_result funding_lockin_cb(struct channel *channel,
* before. If we are at the right depth, call the callback
* directly, otherwise schedule a callback */
if (depth >= ANNOUNCE_MIN_DEPTH)
funding_announce_cb(channel, tx, depth, NULL);
funding_announce_cb(channel, tx, depth);
else
watch_txid(channel, ld->topology, channel, &txid,
funding_announce_cb, NULL);
funding_announce_cb);
return DELETE_WATCH;
}

void channel_watch_funding(struct lightningd *ld, struct channel *channel)
{
/* FIXME: Remove arg from cb? */
watch_txid(channel, ld->topology, channel,
&channel->funding_txid, funding_lockin_cb, NULL);
&channel->funding_txid, funding_lockin_cb);
watch_txo(channel, ld->topology, channel,
&channel->funding_txid, channel->funding_outnum,
funding_spent, NULL);
funding_spent);
}

struct getpeers_args {
Expand Down
89 changes: 57 additions & 32 deletions lightningd/watch.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,39 @@
#include <lightningd/peer_control.h>
#include <lightningd/watch.h>

/* Watching an output */
struct txowatch {
struct chain_topology *topo;

/* Channel who owns us. */
struct channel *channel;

/* Output to watch. */
struct txwatch_output out;

/* A new tx. */
enum watch_result (*cb)(struct channel *channel,
const struct bitcoin_tx *tx,
size_t input_num,
const struct block *block);
};

struct txwatch {
struct chain_topology *topo;

/* Channel who owns us. */
struct channel *channel;

/* Transaction to watch. */
struct bitcoin_txid txid;
unsigned int depth;

/* A new depth (0 if kicked out, otherwise 1 = tip, etc.) */
enum watch_result (*cb)(struct channel *channel,
const struct bitcoin_tx *tx,
unsigned int depth);
};

const struct txwatch_output *txowatch_keyof(const struct txowatch *w)
{
return &w->out;
Expand Down Expand Up @@ -86,15 +119,13 @@ static void destroy_txwatch(struct txwatch *w)
txwatch_hash_del(&w->topo->txwatches, w);
}

struct txwatch *watch_txid_(const tal_t *ctx,
struct chain_topology *topo,
struct channel *channel,
const struct bitcoin_txid *txid,
enum watch_result (*cb)(struct channel *channel,
struct txwatch *watch_txid(const tal_t *ctx,
struct chain_topology *topo,
struct channel *channel,
const struct bitcoin_txid *txid,
enum watch_result (*cb)(struct channel *channel,
const struct bitcoin_tx *,
unsigned int depth,
void *arg),
void *cb_arg)
unsigned int depth))
{
struct txwatch *w;

Expand All @@ -104,7 +135,6 @@ struct txwatch *watch_txid_(const tal_t *ctx,
w->txid = *txid;
w->channel = channel;
w->cb = cb;
w->cbdata = cb_arg;

txwatch_hash_add(&w->topo->txwatches, w);
tal_add_destructor(w, destroy_txwatch);
Expand Down Expand Up @@ -136,33 +166,29 @@ bool watching_txid(const struct chain_topology *topo,
return txwatch_hash_get(&topo->txwatches, txid) != NULL;
}

struct txwatch *watch_tx_(const tal_t *ctx,
struct chain_topology *topo,
struct channel *channel,
const struct bitcoin_tx *tx,
enum watch_result (*cb)(struct channel *channel,
struct txwatch *watch_tx(const tal_t *ctx,
struct chain_topology *topo,
struct channel *channel,
const struct bitcoin_tx *tx,
enum watch_result (*cb)(struct channel *channel,
const struct bitcoin_tx *,
unsigned int depth,
void *arg),
void *cb_arg)
unsigned int depth))
{
struct bitcoin_txid txid;

bitcoin_txid(tx, &txid);
return watch_txid(ctx, topo, channel, &txid, cb, cb_arg);
return watch_txid(ctx, topo, channel, &txid, cb);
}

struct txowatch *watch_txo_(const tal_t *ctx,
struct chain_topology *topo,
struct channel *channel,
const struct bitcoin_txid *txid,
unsigned int output,
enum watch_result (*cb)(struct channel *channel,
const struct bitcoin_tx *tx,
size_t input_num,
const struct block *block,
void *),
void *cbdata)
struct txowatch *watch_txo(const tal_t *ctx,
struct chain_topology *topo,
struct channel *channel,
const struct bitcoin_txid *txid,
unsigned int output,
enum watch_result (*cb)(struct channel *channel,
const struct bitcoin_tx *tx,
size_t input_num,
const struct block *block))
{
struct txowatch *w = tal(ctx, struct txowatch);

Expand All @@ -171,7 +197,6 @@ struct txowatch *watch_txo_(const tal_t *ctx,
w->out.index = output;
w->channel = channel;
w->cb = cb;
w->cbdata = cbdata;

txowatch_hash_add(&w->topo->txowatches, w);
tal_add_destructor(w, destroy_txowatch);
Expand All @@ -194,7 +219,7 @@ static bool txw_fire(struct chain_topology *topo,
txw->depth, depth,
type_to_string(ltmp, struct bitcoin_txid, &txw->txid));
txw->depth = depth;
r = txw->cb(txw->channel, tx, txw->depth, txw->cbdata);
r = txw->cb(txw->channel, tx, txw->depth);
switch (r) {
case DELETE_WATCH:
tal_free(txw);
Expand Down Expand Up @@ -235,7 +260,7 @@ void txowatch_fire(struct chain_topology *topo,
txow->out.index,
type_to_string(ltmp, struct bitcoin_txid, &txid));

r = txow->cb(txow->channel, tx, input_num, block, txow->cbdata);
r = txow->cb(txow->channel, tx, input_num, block);
switch (r) {
case DELETE_WATCH:
tal_free(txow);
Expand Down
121 changes: 26 additions & 95 deletions lightningd/watch.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

struct bitcoin_tx;
struct block;
struct channel;
struct chain_topology;
struct txowatch;
struct txwatch;

enum watch_result {
DELETE_WATCH = -1,
Expand All @@ -21,118 +25,45 @@ struct txwatch_output {
unsigned int index;
};

/* Watching an output */
struct txowatch {
struct chain_topology *topo;

/* Channel who owns us. */
struct channel *channel;

/* Output to watch. */
struct txwatch_output out;

/* A new tx. */
enum watch_result (*cb)(struct channel *channel,
const struct bitcoin_tx *tx,
size_t input_num,
const struct block *block,
void *cbdata);

void *cbdata;
};

const struct txwatch_output *txowatch_keyof(const struct txowatch *w);
size_t txo_hash(const struct txwatch_output *out);
bool txowatch_eq(const struct txowatch *w, const struct txwatch_output *out);

HTABLE_DEFINE_TYPE(struct txowatch, txowatch_keyof, txo_hash, txowatch_eq,
txowatch_hash);

struct txwatch {
struct chain_topology *topo;

/* Channel who owns us. */
struct channel *channel;

/* Transaction to watch. */
struct bitcoin_txid txid;
unsigned int depth;

/* A new depth (0 if kicked out, otherwise 1 = tip, etc.) */
enum watch_result (*cb)(struct channel *channel,
const struct bitcoin_tx *tx,
unsigned int depth,
void *cbdata);

void *cbdata;
};

const struct bitcoin_txid *txwatch_keyof(const struct txwatch *w);
size_t txid_hash(const struct bitcoin_txid *txid);
bool txwatch_eq(const struct txwatch *w, const struct bitcoin_txid *txid);
HTABLE_DEFINE_TYPE(struct txwatch, txwatch_keyof, txid_hash, txwatch_eq,
txwatch_hash);


struct txwatch *watch_txid_(const tal_t *ctx,
struct chain_topology *topo,
struct channel *channel,
const struct bitcoin_txid *txid,
enum watch_result (*cb)(struct channel *channel,
struct txwatch *watch_txid(const tal_t *ctx,
struct chain_topology *topo,
struct channel *channel,
const struct bitcoin_txid *txid,
enum watch_result (*cb)(struct channel *channel,
const struct bitcoin_tx *,
unsigned int depth,
void *),
void *cbdata);
unsigned int depth));

#define watch_txid(ctx, topo, channel_, txid, cb, cbdata) \
watch_txid_((ctx), (topo), (channel_), (txid), \
typesafe_cb_preargs(enum watch_result, void *, \
(cb), (cbdata), \
struct channel *, \
const struct bitcoin_tx *, \
unsigned int depth), \
(cbdata))

struct txwatch *watch_tx_(const tal_t *ctx,
struct chain_topology *topo,
struct channel *channel,
const struct bitcoin_tx *tx,
enum watch_result (*cb)(struct channel *channel,
struct txwatch *watch_tx(const tal_t *ctx,
struct chain_topology *topo,
struct channel *channel,
const struct bitcoin_tx *tx,
enum watch_result (*cb)(struct channel *channel,
const struct bitcoin_tx *,
unsigned int depth,
void *),
void *cbdata);

#define watch_tx(ctx, topo, channel_, tx, cb, cbdata) \
watch_tx_((ctx), (topo), (channel_), (tx), \
typesafe_cb_preargs(enum watch_result, void *, \
(cb), (cbdata), \
struct channel *, \
const struct bitcoin_tx *, \
unsigned int depth), \
(cbdata))

struct txowatch *watch_txo_(const tal_t *ctx,
struct chain_topology *topo,
struct channel *channel,
const struct bitcoin_txid *txid,
unsigned int output,
enum watch_result (*cb)(struct channel *channel,
const struct bitcoin_tx *tx,
size_t input_num,
const struct block *block,
void *),
void *cbdata);

#define watch_txo(ctx, topo, channel_, txid, outnum, cb, cbdata) \
watch_txo_((ctx), (topo), (channel_), (txid), (outnum), \
typesafe_cb_preargs(enum watch_result, void *, \
(cb), (cbdata), \
struct channel *, \
const struct bitcoin_tx *, \
size_t, \
const struct block *block), \
(cbdata))
unsigned int depth));

struct txowatch *watch_txo(const tal_t *ctx,
struct chain_topology *topo,
struct channel *channel,
const struct bitcoin_txid *txid,
unsigned int output,
enum watch_result (*cb)(struct channel *channel,
const struct bitcoin_tx *tx,
size_t input_num,
const struct block *block));

struct txwatch *find_txwatch(struct chain_topology *topo,
const struct bitcoin_txid *txid,
Expand Down
Loading

0 comments on commit 719290a

Please sign in to comment.