Skip to content

Commit

Permalink
openingd: remove #if DEVELOPER in favor of runtime flag.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Sep 21, 2023
1 parent dba4af3 commit 01119fb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 57 deletions.
13 changes: 7 additions & 6 deletions openingd/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,16 @@ bool check_config_bounds(const tal_t *ctx,
}

u8 *no_upfront_shutdown_script(const tal_t *ctx,
bool developer,
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
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));
}

/* BOLT #2:
*
Expand Down
1 change: 1 addition & 0 deletions openingd/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ bool anchors_negotiated(struct feature_set *our_features,
const u8 *their_features);

u8 *no_upfront_shutdown_script(const tal_t *ctx,
bool developer,
struct feature_set *our_features,
const u8 *their_features);

Expand Down
38 changes: 15 additions & 23 deletions openingd/dualopend.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ static struct tx_state *new_tx_state(const tal_t *ctx)
struct state {
struct per_peer_state *pps;

/* --developer? */
bool developer;

/* Features they offered */
u8 *their_features;

Expand Down Expand Up @@ -957,15 +960,8 @@ static void set_remote_upfront_shutdown(struct state *state,
peer_failed_err(state->pps, &state->channel_id, "%s", err);
}

/* Memory leak detection is DEVELOPER-only because we go to great lengths to
* record the backtrace when allocations occur: without that, the leak
* detection tends to be useless for diagnosing where the leak came from, but
* it has significant overhead.
*
* FIXME: dualopend doesn't always listen to lightningd, so we call this
* at closing time, rather than when it askes.
*/
#if DEVELOPER
/* FIXME: dualopend doesn't always listen to lightningd, so we call this
* at closing time, rather than when it asks. */
static void handle_dev_memleak(struct state *state, const u8 *msg)
{
struct htable *memtable;
Expand All @@ -985,7 +981,6 @@ static void handle_dev_memleak(struct state *state, const u8 *msg)
take(towire_dualopend_dev_memleak_reply(NULL,
found_leak)));
}
#endif /* DEVELOPER */

