closingd: use global tmpctx.

This one is a bit more subtle than others: we can't just free it
inside the read_peer_msg() loop since we hand tmpctx in, so open-code
the two callers and it's clearer.

They're inside the only loops closingd has.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-03-15 15:00:41 +10:30
parent f2adb76d04
commit 40fe5b7bac
1 changed files with 15 additions and 29 deletions

View File

@ -77,22 +77,6 @@ static struct bitcoin_tx *close_tx(const tal_t *ctx,
return tx;
}
/* Handle random messages we might get, returning the first non-handled one. */
static u8 *closing_read_peer_msg(const tal_t *ctx,
struct crypto_state *cs,
u64 gossip_index,
const struct channel_id *channel)
{
u8 *msg;
while ((msg = read_peer_msg(ctx, cs, gossip_index, channel,
sync_crypto_write_arg,
status_fail_io,
NULL)) == NULL);
return msg;
}
static void do_reconnect(struct crypto_state *cs,
u64 gossip_index,
const struct channel_id *channel_id,
@ -101,7 +85,6 @@ static void do_reconnect(struct crypto_state *cs,
{
u8 *msg;
struct channel_id their_channel_id;
const tal_t *tmpctx = tal_tmpctx(NULL);
u64 next_local_commitment_number, next_remote_revocation_number;
/* BOLT #2:
@ -122,7 +105,11 @@ static void do_reconnect(struct crypto_state *cs,
peer_failed_connection_lost();
/* Wait for them to say something interesting */
msg = closing_read_peer_msg(tmpctx, cs, gossip_index, channel_id);
while ((msg = read_peer_msg(tmpctx, cs, gossip_index, channel_id,
sync_crypto_write_arg,
status_fail_io,
NULL)) == NULL)
clean_tmpctx();
if (!fromwire_channel_reestablish(msg, &their_channel_id,
&next_local_commitment_number,
@ -148,7 +135,6 @@ static void do_reconnect(struct crypto_state *cs,
/* Since we always transmit closing_signed immediately, if
* we're reconnecting we consider ourselves to have transmitted once,
* and we'll immediately do the retransmit now anyway. */
tal_free(tmpctx);
}
static void send_offer(struct crypto_state *cs,
@ -166,7 +152,6 @@ static void send_offer(struct crypto_state *cs,
const struct secrets *secrets,
uint64_t fee_to_offer)
{
const tal_t *tmpctx = tal_tmpctx(NULL);
struct bitcoin_tx *tx;
secp256k1_ecdsa_signature our_sig;
u8 *msg;
@ -203,8 +188,6 @@ static void send_offer(struct crypto_state *cs,
msg = towire_closing_signed(tmpctx, channel_id, fee_to_offer, &our_sig);
if (!sync_crypto_write(cs, PEER_FD, take(msg)))
peer_failed_connection_lost();
tal_free(tmpctx);
}
static void tell_master_their_offer(const secp256k1_ecdsa_signature *their_sig,
@ -238,7 +221,6 @@ static uint64_t receive_offer(struct crypto_state *cs,
uint64_t our_dust_limit,
u64 min_fee_to_accept)
{
const tal_t *tmpctx = tal_tmpctx(NULL);
u8 *msg;
struct channel_id their_channel_id;
u64 received_fee;
@ -247,7 +229,12 @@ static uint64_t receive_offer(struct crypto_state *cs,
/* Wait for them to say something interesting */
do {
msg = closing_read_peer_msg(tmpctx, cs, gossip_index, channel_id);
clean_tmpctx();
msg = read_peer_msg(tmpctx, cs, gossip_index, channel_id,
sync_crypto_write_arg,
status_fail_io,
NULL);
/* BOLT #2:
*
@ -256,14 +243,14 @@ static uint64_t receive_offer(struct crypto_state *cs,
*/
/* This should only happen if we've made no commitments, but
* we don't have to check that: it's their problem. */
if (fromwire_peektype(msg) == WIRE_FUNDING_LOCKED)
if (msg && fromwire_peektype(msg) == WIRE_FUNDING_LOCKED)
msg = tal_free(msg);
/* BOLT #2:
*
* ...if the node has sent a previous `shutdown` it MUST
* retransmit it.
*/
else if (fromwire_peektype(msg) == WIRE_SHUTDOWN)
else if (msg && fromwire_peektype(msg) == WIRE_SHUTDOWN)
msg = tal_free(msg);
} while (!msg);
@ -339,7 +326,6 @@ static uint64_t receive_offer(struct crypto_state *cs,
tell_master_their_offer(&their_sig, tx);
}
tal_free(tmpctx);
return received_fee;
}
@ -437,7 +423,7 @@ static u64 adjust_offer(struct crypto_state *cs,
int main(int argc, char *argv[])
{
struct crypto_state cs;
const tal_t *ctx = tal_tmpctx(NULL);
const tal_t *ctx = tal(NULL, char);
u8 *msg;
struct privkey seed;
struct pubkey funding_pubkey[NUM_SIDES];
@ -461,7 +447,7 @@ int main(int argc, char *argv[])
status_setup_sync(REQ_FD);
msg = wire_sync_read(ctx, REQ_FD);
msg = wire_sync_read(tmpctx, REQ_FD);
if (!fromwire_closing_init(ctx, msg,
&cs, &gossip_index, &seed,
&funding_txid, &funding_txout,