From 205a7057c91c2d4ffcfc690cc36807092f5699e3 Mon Sep 17 00:00:00 2001 From: niftynei Date: Tue, 15 Dec 2020 12:28:04 -0600 Subject: [PATCH] df: use dev-env flagged upfront shutdown script This lets the test_option_upfront_shutdown_script test pass --- openingd/common.c | 28 ++++++++++++++++++++++++++++ openingd/common.h | 3 +++ openingd/dualopend.c | 28 +++++++++++++++++++++------- openingd/openingd.c | 35 ++++++++--------------------------- 4 files changed, 60 insertions(+), 34 deletions(-) diff --git a/openingd/common.c b/openingd/common.c index 0aa02bcb4..dd3fd87e3 100644 --- a/openingd/common.c +++ b/openingd/common.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -194,3 +195,30 @@ bool check_config_bounds(const tal_t *ctx, return true; } + +u8 *no_upfront_shutdown_script(const tal_t *ctx, + struct feature_set *our_features, + const u8 *their_features) +{ +#if DEVELOPER + /* This is a hack, for feature testing */ + const char *e = getenv("DEV_OPENINGD_UPFRONT_SHUTDOWN_SCRIPT"); + if (e) + return tal_hexdata(ctx, e, strlen(e)); +#endif + + /* BOLT #2: + * + * - if both nodes advertised the `option_upfront_shutdown_script` + * feature: + * - MUST include `upfront_shutdown_script` with either a valid + * `shutdown_scriptpubkey` as required by `shutdown` + * `scriptpubkey`, or a zero-length `shutdown_scriptpubkey` + * (ie. `0x0000`). + */ + if (feature_negotiated(our_features, their_features, + OPT_UPFRONT_SHUTDOWN_SCRIPT)) + return tal_arr(ctx, u8, 0); + + return NULL; +} diff --git a/openingd/common.h b/openingd/common.h index 950d01932..8edbd9905 100644 --- a/openingd/common.h +++ b/openingd/common.h @@ -18,4 +18,7 @@ bool check_config_bounds(const tal_t *ctx, bool option_anchor_outputs, char **err_reason); +u8 *no_upfront_shutdown_script(const tal_t *ctx, + struct feature_set *our_features, + const u8 *their_features); #endif /* LIGHTNING_OPENINGD_COMMON_H */ diff --git a/openingd/dualopend.c b/openingd/dualopend.c index 466b97eb6..21cc049e5 100644 --- a/openingd/dualopend.c +++ b/openingd/dualopend.c @@ -1573,12 +1573,20 @@ static void accepter_start(struct state *state, const u8 *oc2_msg) /* If we have an upfront shutdown script, send it to our peer */ struct tlv_accept_tlvs *a_tlv = tlv_accept_tlvs_new(state); - if (state->upfront_shutdown_script[LOCAL]) { - a_tlv->option_upfront_shutdown_script = tal(a_tlv, - struct tlv_accept_tlvs_option_upfront_shutdown_script); - a_tlv->option_upfront_shutdown_script->shutdown_scriptpubkey = - tal_dup_arr(a_tlv, u8, state->upfront_shutdown_script[LOCAL], - tal_count(state->upfront_shutdown_script[LOCAL]), 0); + if (!state->upfront_shutdown_script[LOCAL]) + state->upfront_shutdown_script[LOCAL] + = no_upfront_shutdown_script(state, + state->our_features, + state->their_features); + + if (tal_bytelen(state->upfront_shutdown_script[LOCAL])) { + a_tlv->option_upfront_shutdown_script + = tal(a_tlv, struct tlv_accept_tlvs_option_upfront_shutdown_script); + a_tlv->option_upfront_shutdown_script->shutdown_scriptpubkey + = tal_dup_arr(a_tlv, u8, + state->upfront_shutdown_script[LOCAL], + tal_count(state->upfront_shutdown_script[LOCAL]), + 0); } msg = towire_accept_channel2(tmpctx, &state->channel_id, @@ -1868,7 +1876,13 @@ static void opener_start(struct state *state, u8 *msg) } feerate_best = state->feerate_per_kw_funding; - if (state->upfront_shutdown_script[LOCAL]) { + if (!state->upfront_shutdown_script[LOCAL]) + state->upfront_shutdown_script[LOCAL] + = no_upfront_shutdown_script(state, + state->our_features, + state->their_features); + + if (tal_bytelen(state->upfront_shutdown_script[LOCAL])) { open_tlv->option_upfront_shutdown_script = tal(open_tlv, struct tlv_opening_tlvs_option_upfront_shutdown_script); diff --git a/openingd/openingd.c b/openingd/openingd.c index 81e53c497..fc1066089 100644 --- a/openingd/openingd.c +++ b/openingd/openingd.c @@ -119,31 +119,6 @@ struct state { struct feature_set *our_features; }; -static u8 *no_upfront_shutdown_script(const tal_t *ctx, struct state *state) -{ -#if DEVELOPER - /* This is a hack, for feature testing */ - const char *e = getenv("DEV_OPENINGD_UPFRONT_SHUTDOWN_SCRIPT"); - if (e) - return tal_hexdata(ctx, e, strlen(e)); -#endif - - /* BOLT #2: - * - * - if both nodes advertised the `option_upfront_shutdown_script` - * feature: - * - MUST include `upfront_shutdown_script` with either a valid - * `shutdown_scriptpubkey` as required by `shutdown` - * `scriptpubkey`, or a zero-length `shutdown_scriptpubkey` - * (ie. `0x0000`). - */ - if (feature_negotiated(state->our_features, state->their_features, - OPT_UPFRONT_SHUTDOWN_SCRIPT)) - return tal_arr(ctx, u8, 0); - - return NULL; -} - /*~ If we can't agree on parameters, we fail to open the channel. If we're * the opener, we need to tell lightningd, otherwise it never really notices. */ static void negotiation_aborted(struct state *state, bool am_opener, @@ -371,7 +346,10 @@ static u8 *funder_channel_start(struct state *state, u8 channel_flags) return NULL; if (!state->upfront_shutdown_script[LOCAL]) - state->upfront_shutdown_script[LOCAL] = no_upfront_shutdown_script(state, state); + state->upfront_shutdown_script[LOCAL] + = no_upfront_shutdown_script(state, + state->our_features, + state->their_features); open_tlvs = tlv_open_channel_tlvs_new(tmpctx); open_tlvs->upfront_shutdown_script @@ -955,7 +933,10 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg) } if (!state->upfront_shutdown_script[LOCAL]) - state->upfront_shutdown_script[LOCAL] = no_upfront_shutdown_script(state, state); + state->upfront_shutdown_script[LOCAL] + = no_upfront_shutdown_script(state, + state->our_features, + state->their_features); /* OK, we accept! */ accept_tlvs = tlv_accept_channel_tlvs_new(tmpctx);