From e0c4865eeaa2aa6d84f7b73b0410eb3e74de9f58 Mon Sep 17 00:00:00 2001 From: niftynei Date: Thu, 22 Oct 2020 13:45:55 -0500 Subject: [PATCH] mfc: add a 'fail_destination' helper Caches state at which we failed + sets error --- plugins/spender/multifundchannel.c | 23 ++++++++++++++--------- plugins/spender/multifundchannel.h | 8 ++++++++ plugins/spender/openchannel.c | 17 +++++++---------- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/plugins/spender/multifundchannel.c b/plugins/spender/multifundchannel.c index e3bbf7886..d0ad98f9d 100644 --- a/plugins/spender/multifundchannel.c +++ b/plugins/spender/multifundchannel.c @@ -564,9 +564,7 @@ connect_err(struct command *cmd, json_tok_full_len(code_tok), json_tok_full(buf, code_tok)); - dest->state = MULTIFUNDCHANNEL_CONNECT_FAILED; - dest->error = json_strdup(mfc, buf, error); - + fail_destination(dest, take(json_strdup(NULL, buf, error))); return connect_done(dest); } @@ -1110,9 +1108,7 @@ fundchannel_start_err(struct command *cmd, completed, we can then fail. */ - dest->state = MULTIFUNDCHANNEL_START_FAILED; - dest->error = json_strdup(dest->mfc, buf, error); - + fail_destination(dest, take(json_strdup(NULL, buf, error))); return fundchannel_start_done(dest); } @@ -1408,9 +1404,7 @@ fundchannel_complete_err(struct command *cmd, json_tok_full_len(error), json_tok_full(buf, error)); - dest->state = MULTIFUNDCHANNEL_COMPLETE_FAILED; - dest->error = json_strdup(mfc, buf, error); - + fail_destination(dest, take(json_strdup(NULL, buf, error))); return fundchannel_complete_done(dest); } @@ -1706,6 +1700,17 @@ static bool dest_failed(struct multifundchannel_destination *dest) abort(); } +void fail_destination(struct multifundchannel_destination *dest, + char *error TAKES) +{ + dest->fail_state = dest->state; + dest->state = MULTIFUNDCHANNEL_FAILED; + if (taken(error)) + dest->error = tal_steal(dest->mfc, error); + else + dest->error = tal_strdup(dest->mfc, error); +} + /* Filter the failing destinations. */ static struct command_result * post_cleanup_redo_multifundchannel(struct multifundchannel_redo *redo) diff --git a/plugins/spender/multifundchannel.h b/plugins/spender/multifundchannel.h index feb6a108c..a07ce1acd 100644 --- a/plugins/spender/multifundchannel.h +++ b/plugins/spender/multifundchannel.h @@ -71,6 +71,9 @@ struct multifundchannel_destination { */ enum multifundchannel_state state; + /* Last known state before failure */ + enum multifundchannel_state fail_state; + /* the actual target script and address. */ const u8 *funding_script; const char *funding_addr; @@ -213,6 +216,11 @@ mfc_forward_error(struct command *cmd, const char *buf, const jsmntok_t *error, struct multifundchannel_command *); +/* When a destination fails, we record the furthest state + * reached, and the error message for the failure */ +void fail_destination(struct multifundchannel_destination *dest, + char *error TAKES); + /* Use this instead of command_finished. */ struct command_result * mfc_finished(struct multifundchannel_command *, struct json_stream *response); diff --git a/plugins/spender/openchannel.c b/plugins/spender/openchannel.c index 3e1622fa0..8823d51ff 100644 --- a/plugins/spender/openchannel.c +++ b/plugins/spender/openchannel.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -455,8 +456,7 @@ openchannel_signed_err(struct command *cmd, json_tok_full_len(code_tok), json_tok_full(buf, code_tok)); - dest->state = MULTIFUNDCHANNEL_FAILED; - dest->error = json_strdup(dest->mfc, buf, error); + fail_destination(dest, take(json_strdup(NULL, buf, error))); return after_openchannel_signed(mfc); } @@ -808,9 +808,7 @@ openchannel_update_err(struct command *cmd, json_tok_full_len(error), json_tok_full(buf, error)); - dest->state = MULTIFUNDCHANNEL_FAILED; - dest->error = json_strdup(mfc, buf, error); - + fail_destination(dest, take(json_strdup(NULL, buf, error))); return openchannel_update_returned(dest); } @@ -1025,8 +1023,9 @@ openchannel_init_ok(struct command *cmd, /* Port any updates onto 'parent' PSBT */ if (!update_parent_psbt(dest->mfc, dest, dest->psbt, dest->updated_psbt, &mfc->psbt)) { - dest->state = MULTIFUNDCHANNEL_FAILED; - dest->error = "Unable to update parent with node's PSBT"; + fail_destination(dest, + take(tal_fmt(NULL, "Unable to update parent" + " with node's PSBT"))); } /* Clone updated-psbt to psbt, so original changeset @@ -1069,9 +1068,7 @@ openchannel_init_err(struct command *cmd, json_tok_full_len(code_tok), json_tok_full(buf, code_tok)); - dest->state = MULTIFUNDCHANNEL_START_FAILED; - dest->error = json_strdup(dest->mfc, buf, error); - + fail_destination(dest, take(json_strdup(NULL, buf, error))); return openchannel_init_done(dest); }