From 188d0b76a4b6c691b79f7a5676ef3360e10feb2f Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 25 Sep 2015 11:51:19 +0930 Subject: [PATCH] state: use an enum for the completion. Instead of overloading complete_data. Signed-off-by: Rusty Russell --- state.c | 30 +++++++++++++++++++----------- state.h | 14 +++++++++----- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/state.c b/state.c index a10dad1a1..26f493fad 100644 --- a/state.c +++ b/state.c @@ -1,8 +1,6 @@ #include #include -char cmd_requeue; - static inline bool high_priority(enum state state) { return (state & 1) == (STATE_NORMAL_HIGHPRIO & 1); @@ -20,7 +18,8 @@ static inline bool high_priority(enum state state) #define INIT_EFFECT_unwatch NULL #define INIT_EFFECT_defer INPUT_NONE #define INIT_EFFECT_complete INPUT_NONE -#define INIT_EFFECT_complete_data NULL +#define INIT_EFFECT_status CMD_STATUS_ONGOING +#define INIT_EFFECT_faildata NULL #define INIT_EFFECT_stop_packets false #define INIT_EFFECT_stop_commands false #define INIT_EFFECT_close_timeout INPUT_NONE @@ -43,7 +42,8 @@ void state_effect_init(struct state_effect *effect) effect->unwatch = INIT_EFFECT_unwatch; effect->defer = INIT_EFFECT_defer; effect->complete = INIT_EFFECT_complete; - effect->complete_data = INIT_EFFECT_complete_data; + effect->status = INIT_EFFECT_status; + effect->faildata = INIT_EFFECT_faildata; effect->stop_packets = INIT_EFFECT_stop_packets; effect->stop_commands = INIT_EFFECT_stop_commands; effect->close_timeout = INIT_EFFECT_close_timeout; @@ -72,15 +72,23 @@ static void fail_cmd(struct state_effect *effect, void *faildata) { set_effect(effect, complete, input); - /* Use dummy value if they don't want one. */ - set_effect(effect, complete_data, faildata ? faildata : effect); + set_effect(effect, status, CMD_STATUS_FAILED); + if (faildata) + set_effect(effect, faildata, faildata); } static void requeue_cmd(struct state_effect *effect, const enum state_input input) { set_effect(effect, complete, input); - set_effect(effect, complete_data, &cmd_requeue); + set_effect(effect, status, CMD_STATUS_REQUEUE); +} + +static void complete_cmd(struct state_effect *effect, + const enum state_input input) +{ + set_effect(effect, complete, input); + set_effect(effect, status, CMD_STATUS_SUCCESS); } enum state state(const enum state state, const struct state_data *sdata, @@ -473,7 +481,7 @@ enum state state(const enum state state, const struct state_data *sdata, idata->pkt); if (err) goto err_start_unilateral_close; - set_effect(effect, complete, CMD_SEND_UPDATE_ANY); + complete_cmd(effect, CMD_SEND_UPDATE_ANY); return toggle_prio(state, STATE_NORMAL); } else if (input_is(input, BITCOIN_ANCHOR_UNSPENT)) { fail_cmd(effect, CMD_SEND_UPDATE_ANY, NULL); @@ -533,7 +541,7 @@ enum state state(const enum state state, const struct state_data *sdata, idata->pkt); if (err) goto err_start_unilateral_close; - set_effect(effect, complete, CMD_CLOSE); + complete_cmd(effect, CMD_CLOSE); set_effect(effect, send, pkt_close_ack(effect, sdata)); set_effect(effect, broadcast, bitcoin_close(effect, sdata)); @@ -546,7 +554,7 @@ enum state state(const enum state state, const struct state_data *sdata, idata->pkt); if (err) goto err_start_unilateral_close; - set_effect(effect, complete, CMD_CLOSE); + complete_cmd(effect, CMD_CLOSE); set_effect(effect, send, pkt_close_ack(effect, sdata)); set_effect(effect, broadcast, bitcoin_close(effect, sdata)); @@ -1027,7 +1035,7 @@ instant_close: * Closing, but we haven't sent anything to the blockchain so * there's nothing to clean up. */ - set_effect(effect, complete, CMD_CLOSE); + complete_cmd(effect, CMD_CLOSE); /* FIXME: Should we tell other side we're going? */ set_effect(effect, stop_packets, true); set_effect(effect, stop_commands, true); diff --git a/state.h b/state.h index 7f4ad54db..8b6f3c061 100644 --- a/state.h +++ b/state.h @@ -4,14 +4,19 @@ #include #include +enum cmd_complete_status { + CMD_STATUS_ONGOING, + CMD_STATUS_FAILED, + CMD_STATUS_SUCCESS, + CMD_STATUS_REQUEUE +}; + /* * This is the core state machine. * * Calling the state machine with an input simply returns the new state, * and populates the "effect" struct with what it wants done. */ -extern char cmd_requeue; - struct state_effect { /* Transaction to broadcast. */ struct bitcoin_tx *broadcast; @@ -30,9 +35,8 @@ struct state_effect { /* Complete a command. */ enum state_input complete; - /* NULL on success, &cmd_requeue on requeue, otherwise - * command-specific fail information. */ - void *complete_data; + enum cmd_complete_status status; + void *faildata; /* Stop taking packets? commands? */ bool stop_packets, stop_commands;