2015-09-25 04:21:18 +02:00
|
|
|
#include <ccan/build_assert/build_assert.h>
|
2016-01-21 21:11:46 +01:00
|
|
|
#include <state.h>
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
static inline bool high_priority(enum state state)
|
|
|
|
{
|
|
|
|
return (state & 1) == (STATE_NORMAL_HIGHPRIO & 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define prio(state, name) \
|
|
|
|
(high_priority(state) ? name##_HIGHPRIO : name##_LOWPRIO)
|
|
|
|
|
|
|
|
#define toggle_prio(state, name) \
|
|
|
|
(!high_priority(state) ? name##_HIGHPRIO : name##_LOWPRIO)
|
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
/* STATE_CLOSE* can be treated as a bitset offset from STATE_CLOSED */
|
|
|
|
#define BITS_TO_STATE(bits) (STATE_CLOSED + (bits))
|
|
|
|
#define STATE_TO_BITS(state) ((state) - STATE_CLOSED)
|
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
/* For the rare cases where state may not change */
|
|
|
|
static enum command_status next_state_nocheck(struct peer *peer,
|
|
|
|
enum command_status cstatus,
|
|
|
|
const enum state state)
|
|
|
|
{
|
|
|
|
peer->state = state;
|
|
|
|
return cstatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum command_status next_state(struct peer *peer,
|
|
|
|
enum command_status cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
const enum state state)
|
2015-09-25 04:21:18 +02:00
|
|
|
{
|
2016-01-21 21:11:46 +01:00
|
|
|
assert(peer->state != state);
|
|
|
|
return next_state_nocheck(peer, cstatus, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Simple marker to note we don't update state.
|
|
|
|
*
|
|
|
|
* This happens in two cases:
|
|
|
|
* - We're ignoring packets while closing.
|
|
|
|
* - We stop watching an on-chain HTLC: we indicate that we want
|
|
|
|
* INPUT_NO_MORE_HTLCS when we get the last one.
|
|
|
|
*/
|
|
|
|
static enum command_status unchanged_state(enum command_status cstatus)
|
|
|
|
{
|
|
|
|
return cstatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This may not actually change the state. */
|
|
|
|
static enum command_status next_state_bits(struct peer *peer,
|
|
|
|
enum command_status cstatus,
|
|
|
|
unsigned int bits)
|
|
|
|
{
|
|
|
|
return next_state_nocheck(peer, cstatus, BITS_TO_STATE(bits));
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
static void set_peer_cond(struct peer *peer, enum state_peercond cond)
|
|
|
|
{
|
|
|
|
assert(peer->cond != cond);
|
|
|
|
peer->cond = cond;
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
static void change_peer_cond(struct peer *peer,
|
|
|
|
enum state_peercond old,
|
|
|
|
enum state_peercond new)
|
|
|
|
{
|
|
|
|
assert(peer->cond == old);
|
|
|
|
peer->cond = new;
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
static void complete_cmd(struct peer *peer, enum command_status *statusp,
|
|
|
|
enum command_status status)
|
|
|
|
{
|
|
|
|
change_peer_cond(peer, PEER_BUSY, PEER_CMD_OK);
|
|
|
|
*statusp = status;
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:47 +01:00
|
|
|
static void queue_pkt(Pkt **out, Pkt *pkt)
|
|
|
|
{
|
|
|
|
assert(!*out);
|
|
|
|
assert(pkt);
|
|
|
|
*out = pkt;
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:47 +01:00
|
|
|
static void queue_tx_broadcast(const struct bitcoin_tx **broadcast,
|
|
|
|
const struct bitcoin_tx *tx)
|
2016-01-21 21:11:47 +01:00
|
|
|
{
|
|
|
|
assert(!*broadcast);
|
|
|
|
assert(tx);
|
|
|
|
*broadcast = tx;
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
enum command_status state(const tal_t *ctx,
|
|
|
|
struct peer *peer,
|
|
|
|
const enum state_input input,
|
|
|
|
const union input *idata,
|
2016-01-21 21:11:47 +01:00
|
|
|
Pkt **out,
|
2016-01-21 21:11:47 +01:00
|
|
|
const struct bitcoin_tx **broadcast)
|
2015-09-25 04:21:18 +02:00
|
|
|
{
|
|
|
|
Pkt *decline;
|
2016-01-21 21:11:47 +01:00
|
|
|
const struct bitcoin_tx *tx;
|
2015-09-25 04:21:18 +02:00
|
|
|
Pkt *err;
|
2016-01-21 21:11:46 +01:00
|
|
|
enum command_status cstatus = CMD_NONE;
|
|
|
|
|
2016-01-21 21:11:47 +01:00
|
|
|
*out = NULL;
|
2016-01-21 21:11:47 +01:00
|
|
|
*broadcast = NULL;
|
2016-01-21 21:11:47 +01:00
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
switch (peer->state) {
|
2015-09-25 04:21:18 +02:00
|
|
|
/*
|
|
|
|
* Initial channel opening states.
|
|
|
|
*/
|
2016-01-21 21:11:46 +01:00
|
|
|
case STATE_INIT:
|
|
|
|
if (input_is(input, CMD_OPEN_WITH_ANCHOR)) {
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out,
|
2016-01-21 21:11:46 +01:00
|
|
|
pkt_open(ctx, peer,
|
|
|
|
OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR));
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
STATE_OPEN_WAIT_FOR_OPEN_WITHANCHOR);
|
|
|
|
} else if (input_is(input, CMD_OPEN_WITHOUT_ANCHOR)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out,
|
2016-01-21 21:11:46 +01:00
|
|
|
pkt_open(ctx, peer,
|
|
|
|
OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR));
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
STATE_OPEN_WAIT_FOR_OPEN_NOANCHOR);
|
|
|
|
}
|
|
|
|
break;
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_OPEN_WAIT_FOR_OPEN_NOANCHOR:
|
|
|
|
if (input_is(input, PKT_OPEN)) {
|
2016-01-21 21:11:47 +01:00
|
|
|
err = accept_pkt_open(ctx, peer, idata->pkt);
|
2016-01-21 21:11:46 +01:00
|
|
|
if (err) {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto err_close_nocleanup;
|
2016-01-21 21:11:46 +01:00
|
|
|
}
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_OPEN_WAIT_FOR_ANCHOR);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, CMD_CLOSE)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto instant_close;
|
|
|
|
} else if (input_is_pkt(input)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto unexpected_pkt_nocleanup;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STATE_OPEN_WAIT_FOR_OPEN_WITHANCHOR:
|
|
|
|
if (input_is(input, PKT_OPEN)) {
|
2016-01-21 21:11:47 +01:00
|
|
|
err = accept_pkt_open(ctx, peer, idata->pkt);
|
2016-01-21 21:11:46 +01:00
|
|
|
if (err) {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto err_close_nocleanup;
|
2016-01-21 21:11:46 +01:00
|
|
|
}
|
2016-01-21 21:11:47 +01:00
|
|
|
bitcoin_create_anchor(peer, BITCOIN_ANCHOR_CREATED);
|
|
|
|
return next_state(peer, cstatus,
|
|
|
|
STATE_OPEN_WAIT_FOR_ANCHOR_CREATE);
|
|
|
|
} else if (input_is(input, CMD_CLOSE)) {
|
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
|
|
|
goto instant_close;
|
|
|
|
} else if (input_is_pkt(input)) {
|
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
|
|
|
goto unexpected_pkt_nocleanup;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STATE_OPEN_WAIT_FOR_ANCHOR_CREATE:
|
|
|
|
if (input_is(input, BITCOIN_ANCHOR_CREATED)) {
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out, pkt_anchor(ctx, peer));
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:45 +01:00
|
|
|
STATE_OPEN_WAIT_FOR_COMMIT_SIG);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, CMD_CLOSE)) {
|
2016-01-21 21:11:47 +01:00
|
|
|
bitcoin_release_anchor(peer, BITCOIN_ANCHOR_CREATED);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto instant_close;
|
|
|
|
} else if (input_is_pkt(input)) {
|
2016-01-21 21:11:47 +01:00
|
|
|
bitcoin_release_anchor(peer, BITCOIN_ANCHOR_CREATED);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto unexpected_pkt_nocleanup;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STATE_OPEN_WAIT_FOR_ANCHOR:
|
|
|
|
if (input_is(input, PKT_OPEN_ANCHOR)) {
|
2016-01-21 21:11:47 +01:00
|
|
|
err = accept_pkt_anchor(ctx, peer, idata->pkt);
|
2016-01-21 21:11:46 +01:00
|
|
|
if (err) {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto err_close_nocleanup;
|
2016-01-21 21:11:46 +01:00
|
|
|
}
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out,
|
2016-01-21 21:11:45 +01:00
|
|
|
pkt_open_commit_sig(ctx, peer));
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_watch_anchor(peer,
|
|
|
|
BITCOIN_ANCHOR_DEPTHOK,
|
|
|
|
BITCOIN_ANCHOR_TIMEOUT,
|
|
|
|
BITCOIN_ANCHOR_UNSPENT,
|
|
|
|
BITCOIN_ANCHOR_THEIRSPEND,
|
|
|
|
BITCOIN_ANCHOR_OTHERSPEND);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:45 +01:00
|
|
|
STATE_OPEN_WAITING_THEIRANCHOR);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, CMD_CLOSE)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto instant_close;
|
|
|
|
} else if (input_is_pkt(input)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto unexpected_pkt_nocleanup;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STATE_OPEN_WAIT_FOR_COMMIT_SIG:
|
|
|
|
if (input_is(input, PKT_OPEN_COMMIT_SIG)) {
|
2016-01-21 21:11:47 +01:00
|
|
|
err = accept_pkt_open_commit_sig(ctx, peer, idata->pkt);
|
2016-01-21 21:11:46 +01:00
|
|
|
if (err) {
|
2016-01-21 21:11:47 +01:00
|
|
|
bitcoin_release_anchor(peer, INPUT_NONE);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto err_start_unilateral_close;
|
2016-01-21 21:11:46 +01:00
|
|
|
}
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_tx_broadcast(broadcast, bitcoin_anchor(ctx, peer));
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_watch_anchor(peer,
|
|
|
|
BITCOIN_ANCHOR_DEPTHOK,
|
|
|
|
INPUT_NONE,
|
|
|
|
BITCOIN_ANCHOR_UNSPENT,
|
|
|
|
BITCOIN_ANCHOR_THEIRSPEND,
|
|
|
|
BITCOIN_ANCHOR_OTHERSPEND);
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
STATE_OPEN_WAITING_OURANCHOR);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, CMD_CLOSE)) {
|
2016-01-21 21:11:47 +01:00
|
|
|
bitcoin_release_anchor(peer, INPUT_NONE);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto instant_close;
|
|
|
|
} else if (input_is_pkt(input)) {
|
2016-01-21 21:11:47 +01:00
|
|
|
bitcoin_release_anchor(peer, INPUT_NONE);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto unexpected_pkt_nocleanup;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STATE_OPEN_WAITING_OURANCHOR:
|
2016-01-21 21:11:46 +01:00
|
|
|
if (input_is(input, PKT_OPEN_COMPLETE)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
STATE_OPEN_WAITING_OURANCHOR_THEYCOMPLETED);
|
|
|
|
}
|
|
|
|
/* Fall thru */
|
|
|
|
case STATE_OPEN_WAITING_OURANCHOR_THEYCOMPLETED:
|
2015-09-25 04:21:18 +02:00
|
|
|
if (input_is(input, BITCOIN_ANCHOR_DEPTHOK)) {
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out,
|
2016-01-21 21:11:45 +01:00
|
|
|
pkt_open_complete(ctx, peer));
|
2016-01-21 21:11:46 +01:00
|
|
|
if (peer->state == STATE_OPEN_WAITING_OURANCHOR_THEYCOMPLETED) {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_SUCCESS);
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
STATE_NORMAL_HIGHPRIO);
|
2016-01-21 21:11:46 +01:00
|
|
|
}
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:45 +01:00
|
|
|
STATE_OPEN_WAIT_FOR_COMPLETE_OURANCHOR);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_UNSPENT)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto anchor_unspent;
|
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) {
|
|
|
|
/* We no longer care about anchor depth. */
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_unwatch_anchor_depth(peer,
|
|
|
|
BITCOIN_ANCHOR_DEPTHOK,
|
|
|
|
INPUT_NONE);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto them_unilateral;
|
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_OTHERSPEND)) {
|
|
|
|
/* This should be impossible. */
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_ERR_INFORMATION_LEAK);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, CMD_CLOSE)) {
|
|
|
|
/* We no longer care about anchor depth. */
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_unwatch_anchor_depth(peer,
|
|
|
|
BITCOIN_ANCHOR_DEPTHOK,
|
|
|
|
INPUT_NONE);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto start_closing;
|
|
|
|
} else if (input_is(input, PKT_CLOSE)) {
|
|
|
|
/* We no longer care about anchor depth. */
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_unwatch_anchor_depth(peer,
|
|
|
|
BITCOIN_ANCHOR_DEPTHOK,
|
|
|
|
INPUT_NONE);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto accept_closing;
|
|
|
|
} else if (input_is_pkt(input)) {
|
|
|
|
/* We no longer care about anchor depth. */
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_unwatch_anchor_depth(peer,
|
|
|
|
BITCOIN_ANCHOR_DEPTHOK,
|
|
|
|
INPUT_NONE);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2016-01-21 21:11:46 +01:00
|
|
|
goto unexpected_pkt_nocleanup;
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STATE_OPEN_WAITING_THEIRANCHOR:
|
2016-01-21 21:11:46 +01:00
|
|
|
if (input_is(input, PKT_OPEN_COMPLETE)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
STATE_OPEN_WAITING_THEIRANCHOR_THEYCOMPLETED);
|
|
|
|
}
|
|
|
|
/* Fall thru */
|
|
|
|
case STATE_OPEN_WAITING_THEIRANCHOR_THEYCOMPLETED:
|
2015-09-25 04:21:18 +02:00
|
|
|
if (input_is(input, BITCOIN_ANCHOR_TIMEOUT)) {
|
|
|
|
/* Anchor didn't reach blockchain in reasonable time. */
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out,
|
2016-01-21 21:11:45 +01:00
|
|
|
pkt_err(ctx, "Anchor timed out"));
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_ERR_ANCHOR_TIMEOUT);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_DEPTHOK)) {
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out,
|
2016-01-21 21:11:45 +01:00
|
|
|
pkt_open_complete(ctx, peer));
|
2016-01-21 21:11:46 +01:00
|
|
|
if (peer->state == STATE_OPEN_WAITING_THEIRANCHOR_THEYCOMPLETED) {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_SUCCESS);
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
STATE_NORMAL_LOWPRIO);
|
|
|
|
}
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
STATE_OPEN_WAIT_FOR_COMPLETE_THEIRANCHOR);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_UNSPENT)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto anchor_unspent;
|
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_OTHERSPEND)) {
|
|
|
|
/* This should be impossible. */
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
STATE_ERR_INFORMATION_LEAK);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) {
|
|
|
|
/* We no longer care about anchor depth. */
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_unwatch_anchor_depth(peer,
|
|
|
|
BITCOIN_ANCHOR_DEPTHOK,
|
|
|
|
BITCOIN_ANCHOR_TIMEOUT);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto them_unilateral;
|
|
|
|
} else if (input_is(input, CMD_CLOSE)) {
|
|
|
|
/* We no longer care about anchor depth. */
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_unwatch_anchor_depth(peer,
|
|
|
|
BITCOIN_ANCHOR_DEPTHOK,
|
|
|
|
BITCOIN_ANCHOR_TIMEOUT);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto start_closing;
|
|
|
|
} else if (input_is(input, PKT_CLOSE)) {
|
|
|
|
/* We no longer care about anchor depth. */
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_unwatch_anchor_depth(peer,
|
|
|
|
BITCOIN_ANCHOR_DEPTHOK,
|
|
|
|
BITCOIN_ANCHOR_TIMEOUT);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto accept_closing;
|
|
|
|
} else if (input_is_pkt(input)) {
|
|
|
|
/* We no longer care about anchor depth. */
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_unwatch_anchor_depth(peer,
|
|
|
|
BITCOIN_ANCHOR_DEPTHOK,
|
|
|
|
BITCOIN_ANCHOR_TIMEOUT);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto unexpected_pkt;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STATE_OPEN_WAIT_FOR_COMPLETE_OURANCHOR:
|
|
|
|
case STATE_OPEN_WAIT_FOR_COMPLETE_THEIRANCHOR:
|
|
|
|
if (input_is(input, PKT_OPEN_COMPLETE)) {
|
|
|
|
/* Ready for business! Anchorer goes first. */
|
2016-01-21 21:11:46 +01:00
|
|
|
if (peer->state == STATE_OPEN_WAIT_FOR_COMPLETE_OURANCHOR) {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_SUCCESS);
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
STATE_NORMAL_HIGHPRIO);
|
2016-01-21 21:11:46 +01:00
|
|
|
} else {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_SUCCESS);
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
STATE_NORMAL_LOWPRIO);
|
2016-01-21 21:11:46 +01:00
|
|
|
}
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_UNSPENT)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto anchor_unspent;
|
|
|
|
/* Nobody should be able to spend anchor, except via the
|
|
|
|
* commit txs. */
|
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_OTHERSPEND)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
STATE_ERR_INFORMATION_LEAK);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto them_unilateral;
|
|
|
|
} else if (input_is(input, CMD_CLOSE)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto start_closing;
|
|
|
|
} else if (input_is(input, PKT_CLOSE)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto accept_closing;
|
|
|
|
} else if (input_is_pkt(input)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto unexpected_pkt;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Channel normal operating states.
|
|
|
|
*/
|
|
|
|
case STATE_NORMAL_LOWPRIO:
|
|
|
|
case STATE_NORMAL_HIGHPRIO:
|
2016-01-21 21:11:46 +01:00
|
|
|
assert(peer->cond == PEER_CMD_OK);
|
2015-09-25 04:21:18 +02:00
|
|
|
if (input_is(input, CMD_SEND_HTLC_UPDATE)) {
|
2015-09-25 04:21:18 +02:00
|
|
|
/* We are to send an HTLC update. */
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out,
|
2016-01-21 21:11:45 +01:00
|
|
|
pkt_htlc_update(ctx, peer,
|
2015-09-25 04:21:18 +02:00
|
|
|
idata->htlc_prog));
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
|
|
|
prio(peer->state, STATE_WAIT_FOR_HTLC_ACCEPT));
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, CMD_SEND_HTLC_FULFILL)) {
|
|
|
|
/* We are to send an HTLC fulfill. */
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out,
|
2016-01-21 21:11:45 +01:00
|
|
|
pkt_htlc_fulfill(ctx, peer,
|
2015-09-25 04:21:18 +02:00
|
|
|
idata->htlc_prog));
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
|
|
|
prio(peer->state, STATE_WAIT_FOR_UPDATE_ACCEPT));
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, CMD_SEND_HTLC_TIMEDOUT)) {
|
|
|
|
/* We are to send an HTLC timedout. */
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out,
|
2016-01-21 21:11:45 +01:00
|
|
|
pkt_htlc_timedout(ctx, peer,
|
2015-09-25 04:21:18 +02:00
|
|
|
idata->htlc_prog));
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
|
|
|
prio(peer->state, STATE_WAIT_FOR_UPDATE_ACCEPT));
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, CMD_SEND_HTLC_ROUTEFAIL)) {
|
|
|
|
/* We are to send an HTLC routefail. */
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out,
|
2016-01-21 21:11:45 +01:00
|
|
|
pkt_htlc_routefail(ctx, peer,
|
2015-09-25 04:21:18 +02:00
|
|
|
idata->htlc_prog));
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
|
|
|
prio(peer->state, STATE_WAIT_FOR_UPDATE_ACCEPT));
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, CMD_CLOSE)) {
|
|
|
|
goto start_closing;
|
|
|
|
} else if (input_is(input, PKT_UPDATE_ADD_HTLC)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto accept_htlc_update;
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, PKT_UPDATE_FULFILL_HTLC)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto accept_htlc_fulfill;
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, PKT_UPDATE_TIMEDOUT_HTLC)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto accept_htlc_timedout;
|
|
|
|
} else if (input_is(input, PKT_UPDATE_ROUTEFAIL_HTLC)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto accept_htlc_routefail;
|
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) {
|
|
|
|
goto them_unilateral;
|
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_OTHERSPEND)) {
|
|
|
|
goto old_commit_spotted;
|
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_UNSPENT)) {
|
|
|
|
goto anchor_unspent;
|
|
|
|
} else if (input_is(input, PKT_CLOSE)) {
|
|
|
|
goto accept_closing;
|
|
|
|
} else if (input_is_pkt(input)) {
|
|
|
|
goto unexpected_pkt;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STATE_WAIT_FOR_HTLC_ACCEPT_LOWPRIO:
|
|
|
|
case STATE_WAIT_FOR_HTLC_ACCEPT_HIGHPRIO:
|
|
|
|
/* HTLCs can also evoke a refusal. */
|
|
|
|
if (input_is(input, PKT_UPDATE_DECLINE_HTLC)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_declined(peer, idata->pkt);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:19 +02:00
|
|
|
/* No update means no priority change. */
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
|
|
|
prio(peer->state, STATE_NORMAL));
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
|
|
|
/* Fall thru */
|
|
|
|
case STATE_WAIT_FOR_UPDATE_ACCEPT_LOWPRIO:
|
|
|
|
case STATE_WAIT_FOR_UPDATE_ACCEPT_HIGHPRIO:
|
|
|
|
if (input_is(input, PKT_UPDATE_ADD_HTLC)) {
|
2015-09-25 04:21:18 +02:00
|
|
|
/* If we're high priority, ignore their packet */
|
2016-01-21 21:11:46 +01:00
|
|
|
if (high_priority(peer->state))
|
2016-01-21 21:11:46 +01:00
|
|
|
return cstatus;
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/* Otherwise, process their request first: defer ours */
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_ours_deferred(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_REQUEUE);
|
2016-01-21 21:11:46 +01:00
|
|
|
/* Stay busy, since we're processing theirs. */
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto accept_htlc_update;
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, PKT_UPDATE_FULFILL_HTLC)) {
|
2015-09-25 04:21:18 +02:00
|
|
|
/* If we're high priority, ignore their packet */
|
2016-01-21 21:11:46 +01:00
|
|
|
if (high_priority(peer->state))
|
2016-01-21 21:11:46 +01:00
|
|
|
return cstatus;
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/* Otherwise, process their request first: defer ours */
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_ours_deferred(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_REQUEUE);
|
2016-01-21 21:11:46 +01:00
|
|
|
/* Stay busy, since we're processing theirs. */
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto accept_htlc_fulfill;
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, PKT_UPDATE_TIMEDOUT_HTLC)) {
|
|
|
|
/* If we're high priority, ignore their packet */
|
2016-01-21 21:11:46 +01:00
|
|
|
if (high_priority(peer->state))
|
2016-01-21 21:11:46 +01:00
|
|
|
return cstatus;
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/* Otherwise, process their request first: defer ours */
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_ours_deferred(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_REQUEUE);
|
2016-01-21 21:11:46 +01:00
|
|
|
/* Stay busy, since we're processing theirs. */
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto accept_htlc_timedout;
|
|
|
|
} else if (input_is(input, PKT_UPDATE_ROUTEFAIL_HTLC)) {
|
|
|
|
/* If we're high priority, ignore their packet */
|
2016-01-21 21:11:46 +01:00
|
|
|
if (high_priority(peer->state))
|
2016-01-21 21:11:46 +01:00
|
|
|
return cstatus;
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/* Otherwise, process their request first: defer ours */
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_ours_deferred(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_REQUEUE);
|
2016-01-21 21:11:46 +01:00
|
|
|
/* Stay busy, since we're processing theirs. */
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto accept_htlc_routefail;
|
|
|
|
} else if (input_is(input, PKT_UPDATE_ACCEPT)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
err = accept_pkt_update_accept(ctx, peer, idata->pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
if (err) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto err_start_unilateral_close;
|
|
|
|
}
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out,
|
2016-01-21 21:11:45 +01:00
|
|
|
pkt_update_signature(ctx, peer));
|
2015-09-25 04:21:18 +02:00
|
|
|
/* HTLC is signed (though old tx not revoked yet!) */
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
|
|
|
prio(peer->state, STATE_WAIT_FOR_UPDATE_COMPLETE));
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_UNSPENT)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto anchor_unspent;
|
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto them_unilateral;
|
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_OTHERSPEND)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto old_commit_spotted;
|
2015-09-25 04:21:19 +02:00
|
|
|
} else if (input_is(input, CMD_CLOSE)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:19 +02:00
|
|
|
goto start_closing;
|
2016-01-21 21:11:46 +01:00
|
|
|
} else if (input_is(input, PKT_CLOSE)) {
|
|
|
|
peer_htlc_aborted(peer);
|
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
|
|
|
goto accept_closing;
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is_pkt(input)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto unexpected_pkt;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STATE_WAIT_FOR_UPDATE_COMPLETE_LOWPRIO:
|
|
|
|
case STATE_WAIT_FOR_UPDATE_COMPLETE_HIGHPRIO:
|
|
|
|
if (input_is(input, PKT_UPDATE_COMPLETE)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
err = accept_pkt_update_complete(ctx, peer, idata->pkt);
|
2015-09-29 02:17:56 +02:00
|
|
|
if (err) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto err_start_unilateral_close;
|
2015-09-29 02:17:56 +02:00
|
|
|
}
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_done(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_SUCCESS);
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
|
|
|
toggle_prio(peer->state, STATE_NORMAL));
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_UNSPENT)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto anchor_unspent;
|
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto them_unilateral;
|
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_OTHERSPEND)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto old_commit_spotted;
|
|
|
|
} else if (input_is(input, PKT_CLOSE)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto accept_closing;
|
2015-09-25 04:21:19 +02:00
|
|
|
} else if (input_is(input, CMD_CLOSE)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:19 +02:00
|
|
|
goto start_closing;
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is_pkt(input)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto unexpected_pkt;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STATE_WAIT_FOR_UPDATE_SIG_LOWPRIO:
|
|
|
|
case STATE_WAIT_FOR_UPDATE_SIG_HIGHPRIO:
|
|
|
|
if (input_is(input, PKT_UPDATE_SIGNATURE)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
err = accept_pkt_update_signature(ctx, peer, idata->pkt);
|
2015-09-29 02:17:56 +02:00
|
|
|
if (err) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto err_start_unilateral_close;
|
2015-09-29 02:17:56 +02:00
|
|
|
}
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out,
|
2016-01-21 21:11:45 +01:00
|
|
|
pkt_update_complete(ctx, peer));
|
2016-01-21 21:11:46 +01:00
|
|
|
|
|
|
|
peer_htlc_done(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_BUSY, PEER_CMD_OK);
|
2015-09-25 04:21:18 +02:00
|
|
|
/* Toggle between high and low priority states. */
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
|
|
|
toggle_prio(peer->state, STATE_NORMAL));
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_UNSPENT)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto anchor_unspent;
|
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto them_unilateral;
|
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_OTHERSPEND)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto old_commit_spotted;
|
|
|
|
} else if (input_is(input, CMD_CLOSE)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto start_closing;
|
2015-09-25 04:21:19 +02:00
|
|
|
} else if (input_is(input, PKT_CLOSE)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2015-09-25 04:21:19 +02:00
|
|
|
goto accept_closing;
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is_pkt(input)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_aborted(peer);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto unexpected_pkt;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_WAIT_FOR_CLOSE_COMPLETE:
|
|
|
|
if (input_is(input, PKT_CLOSE_COMPLETE)) {
|
2016-01-21 21:11:47 +01:00
|
|
|
err = accept_pkt_close_complete(ctx, peer, idata->pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
if (err)
|
2015-09-29 02:17:56 +02:00
|
|
|
goto err_start_unilateral_close_already_closing;
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out,
|
2016-01-21 21:11:45 +01:00
|
|
|
pkt_close_ack(ctx, peer));
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_tx_broadcast(broadcast, bitcoin_close(ctx, peer));
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_CLOSING, PEER_CLOSED);
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_CLOSE_WAIT_CLOSE);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, PKT_CLOSE)) {
|
|
|
|
/* We can use the sig just like CLOSE_COMPLETE */
|
2016-01-21 21:11:45 +01:00
|
|
|
err = accept_pkt_simultaneous_close(ctx, peer,
|
2016-01-21 21:11:47 +01:00
|
|
|
idata->pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
if (err)
|
2015-09-29 02:17:56 +02:00
|
|
|
goto err_start_unilateral_close_already_closing;
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out,
|
2016-01-21 21:11:45 +01:00
|
|
|
pkt_close_ack(ctx, peer));
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_tx_broadcast(broadcast, bitcoin_close(ctx, peer));
|
2016-01-21 21:11:46 +01:00
|
|
|
set_peer_cond(peer, PEER_CLOSED);
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_CLOSE_WAIT_CLOSE);
|
2015-09-29 02:17:56 +02:00
|
|
|
} else if (input_is(input, PKT_ERROR)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_unexpected_pkt(peer, idata->pkt);
|
2015-09-29 02:17:56 +02:00
|
|
|
goto start_unilateral_close_already_closing;
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is_pkt(input)) {
|
|
|
|
/* We ignore all other packets while closing. */
|
2016-01-21 21:11:46 +01:00
|
|
|
return unchanged_state(cstatus);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, INPUT_CLOSE_COMPLETE_TIMEOUT)) {
|
|
|
|
/* They didn't respond in time. Unilateral close. */
|
2016-01-21 21:11:45 +01:00
|
|
|
err = pkt_err(ctx, "Close timed out");
|
2015-09-29 02:17:56 +02:00
|
|
|
goto err_start_unilateral_close_already_closing;
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
|
|
|
goto fail_during_close;
|
|
|
|
|
|
|
|
case STATE_WAIT_FOR_CLOSE_ACK:
|
|
|
|
if (input_is(input, PKT_CLOSE_ACK)) {
|
2016-01-21 21:11:47 +01:00
|
|
|
err = accept_pkt_close_ack(ctx, peer, idata->pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
if (err)
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out, err);
|
2016-01-21 21:11:46 +01:00
|
|
|
set_peer_cond(peer, PEER_CLOSED);
|
2015-09-25 04:21:18 +02:00
|
|
|
/* Just wait for close to happen now. */
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_CLOSE_WAIT_CLOSE);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is_pkt(input)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_unexpected_pkt(peer, idata->pkt);
|
|
|
|
/* Don't reply to an error with an error. */
|
|
|
|
if (!input_is(input, PKT_ERROR)) {
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out,
|
2016-01-21 21:11:46 +01:00
|
|
|
pkt_err_unexpected(ctx, idata->pkt));
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
2016-01-21 21:11:46 +01:00
|
|
|
set_peer_cond(peer, PEER_CLOSED);
|
2015-09-25 04:21:18 +02:00
|
|
|
/* Just wait for close to happen now. */
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_CLOSE_WAIT_CLOSE);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_CLOSE_DONE)) {
|
|
|
|
/* They didn't ack, but we're closed, so stop. */
|
2016-01-21 21:11:46 +01:00
|
|
|
set_peer_cond(peer, PEER_CLOSED);
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_CLOSED);
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
|
|
|
goto fail_during_close;
|
|
|
|
|
|
|
|
/* Close states are regular: handle as a group. */
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_HTLCS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_STEAL:
|
|
|
|
case STATE_CLOSE_WAIT_SPENDTHEM:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_SPENDTHEM_WITH_HTLCS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_STEAL_SPENDTHEM:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_STEAL_SPENDTHEM_WITH_HTLCS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_CLOSE:
|
|
|
|
case STATE_CLOSE_WAIT_STEAL_CLOSE:
|
|
|
|
case STATE_CLOSE_WAIT_SPENDTHEM_CLOSE:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_SPENDTHEM_CLOSE_WITH_HTLCS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_WITH_HTLCS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_STEAL_OURCOMMIT:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_STEAL_OURCOMMIT_WITH_HTLCS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_SPENDTHEM_OURCOMMIT:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_SPENDTHEM_OURCOMMIT_WITH_HTLCS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_STEAL_SPENDTHEM_OURCOMMIT:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_STEAL_SPENDTHEM_OURCOMMIT_WITH_HTLCS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_CLOSE_OURCOMMIT:
|
|
|
|
case STATE_CLOSE_WAIT_STEAL_CLOSE_OURCOMMIT:
|
|
|
|
case STATE_CLOSE_WAIT_SPENDTHEM_CLOSE_OURCOMMIT:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_SPENDTHEM_CLOSE_OURCOMMIT_WITH_HTLCS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_OURCOMMIT:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_OURCOMMIT_WITH_HTLCS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_STEAL_SPENDOURS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_STEAL_SPENDOURS_WITH_HTLCS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_SPENDTHEM_SPENDOURS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_SPENDTHEM_SPENDOURS_WITH_HTLCS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_STEAL_SPENDTHEM_SPENDOURS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_STEAL_SPENDTHEM_SPENDOURS_WITH_HTLCS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_CLOSE_SPENDOURS:
|
|
|
|
case STATE_CLOSE_WAIT_STEAL_CLOSE_SPENDOURS:
|
|
|
|
case STATE_CLOSE_WAIT_SPENDTHEM_CLOSE_SPENDOURS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_SPENDTHEM_CLOSE_SPENDOURS_WITH_HTLCS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_SPENDOURS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_SPENDOURS_WITH_HTLCS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_OURCOMMIT:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_OURCOMMIT_WITH_HTLCS:
|
|
|
|
case STATE_CLOSE_WAIT_SPENDOURS:
|
|
|
|
case STATE_CLOSE_WAIT_SPENDOURS_WITH_HTLCS: {
|
2016-01-21 21:11:46 +01:00
|
|
|
unsigned int bits;
|
2015-09-25 04:21:18 +02:00
|
|
|
enum state_input closed;
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
bits = STATE_TO_BITS(peer->state);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2015-09-25 04:21:18 +02:00
|
|
|
/* Once we see a steal or spend completely buried, we
|
|
|
|
* close unless we're still waiting for htlcs*/
|
|
|
|
if (bits & STATE_CLOSE_HTLCS_BIT)
|
|
|
|
closed = STATE_CLOSE_WAIT_HTLCS;
|
|
|
|
else
|
|
|
|
closed = STATE_CLOSED;
|
|
|
|
|
2015-09-25 04:21:18 +02:00
|
|
|
if ((bits & STATE_CLOSE_STEAL_BIT)
|
|
|
|
&& input_is(input, BITCOIN_STEAL_DONE)) {
|
2015-09-25 04:21:18 +02:00
|
|
|
/* One a steal is complete, we don't care about htlcs
|
|
|
|
* (we stole them all) */
|
|
|
|
if (bits & STATE_CLOSE_HTLCS_BIT)
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_unwatch_all_htlc_outputs(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_CLOSED);
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((bits & STATE_CLOSE_SPENDTHEM_BIT)
|
|
|
|
&& input_is(input, BITCOIN_SPEND_THEIRS_DONE)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
BUILD_ASSERT(!(STATE_TO_BITS(STATE_CLOSE_WAIT_HTLCS)
|
2015-09-25 04:21:18 +02:00
|
|
|
& STATE_CLOSE_SPENDTHEM_BIT));
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, closed);
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((bits & STATE_CLOSE_CLOSE_BIT)
|
|
|
|
&& input_is(input, BITCOIN_CLOSE_DONE)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
BUILD_ASSERT(!(STATE_TO_BITS(STATE_CLOSE_WAIT_HTLCS)
|
2015-09-25 04:21:18 +02:00
|
|
|
& STATE_CLOSE_CLOSE_BIT));
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, closed);
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((bits & STATE_CLOSE_OURCOMMIT_BIT)
|
|
|
|
&& input_is(input, BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
BUILD_ASSERT(!(STATE_TO_BITS(STATE_CLOSE_WAIT_HTLCS)
|
2015-09-25 04:21:18 +02:00
|
|
|
& STATE_CLOSE_OURCOMMIT_BIT));
|
2016-01-21 21:11:45 +01:00
|
|
|
tx = bitcoin_spend_ours(ctx, peer);
|
2015-09-25 04:21:18 +02:00
|
|
|
/* Now we need to wait for our commit to be done. */
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_tx_broadcast(broadcast, tx);
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_watch_tx(peer, tx, BITCOIN_SPEND_OURS_DONE);
|
2015-09-25 04:21:18 +02:00
|
|
|
bits &= ~STATE_CLOSE_OURCOMMIT_BIT;
|
|
|
|
bits |= STATE_CLOSE_SPENDOURS_BIT;
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, BITS_TO_STATE(bits));
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((bits & STATE_CLOSE_SPENDOURS_BIT)
|
|
|
|
&& input_is(input, BITCOIN_SPEND_OURS_DONE)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
BUILD_ASSERT(!(STATE_TO_BITS(STATE_CLOSE_WAIT_HTLCS)
|
2015-09-25 04:21:18 +02:00
|
|
|
& STATE_CLOSE_SPENDOURS_BIT));
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, closed);
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
2015-09-25 04:21:18 +02:00
|
|
|
/* If we have htlcs, we can get other inputs... */
|
|
|
|
if (bits & STATE_CLOSE_HTLCS_BIT) {
|
|
|
|
if (input_is(input, INPUT_NO_MORE_HTLCS)) {
|
|
|
|
/* Clear bit, might lead to STATE_CLOSED. */
|
2016-01-21 21:11:46 +01:00
|
|
|
BUILD_ASSERT((BITS_TO_STATE(STATE_TO_BITS(STATE_CLOSE_WAIT_HTLCS) & ~STATE_CLOSE_HTLCS_BIT)) == STATE_CLOSED);
|
2015-09-25 04:21:18 +02:00
|
|
|
bits &= ~STATE_CLOSE_HTLCS_BIT;
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
BITS_TO_STATE(bits));
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_HTLC_TOTHEM_SPENT)) {
|
2016-01-21 21:11:47 +01:00
|
|
|
const struct htlc *htlc;
|
2015-09-25 04:21:18 +02:00
|
|
|
/* They revealed R value. */
|
2016-01-21 21:11:47 +01:00
|
|
|
htlc = peer_tx_revealed_r_value(peer, idata->btc);
|
2015-09-25 04:21:18 +02:00
|
|
|
/* We don't care any more. */
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_unwatch_htlc_output(peer, htlc,
|
2016-01-21 21:11:47 +01:00
|
|
|
INPUT_NO_MORE_HTLCS);
|
2016-01-21 21:11:46 +01:00
|
|
|
return unchanged_state(cstatus);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_HTLC_TOTHEM_TIMEOUT)){
|
2016-01-21 21:11:45 +01:00
|
|
|
tx = bitcoin_htlc_timeout(ctx,
|
2016-01-21 21:11:45 +01:00
|
|
|
peer,
|
2016-01-21 21:11:45 +01:00
|
|
|
idata->htlc);
|
2015-09-25 04:21:18 +02:00
|
|
|
/* HTLC timed out, spend it back to us. */
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_tx_broadcast(broadcast, tx);
|
2015-09-25 04:21:18 +02:00
|
|
|
/* Don't unwatch yet; they could yet
|
|
|
|
* try to spend, revealing rvalue. */
|
|
|
|
|
|
|
|
/* We're done when that gets buried. */
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_watch_htlc_spend(peer, tx, idata->htlc,
|
|
|
|
BITCOIN_HTLC_RETURN_SPEND_DONE);
|
2016-01-21 21:11:46 +01:00
|
|
|
return unchanged_state(cstatus);
|
2015-09-25 04:21:19 +02:00
|
|
|
} else if (input_is(input, INPUT_RVALUE)) {
|
2016-01-21 21:11:45 +01:00
|
|
|
tx = bitcoin_htlc_spend(ctx, peer,
|
2016-01-21 21:11:45 +01:00
|
|
|
idata->htlc);
|
|
|
|
|
2015-09-25 04:21:18 +02:00
|
|
|
/* Spend it... */
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_tx_broadcast(broadcast, tx);
|
2015-09-25 04:21:18 +02:00
|
|
|
/* We're done when it gets buried. */
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_watch_htlc_spend(peer, tx, idata->htlc,
|
|
|
|
BITCOIN_HTLC_FULFILL_SPEND_DONE);
|
2015-09-25 04:21:18 +02:00
|
|
|
/* Don't care about this one any more. */
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_unwatch_htlc_output(peer, idata->htlc,
|
|
|
|
INPUT_NO_MORE_HTLCS);
|
2016-01-21 21:11:46 +01:00
|
|
|
return unchanged_state(cstatus);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_HTLC_FULFILL_SPEND_DONE)) {
|
|
|
|
/* Stop watching spend, send
|
|
|
|
* INPUT_NO_MORE_HTLCS when done. */
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_unwatch_htlc_spend(peer, idata->htlc,
|
|
|
|
INPUT_NO_MORE_HTLCS);
|
2016-01-21 21:11:46 +01:00
|
|
|
return unchanged_state(cstatus);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_HTLC_RETURN_SPEND_DONE)) {
|
|
|
|
/* Stop watching spend, send
|
|
|
|
* INPUT_NO_MORE_HTLCS when done. */
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_unwatch_htlc_spend(peer, idata->htlc,
|
|
|
|
INPUT_NO_MORE_HTLCS);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/* Don't need to watch the HTLC output any more,
|
|
|
|
* either. */
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_unwatch_htlc_output(peer, idata->htlc,
|
|
|
|
INPUT_NO_MORE_HTLCS);
|
2016-01-21 21:11:46 +01:00
|
|
|
return unchanged_state(cstatus);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_HTLC_TOUS_TIMEOUT)) {
|
|
|
|
/* They can spend, we no longer care
|
|
|
|
* about this HTLC. */
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_unwatch_htlc_output(peer, idata->htlc,
|
|
|
|
INPUT_NO_MORE_HTLCS);
|
2016-01-21 21:11:46 +01:00
|
|
|
return unchanged_state(cstatus);
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we're just waiting for HTLCs, anything else is an error */
|
2016-01-21 21:11:46 +01:00
|
|
|
if (peer->state == STATE_CLOSE_WAIT_HTLCS)
|
2015-09-25 04:21:18 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now, other side can always spring a commit transaction on us
|
|
|
|
* (even if they already have, due to tx malleability).
|
|
|
|
*/
|
2015-09-25 04:21:18 +02:00
|
|
|
if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) {
|
2016-01-21 21:11:45 +01:00
|
|
|
tx = bitcoin_spend_theirs(ctx, peer, idata->btc);
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_tx_broadcast(broadcast, tx);
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_watch_tx(peer, tx, BITCOIN_SPEND_THEIRS_DONE);
|
|
|
|
/* HTLC watches: if any, set HTLCs bit. */
|
|
|
|
if (peer_watch_their_htlc_outputs(peer, idata->btc,
|
2015-09-25 04:21:18 +02:00
|
|
|
BITCOIN_HTLC_TOUS_TIMEOUT,
|
|
|
|
BITCOIN_HTLC_TOTHEM_SPENT,
|
2016-01-21 21:11:47 +01:00
|
|
|
BITCOIN_HTLC_TOTHEM_TIMEOUT))
|
2015-09-25 04:21:18 +02:00
|
|
|
bits |= STATE_CLOSE_HTLCS_BIT;
|
2016-01-21 21:11:47 +01:00
|
|
|
|
2015-09-25 04:21:18 +02:00
|
|
|
bits |= STATE_CLOSE_SPENDTHEM_BIT;
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state_bits(peer, cstatus, bits);
|
2015-09-25 04:21:18 +02:00
|
|
|
/* This can happen multiple times: need to steal ALL */
|
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_OTHERSPEND)) {
|
2016-01-21 21:11:45 +01:00
|
|
|
tx = bitcoin_steal(ctx, peer, idata->btc);
|
2016-01-21 21:11:45 +01:00
|
|
|
if (!tx)
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:45 +01:00
|
|
|
STATE_ERR_INFORMATION_LEAK);
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_tx_broadcast(broadcast, tx);
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_watch_tx(peer, tx, BITCOIN_STEAL_DONE);
|
2015-09-25 04:21:18 +02:00
|
|
|
bits |= STATE_CLOSE_STEAL_BIT;
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state_bits(peer, cstatus, bits);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_UNSPENT))
|
|
|
|
goto anchor_unspent;
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2015-09-25 04:21:18 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Should never happen. */
|
|
|
|
case STATE_ERR_INTERNAL:
|
|
|
|
case STATE_ERR_INFORMATION_LEAK:
|
|
|
|
case STATE_ERR_ANCHOR_TIMEOUT:
|
|
|
|
case STATE_ERR_ANCHOR_LOST:
|
|
|
|
case STATE_CLOSED:
|
|
|
|
case STATE_MAX:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_UNUSED_CLOSE_WAIT_STEAL_WITH_HTLCS:
|
|
|
|
case STATE_UNUSED_CLOSE_WAIT_CLOSE_WITH_HTLCS:
|
|
|
|
case STATE_UNUSED_CLOSE_WAIT_STEAL_CLOSE_WITH_HTLCS:
|
|
|
|
case STATE_UNUSED_CLOSE_WAIT_CLOSE_OURCOMMIT_WITH_HTLCS:
|
|
|
|
case STATE_UNUSED_CLOSE_WAIT_STEAL_CLOSE_OURCOMMIT_WITH_HTLCS:
|
|
|
|
case STATE_UNUSED_CLOSE_WAIT_CLOSE_SPENDOURS_WITH_HTLCS:
|
|
|
|
case STATE_UNUSED_CLOSE_WAIT_STEAL_CLOSE_SPENDOURS_WITH_HTLCS:
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_ERR_INTERNAL);
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* State machine should handle all possible states. */
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_ERR_INTERNAL);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
unexpected_pkt:
|
|
|
|
/*
|
|
|
|
* We got a weird packet, so we need to close unilaterally.
|
|
|
|
*/
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_unexpected_pkt(peer, idata->pkt);
|
|
|
|
|
2015-09-25 04:21:18 +02:00
|
|
|
/* Don't reply to an error with an error. */
|
|
|
|
if (input_is(input, PKT_ERROR)) {
|
|
|
|
goto start_unilateral_close;
|
|
|
|
}
|
2016-01-21 21:11:46 +01:00
|
|
|
err = pkt_err_unexpected(ctx, idata->pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto err_start_unilateral_close;
|
|
|
|
|
|
|
|
unexpected_pkt_nocleanup:
|
|
|
|
/*
|
|
|
|
* Unexpected packet, but nothing sent to chain yet, so no cleanup.
|
|
|
|
*/
|
2016-01-21 21:11:46 +01:00
|
|
|
/* Don't reply to an error with an error. */
|
|
|
|
if (input_is(input, PKT_ERROR)) {
|
|
|
|
goto close_nocleanup;
|
|
|
|
}
|
2016-01-21 21:11:46 +01:00
|
|
|
err = pkt_err_unexpected(ctx, idata->pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
goto err_close_nocleanup;
|
|
|
|
|
|
|
|
anchor_unspent:
|
|
|
|
/*
|
|
|
|
* Bitcoind tells us anchor got double-spent. If we double-spent it
|
|
|
|
* then we're malfunctioning. If they double-spent it, then they
|
|
|
|
* managed to cheat us: post_to_reddit();
|
|
|
|
*/
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_ERR_ANCHOR_LOST);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
err_close_nocleanup:
|
|
|
|
/*
|
|
|
|
* Something went wrong, but we haven't sent anything to the blockchain
|
|
|
|
* so there's nothing to clean up.
|
|
|
|
*/
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out, err);
|
2016-01-21 21:11:46 +01:00
|
|
|
|
|
|
|
close_nocleanup:
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_CMD_OK, PEER_CLOSED);
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_CLOSED);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
err_start_unilateral_close:
|
|
|
|
/*
|
|
|
|
* They timed out, or were broken; we are going to close unilaterally.
|
|
|
|
*/
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out, err);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
start_unilateral_close:
|
|
|
|
/*
|
|
|
|
* Close unilaterally.
|
|
|
|
*/
|
|
|
|
/* No more inputs, no more commands. */
|
2016-01-21 21:11:46 +01:00
|
|
|
set_peer_cond(peer, PEER_CLOSED);
|
2016-01-21 21:11:45 +01:00
|
|
|
tx = bitcoin_commit(ctx, peer);
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_tx_broadcast(broadcast, tx);
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_watch_delayed(peer, tx, BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/* HTLC watches. */
|
2016-01-21 21:11:47 +01:00
|
|
|
if (peer_watch_our_htlc_outputs(peer, tx,
|
2015-09-25 04:21:18 +02:00
|
|
|
BITCOIN_HTLC_TOUS_TIMEOUT,
|
|
|
|
BITCOIN_HTLC_TOTHEM_SPENT,
|
2016-01-21 21:11:47 +01:00
|
|
|
BITCOIN_HTLC_TOTHEM_TIMEOUT))
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:45 +01:00
|
|
|
STATE_CLOSE_WAIT_OURCOMMIT_WITH_HTLCS);
|
2016-01-21 21:11:47 +01:00
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_CLOSE_WAIT_OURCOMMIT);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2015-09-29 02:17:56 +02:00
|
|
|
err_start_unilateral_close_already_closing:
|
|
|
|
/*
|
|
|
|
* They timed out, or were broken; we are going to close unilaterally.
|
|
|
|
*/
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out, err);
|
2015-09-29 02:17:56 +02:00
|
|
|
|
|
|
|
start_unilateral_close_already_closing:
|
|
|
|
/*
|
|
|
|
* Close unilaterally.
|
|
|
|
*/
|
|
|
|
/* No more inputs, no more commands. */
|
2016-01-21 21:11:46 +01:00
|
|
|
set_peer_cond(peer, PEER_CLOSED);
|
2016-01-21 21:11:45 +01:00
|
|
|
tx = bitcoin_commit(ctx, peer);
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_tx_broadcast(broadcast, tx);
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_watch_delayed(peer, tx, BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED);
|
2015-09-29 02:17:56 +02:00
|
|
|
|
|
|
|
/* We agreed to close: shouldn't have any HTLCs */
|
2016-01-21 21:11:45 +01:00
|
|
|
if (committed_to_htlcs(peer))
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_ERR_INTERNAL);
|
2015-09-29 02:17:56 +02:00
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_CLOSE_WAIT_CLOSE_OURCOMMIT);
|
2015-09-29 02:17:56 +02:00
|
|
|
|
2015-09-25 04:21:18 +02:00
|
|
|
them_unilateral:
|
2015-09-25 04:21:18 +02:00
|
|
|
assert(input == BITCOIN_ANCHOR_THEIRSPEND);
|
|
|
|
|
2015-09-25 04:21:18 +02:00
|
|
|
/*
|
|
|
|
* Bitcoind tells us they did unilateral close.
|
|
|
|
*/
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out, pkt_err(ctx, "Commit tx noticed"));
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/* No more inputs, no more commands. */
|
2016-01-21 21:11:46 +01:00
|
|
|
set_peer_cond(peer, PEER_CLOSED);
|
2016-01-21 21:11:45 +01:00
|
|
|
tx = bitcoin_spend_theirs(ctx, peer, idata->btc);
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_tx_broadcast(broadcast, tx);
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_watch_tx(peer, tx, BITCOIN_SPEND_THEIRS_DONE);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/* HTLC watches (based on what they broadcast, which *may* be out
|
|
|
|
* of step with our current state by +/- 1 htlc. */
|
2016-01-21 21:11:47 +01:00
|
|
|
if (peer_watch_their_htlc_outputs(peer, idata->btc,
|
2015-09-25 04:21:18 +02:00
|
|
|
BITCOIN_HTLC_TOUS_TIMEOUT,
|
|
|
|
BITCOIN_HTLC_TOTHEM_SPENT,
|
2016-01-21 21:11:47 +01:00
|
|
|
BITCOIN_HTLC_TOTHEM_TIMEOUT))
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
STATE_CLOSE_WAIT_SPENDTHEM_WITH_HTLCS);
|
2016-01-21 21:11:47 +01:00
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_CLOSE_WAIT_SPENDTHEM);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
accept_htlc_update:
|
2016-01-21 21:11:47 +01:00
|
|
|
err = accept_pkt_htlc_update(ctx, peer, idata->pkt, &decline);
|
2015-09-25 04:21:18 +02:00
|
|
|
if (err)
|
|
|
|
goto err_start_unilateral_close;
|
|
|
|
if (decline) {
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out, decline);
|
2016-01-21 21:11:46 +01:00
|
|
|
peer_htlc_declined(peer, decline);
|
2015-09-25 04:21:19 +02:00
|
|
|
/* No update means no priority change. */
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_BUSY, PEER_CMD_OK);
|
2016-01-21 21:11:46 +01:00
|
|
|
/* We may already be in STATE_NORMAL */
|
|
|
|
return next_state_nocheck(peer, cstatus,
|
|
|
|
prio(peer->state, STATE_NORMAL));
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out, pkt_update_accept(ctx, peer));
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
|
|
|
prio(peer->state, STATE_WAIT_FOR_UPDATE_SIG));
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
accept_htlc_routefail:
|
2016-01-21 21:11:47 +01:00
|
|
|
err = accept_pkt_htlc_routefail(ctx, peer, idata->pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
if (err)
|
|
|
|
goto err_start_unilateral_close;
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out, pkt_update_accept(ctx, peer));
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
|
|
|
prio(peer->state, STATE_WAIT_FOR_UPDATE_SIG));
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
accept_htlc_timedout:
|
2016-01-21 21:11:47 +01:00
|
|
|
err = accept_pkt_htlc_timedout(ctx, peer, idata->pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
if (err)
|
|
|
|
goto err_start_unilateral_close;
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out, pkt_update_accept(ctx, peer));
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
|
|
|
prio(peer->state, STATE_WAIT_FOR_UPDATE_SIG));
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2015-09-25 04:21:18 +02:00
|
|
|
accept_htlc_fulfill:
|
2016-01-21 21:11:46 +01:00
|
|
|
err = accept_pkt_htlc_fulfill(ctx, peer, idata->pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
if (err)
|
|
|
|
goto err_start_unilateral_close;
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out, pkt_update_accept(ctx, peer));
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
|
|
|
prio(peer->state, STATE_WAIT_FOR_UPDATE_SIG));
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
start_closing:
|
|
|
|
/*
|
|
|
|
* Start a mutual close.
|
|
|
|
*/
|
2015-09-25 04:21:18 +02:00
|
|
|
/* Protocol doesn't (currently?) allow closing with HTLCs. */
|
2016-01-21 21:11:45 +01:00
|
|
|
if (committed_to_htlcs(peer)) {
|
2016-01-21 21:11:45 +01:00
|
|
|
err = pkt_err(ctx, "Close forced due to HTLCs");
|
2015-09-25 04:21:18 +02:00
|
|
|
goto err_start_unilateral_close;
|
|
|
|
}
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_watch_close(peer, BITCOIN_CLOSE_DONE, INPUT_CLOSE_COMPLETE_TIMEOUT);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
/* No more commands, we're already closing. */
|
|
|
|
set_peer_cond(peer, PEER_CLOSING);
|
|
|
|
|
2015-09-25 04:21:18 +02:00
|
|
|
/* As soon as we send packet, they could close. */
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out, pkt_close(ctx, peer));
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_WAIT_FOR_CLOSE_COMPLETE);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
accept_closing:
|
2016-01-21 21:11:47 +01:00
|
|
|
err = accept_pkt_close(ctx, peer, idata->pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
if (err)
|
|
|
|
goto err_start_unilateral_close;
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_watch_close(peer, BITCOIN_CLOSE_DONE, INPUT_NONE);
|
|
|
|
/* Send close TX. */
|
|
|
|
queue_tx_broadcast(broadcast, bitcoin_close(ctx, peer));
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out, pkt_close_complete(ctx, peer));
|
2015-09-25 04:21:18 +02:00
|
|
|
/* No more commands, we're already closing. */
|
2016-01-21 21:11:46 +01:00
|
|
|
set_peer_cond(peer, PEER_CLOSING);
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_WAIT_FOR_CLOSE_ACK);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
instant_close:
|
|
|
|
/*
|
|
|
|
* Closing, but we haven't sent anything to the blockchain so
|
|
|
|
* there's nothing to clean up.
|
|
|
|
*/
|
|
|
|
/* FIXME: Should we tell other side we're going? */
|
2016-01-21 21:11:46 +01:00
|
|
|
set_peer_cond(peer, PEER_CLOSED);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/* We can't have any HTLCs, since we haven't started. */
|
2016-01-21 21:11:45 +01:00
|
|
|
if (committed_to_htlcs(peer))
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_ERR_INTERNAL);
|
|
|
|
return next_state(peer, cstatus, STATE_CLOSED);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
fail_during_close:
|
|
|
|
/*
|
|
|
|
* We've broadcast close tx; if anything goes wrong, we just close
|
|
|
|
* connection and wait.
|
|
|
|
*/
|
2016-01-21 21:11:46 +01:00
|
|
|
set_peer_cond(peer, PEER_CLOSED);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/* Once close tx is deep enough, we consider it done. */
|
|
|
|
if (input_is(input, BITCOIN_CLOSE_DONE)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_CLOSED);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) {
|
|
|
|
/* A reorganization could make this happen. */
|
2016-01-21 21:11:45 +01:00
|
|
|
tx = bitcoin_spend_theirs(ctx, peer, idata->btc);
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_tx_broadcast(broadcast, tx);
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_watch_tx(peer, tx, BITCOIN_SPEND_THEIRS_DONE);
|
|
|
|
if (peer_watch_their_htlc_outputs(peer, idata->btc,
|
2015-09-25 04:21:18 +02:00
|
|
|
BITCOIN_HTLC_TOUS_TIMEOUT,
|
|
|
|
BITCOIN_HTLC_TOTHEM_SPENT,
|
2016-01-21 21:11:47 +01:00
|
|
|
BITCOIN_HTLC_TOTHEM_TIMEOUT)) {
|
|
|
|
/* Expect either close or spendthem to complete */
|
2015-09-25 04:21:19 +02:00
|
|
|
/* FIXME: Make sure caller uses INPUT_RVAL
|
|
|
|
* if they were in the middle of FULFILL! */
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
STATE_CLOSE_WAIT_SPENDTHEM_CLOSE_WITH_HTLCS);
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
STATE_CLOSE_WAIT_SPENDTHEM_CLOSE);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_OTHERSPEND)) {
|
2016-01-21 21:11:45 +01:00
|
|
|
tx = bitcoin_steal(ctx, peer, idata->btc);
|
2016-01-21 21:11:45 +01:00
|
|
|
if (!tx)
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:45 +01:00
|
|
|
STATE_ERR_INFORMATION_LEAK);
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_tx_broadcast(broadcast, tx);
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_watch_tx(peer, tx, BITCOIN_STEAL_DONE);
|
2015-09-25 04:21:18 +02:00
|
|
|
/* Expect either close or steal to complete */
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
STATE_CLOSE_WAIT_STEAL_CLOSE);
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, BITCOIN_ANCHOR_UNSPENT)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_ERR_ANCHOR_LOST);
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_ERR_INTERNAL);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
old_commit_spotted:
|
|
|
|
/*
|
|
|
|
* bitcoind reported a broadcast of the not-latest commit tx.
|
|
|
|
*/
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_pkt(out, pkt_err(ctx, "Otherspend noticed"));
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/* No more packets, no more commands. */
|
2016-01-21 21:11:46 +01:00
|
|
|
set_peer_cond(peer, PEER_CLOSED);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/* If we can't find it, we're lost. */
|
2016-01-21 21:11:45 +01:00
|
|
|
tx = bitcoin_steal(ctx, peer, idata->btc);
|
2016-01-21 21:11:45 +01:00
|
|
|
if (!tx)
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus,
|
2016-01-21 21:11:46 +01:00
|
|
|
STATE_ERR_INFORMATION_LEAK);
|
2016-01-21 21:11:47 +01:00
|
|
|
queue_tx_broadcast(broadcast, tx);
|
2016-01-21 21:11:47 +01:00
|
|
|
peer_watch_tx(peer, tx, BITCOIN_STEAL_DONE);
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_CLOSE_WAIT_STEAL);
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|