Browse Source

state: return tx to be queued directly.

Instead of effect->broadcast_tx.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
1a20e8094f
  1. 41
      state.c
  2. 6
      state.h
  3. 8
      test/test_state_coverage.c

41
state.c

@ -91,11 +91,20 @@ static void queue_pkt(Pkt **out, Pkt *pkt)
*out = pkt; *out = pkt;
} }
static void queue_tx_broadcast(struct bitcoin_tx **broadcast,
struct bitcoin_tx *tx)
{
assert(!*broadcast);
assert(tx);
*broadcast = tx;
}
enum command_status state(const tal_t *ctx, enum command_status state(const tal_t *ctx,
struct peer *peer, struct peer *peer,
const enum state_input input, const enum state_input input,
const union input *idata, const union input *idata,
Pkt **out, Pkt **out,
struct bitcoin_tx **broadcast,
struct state_effect **effect) struct state_effect **effect)
{ {
Pkt *decline; Pkt *decline;
@ -105,6 +114,7 @@ enum command_status state(const tal_t *ctx,
enum command_status cstatus = CMD_NONE; enum command_status cstatus = CMD_NONE;
*out = NULL; *out = NULL;
*broadcast = NULL;
/* NULL-terminated linked list. */ /* NULL-terminated linked list. */
*effect = NULL; *effect = NULL;
@ -199,8 +209,7 @@ enum command_status state(const tal_t *ctx,
complete_cmd(peer, &cstatus, CMD_FAIL); complete_cmd(peer, &cstatus, CMD_FAIL);
goto err_start_unilateral_close; goto err_start_unilateral_close;
} }
add_effect(effect, broadcast_tx, queue_tx_broadcast(broadcast, bitcoin_anchor(ctx, peer));
bitcoin_anchor(ctx, peer));
add_effect(effect, watch, add_effect(effect, watch,
bitcoin_watch_anchor(ctx, peer, bitcoin_watch_anchor(ctx, peer,
BITCOIN_ANCHOR_DEPTHOK, BITCOIN_ANCHOR_DEPTHOK,
@ -617,8 +626,7 @@ enum command_status state(const tal_t *ctx,
goto err_start_unilateral_close_already_closing; goto err_start_unilateral_close_already_closing;
queue_pkt(out, queue_pkt(out,
pkt_close_ack(ctx, peer)); pkt_close_ack(ctx, peer));
add_effect(effect, broadcast_tx, queue_tx_broadcast(broadcast, bitcoin_close(ctx, peer));
bitcoin_close(ctx, peer));
change_peer_cond(peer, PEER_CLOSING, PEER_CLOSED); change_peer_cond(peer, PEER_CLOSING, PEER_CLOSED);
return next_state(peer, cstatus, STATE_CLOSE_WAIT_CLOSE); return next_state(peer, cstatus, STATE_CLOSE_WAIT_CLOSE);
} else if (input_is(input, PKT_CLOSE)) { } else if (input_is(input, PKT_CLOSE)) {
@ -630,8 +638,7 @@ enum command_status state(const tal_t *ctx,
goto err_start_unilateral_close_already_closing; goto err_start_unilateral_close_already_closing;
queue_pkt(out, queue_pkt(out,
pkt_close_ack(ctx, peer)); pkt_close_ack(ctx, peer));
add_effect(effect, broadcast_tx, queue_tx_broadcast(broadcast, bitcoin_close(ctx, peer));
bitcoin_close(ctx, peer));
set_peer_cond(peer, PEER_CLOSED); set_peer_cond(peer, PEER_CLOSED);
return next_state(peer, cstatus, STATE_CLOSE_WAIT_CLOSE); return next_state(peer, cstatus, STATE_CLOSE_WAIT_CLOSE);
} else if (input_is(input, PKT_ERROR)) { } else if (input_is(input, PKT_ERROR)) {
@ -756,7 +763,7 @@ enum command_status state(const tal_t *ctx,
& STATE_CLOSE_OURCOMMIT_BIT)); & STATE_CLOSE_OURCOMMIT_BIT));
tx = bitcoin_spend_ours(ctx, peer); tx = bitcoin_spend_ours(ctx, peer);
/* Now we need to wait for our commit to be done. */ /* Now we need to wait for our commit to be done. */
add_effect(effect, broadcast_tx, tx); queue_tx_broadcast(broadcast, tx);
add_effect(effect, watch, add_effect(effect, watch,
bitcoin_watch(ctx, tx, bitcoin_watch(ctx, tx,
BITCOIN_SPEND_OURS_DONE)); BITCOIN_SPEND_OURS_DONE));
@ -793,7 +800,7 @@ enum command_status state(const tal_t *ctx,
peer, peer,
idata->htlc); idata->htlc);
/* HTLC timed out, spend it back to us. */ /* HTLC timed out, spend it back to us. */
add_effect(effect, broadcast_tx, tx); queue_tx_broadcast(broadcast, tx);
/* Don't unwatch yet; they could yet /* Don't unwatch yet; they could yet
* try to spend, revealing rvalue. */ * try to spend, revealing rvalue. */
@ -809,7 +816,7 @@ enum command_status state(const tal_t *ctx,
idata->htlc); idata->htlc);
/* Spend it... */ /* Spend it... */
add_effect(effect, broadcast_tx, tx); queue_tx_broadcast(broadcast, tx);
/* We're done when it gets buried. */ /* We're done when it gets buried. */
add_effect(effect, watch_htlc_spend, add_effect(effect, watch_htlc_spend,
htlc_spend_watch(ctx, htlc_spend_watch(ctx,
@ -863,7 +870,7 @@ enum command_status state(const tal_t *ctx,
*/ */
if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) { if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) {
tx = bitcoin_spend_theirs(ctx, peer, idata->btc); tx = bitcoin_spend_theirs(ctx, peer, idata->btc);
add_effect(effect, broadcast_tx, tx); queue_tx_broadcast(broadcast, tx);
add_effect(effect, watch, add_effect(effect, watch,
bitcoin_watch(ctx, tx, bitcoin_watch(ctx, tx,
BITCOIN_SPEND_THEIRS_DONE)); BITCOIN_SPEND_THEIRS_DONE));
@ -885,7 +892,7 @@ enum command_status state(const tal_t *ctx,
if (!tx) if (!tx)
return next_state(peer, cstatus, return next_state(peer, cstatus,
STATE_ERR_INFORMATION_LEAK); STATE_ERR_INFORMATION_LEAK);
add_effect(effect, broadcast_tx, tx); queue_tx_broadcast(broadcast, tx);
add_effect(effect, watch, add_effect(effect, watch,
bitcoin_watch(ctx, tx, bitcoin_watch(ctx, tx,
BITCOIN_STEAL_DONE)); BITCOIN_STEAL_DONE));
@ -973,7 +980,7 @@ start_unilateral_close:
/* No more inputs, no more commands. */ /* No more inputs, no more commands. */
set_peer_cond(peer, PEER_CLOSED); set_peer_cond(peer, PEER_CLOSED);
tx = bitcoin_commit(ctx, peer); tx = bitcoin_commit(ctx, peer);
add_effect(effect, broadcast_tx, tx); queue_tx_broadcast(broadcast, tx);
add_effect(effect, watch, add_effect(effect, watch,
bitcoin_watch_delayed(ctx, tx, bitcoin_watch_delayed(ctx, tx,
BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED)); BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED));
@ -1003,7 +1010,7 @@ start_unilateral_close_already_closing:
/* No more inputs, no more commands. */ /* No more inputs, no more commands. */
set_peer_cond(peer, PEER_CLOSED); set_peer_cond(peer, PEER_CLOSED);
tx = bitcoin_commit(ctx, peer); tx = bitcoin_commit(ctx, peer);
add_effect(effect, broadcast_tx, tx); queue_tx_broadcast(broadcast, tx);
add_effect(effect, watch, add_effect(effect, watch,
bitcoin_watch_delayed(ctx, tx, bitcoin_watch_delayed(ctx, tx,
BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED)); BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED));
@ -1025,7 +1032,7 @@ them_unilateral:
/* No more inputs, no more commands. */ /* No more inputs, no more commands. */
set_peer_cond(peer, PEER_CLOSED); set_peer_cond(peer, PEER_CLOSED);
tx = bitcoin_spend_theirs(ctx, peer, idata->btc); tx = bitcoin_spend_theirs(ctx, peer, idata->btc);
add_effect(effect, broadcast_tx, tx); queue_tx_broadcast(broadcast, tx);
add_effect(effect, watch, add_effect(effect, watch,
bitcoin_watch(ctx, tx, bitcoin_watch(ctx, tx,
BITCOIN_SPEND_THEIRS_DONE)); BITCOIN_SPEND_THEIRS_DONE));
@ -1143,7 +1150,7 @@ fail_during_close:
} else if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) { } else if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) {
/* A reorganization could make this happen. */ /* A reorganization could make this happen. */
tx = bitcoin_spend_theirs(ctx, peer, idata->btc); tx = bitcoin_spend_theirs(ctx, peer, idata->btc);
add_effect(effect, broadcast_tx, tx); queue_tx_broadcast(broadcast, tx);
add_effect(effect, watch, add_effect(effect, watch,
bitcoin_watch(ctx, tx, bitcoin_watch(ctx, tx,
BITCOIN_SPEND_THEIRS_DONE)); BITCOIN_SPEND_THEIRS_DONE));
@ -1166,7 +1173,7 @@ fail_during_close:
if (!tx) if (!tx)
return next_state(peer, cstatus, return next_state(peer, cstatus,
STATE_ERR_INFORMATION_LEAK); STATE_ERR_INFORMATION_LEAK);
add_effect(effect, broadcast_tx, tx); queue_tx_broadcast(broadcast, tx);
add_effect(effect, watch, add_effect(effect, watch,
bitcoin_watch(ctx, tx, BITCOIN_STEAL_DONE)); bitcoin_watch(ctx, tx, BITCOIN_STEAL_DONE));
/* Expect either close or steal to complete */ /* Expect either close or steal to complete */
@ -1191,7 +1198,7 @@ old_commit_spotted:
if (!tx) if (!tx)
return next_state(peer, cstatus, return next_state(peer, cstatus,
STATE_ERR_INFORMATION_LEAK); STATE_ERR_INFORMATION_LEAK);
add_effect(effect, broadcast_tx, tx); queue_tx_broadcast(broadcast, tx);
add_effect(effect, watch, add_effect(effect, watch,
bitcoin_watch(ctx, tx, BITCOIN_STEAL_DONE)); bitcoin_watch(ctx, tx, BITCOIN_STEAL_DONE));
return next_state(peer, cstatus, STATE_CLOSE_WAIT_STEAL); return next_state(peer, cstatus, STATE_CLOSE_WAIT_STEAL);

6
state.h

@ -7,7 +7,6 @@
#include <stdbool.h> #include <stdbool.h>
enum state_effect_type { enum state_effect_type {
STATE_EFFECT_broadcast_tx,
STATE_EFFECT_watch, STATE_EFFECT_watch,
STATE_EFFECT_unwatch, STATE_EFFECT_unwatch,
/* FIXME: Use a watch for this?. */ /* FIXME: Use a watch for this?. */
@ -30,9 +29,6 @@ struct state_effect {
enum state_effect_type etype; enum state_effect_type etype;
union { union {
/* Transaction to broadcast. */
struct bitcoin_tx *broadcast_tx;
/* Event to watch for. */ /* Event to watch for. */
struct watch *watch; struct watch *watch;
@ -62,6 +58,7 @@ static inline bool state_is_error(enum state s)
} }
struct peer; struct peer;
struct bitcoin_tx;
static inline bool input_is_pkt(enum state_input input) static inline bool input_is_pkt(enum state_input input)
{ {
@ -81,6 +78,7 @@ enum command_status state(const tal_t *ctx,
const enum state_input input, const enum state_input input,
const union input *idata, const union input *idata,
Pkt **out, Pkt **out,
struct bitcoin_tx **broadcast,
struct state_effect **effect); struct state_effect **effect);
/* Any CMD_SEND_HTLC_* */ /* Any CMD_SEND_HTLC_* */

8
test/test_state_coverage.c

@ -1494,8 +1494,6 @@ static const char *apply_effects(struct peer *peer,
*effects |= (1ULL << effect->etype); *effects |= (1ULL << effect->etype);
switch (effect->etype) { switch (effect->etype) {
case STATE_EFFECT_broadcast_tx:
break;
case STATE_EFFECT_watch: case STATE_EFFECT_watch:
/* We can have multiple steals or spendtheirs /* We can have multiple steals or spendtheirs
in flight, so make exceptions for in flight, so make exceptions for
@ -1925,6 +1923,7 @@ static void try_input(const struct peer *peer,
struct state_effect *effect; struct state_effect *effect;
const char *problem; const char *problem;
Pkt *output; Pkt *output;
struct bitcoin_tx *broadcast;
const tal_t *ctx = tal(NULL, char); const tal_t *ctx = tal(NULL, char);
enum command_status cstatus; enum command_status cstatus;
@ -1936,7 +1935,7 @@ static void try_input(const struct peer *peer,
copy.trail = &t; copy.trail = &t;
eliminate_input(&hist->inputs_per_state[copy.state], i); eliminate_input(&hist->inputs_per_state[copy.state], i);
cstatus = state(ctx, &copy, i, idata, &output, &effect); cstatus = state(ctx, &copy, i, idata, &output, &broadcast, &effect);
normalpath &= normal_path(i, peer->state, copy.state); normalpath &= normal_path(i, peer->state, copy.state);
errorpath |= error_path(i, peer->state, copy.state); errorpath |= error_path(i, peer->state, copy.state);
@ -2350,11 +2349,12 @@ static enum state_input **map_inputs(void)
if (!state_is_error(i)) { if (!state_is_error(i)) {
struct peer dummy; struct peer dummy;
struct state_effect *effect; struct state_effect *effect;
struct bitcoin_tx *dummy_tx;
Pkt *dummy_pkt; Pkt *dummy_pkt;
memset(&dummy, 0, sizeof(dummy)); memset(&dummy, 0, sizeof(dummy));
dummy.state = i; dummy.state = i;
state(ctx, &dummy, INPUT_NONE, NULL, &dummy_pkt, state(ctx, &dummy, INPUT_NONE, NULL, &dummy_pkt,
&effect); &dummy_tx, &effect);
} }
inps[i] = mapping_inputs; inps[i] = mapping_inputs;
} }

Loading…
Cancel
Save