static u8 *psbt_to_tx_sigs_msg(const tal_t *ctx,
struct state *state,
Expand Down Expand Up @@ -1146,12 +1141,10 @@ fetch_psbt_changes(struct state *state,
wire_sync_write(REQ_FD, take(msg));

msg = wire_sync_read(tmpctx, REQ_FD);
#if DEVELOPER
while (fromwire_dualopend_dev_memleak(msg)) {
while (state->developer && fromwire_dualopend_dev_memleak(msg)) {
handle_dev_memleak(state, msg);
msg = wire_sync_read(tmpctx, REQ_FD);
}
#endif

if (fromwire_dualopend_fail(msg, msg, &err)) {
open_abort(state, "%s", err);
Expand Down Expand Up @@ -2466,7 +2459,7 @@ static void accepter_start(struct state *state, const u8 *oc2_msg)
struct tlv_accept_tlvs *a_tlv = tlv_accept_tlvs_new(tmpctx);
if (!state->upfront_shutdown_script[LOCAL])
state->upfront_shutdown_script[LOCAL]
= no_upfront_shutdown_script(state,
= no_upfront_shutdown_script(state, state->developer,
state->our_features,
state->their_features);

Expand Down Expand Up @@ -2942,7 +2935,7 @@ static void opener_start(struct state *state, u8 *msg)

if (!state->upfront_shutdown_script[LOCAL])
state->upfront_shutdown_script[LOCAL]
= no_upfront_shutdown_script(state,
= no_upfront_shutdown_script(state, state->developer,
state->our_features,
state->their_features);

Expand Down Expand Up @@ -3389,12 +3382,10 @@ static void rbf_wrap_up(struct state *state,
wire_sync_write(REQ_FD, take(msg));
msg = wire_sync_read(tmpctx, REQ_FD);

#if DEVELOPER
while (fromwire_dualopend_dev_memleak(msg)) {
while (state->developer && fromwire_dualopend_dev_memleak(msg)) {
handle_dev_memleak(state, msg);
msg = wire_sync_read(tmpctx, REQ_FD);
}
#endif

if ((msg_type = fromwire_peektype(msg)) == WIRE_DUALOPEND_FAIL) {
if (!fromwire_dualopend_fail(msg, msg, &err_reason))
Expand Down Expand Up @@ -4034,10 +4025,11 @@ static u8 *handle_master_in(struct state *state)

switch (t) {
case WIRE_DUALOPEND_DEV_MEMLEAK:
#if DEVELOPER
handle_dev_memleak(state, msg);
#endif
return NULL;
if (state->developer) {
handle_dev_memleak(state, msg);
return NULL;
}
break;
case WIRE_DUALOPEND_OPENER_INIT:
opener_start(state, msg);
return NULL;
Expand Down Expand Up @@ -4230,7 +4222,7 @@ int main(int argc, char *argv[])
struct amount_msat our_msat;
bool from_abort;

subdaemon_setup(argc, argv);
state->developer = subdaemon_setup(argc, argv);

/* Init the holder for the funding transaction attempt */
state->tx_state = new_tx_state(state);
Expand Down
46 changes: 18 additions & 28 deletions openingd/openingd.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@
#define REQ_FD STDIN_FILENO
#define HSM_FD 4

#if DEVELOPER
/* If --dev-force-tmp-channel-id is set, it ends up here */
static struct channel_id *dev_force_tmp_channel_id;
#endif /* DEVELOPER */

/* Global state structure. This is only for the one specific peer and channel */
struct state {
struct per_peer_state *pps;

/* --developer? */
bool developer;

/* If --dev-force-tmp-channel-id is set, it ends up here */
struct channel_id *dev_force_tmp_channel_id;

/* Features they offered */
u8 *their_features;

Expand Down Expand Up @@ -227,11 +228,10 @@ static u8 *opening_negotiate_msg(const tal_t *ctx, struct state *state,

static bool setup_channel_funder(struct state *state)
{
#if DEVELOPER
/* --dev-force-tmp-channel-id specified */
if (dev_force_tmp_channel_id)
state->channel_id = *dev_force_tmp_channel_id;
#endif
if (state->dev_force_tmp_channel_id)
state->channel_id = *state->dev_force_tmp_channel_id;

/* BOLT #2:
*
* The sending node:
Expand Down Expand Up @@ -326,7 +326,7 @@ static u8 *funder_channel_start(struct state *state, u8 channel_flags,

if (!state->upfront_shutdown_script[LOCAL])
state->upfront_shutdown_script[LOCAL]
= no_upfront_shutdown_script(state,
= no_upfront_shutdown_script(state, state->developer,
state->our_features,
state->their_features);

Expand Down Expand Up @@ -1120,7 +1120,7 @@ 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,
= no_upfront_shutdown_script(state, state->developer,
state->our_features,
state->their_features);

Expand Down Expand Up @@ -1405,11 +1405,6 @@ static u8 *handle_peer_in(struct state *state)
peer_failed_connection_lost();
}

/* Memory leak detection is DEVELOPER-only because we go to great lengths to
* record the backtrace when allocations occur: without that, the leak
* detection tends to be useless for diagnosing where the leak came from, but
* it has significant overhead. */
#if DEVELOPER
static void handle_dev_memleak(struct state *state, const u8 *msg)
{
struct htable *memtable;
Expand All @@ -1429,7 +1424,6 @@ static void handle_dev_memleak(struct state *state, const u8 *msg)
take(towire_openingd_dev_memleak_reply(NULL,
found_leak)));
}
#endif /* DEVELOPER */

/* Standard lightningd-fd-is-ready-to-read demux code. Again, we could hang
* here, but if we can't trust our parent, who can we trust? */
Expand Down Expand Up @@ -1480,10 +1474,11 @@ static u8 *handle_master_in(struct state *state)
negotiation_aborted(state, "Channel open canceled by RPC");
return NULL;
case WIRE_OPENINGD_DEV_MEMLEAK:
#if DEVELOPER
handle_dev_memleak(state, msg);
return NULL;
#endif
if (state->developer) {
handle_dev_memleak(state, msg);
return NULL;
}
/* fall thru */
case WIRE_OPENINGD_DEV_MEMLEAK_REPLY:
case WIRE_OPENINGD_INIT:
case WIRE_OPENINGD_FUNDER_REPLY:
Expand All @@ -1507,9 +1502,8 @@ int main(int argc, char *argv[])
struct pollfd pollfd[2];
struct state *state = tal(NULL, struct state);
struct secret *none;
struct channel_id *force_tmp_channel_id;

subdaemon_setup(argc, argv);
state->developer = subdaemon_setup(argc, argv);

/*~ This makes status_failed, status_debug etc work synchronously by
* writing to REQ_FD */
Expand All @@ -1528,14 +1522,10 @@ int main(int argc, char *argv[])
&state->our_funding_pubkey,
&state->minimum_depth,
&state->min_feerate, &state->max_feerate,
&force_tmp_channel_id,
&state->dev_force_tmp_channel_id,
&state->allowdustreserve))
master_badmsg(WIRE_OPENINGD_INIT, msg);

#if DEVELOPER
dev_force_tmp_channel_id = force_tmp_channel_id;
#endif

/* 3 == peer, 4 = hsmd */
state->pps = new_per_peer_state(state);
per_peer_state_set_fd(state->pps, 3);
Expand Down

0 comments on commit 01119fb

Please sign in to comment.