diff --git a/daemon/bitcoind.c b/daemon/bitcoind.c index 3b029fd0a..edba687fb 100644 --- a/daemon/bitcoind.c +++ b/daemon/bitcoind.c @@ -282,14 +282,13 @@ static void process_sendrawtx(struct bitcoin_cli *bcli) cb(bcli->dstate, msg, bcli->cb_arg); } -void bitcoind_sendrawtx_(struct peer *peer, - struct lightningd_state *dstate, +void bitcoind_sendrawtx_(struct peer *peer,struct lightningd_state *dstate, const char *hextx, void (*cb)(struct lightningd_state *dstate, const char *msg, void *), void *arg) { - start_bitcoin_cli(dstate, peer, process_sendrawtx, true, cb, arg, + start_bitcoin_cli(dstate, NULL, process_sendrawtx, true, cb, arg, "sendrawtransaction", hextx, NULL); } diff --git a/daemon/chaintopology.c b/daemon/chaintopology.c index 697ef0bc3..582385119 100644 --- a/daemon/chaintopology.c +++ b/daemon/chaintopology.c @@ -12,6 +12,7 @@ #include #include #include +#include #include struct block { @@ -212,8 +213,8 @@ size_t get_tx_depth(struct lightningd_state *dstate, return topo->tip->height - b->height + 1; } -static void try_broadcast(struct lightningd_state *dstate, - const char *msg, char **txs) +static void broadcast_remainder(struct lightningd_state *dstate, + const char *msg, char **txs) { size_t num_txs = tal_count(txs); const char *this_tx; @@ -237,7 +238,7 @@ static void try_broadcast(struct lightningd_state *dstate, this_tx = txs[num_txs-1]; tal_resize(&txs, num_txs-1); - bitcoind_sendrawtx(NULL, dstate, this_tx, try_broadcast, txs); + bitcoind_sendrawtx(NULL, dstate, this_tx, broadcast_remainder, txs); } /* FIXME: This is dumb. We can group txs and avoid bothering bitcoind @@ -253,46 +254,48 @@ static void rebroadcast_txs(struct lightningd_state *dstate) struct outgoing_tx *otx; list_for_each(&peer->outgoing_txs, otx, list) { - u8 *rawtx; - if (block_for_tx(dstate, &otx->txid)) continue; tal_resize(&txs, num_txs+1); - rawtx = linearize_tx(txs, otx->tx); - txs[num_txs] = tal_hexstr(txs, rawtx, tal_count(rawtx)); + txs[num_txs] = tal_strdup(txs, otx->hextx); num_txs++; } } if (num_txs) bitcoind_sendrawtx(NULL, dstate, txs[num_txs-1], - try_broadcast, txs); + broadcast_remainder, txs); else tal_free(txs); } static void destroy_outgoing_tx(struct outgoing_tx *otx) { - list_del(&otx->list); + list_del_from(&otx->peer->outgoing_txs, &otx->list); +} + +static void broadcast_done(struct lightningd_state *dstate, + const char *msg, struct outgoing_tx *otx) +{ + /* For continual rebroadcasting */ + list_add_tail(&otx->peer->outgoing_txs, &otx->list); + tal_add_destructor(otx, destroy_outgoing_tx); } void broadcast_tx(struct peer *peer, const struct bitcoin_tx *tx) { struct outgoing_tx *otx = tal(peer, struct outgoing_tx); - char **txs = tal_arr(peer->dstate, char *, 1); - u8 *rawtx; + const u8 *rawtx = linearize_tx(otx, tx); - otx->tx = tal_steal(otx, tx); - bitcoin_txid(otx->tx, &otx->txid); - list_add_tail(&peer->outgoing_txs, &otx->list); - tal_add_destructor(otx, destroy_outgoing_tx); + otx->peer = peer; + bitcoin_txid(tx, &otx->txid); + otx->hextx = tal_hexstr(otx, rawtx, tal_count(rawtx)); + tal_free(rawtx); log_add_struct(peer->log, " (tx %s)", struct sha256_double, &otx->txid); - rawtx = linearize_tx(txs, otx->tx); - txs[0] = tal_hexstr(txs, rawtx, tal_count(rawtx)); - bitcoind_sendrawtx(peer, peer->dstate, txs[0], try_broadcast, txs); + bitcoind_sendrawtx(peer, peer->dstate, otx->hextx, broadcast_done, otx); } static void free_blocks(struct lightningd_state *dstate, struct block *b) diff --git a/daemon/chaintopology.h b/daemon/chaintopology.h index ac9e9f154..613ae9fc5 100644 --- a/daemon/chaintopology.h +++ b/daemon/chaintopology.h @@ -38,7 +38,7 @@ u32 get_block_height(struct lightningd_state *dstate); /* Get fee rate. */ u64 get_feerate(struct lightningd_state *dstate); -/* Broadcast a single tx, and rebroadcast as reqd (takes ownership of tx) */ +/* Broadcast a single tx, and rebroadcast as reqd (copies tx). */ void broadcast_tx(struct peer *peer, const struct bitcoin_tx *tx); void setup_topology(struct lightningd_state *dstate); diff --git a/daemon/peer.h b/daemon/peer.h index 344ccd239..1e612173b 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -79,7 +79,8 @@ struct peer_visible_state { /* Off peer->outgoing_txs */ struct outgoing_tx { struct list_node list; - const struct bitcoin_tx *tx; + struct peer *peer; + const char *hextx; struct sha256_double txid; };