state: add _THEYCOMPLETED states to reflect receiving PKT_OPEN_COMPLETE

This is cleaner than deferring the packet receive and asking for it later.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-01-22 06:41:46 +10:30
parent 3c1b2ad1c1
commit 3ab4ba1e6f
3 changed files with 40 additions and 16 deletions

32
state.c
View File

@ -153,19 +153,24 @@ struct state_effect *state(const tal_t *ctx,
} }
break; break;
case STATE_OPEN_WAITING_OURANCHOR: case STATE_OPEN_WAITING_OURANCHOR:
if (input_is(input, PKT_OPEN_COMPLETE)) {
return next_state(ctx, effect,
STATE_OPEN_WAITING_OURANCHOR_THEYCOMPLETED);
}
/* Fall thru */
case STATE_OPEN_WAITING_OURANCHOR_THEYCOMPLETED:
if (input_is(input, BITCOIN_ANCHOR_DEPTHOK)) { if (input_is(input, BITCOIN_ANCHOR_DEPTHOK)) {
add_effect(&effect, send_pkt, add_effect(&effect, send_pkt,
pkt_open_complete(ctx, peer)); pkt_open_complete(ctx, peer));
if (state == STATE_OPEN_WAITING_OURANCHOR_THEYCOMPLETED) {
add_effect(&effect, cmd_success, CMD_OPEN_WITH_ANCHOR);
return next_state(ctx, effect, STATE_NORMAL_HIGHPRIO);
}
return next_state(ctx, effect, return next_state(ctx, effect,
STATE_OPEN_WAIT_FOR_COMPLETE_OURANCHOR); STATE_OPEN_WAIT_FOR_COMPLETE_OURANCHOR);
} else if (input_is(input, BITCOIN_ANCHOR_UNSPENT)) { } else if (input_is(input, BITCOIN_ANCHOR_UNSPENT)) {
add_effect(&effect, cmd_fail, NULL); add_effect(&effect, cmd_fail, NULL);
goto anchor_unspent; goto anchor_unspent;
} else if (input_is(input, PKT_OPEN_COMPLETE)) {
/* Ignore until we've hit depth ourselves. */
add_effect(&effect, cmd_defer, input);
/* No state change. */
return effect;
} else if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) { } else if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) {
/* We no longer care about anchor depth. */ /* We no longer care about anchor depth. */
add_effect(&effect, unwatch, add_effect(&effect, unwatch,
@ -204,6 +209,12 @@ struct state_effect *state(const tal_t *ctx,
} }
break; break;
case STATE_OPEN_WAITING_THEIRANCHOR: case STATE_OPEN_WAITING_THEIRANCHOR:
if (input_is(input, PKT_OPEN_COMPLETE)) {
return next_state(ctx, effect,
STATE_OPEN_WAITING_THEIRANCHOR_THEYCOMPLETED);
}
/* Fall thru */
case STATE_OPEN_WAITING_THEIRANCHOR_THEYCOMPLETED:
if (input_is(input, BITCOIN_ANCHOR_TIMEOUT)) { if (input_is(input, BITCOIN_ANCHOR_TIMEOUT)) {
/* Anchor didn't reach blockchain in reasonable time. */ /* Anchor didn't reach blockchain in reasonable time. */
add_effect(&effect, send_pkt, add_effect(&effect, send_pkt,
@ -212,6 +223,12 @@ struct state_effect *state(const tal_t *ctx,
} else if (input_is(input, BITCOIN_ANCHOR_DEPTHOK)) { } else if (input_is(input, BITCOIN_ANCHOR_DEPTHOK)) {
add_effect(&effect, send_pkt, add_effect(&effect, send_pkt,
pkt_open_complete(ctx, peer)); pkt_open_complete(ctx, peer));
if (state == STATE_OPEN_WAITING_THEIRANCHOR_THEYCOMPLETED) {
add_effect(&effect, cmd_success,
CMD_OPEN_WITHOUT_ANCHOR);
return next_state(ctx, effect,
STATE_NORMAL_LOWPRIO);
}
return next_state(ctx, effect, STATE_OPEN_WAIT_FOR_COMPLETE_THEIRANCHOR); return next_state(ctx, effect, STATE_OPEN_WAIT_FOR_COMPLETE_THEIRANCHOR);
} else if (input_is(input, BITCOIN_ANCHOR_UNSPENT)) { } else if (input_is(input, BITCOIN_ANCHOR_UNSPENT)) {
add_effect(&effect, cmd_fail, NULL); add_effect(&effect, cmd_fail, NULL);
@ -227,11 +244,6 @@ struct state_effect *state(const tal_t *ctx,
BITCOIN_ANCHOR_TIMEOUT)); BITCOIN_ANCHOR_TIMEOUT));
add_effect(&effect, cmd_fail, NULL); add_effect(&effect, cmd_fail, NULL);
goto them_unilateral; goto them_unilateral;
} else if (input_is(input, PKT_OPEN_COMPLETE)) {
/* Ignore until we've hit depth ourselves. */
add_effect(&effect, cmd_defer, input);
/* No state change. */
return effect;
} else if (input_is(input, CMD_CLOSE)) { } else if (input_is(input, CMD_CLOSE)) {
/* We no longer care about anchor depth. */ /* We no longer care about anchor depth. */
add_effect(&effect, unwatch, add_effect(&effect, unwatch,

View File

@ -23,6 +23,8 @@ enum state {
STATE_OPEN_WAIT_FOR_COMMIT_SIG, STATE_OPEN_WAIT_FOR_COMMIT_SIG,
STATE_OPEN_WAITING_OURANCHOR, STATE_OPEN_WAITING_OURANCHOR,
STATE_OPEN_WAITING_THEIRANCHOR, STATE_OPEN_WAITING_THEIRANCHOR,
STATE_OPEN_WAITING_OURANCHOR_THEYCOMPLETED,
STATE_OPEN_WAITING_THEIRANCHOR_THEYCOMPLETED,
STATE_OPEN_WAIT_FOR_COMPLETE_OURANCHOR, STATE_OPEN_WAIT_FOR_COMPLETE_OURANCHOR,
STATE_OPEN_WAIT_FOR_COMPLETE_THEIRANCHOR, STATE_OPEN_WAIT_FOR_COMPLETE_THEIRANCHOR,

View File

@ -1858,13 +1858,17 @@ static const char *simplify_state(enum state s)
static bool waiting_statepair(enum state a, enum state b) static bool waiting_statepair(enum state a, enum state b)
{ {
if (a > b)
return waiting_statepair(b, a);
/* We don't need inputs if we're waiting for anchors. */ /* We don't need inputs if we're waiting for anchors. */
if (a == STATE_OPEN_WAITING_OURANCHOR) if (a == STATE_OPEN_WAITING_OURANCHOR
|| a == STATE_OPEN_WAITING_OURANCHOR_THEYCOMPLETED
|| a == STATE_OPEN_WAITING_THEIRANCHOR
|| a == STATE_OPEN_WAITING_THEIRANCHOR_THEYCOMPLETED)
return true; return true;
if (b == STATE_OPEN_WAITING_THEIRANCHOR)
if (b == STATE_OPEN_WAITING_OURANCHOR
|| b == STATE_OPEN_WAITING_OURANCHOR_THEYCOMPLETED
|| b == STATE_OPEN_WAITING_THEIRANCHOR
|| b == STATE_OPEN_WAITING_THEIRANCHOR_THEYCOMPLETED)
return true; return true;
/* We don't need inputs at start of main loop. */ /* We don't need inputs at start of main loop. */
@ -1872,6 +1876,10 @@ static bool waiting_statepair(enum state a, enum state b)
&& b == STATE_NORMAL_HIGHPRIO) && b == STATE_NORMAL_HIGHPRIO)
return true; return true;
if (a == STATE_NORMAL_HIGHPRIO
&& b == STATE_NORMAL_LOWPRIO)
return true;
return false; return false;
} }
@ -2493,13 +2501,15 @@ int main(int argc, char *argv[])
if (i == STATE_OPEN_WAIT_FOR_OPEN_NOANCHOR if (i == STATE_OPEN_WAIT_FOR_OPEN_NOANCHOR
|| i == STATE_OPEN_WAIT_FOR_ANCHOR || i == STATE_OPEN_WAIT_FOR_ANCHOR
|| i == STATE_OPEN_WAITING_THEIRANCHOR || i == STATE_OPEN_WAITING_THEIRANCHOR
|| i == STATE_OPEN_WAITING_THEIRANCHOR_THEYCOMPLETED
|| i == STATE_OPEN_WAIT_FOR_COMPLETE_THEIRANCHOR || i == STATE_OPEN_WAIT_FOR_COMPLETE_THEIRANCHOR
|| i == STATE_ERR_ANCHOR_TIMEOUT) || i == STATE_ERR_ANCHOR_TIMEOUT)
a_expect = false; a_expect = false;
if (i == STATE_OPEN_WAIT_FOR_OPEN_WITHANCHOR if (i == STATE_OPEN_WAIT_FOR_OPEN_WITHANCHOR
|| i == STATE_OPEN_WAIT_FOR_COMMIT_SIG || i == STATE_OPEN_WAIT_FOR_COMMIT_SIG
|| i == STATE_OPEN_WAIT_FOR_COMPLETE_OURANCHOR || i == STATE_OPEN_WAIT_FOR_COMPLETE_OURANCHOR
|| i == STATE_OPEN_WAITING_OURANCHOR) || i == STATE_OPEN_WAITING_OURANCHOR
|| i == STATE_OPEN_WAITING_OURANCHOR_THEYCOMPLETED)
b_expect = false; b_expect = false;
if (i == STATE_ERR_INTERNAL) if (i == STATE_ERR_INTERNAL)
a_expect = b_expect = false; a_expect = b_expect = false;