Skip to content

Commit

Permalink
state: return Pkt to be queued directly.
Browse files Browse the repository at this point in the history
Instead of effect->send_pkt.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jan 21, 2016
1 parent 7383da5 commit e984df4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 87 deletions.
68 changes: 39 additions & 29 deletions state.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,18 @@ static void complete_cmd(struct peer *peer, enum command_status *statusp,
*statusp = status;
}

static void queue_pkt(Pkt **out, Pkt *pkt)
{
assert(!*out);
assert(pkt);
*out = pkt;
}

enum command_status state(const tal_t *ctx,
struct peer *peer,
const enum state_input input,
const union input *idata,
Pkt **out,
struct state_effect **effect)
{
Pkt *decline;
Expand All @@ -96,6 +104,8 @@ enum command_status state(const tal_t *ctx,
struct htlc_watch *htlcs;
enum command_status cstatus = CMD_NONE;

*out = NULL;

/* NULL-terminated linked list. */
*effect = NULL;

Expand All @@ -105,15 +115,15 @@ enum command_status state(const tal_t *ctx,
*/
case STATE_INIT:
if (input_is(input, CMD_OPEN_WITH_ANCHOR)) {
add_effect(effect, send_pkt,
queue_pkt(out,
pkt_open(ctx, peer,
OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR));
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
return next_state(peer, cstatus,
STATE_OPEN_WAIT_FOR_OPEN_WITHANCHOR);
} else if (input_is(input, CMD_OPEN_WITHOUT_ANCHOR)) {
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
add_effect(effect, send_pkt,
queue_pkt(out,
pkt_open(ctx, peer,
OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR));
return next_state(peer, cstatus,
Expand Down Expand Up @@ -143,7 +153,7 @@ enum command_status state(const tal_t *ctx,
complete_cmd(peer, &cstatus, CMD_FAIL);
goto err_close_nocleanup;
}
add_effect(effect, send_pkt, pkt_anchor(ctx, peer));
queue_pkt(out, pkt_anchor(ctx, peer));
return next_state(peer, cstatus,
STATE_OPEN_WAIT_FOR_COMMIT_SIG);
} else if (input_is(input, CMD_CLOSE)) {
Expand All @@ -161,7 +171,7 @@ enum command_status state(const tal_t *ctx,
complete_cmd(peer, &cstatus, CMD_FAIL);
goto err_close_nocleanup;
}
add_effect(effect, send_pkt,
queue_pkt(out,
pkt_open_commit_sig(ctx, peer));
add_effect(effect, watch,
bitcoin_watch_anchor(ctx, peer,
Expand Down Expand Up @@ -216,7 +226,7 @@ enum command_status state(const tal_t *ctx,
/* Fall thru */
case STATE_OPEN_WAITING_OURANCHOR_THEYCOMPLETED:
if (input_is(input, BITCOIN_ANCHOR_DEPTHOK)) {
add_effect(effect, send_pkt,
queue_pkt(out,
pkt_open_complete(ctx, peer));
if (peer->state == STATE_OPEN_WAITING_OURANCHOR_THEYCOMPLETED) {
complete_cmd(peer, &cstatus, CMD_SUCCESS);
Expand Down Expand Up @@ -274,11 +284,11 @@ enum command_status state(const tal_t *ctx,
case STATE_OPEN_WAITING_THEIRANCHOR_THEYCOMPLETED:
if (input_is(input, BITCOIN_ANCHOR_TIMEOUT)) {
/* Anchor didn't reach blockchain in reasonable time. */
add_effect(effect, send_pkt,
queue_pkt(out,
pkt_err(ctx, "Anchor timed out"));
return next_state(peer, cstatus, STATE_ERR_ANCHOR_TIMEOUT);
} else if (input_is(input, BITCOIN_ANCHOR_DEPTHOK)) {
add_effect(effect, send_pkt,
queue_pkt(out,
pkt_open_complete(ctx, peer));
if (peer->state == STATE_OPEN_WAITING_THEIRANCHOR_THEYCOMPLETED) {
complete_cmd(peer, &cstatus, CMD_SUCCESS);
Expand Down Expand Up @@ -372,31 +382,31 @@ enum command_status state(const tal_t *ctx,
assert(peer->cond == PEER_CMD_OK);
if (input_is(input, CMD_SEND_HTLC_UPDATE)) {
/* We are to send an HTLC update. */
add_effect(effect, send_pkt,
queue_pkt(out,
pkt_htlc_update(ctx, peer,
idata->htlc_prog));
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
return next_state(peer, cstatus,
prio(peer->state, STATE_WAIT_FOR_HTLC_ACCEPT));
} else if (input_is(input, CMD_SEND_HTLC_FULFILL)) {
/* We are to send an HTLC fulfill. */
add_effect(effect, send_pkt,
queue_pkt(out,
pkt_htlc_fulfill(ctx, peer,
idata->htlc_prog));
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
return next_state(peer, cstatus,
prio(peer->state, STATE_WAIT_FOR_UPDATE_ACCEPT));
} else if (input_is(input, CMD_SEND_HTLC_TIMEDOUT)) {
/* We are to send an HTLC timedout. */
add_effect(effect, send_pkt,
queue_pkt(out,
pkt_htlc_timedout(ctx, peer,
idata->htlc_prog));
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
return next_state(peer, cstatus,
prio(peer->state, STATE_WAIT_FOR_UPDATE_ACCEPT));
} else if (input_is(input, CMD_SEND_HTLC_ROUTEFAIL)) {
/* We are to send an HTLC routefail. */
add_effect(effect, send_pkt,
queue_pkt(out,
pkt_htlc_routefail(ctx, peer,
idata->htlc_prog));
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
Expand Down Expand Up @@ -492,7 +502,7 @@ enum command_status state(const tal_t *ctx,
complete_cmd(peer, &cstatus, CMD_FAIL);
goto err_start_unilateral_close;
}
add_effect(effect, send_pkt,
queue_pkt(out,
pkt_update_signature(ctx, peer));
/* HTLC is signed (though old tx not revoked yet!) */
return next_state(peer, cstatus,
Expand Down Expand Up @@ -570,7 +580,7 @@ enum command_status state(const tal_t *ctx,
peer_htlc_aborted(peer);
goto err_start_unilateral_close;
}
add_effect(effect, send_pkt,
queue_pkt(out,
pkt_update_complete(ctx, peer));

peer_htlc_done(peer);
Expand Down Expand Up @@ -605,7 +615,7 @@ enum command_status state(const tal_t *ctx,
effect);
if (err)
goto err_start_unilateral_close_already_closing;
add_effect(effect, send_pkt,
queue_pkt(out,
pkt_close_ack(ctx, peer));
add_effect(effect, broadcast_tx,
bitcoin_close(ctx, peer));
Expand All @@ -618,7 +628,7 @@ enum command_status state(const tal_t *ctx,
effect);
if (err)
goto err_start_unilateral_close_already_closing;
add_effect(effect, send_pkt,
queue_pkt(out,
pkt_close_ack(ctx, peer));
add_effect(effect, broadcast_tx,
bitcoin_close(ctx, peer));
Expand All @@ -642,15 +652,15 @@ enum command_status state(const tal_t *ctx,
err = accept_pkt_close_ack(ctx, peer, idata->pkt,
effect);
if (err)
add_effect(effect, send_pkt, err);
queue_pkt(out, err);
set_peer_cond(peer, PEER_CLOSED);
/* Just wait for close to happen now. */
return next_state(peer, cstatus, STATE_CLOSE_WAIT_CLOSE);
} else if (input_is_pkt(input)) {
peer_unexpected_pkt(peer, idata->pkt);
/* Don't reply to an error with an error. */
if (!input_is(input, PKT_ERROR)) {
add_effect(effect, send_pkt,
queue_pkt(out,
pkt_err_unexpected(ctx, idata->pkt));
}
set_peer_cond(peer, PEER_CLOSED);
Expand Down Expand Up @@ -944,7 +954,7 @@ enum command_status state(const tal_t *ctx,
* Something went wrong, but we haven't sent anything to the blockchain
* so there's nothing to clean up.
*/
add_effect(effect, send_pkt, err);
queue_pkt(out, err);

close_nocleanup:
change_peer_cond(peer, PEER_CMD_OK, PEER_CLOSED);
Expand All @@ -954,7 +964,7 @@ enum command_status state(const tal_t *ctx,
/*
* They timed out, or were broken; we are going to close unilaterally.
*/
add_effect(effect, send_pkt, err);
queue_pkt(out, err);

start_unilateral_close:
/*
Expand Down Expand Up @@ -984,7 +994,7 @@ enum command_status state(const tal_t *ctx,
/*
* They timed out, or were broken; we are going to close unilaterally.
*/
add_effect(effect, send_pkt, err);
queue_pkt(out, err);

start_unilateral_close_already_closing:
/*
Expand All @@ -1010,7 +1020,7 @@ enum command_status state(const tal_t *ctx,
/*
* Bitcoind tells us they did unilateral close.
*/
add_effect(effect, send_pkt, pkt_err(ctx, "Commit tx noticed"));
queue_pkt(out, pkt_err(ctx, "Commit tx noticed"));

/* No more inputs, no more commands. */
set_peer_cond(peer, PEER_CLOSED);
Expand Down Expand Up @@ -1038,39 +1048,39 @@ enum command_status state(const tal_t *ctx,
if (err)
goto err_start_unilateral_close;
if (decline) {
add_effect(effect, send_pkt, decline);
queue_pkt(out, decline);
peer_htlc_declined(peer, decline);
/* No update means no priority change. */
change_peer_cond(peer, PEER_BUSY, PEER_CMD_OK);
/* We may already be in STATE_NORMAL */
return next_state_nocheck(peer, cstatus,
prio(peer->state, STATE_NORMAL));
}
add_effect(effect, send_pkt, pkt_update_accept(ctx, peer));
queue_pkt(out, pkt_update_accept(ctx, peer));
return next_state(peer, cstatus,
prio(peer->state, STATE_WAIT_FOR_UPDATE_SIG));

accept_htlc_routefail:
err = accept_pkt_htlc_routefail(ctx, peer, idata->pkt, effect);
if (err)
goto err_start_unilateral_close;
add_effect(effect, send_pkt, pkt_update_accept(ctx, peer));
queue_pkt(out, pkt_update_accept(ctx, peer));
return next_state(peer, cstatus,
prio(peer->state, STATE_WAIT_FOR_UPDATE_SIG));

accept_htlc_timedout:
err = accept_pkt_htlc_timedout(ctx, peer, idata->pkt, effect);
if (err)
goto err_start_unilateral_close;
add_effect(effect, send_pkt, pkt_update_accept(ctx, peer));
queue_pkt(out, pkt_update_accept(ctx, peer));
return next_state(peer, cstatus,
prio(peer->state, STATE_WAIT_FOR_UPDATE_SIG));

accept_htlc_fulfill:
err = accept_pkt_htlc_fulfill(ctx, peer, idata->pkt);
if (err)
goto err_start_unilateral_close;
add_effect(effect, send_pkt, pkt_update_accept(ctx, peer));
queue_pkt(out, pkt_update_accept(ctx, peer));
return next_state(peer, cstatus,
prio(peer->state, STATE_WAIT_FOR_UPDATE_SIG));

Expand All @@ -1092,7 +1102,7 @@ enum command_status state(const tal_t *ctx,
set_peer_cond(peer, PEER_CLOSING);

/* As soon as we send packet, they could close. */
add_effect(effect, send_pkt, pkt_close(ctx, peer));
queue_pkt(out, pkt_close(ctx, peer));
return next_state(peer, cstatus, STATE_WAIT_FOR_CLOSE_COMPLETE);

accept_closing:
Expand All @@ -1102,7 +1112,7 @@ enum command_status state(const tal_t *ctx,
/* As soon as we send packet, they could close. */
add_effect(effect, watch,
bitcoin_watch_close(ctx, peer, BITCOIN_CLOSE_DONE));
add_effect(effect, send_pkt, pkt_close_complete(ctx, peer));
queue_pkt(out, pkt_close_complete(ctx, peer));
/* No more commands, we're already closing. */
set_peer_cond(peer, PEER_CLOSING);
return next_state(peer, cstatus, STATE_WAIT_FOR_CLOSE_ACK);
Expand Down Expand Up @@ -1171,7 +1181,7 @@ enum command_status state(const tal_t *ctx,
/*
* bitcoind reported a broadcast of the not-latest commit tx.
*/
add_effect(effect, send_pkt, pkt_err(ctx, "Otherspend noticed"));
queue_pkt(out, pkt_err(ctx, "Otherspend noticed"));

/* No more packets, no more commands. */
set_peer_cond(peer, PEER_CLOSED);
Expand Down
5 changes: 1 addition & 4 deletions state.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

enum state_effect_type {
STATE_EFFECT_broadcast_tx,
STATE_EFFECT_send_pkt,
STATE_EFFECT_watch,
STATE_EFFECT_unwatch,
/* FIXME: Use a watch for this?. */
Expand All @@ -34,9 +33,6 @@ struct state_effect {
/* Transaction to broadcast. */
struct bitcoin_tx *broadcast_tx;

/* Packet to send. */
Pkt *send_pkt;

/* Event to watch for. */
struct watch *watch;

Expand Down Expand Up @@ -84,6 +80,7 @@ enum command_status state(const tal_t *ctx,
struct peer *peer,
const enum state_input input,
const union input *idata,
Pkt **out,
struct state_effect **effect);

/* Any CMD_SEND_HTLC_* */
Expand Down
Loading

0 comments on commit e984df4

Please sign in to comment.