Browse Source

broadcast_tx: add optional failed callback.

And if that's set, don't rebroadcast.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
7aa01b0e50
  1. 10
      daemon/chaintopology.c
  2. 8
      daemon/chaintopology.h
  3. 19
      daemon/peer.c
  4. 1
      daemon/peer.h

10
daemon/chaintopology.c

@ -279,12 +279,19 @@ static void broadcast_done(struct lightningd_state *dstate,
int exitstatus, const char *msg, int exitstatus, const char *msg,
struct outgoing_tx *otx) struct outgoing_tx *otx)
{ {
if (otx->failed && exitstatus != 0) {
otx->failed(otx->peer, exitstatus, msg);
tal_free(otx);
} else {
/* For continual rebroadcasting */ /* For continual rebroadcasting */
list_add_tail(&otx->peer->outgoing_txs, &otx->list); list_add_tail(&otx->peer->outgoing_txs, &otx->list);
tal_add_destructor(otx, destroy_outgoing_tx); tal_add_destructor(otx, destroy_outgoing_tx);
} }
}
void broadcast_tx(struct peer *peer, const struct bitcoin_tx *tx) void broadcast_tx(struct peer *peer, const struct bitcoin_tx *tx,
void (*failed)(struct peer *peer,
int exitstatus, const char *err))
{ {
struct outgoing_tx *otx = tal(peer, struct outgoing_tx); struct outgoing_tx *otx = tal(peer, struct outgoing_tx);
const u8 *rawtx = linearize_tx(otx, tx); const u8 *rawtx = linearize_tx(otx, tx);
@ -292,6 +299,7 @@ void broadcast_tx(struct peer *peer, const struct bitcoin_tx *tx)
otx->peer = peer; otx->peer = peer;
bitcoin_txid(tx, &otx->txid); bitcoin_txid(tx, &otx->txid);
otx->hextx = tal_hexstr(otx, rawtx, tal_count(rawtx)); otx->hextx = tal_hexstr(otx, rawtx, tal_count(rawtx));
otx->failed = failed;
tal_free(rawtx); tal_free(rawtx);
log_add_struct(peer->log, " (tx %s)", struct sha256_double, &otx->txid); log_add_struct(peer->log, " (tx %s)", struct sha256_double, &otx->txid);

8
daemon/chaintopology.h

@ -38,8 +38,12 @@ u32 get_block_height(struct lightningd_state *dstate);
/* Get fee rate. */ /* Get fee rate. */
u64 get_feerate(struct lightningd_state *dstate); u64 get_feerate(struct lightningd_state *dstate);
/* Broadcast a single tx, and rebroadcast as reqd (copies tx). */ /* Broadcast a single tx, and rebroadcast as reqd (copies tx).
void broadcast_tx(struct peer *peer, const struct bitcoin_tx *tx); * If failed is non-NULL, call that and don't rebroadcast. */
void broadcast_tx(struct peer *peer, const struct bitcoin_tx *tx,
void (*failed)(struct peer *peer,
int exitstatus,
const char *err));
void setup_topology(struct lightningd_state *dstate); void setup_topology(struct lightningd_state *dstate);

19
daemon/peer.c

@ -342,12 +342,12 @@ static void peer_breakdown(struct peer *peer)
/* If we have a closing tx, use it. */ /* If we have a closing tx, use it. */
if (peer->closing.their_sig) { if (peer->closing.their_sig) {
log_unusual(peer->log, "Peer breakdown: sending close tx"); log_unusual(peer->log, "Peer breakdown: sending close tx");
broadcast_tx(peer, bitcoin_close(peer)); broadcast_tx(peer, bitcoin_close(peer), NULL);
/* If we have a signed commit tx (maybe not if we just offered /* If we have a signed commit tx (maybe not if we just offered
* anchor, or they supplied anchor, or no outputs to us). */ * anchor, or they supplied anchor, or no outputs to us). */
} else if (peer->local.commit && peer->local.commit->sig) { } else if (peer->local.commit && peer->local.commit->sig) {
log_unusual(peer->log, "Peer breakdown: sending commit tx"); log_unusual(peer->log, "Peer breakdown: sending commit tx");
broadcast_tx(peer, bitcoin_commit(peer)); broadcast_tx(peer, bitcoin_commit(peer), NULL);
} else { } else {
log_info(peer->log, "Peer breakdown: nothing to do"); log_info(peer->log, "Peer breakdown: nothing to do");
/* We close immediately. */ /* We close immediately. */
@ -963,7 +963,7 @@ static bool closing_pkt_in(struct peer *peer, const Pkt *pkt)
* matching `close_fee` it SHOULD close the connection and * matching `close_fee` it SHOULD close the connection and
* SHOULD sign and broadcast the final closing transaction. * SHOULD sign and broadcast the final closing transaction.
*/ */
broadcast_tx(peer, bitcoin_close(peer)); broadcast_tx(peer, bitcoin_close(peer), NULL);
return false; return false;
} }
@ -1553,7 +1553,7 @@ static void state_single(struct peer *peer,
log_add(peer->log, " (out %s)", pkt_name(outpkt->pkt_case)); log_add(peer->log, " (out %s)", pkt_name(outpkt->pkt_case));
} }
if (broadcast) if (broadcast)
broadcast_tx(peer, broadcast); broadcast_tx(peer, broadcast, NULL);
if (state_is_error(peer->state)) { if (state_is_error(peer->state)) {
/* Breakdown is common, others less so. */ /* Breakdown is common, others less so. */
@ -1694,7 +1694,7 @@ static bool fulfill_onchain(struct peer *peer, struct htlc *htlc)
return false; return false;
peer->onchain.resolved[i] peer->onchain.resolved[i]
= htlc_fulfill_tx(peer, i); = htlc_fulfill_tx(peer, i);
broadcast_tx(peer, peer->onchain.resolved[i]); broadcast_tx(peer, peer->onchain.resolved[i], NULL);
return true; return true;
} }
} }
@ -3301,7 +3301,7 @@ static enum watch_result our_htlc_depth(struct peer *peer,
peer, peer,
peer->onchain.resolved[out_num], peer->onchain.resolved[out_num],
our_htlc_timeout_depth, h); our_htlc_timeout_depth, h);
broadcast_tx(peer, peer->onchain.resolved[out_num]); broadcast_tx(peer, peer->onchain.resolved[out_num], NULL);
} }
return DELETE_WATCH; return DELETE_WATCH;
} }
@ -3370,7 +3370,8 @@ static enum watch_result our_main_output_depth(struct peer *peer,
*/ */
peer->onchain.resolved[peer->onchain.to_us_idx] peer->onchain.resolved[peer->onchain.to_us_idx]
= bitcoin_spend_ours(peer); = bitcoin_spend_ours(peer);
broadcast_tx(peer, peer->onchain.resolved[peer->onchain.to_us_idx]); broadcast_tx(peer, peer->onchain.resolved[peer->onchain.to_us_idx],
NULL);
return DELETE_WATCH; return DELETE_WATCH;
} }
@ -3486,7 +3487,7 @@ static void resolve_their_htlc(struct peer *peer, unsigned int out_num)
*/ */
if (peer->onchain.htlcs[out_num]->r) { if (peer->onchain.htlcs[out_num]->r) {
peer->onchain.resolved[out_num] = htlc_fulfill_tx(peer, out_num); peer->onchain.resolved[out_num] = htlc_fulfill_tx(peer, out_num);
broadcast_tx(peer, peer->onchain.resolved[out_num]); broadcast_tx(peer, peer->onchain.resolved[out_num], NULL);
} else { } else {
/* BOLT #onchain: /* BOLT #onchain:
* *
@ -3773,7 +3774,7 @@ static void resolve_their_steal(struct peer *peer,
peer->onchain.wscripts[i]); peer->onchain.wscripts[i]);
} }
broadcast_tx(peer, steal_tx); broadcast_tx(peer, steal_tx, NULL);
} }
static struct sha256 *get_rhash(struct peer *peer, u64 commit_num, static struct sha256 *get_rhash(struct peer *peer, u64 commit_num,

1
daemon/peer.h

@ -82,6 +82,7 @@ struct outgoing_tx {
struct peer *peer; struct peer *peer;
const char *hextx; const char *hextx;
struct sha256_double txid; struct sha256_double txid;
void (*failed)(struct peer *peer, int exitstatus, const char *err);
}; };
struct peer { struct peer {

Loading…
Cancel
Save