mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 05:12:45 +01:00
bitcoind_sendrawtx: don't share callback with retransmission case.
This is in preparation for the next step. Note that we now don't add it to the linked list of txs we've send until after it's sent by the immediate callback; this means it won't get broadcast by the timer until after it's been done by broadcast_tx. Also, this means we no longer steal the tx in broadcast_tx(); but we'll fix up the leaks 4 patches later. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
7d1137c45e
commit
4cbe9785a8
@ -282,14 +282,13 @@ static void process_sendrawtx(struct bitcoin_cli *bcli)
|
|||||||
cb(bcli->dstate, msg, bcli->cb_arg);
|
cb(bcli->dstate, msg, bcli->cb_arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void bitcoind_sendrawtx_(struct peer *peer,
|
void bitcoind_sendrawtx_(struct peer *peer,struct lightningd_state *dstate,
|
||||||
struct lightningd_state *dstate,
|
|
||||||
const char *hextx,
|
const char *hextx,
|
||||||
void (*cb)(struct lightningd_state *dstate,
|
void (*cb)(struct lightningd_state *dstate,
|
||||||
const char *msg, void *),
|
const char *msg, void *),
|
||||||
void *arg)
|
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);
|
"sendrawtransaction", hextx, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
#include <ccan/asort/asort.h>
|
#include <ccan/asort/asort.h>
|
||||||
#include <ccan/io/io.h>
|
#include <ccan/io/io.h>
|
||||||
#include <ccan/structeq/structeq.h>
|
#include <ccan/structeq/structeq.h>
|
||||||
|
#include <ccan/tal/str/str.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
|
||||||
struct block {
|
struct block {
|
||||||
@ -212,8 +213,8 @@ size_t get_tx_depth(struct lightningd_state *dstate,
|
|||||||
return topo->tip->height - b->height + 1;
|
return topo->tip->height - b->height + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void try_broadcast(struct lightningd_state *dstate,
|
static void broadcast_remainder(struct lightningd_state *dstate,
|
||||||
const char *msg, char **txs)
|
const char *msg, char **txs)
|
||||||
{
|
{
|
||||||
size_t num_txs = tal_count(txs);
|
size_t num_txs = tal_count(txs);
|
||||||
const char *this_tx;
|
const char *this_tx;
|
||||||
@ -237,7 +238,7 @@ static void try_broadcast(struct lightningd_state *dstate,
|
|||||||
this_tx = txs[num_txs-1];
|
this_tx = txs[num_txs-1];
|
||||||
tal_resize(&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
|
/* 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;
|
struct outgoing_tx *otx;
|
||||||
|
|
||||||
list_for_each(&peer->outgoing_txs, otx, list) {
|
list_for_each(&peer->outgoing_txs, otx, list) {
|
||||||
u8 *rawtx;
|
|
||||||
|
|
||||||
if (block_for_tx(dstate, &otx->txid))
|
if (block_for_tx(dstate, &otx->txid))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
tal_resize(&txs, num_txs+1);
|
tal_resize(&txs, num_txs+1);
|
||||||
rawtx = linearize_tx(txs, otx->tx);
|
txs[num_txs] = tal_strdup(txs, otx->hextx);
|
||||||
txs[num_txs] = tal_hexstr(txs, rawtx, tal_count(rawtx));
|
|
||||||
num_txs++;
|
num_txs++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (num_txs)
|
if (num_txs)
|
||||||
bitcoind_sendrawtx(NULL, dstate, txs[num_txs-1],
|
bitcoind_sendrawtx(NULL, dstate, txs[num_txs-1],
|
||||||
try_broadcast, txs);
|
broadcast_remainder, txs);
|
||||||
else
|
else
|
||||||
tal_free(txs);
|
tal_free(txs);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void destroy_outgoing_tx(struct outgoing_tx *otx)
|
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)
|
void broadcast_tx(struct peer *peer, const struct bitcoin_tx *tx)
|
||||||
{
|
{
|
||||||
struct outgoing_tx *otx = tal(peer, struct outgoing_tx);
|
struct outgoing_tx *otx = tal(peer, struct outgoing_tx);
|
||||||
char **txs = tal_arr(peer->dstate, char *, 1);
|
const u8 *rawtx = linearize_tx(otx, tx);
|
||||||
u8 *rawtx;
|
|
||||||
|
|
||||||
otx->tx = tal_steal(otx, tx);
|
otx->peer = peer;
|
||||||
bitcoin_txid(otx->tx, &otx->txid);
|
bitcoin_txid(tx, &otx->txid);
|
||||||
list_add_tail(&peer->outgoing_txs, &otx->list);
|
otx->hextx = tal_hexstr(otx, rawtx, tal_count(rawtx));
|
||||||
tal_add_destructor(otx, destroy_outgoing_tx);
|
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);
|
||||||
|
|
||||||
rawtx = linearize_tx(txs, otx->tx);
|
bitcoind_sendrawtx(peer, peer->dstate, otx->hextx, broadcast_done, otx);
|
||||||
txs[0] = tal_hexstr(txs, rawtx, tal_count(rawtx));
|
|
||||||
bitcoind_sendrawtx(peer, peer->dstate, txs[0], try_broadcast, txs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void free_blocks(struct lightningd_state *dstate, struct block *b)
|
static void free_blocks(struct lightningd_state *dstate, struct block *b)
|
||||||
|
@ -38,7 +38,7 @@ 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 (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 broadcast_tx(struct peer *peer, const struct bitcoin_tx *tx);
|
||||||
|
|
||||||
void setup_topology(struct lightningd_state *dstate);
|
void setup_topology(struct lightningd_state *dstate);
|
||||||
|
@ -79,7 +79,8 @@ struct peer_visible_state {
|
|||||||
/* Off peer->outgoing_txs */
|
/* Off peer->outgoing_txs */
|
||||||
struct outgoing_tx {
|
struct outgoing_tx {
|
||||||
struct list_node list;
|
struct list_node list;
|
||||||
const struct bitcoin_tx *tx;
|
struct peer *peer;
|
||||||
|
const char *hextx;
|
||||||
struct sha256_double txid;
|
struct sha256_double txid;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user