onchaind: helper to read and queue unwanted messages.

We only do this in one place now, but we're going to add another.

Also, make queued messages const.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2023-03-23 16:18:09 +10:30
parent e51f629e34
commit 3a61f3a350
2 changed files with 29 additions and 19 deletions

View File

@@ -71,8 +71,8 @@ static u32 reasonable_depth;
/* The messages to send at that depth. */ /* The messages to send at that depth. */
static u8 **missing_htlc_msgs; static u8 **missing_htlc_msgs;
/* The messages which were sent to us before init_reply was processed. */ /* The messages which were sent to us while waiting for a specific msg. */
static u8 **queued_msgs; static const u8 **queued_msgs;
/* Our recorded channel balance at 'chain time' */ /* Our recorded channel balance at 'chain time' */
static struct amount_msat our_msat; static struct amount_msat our_msat;
@@ -151,6 +151,20 @@ static const char *output_type_name(enum output_type output_type)
return "unknown"; return "unknown";
} }
static const u8 *queue_until_msg(const tal_t *ctx, enum onchaind_wire mtype)
{
const u8 *msg;
while ((msg = wire_sync_read(ctx, REQ_FD)) != NULL) {
if (fromwire_peektype(msg) == mtype)
return msg;
/* Process later */
tal_arr_expand(&queued_msgs, tal_steal(queued_msgs, msg));
}
status_failed(STATUS_FAIL_HSM_IO, "Waiting for %s: connection lost",
onchaind_wire_name(mtype));
}
/* helper to compare output script with our tal'd script */ /* helper to compare output script with our tal'd script */
static bool wally_tx_output_scripteq(const struct wally_tx_output *out, static bool wally_tx_output_scripteq(const struct wally_tx_output *out,
const u8 *script) const u8 *script)
@@ -2133,7 +2147,7 @@ static void wait_for_resolved(struct tracked_output **outs)
billboard_update(outs); billboard_update(outs);
while (num_not_irrevocably_resolved(outs) != 0) { while (num_not_irrevocably_resolved(outs) != 0) {
u8 *msg; const u8 *msg;
enum onchaind_wire mtype; enum onchaind_wire mtype;
if (tal_count(queued_msgs)) { if (tal_count(queued_msgs)) {
@@ -2214,7 +2228,7 @@ static int cmp_htlc_with_tells_cltv(const struct htlc_with_tells *a,
static struct htlcs_info *init_reply(const tal_t *ctx, const char *what) static struct htlcs_info *init_reply(const tal_t *ctx, const char *what)
{ {
struct htlcs_info *htlcs_info = tal(ctx, struct htlcs_info); struct htlcs_info *htlcs_info = tal(ctx, struct htlcs_info);
u8 *msg; const u8 *msg;
struct htlc_with_tells *htlcs; struct htlc_with_tells *htlcs;
/* commit_num is 0 for mutual close, but we don't care about HTLCs /* commit_num is 0 for mutual close, but we don't care about HTLCs
@@ -2226,20 +2240,13 @@ static struct htlcs_info *init_reply(const tal_t *ctx, const char *what)
peer_billboard(true, what); peer_billboard(true, what);
/* Read in htlcs */ /* Read in htlcs (ignoring everything else for now) */
for (;;) { msg = queue_until_msg(tmpctx, WIRE_ONCHAIND_HTLCS);
msg = wire_sync_read(queued_msgs, REQ_FD); if (!fromwire_onchaind_htlcs(htlcs_info, msg,
if (fromwire_onchaind_htlcs(tmpctx, msg, &htlcs_info->htlcs,
&htlcs_info->htlcs, &htlcs_info->tell_if_missing,
&htlcs_info->tell_if_missing, &htlcs_info->tell_immediately))
&htlcs_info->tell_immediately)) { master_badmsg(WIRE_ONCHAIND_HTLCS, msg);
tal_free(msg);
break;
}
/* Process later */
tal_arr_expand(&queued_msgs, msg);
}
/* One convenient structure, so we sort them together! */ /* One convenient structure, so we sort them together! */
htlcs = tal_arr(tmpctx, struct htlc_with_tells, tal_count(htlcs_info->htlcs)); htlcs = tal_arr(tmpctx, struct htlc_with_tells, tal_count(htlcs_info->htlcs));
@@ -3914,7 +3921,7 @@ int main(int argc, char *argv[])
status_setup_sync(REQ_FD); status_setup_sync(REQ_FD);
missing_htlc_msgs = tal_arr(ctx, u8 *, 0); missing_htlc_msgs = tal_arr(ctx, u8 *, 0);
queued_msgs = tal_arr(ctx, u8 *, 0); queued_msgs = tal_arr(ctx, const u8 *, 0);
msg = wire_sync_read(tmpctx, REQ_FD); msg = wire_sync_read(tmpctx, REQ_FD);
if (!fromwire_onchaind_init(tmpctx, msg, if (!fromwire_onchaind_init(tmpctx, msg,

View File

@@ -227,6 +227,9 @@ enum mvt_tag *new_tag_arr(const tal_t *ctx UNNEEDED, enum mvt_tag tag UNNEEDED)
/* Generated stub for notleak_ */ /* Generated stub for notleak_ */
void *notleak_(void *ptr UNNEEDED, bool plus_children UNNEEDED) void *notleak_(void *ptr UNNEEDED, bool plus_children UNNEEDED)
{ fprintf(stderr, "notleak_ called!\n"); abort(); } { fprintf(stderr, "notleak_ called!\n"); abort(); }
/* Generated stub for onchaind_wire_name */
const char *onchaind_wire_name(int e UNNEEDED)
{ fprintf(stderr, "onchaind_wire_name called!\n"); abort(); }
/* Generated stub for peer_billboard */ /* Generated stub for peer_billboard */
void peer_billboard(bool perm UNNEEDED, const char *fmt UNNEEDED, ...) void peer_billboard(bool perm UNNEEDED, const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "peer_billboard called!\n"); abort(); } { fprintf(stderr, "peer_billboard called!\n"); abort(); }