2015-09-25 04:21:18 +02:00
|
|
|
#include <ccan/build_assert/build_assert.h>
|
2016-01-21 21:11:49 +01:00
|
|
|
#ifndef TEST_STATE_COVERAGE
|
|
|
|
#include <daemon/peer.h>
|
|
|
|
#endif
|
2016-01-21 21:11:46 +01:00
|
|
|
#include <state.h>
|
2015-09-25 04:21:18 +02:00
|
|
|
|
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.
|
|
|
|
*
|
2016-03-31 08:43:20 +02:00
|
|
|
* This happens in three cases:
|
2016-01-21 21:11:46 +01:00
|
|
|
* - 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.
|
2016-03-31 08:43:20 +02:00
|
|
|
* - HTLC add/remove in STATE_NORMAL.
|
2016-01-21 21:11:46 +01:00
|
|
|
*/
|
|
|
|
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-03-31 08:43:20 +02:00
|
|
|
/* FIXME: We do this when a command succeeds instantly, and
|
|
|
|
* state is unchanged. */
|
|
|
|
static enum command_status instant_cmd_success(struct peer *peer,
|
|
|
|
enum command_status cstatus)
|
|
|
|
{
|
|
|
|
assert(peer->cond == PEER_CMD_OK);
|
|
|
|
assert(cstatus == CMD_NONE);
|
|
|
|
return CMD_SUCCESS;
|
|
|
|
}
|
|
|
|
|
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-03-30 08:27:18 +02:00
|
|
|
enum command_status state(struct peer *peer,
|
2016-01-21 21:11:46 +01:00
|
|
|
const enum state_input input,
|
|
|
|
const union input *idata,
|
2016-01-21 21:11:47 +01:00
|
|
|
const struct bitcoin_tx **broadcast)
|
2015-09-25 04:21:18 +02:00
|
|
|
{
|
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
|
|
|
*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-03-30 08:27:18 +02:00
|
|
|
queue_pkt_open(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-03-30 08:27:18 +02:00
|
|
|
queue_pkt_open(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-03-30 08:27:18 +02:00
|
|
|
err = accept_pkt_open(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);
|
2016-03-24 02:39:44 +01:00
|
|
|
} else if (input_is(input, CMD_CLOSE)
|
|
|
|
|| input_is(input, INPUT_CONNECTION_LOST)) {
|
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-03-30 08:27:18 +02:00
|
|
|
err = accept_pkt_open(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);
|
2016-03-24 02:39:44 +01:00
|
|
|
} else if (input_is(input, CMD_CLOSE)
|
|
|
|
|| input_is(input, INPUT_CONNECTION_LOST)) {
|
2016-01-21 21:11:47 +01:00
|
|
|
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-03-30 08:27:18 +02:00
|
|
|
queue_pkt_anchor(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);
|
2016-03-24 02:39:44 +01:00
|
|
|
} else if (input_is(input, CMD_CLOSE)
|
|
|
|
|| input_is(input, INPUT_CONNECTION_LOST)) {
|
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-03-30 08:27:18 +02:00
|
|
|
err = accept_pkt_anchor(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-03-30 08:27:18 +02:00
|
|
|
queue_pkt_open_commit_sig(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);
|
2016-03-24 02:39:44 +01:00
|
|
|
} else if (input_is(input, CMD_CLOSE)
|
|
|
|
|| input_is(input, INPUT_CONNECTION_LOST)) {
|
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-03-30 08:27:18 +02:00
|
|
|
err = accept_pkt_open_commit_sig(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-03-30 08:27:18 +02:00
|
|
|
queue_tx_broadcast(broadcast, bitcoin_anchor(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);
|
2016-03-24 02:39:44 +01:00
|
|
|
} else if (input_is(input, CMD_CLOSE)
|
|
|
|
|| input_is(input, INPUT_CONNECTION_LOST)) {
|
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-03-30 08:27:18 +02:00
|
|
|
err = accept_pkt_open_complete(peer, idata->pkt);
|
2016-03-08 01:09:15 +01:00
|
|
|
if (err) {
|
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
|
|
|
/* We no longer care about anchor depth. */
|
|
|
|
peer_unwatch_anchor_depth(peer,
|
|
|
|
BITCOIN_ANCHOR_DEPTHOK,
|
|
|
|
INPUT_NONE);
|
|
|
|
goto err_start_unilateral_close;
|
|
|
|
}
|
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-03-30 08:27:18 +02:00
|
|
|
queue_pkt_open_complete(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-03-31 08:43:20 +02:00
|
|
|
return next_state(peer, cstatus, STATE_NORMAL);
|
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);
|
2016-03-24 02:39:41 +01:00
|
|
|
goto start_clearing;
|
2016-03-24 02:39:44 +01:00
|
|
|
} else if (input_is(input, INPUT_CONNECTION_LOST)) {
|
|
|
|
/* We no longer care about anchor depth. */
|
|
|
|
peer_unwatch_anchor_depth(peer,
|
|
|
|
BITCOIN_ANCHOR_DEPTHOK,
|
|
|
|
INPUT_NONE);
|
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
|
|
|
goto start_unilateral_close;
|
2016-03-24 02:39:41 +01:00
|
|
|
} else if (input_is(input, PKT_CLOSE_CLEARING)) {
|
2015-09-25 04:21:18 +02:00
|
|
|
/* 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-03-24 02:39:41 +01:00
|
|
|
goto accept_clearing;
|
2015-09-25 04:21:18 +02:00
|
|
|
} 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-03-08 01:08:15 +01:00
|
|
|
goto unexpected_pkt;
|
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-03-30 08:27:18 +02:00
|
|
|
err = accept_pkt_open_complete(peer, idata->pkt);
|
2016-03-08 01:09:15 +01:00
|
|
|
if (err) {
|
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
|
|
|
/* We no longer care about anchor depth. */
|
|
|
|
peer_unwatch_anchor_depth(peer,
|
|
|
|
BITCOIN_ANCHOR_DEPTHOK,
|
|
|
|
BITCOIN_ANCHOR_TIMEOUT);
|
|
|
|
goto err_start_unilateral_close;
|
|
|
|
}
|
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-03-30 08:27:18 +02:00
|
|
|
queue_pkt_err(peer, pkt_err(peer, "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-03-30 08:27:18 +02:00
|
|
|
queue_pkt_open_complete(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-03-31 08:43:20 +02:00
|
|
|
return next_state(peer, cstatus, STATE_NORMAL);
|
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: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);
|
2016-03-24 02:39:41 +01:00
|
|
|
goto start_clearing;
|
2016-03-24 02:39:44 +01:00
|
|
|
} else if (input_is(input, INPUT_CONNECTION_LOST)) {
|
|
|
|
/* We no longer care about anchor depth. */
|
|
|
|
peer_unwatch_anchor_depth(peer,
|
|
|
|
BITCOIN_ANCHOR_DEPTHOK,
|
|
|
|
BITCOIN_ANCHOR_TIMEOUT);
|
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
|
|
|
goto start_unilateral_close;
|
2016-03-24 02:39:41 +01:00
|
|
|
} else if (input_is(input, PKT_CLOSE_CLEARING)) {
|
2015-09-25 04:21:18 +02:00
|
|
|
/* 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);
|
2016-03-24 02:39:41 +01:00
|
|
|
goto accept_clearing;
|
2015-09-25 04:21:18 +02:00
|
|
|
} 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-03-31 08:43:20 +02:00
|
|
|
return next_state(peer, cstatus, STATE_NORMAL);
|
2016-01-21 21:11:46 +01:00
|
|
|
} else {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_SUCCESS);
|
2016-03-31 08:43:20 +02:00
|
|
|
return next_state(peer, cstatus, STATE_NORMAL);
|
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);
|
2016-03-24 02:39:41 +01:00
|
|
|
goto start_clearing;
|
2016-03-24 02:39:44 +01:00
|
|
|
} else if (input_is(input, INPUT_CONNECTION_LOST)) {
|
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
|
|
|
goto start_unilateral_close;
|
2016-03-24 02:39:41 +01:00
|
|
|
} else if (input_is(input, PKT_CLOSE_CLEARING)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
complete_cmd(peer, &cstatus, CMD_FAIL);
|
2016-03-24 02:39:41 +01:00
|
|
|
goto accept_clearing;
|
2015-09-25 04:21:18 +02:00
|
|
|
} 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.
|
|
|
|
*/
|
2016-03-31 08:43:20 +02:00
|
|
|
case STATE_NORMAL:
|
|
|
|
/*
|
|
|
|
* FIXME: For simplicity, we disallow new staging requests
|
|
|
|
* while a commit is outstanding.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* You can only issue this command one at a time. */
|
|
|
|
if (input_is(input, CMD_SEND_COMMIT)) {
|
|
|
|
queue_pkt_commit(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
change_peer_cond(peer, PEER_CMD_OK, PEER_BUSY);
|
2016-03-31 08:43:20 +02:00
|
|
|
return next_state(peer, cstatus, STATE_NORMAL_COMMITTING);
|
|
|
|
} else if (input_is(input, CMD_SEND_HTLC_ADD)) {
|
|
|
|
/* We are to send an HTLC add. */
|
|
|
|
queue_pkt_htlc_add(peer, idata->htlc_prog);
|
|
|
|
return instant_cmd_success(peer, cstatus);
|
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-03-30 08:27:18 +02:00
|
|
|
queue_pkt_htlc_fulfill(peer, idata->htlc_prog);
|
2016-03-31 08:43:20 +02:00
|
|
|
return instant_cmd_success(peer, cstatus);
|
2016-03-08 01:11:15 +01:00
|
|
|
} else if (input_is(input, CMD_SEND_HTLC_FAIL)) {
|
|
|
|
/* We are to send an HTLC fail. */
|
2016-03-30 08:27:18 +02:00
|
|
|
queue_pkt_htlc_fail(peer, idata->htlc_prog);
|
2016-03-31 08:43:20 +02:00
|
|
|
return instant_cmd_success(peer, cstatus);
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Fall through... */
|
|
|
|
case STATE_NORMAL_COMMITTING:
|
|
|
|
/* Only expect revocation in STATE_NORMAL_COMMITTING */
|
|
|
|
if (peer->state == STATE_NORMAL_COMMITTING
|
|
|
|
&& input_is(input, PKT_UPDATE_REVOCATION)) {
|
|
|
|
err = accept_pkt_revocation(peer, idata->pkt);
|
2015-09-25 04:21:18 +02: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_start_unilateral_close;
|
|
|
|
}
|
2016-03-31 08:43:20 +02:00
|
|
|
complete_cmd(peer, &cstatus, CMD_SUCCESS);
|
|
|
|
return next_state(peer, cstatus, STATE_NORMAL);
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
2016-03-31 08:43:20 +02:00
|
|
|
|
|
|
|
if (input_is(input, CMD_CLOSE)) {
|
|
|
|
goto start_clearing;
|
|
|
|
} else if (input_is(input, PKT_UPDATE_ADD_HTLC)) {
|
|
|
|
err = accept_pkt_htlc_add(peer, idata->pkt);
|
|
|
|
if (err)
|
2015-09-25 04:21:18 +02:00
|
|
|
goto err_start_unilateral_close;
|
2016-03-31 08:43:20 +02:00
|
|
|
return unchanged_state(cstatus);
|
|
|
|
} else if (input_is(input, PKT_UPDATE_FULFILL_HTLC)) {
|
|
|
|
err = accept_pkt_htlc_fulfill(peer, idata->pkt);
|
|
|
|
if (err)
|
|
|
|
goto err_start_unilateral_close;
|
|
|
|
return unchanged_state(cstatus);
|
|
|
|
} else if (input_is(input, PKT_UPDATE_FAIL_HTLC)) {
|
|
|
|
err = accept_pkt_htlc_fail(peer, idata->pkt);
|
|
|
|
if (err)
|
|
|
|
goto err_start_unilateral_close;
|
|
|
|
return unchanged_state(cstatus);
|
|
|
|
} else if (input_is(input, PKT_UPDATE_COMMIT)) {
|
|
|
|
err = accept_pkt_commit(peer, idata->pkt);
|
|
|
|
if (err)
|
|
|
|
goto err_start_unilateral_close;
|
|
|
|
queue_pkt_revocation(peer);
|
|
|
|
return unchanged_state(cstatus);
|
2015-09-25 04:21:18 +02:00
|
|
|
} 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;
|
2016-03-24 02:39:44 +01:00
|
|
|
} else if (input_is(input, INPUT_CONNECTION_LOST)) {
|
|
|
|
goto start_unilateral_close;
|
2016-03-24 02:39:41 +01:00
|
|
|
} else if (input_is(input, PKT_CLOSE_CLEARING)) {
|
|
|
|
goto accept_clearing;
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is_pkt(input)) {
|
|
|
|
goto unexpected_pkt;
|
|
|
|
}
|
|
|
|
break;
|
2016-03-24 02:39:41 +01:00
|
|
|
case STATE_US_CLEARING:
|
|
|
|
/* This is their reply once they're clearing too. */
|
|
|
|
if (input_is(input, PKT_CLOSE_CLEARING)) {
|
2016-03-30 08:27:18 +02:00
|
|
|
err = accept_pkt_close_clearing(peer, idata->pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
if (err)
|
2016-03-24 02:39:41 +01:00
|
|
|
goto err_start_unilateral_close;
|
|
|
|
|
|
|
|
/* Notify us when there are no more htlcs in
|
|
|
|
* either commit tx */
|
|
|
|
peer_watch_htlcs_cleared(peer, INPUT_HTLCS_CLEARED);
|
|
|
|
|
|
|
|
return next_state(peer, cstatus, STATE_BOTH_CLEARING);
|
|
|
|
/* FIXME: We must continue to allow fulfill & fail! */
|
|
|
|
} else if (input_is(input, CMD_SEND_HTLC_FAIL)
|
|
|
|
|| input_is(input, CMD_SEND_HTLC_FULFILL)) {
|
2016-03-30 08:27:18 +02:00
|
|
|
err = pkt_err(peer, "FIXME: cmd during clearing.");
|
2016-03-24 02:39:41 +01:00
|
|
|
goto err_start_unilateral_close;
|
2016-03-24 02:39:44 +01:00
|
|
|
} else if (input_is(input, INPUT_CONNECTION_LOST)) {
|
|
|
|
goto start_unilateral_close;
|
2016-03-24 02:39:41 +01:00
|
|
|
} else if (input_is_pkt(input)) {
|
|
|
|
/* FIXME: We must continue to allow add, fulfill & fail packets */
|
|
|
|
goto unexpected_pkt;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STATE_BOTH_CLEARING:
|
|
|
|
if (input_is(input, INPUT_HTLCS_CLEARED)) {
|
|
|
|
goto start_closing_cleared;
|
|
|
|
} else if (input_is(input, CMD_SEND_HTLC_FAIL)
|
|
|
|
|| input_is(input, CMD_SEND_HTLC_FULFILL)) {
|
2016-03-30 08:27:18 +02:00
|
|
|
err = pkt_err(peer, "FIXME: cmd during clearing.");
|
2016-03-24 02:39:41 +01:00
|
|
|
goto err_start_unilateral_close;
|
2016-03-24 02:39:44 +01:00
|
|
|
} else if (input_is(input, INPUT_CONNECTION_LOST)) {
|
|
|
|
goto start_unilateral_close;
|
2016-03-24 02:39:41 +01:00
|
|
|
} else if (input_is_pkt(input)) {
|
|
|
|
/* FIXME: We must continue to allow fulfill & fail packets */
|
|
|
|
goto unexpected_pkt;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STATE_WAIT_FOR_CLOSE_SIG:
|
|
|
|
if (input_is(input, PKT_CLOSE_SIGNATURE)) {
|
2016-04-11 09:00:43 +02:00
|
|
|
bool acked, we_agree;
|
|
|
|
err = accept_pkt_close_sig(peer, idata->pkt,
|
|
|
|
&acked, &we_agree);
|
2015-09-25 04:21:18 +02:00
|
|
|
if (err)
|
2016-03-24 02:39:41 +01:00
|
|
|
goto err_start_unilateral_close;
|
|
|
|
|
2016-04-11 09:00:43 +02:00
|
|
|
/* Are we about to offer the same fee they did? */
|
|
|
|
if (we_agree) {
|
|
|
|
/* Offer the new fee. */
|
|
|
|
queue_pkt_close_signature(peer);
|
|
|
|
acked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do fees now match? */
|
|
|
|
if (acked) {
|
2016-03-24 02:39:41 +01:00
|
|
|
peer_unwatch_close_timeout(peer,
|
2016-01-21 21:11:47 +01:00
|
|
|
INPUT_CLOSE_COMPLETE_TIMEOUT);
|
2016-03-24 02:39:41 +01:00
|
|
|
|
|
|
|
/* Send close TX. */
|
|
|
|
queue_tx_broadcast(broadcast,
|
2016-03-30 08:27:18 +02:00
|
|
|
bitcoin_close(peer));
|
2016-03-24 02:39:41 +01:00
|
|
|
change_peer_cond(peer,
|
|
|
|
PEER_CLOSING, PEER_CLOSED);
|
|
|
|
return next_state(peer, cstatus,
|
|
|
|
STATE_CLOSE_WAIT_CLOSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Offer the new fee. */
|
2016-03-30 08:27:18 +02:00
|
|
|
queue_pkt_close_signature(peer);
|
2016-01-21 21:11:46 +01:00
|
|
|
return unchanged_state(cstatus);
|
2016-03-24 02:39:44 +01:00
|
|
|
} else if (input_is(input, INPUT_CONNECTION_LOST)) {
|
|
|
|
goto start_unilateral_close;
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is(input, INPUT_CLOSE_COMPLETE_TIMEOUT)) {
|
2016-03-30 08:27:18 +02:00
|
|
|
err = pkt_err(peer, "Close timed out");
|
2016-03-24 02:39:41 +01:00
|
|
|
goto err_start_unilateral_close;
|
2015-09-25 04:21:18 +02:00
|
|
|
} else if (input_is_pkt(input)) {
|
2016-03-24 02:39:41 +01:00
|
|
|
goto unexpected_pkt;
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
2016-03-24 02:39:41 +01:00
|
|
|
break;
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/* 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:
|
2016-05-03 03:35:20 +02:00
|
|
|
case STATE_CLOSE_WAIT_THEIRCOMMIT:
|
|
|
|
case STATE_CLOSE_WAIT_THEIRCOMMIT_WITH_HTLCS:
|
|
|
|
case STATE_CLOSE_WAIT_STEAL_THEIRCOMMIT:
|
|
|
|
case STATE_CLOSE_WAIT_STEAL_THEIRCOMMIT_WITH_HTLCS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_CLOSE:
|
|
|
|
case STATE_CLOSE_WAIT_STEAL_CLOSE:
|
2016-05-03 03:35:20 +02:00
|
|
|
case STATE_CLOSE_WAIT_THEIRCOMMIT_CLOSE:
|
|
|
|
case STATE_CLOSE_WAIT_THEIRCOMMIT_CLOSE_WITH_HTLCS:
|
|
|
|
case STATE_CLOSE_WAIT_STEAL_THEIRCOMMIT_CLOSE:
|
|
|
|
case STATE_CLOSE_WAIT_STEAL_THEIRCOMMIT_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:
|
2016-05-03 03:35:20 +02:00
|
|
|
case STATE_CLOSE_WAIT_THEIRCOMMIT_OURCOMMIT:
|
|
|
|
case STATE_CLOSE_WAIT_THEIRCOMMIT_OURCOMMIT_WITH_HTLCS:
|
|
|
|
case STATE_CLOSE_WAIT_STEAL_THEIRCOMMIT_OURCOMMIT:
|
|
|
|
case STATE_CLOSE_WAIT_STEAL_THEIRCOMMIT_OURCOMMIT_WITH_HTLCS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_CLOSE_OURCOMMIT:
|
|
|
|
case STATE_CLOSE_WAIT_STEAL_CLOSE_OURCOMMIT:
|
2016-05-03 03:35:20 +02:00
|
|
|
case STATE_CLOSE_WAIT_THEIRCOMMIT_CLOSE_OURCOMMIT:
|
|
|
|
case STATE_CLOSE_WAIT_THEIRCOMMIT_CLOSE_OURCOMMIT_WITH_HTLCS:
|
|
|
|
case STATE_CLOSE_WAIT_STEAL_THEIRCOMMIT_CLOSE_OURCOMMIT:
|
|
|
|
case STATE_CLOSE_WAIT_STEAL_THEIRCOMMIT_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:
|
2016-05-03 03:35:20 +02:00
|
|
|
case STATE_CLOSE_WAIT_THEIRCOMMIT_SPENDOURS:
|
|
|
|
case STATE_CLOSE_WAIT_THEIRCOMMIT_SPENDOURS_WITH_HTLCS:
|
|
|
|
case STATE_CLOSE_WAIT_STEAL_THEIRCOMMIT_SPENDOURS:
|
|
|
|
case STATE_CLOSE_WAIT_STEAL_THEIRCOMMIT_SPENDOURS_WITH_HTLCS:
|
2015-09-25 04:21:18 +02:00
|
|
|
case STATE_CLOSE_WAIT_CLOSE_SPENDOURS:
|
|
|
|
case STATE_CLOSE_WAIT_STEAL_CLOSE_SPENDOURS:
|
2016-05-03 03:35:20 +02:00
|
|
|
case STATE_CLOSE_WAIT_THEIRCOMMIT_CLOSE_SPENDOURS:
|
|
|
|
case STATE_CLOSE_WAIT_THEIRCOMMIT_CLOSE_SPENDOURS_WITH_HTLCS:
|
|
|
|
case STATE_CLOSE_WAIT_STEAL_THEIRCOMMIT_CLOSE_SPENDOURS:
|
|
|
|
case STATE_CLOSE_WAIT_STEAL_THEIRCOMMIT_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;
|
|
|
|
|
2016-05-03 03:35:20 +02:00
|
|
|
/* Our steal is deep enough to forget? */
|
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
|
|
|
}
|
|
|
|
|
2016-05-03 03:35:20 +02:00
|
|
|
/* Their commit is buried deep enough to forget? */
|
|
|
|
if ((bits & STATE_CLOSE_THEIRCOMMIT_BIT)
|
|
|
|
&& input_is(input, BITCOIN_THEIRCOMMIT_DONE)) {
|
2016-01-21 21:11:46 +01:00
|
|
|
BUILD_ASSERT(!(STATE_TO_BITS(STATE_CLOSE_WAIT_HTLCS)
|
2016-05-03 03:35:20 +02:00
|
|
|
& STATE_CLOSE_THEIRCOMMIT_BIT));
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, closed);
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:35:20 +02:00
|
|
|
/* Mutual close deep enough to forget? */
|
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
|
|
|
}
|
|
|
|
|
2016-05-03 03:35:20 +02:00
|
|
|
/* Our commit deep enough to spend our output? */
|
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-05-03 03:35:20 +02:00
|
|
|
/* FIXME: If our output was dust, just fire when
|
|
|
|
* ourcommit deep enough to forget. */
|
2016-03-30 08:27:18 +02:00
|
|
|
tx = bitcoin_spend_ours(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
|
|
|
}
|
|
|
|
|
2016-05-03 03:35:20 +02:00
|
|
|
/* Our spend of ourcommit is deep enough to forget? */
|
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)) {
|
|
|
|
/* They revealed R value. */
|
2016-05-03 03:34:20 +02:00
|
|
|
peer_tx_revealed_r_value(peer,
|
|
|
|
idata->htlc_onchain);
|
2015-09-25 04:21:18 +02:00
|
|
|
/* We don't care any more. */
|
2016-05-03 03:34:20 +02:00
|
|
|
peer_unwatch_htlc_output(peer,
|
|
|
|
idata->htlc_onchain,
|
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-05-03 03:34:20 +02:00
|
|
|
tx = bitcoin_htlc_timeout(peer,
|
|
|
|
idata->htlc_onchain);
|
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-05-03 03:34:20 +02:00
|
|
|
peer_watch_htlc_spend(peer, tx,
|
|
|
|
idata->htlc_onchain,
|
2016-01-21 21:11:47 +01:00
|
|
|
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-03-30 08:27:18 +02:00
|
|
|
tx = bitcoin_htlc_spend(peer,
|
2016-05-03 03:34:20 +02:00
|
|
|
idata->htlc_onchain);
|
2016-01-21 21:11:45 +01:00
|
|
|
|
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-05-03 03:34:20 +02:00
|
|
|
peer_watch_htlc_spend(peer, tx,
|
|
|
|
idata->htlc_onchain,
|
2016-01-21 21:11:47 +01:00
|
|
|
BITCOIN_HTLC_FULFILL_SPEND_DONE);
|
2015-09-25 04:21:18 +02:00
|
|
|
/* Don't care about this one any more. */
|
2016-05-03 03:34:20 +02:00
|
|
|
peer_unwatch_htlc_output(peer,
|
|
|
|
idata->htlc_onchain,
|
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_FULFILL_SPEND_DONE)) {
|
|
|
|
/* Stop watching spend, send
|
|
|
|
* INPUT_NO_MORE_HTLCS when done. */
|
2016-05-03 03:34:20 +02:00
|
|
|
peer_unwatch_htlc_spend(peer,
|
|
|
|
idata->htlc_onchain,
|
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_RETURN_SPEND_DONE)) {
|
|
|
|
/* Stop watching spend, send
|
|
|
|
* INPUT_NO_MORE_HTLCS when done. */
|
2016-05-03 03:34:20 +02:00
|
|
|
peer_unwatch_htlc_spend(peer,
|
|
|
|
idata->htlc_onchain,
|
2016-01-21 21:11:47 +01:00
|
|
|
INPUT_NO_MORE_HTLCS);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/* Don't need to watch the HTLC output any more,
|
|
|
|
* either. */
|
2016-05-03 03:34:20 +02:00
|
|
|
peer_unwatch_htlc_output(peer,
|
|
|
|
idata->htlc_onchain,
|
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_TOUS_TIMEOUT)) {
|
|
|
|
/* They can spend, we no longer care
|
|
|
|
* about this HTLC. */
|
2016-05-03 03:34:20 +02:00
|
|
|
peer_unwatch_htlc_output(peer,
|
|
|
|
idata->htlc_onchain,
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
/*
|
2016-05-03 03:35:20 +02:00
|
|
|
* Now, other side can always spring a commit transaction on
|
|
|
|
* us (they might have two valid ones, if they didn't send
|
|
|
|
* revocation preimage yet, so always allow it).
|
2015-09-25 04:21:18 +02:00
|
|
|
*/
|
2015-09-25 04:21:18 +02:00
|
|
|
if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) {
|
2016-05-03 03:35:20 +02:00
|
|
|
peer_watch_tx(peer, idata->tx, BITCOIN_THEIRCOMMIT_DONE);
|
|
|
|
|
2016-01-21 21:11:47 +01:00
|
|
|
/* HTLC watches: if any, set HTLCs bit. */
|
2016-05-03 03:35:20 +02:00
|
|
|
if (peer_watch_their_htlc_outputs(peer, idata->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))
|
2015-09-25 04:21:18 +02:00
|
|
|
bits |= STATE_CLOSE_HTLCS_BIT;
|
2016-01-21 21:11:47 +01:00
|
|
|
|
2016-05-03 03:35:20 +02:00
|
|
|
bits |= STATE_CLOSE_THEIRCOMMIT_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-05-03 03:36:20 +02:00
|
|
|
tx = bitcoin_steal(peer, idata->ci);
|
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-03-30 08:27:18 +02:00
|
|
|
err = pkt_err_unexpected(peer, 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-03-30 08:27:18 +02:00
|
|
|
err = pkt_err_unexpected(peer, 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-03-30 08:27:18 +02:00
|
|
|
queue_pkt_err(peer, 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-03-30 08:27:18 +02:00
|
|
|
queue_pkt_err(peer, err);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
start_unilateral_close:
|
|
|
|
/*
|
|
|
|
* Close unilaterally.
|
|
|
|
*/
|
2016-03-24 02:39:41 +01:00
|
|
|
|
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-03-24 02:39:41 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If they sent us a close tx, that's always cheaper than
|
|
|
|
* broadcasting our last commit tx, and our funds are not
|
|
|
|
* timelocked.
|
|
|
|
*/
|
|
|
|
if (peer_has_close_sig(peer)) {
|
2016-03-30 08:27:18 +02:00
|
|
|
queue_tx_broadcast(broadcast, bitcoin_close(peer));
|
2016-03-24 02:39:41 +01:00
|
|
|
return next_state(peer, cstatus, STATE_CLOSE_WAIT_CLOSE);
|
|
|
|
}
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
tx = bitcoin_commit(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
|
|
|
|
|
|
|
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-03-30 08:27:18 +02:00
|
|
|
queue_pkt_err(peer, pkt_err(peer, "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-05-03 03:35:20 +02:00
|
|
|
peer_watch_tx(peer, idata->tx, BITCOIN_THEIRCOMMIT_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-05-03 03:35:20 +02:00
|
|
|
if (peer_watch_their_htlc_outputs(peer, idata->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-05-03 03:35:20 +02:00
|
|
|
STATE_CLOSE_WAIT_THEIRCOMMIT_WITH_HTLCS);
|
2016-01-21 21:11:47 +01:00
|
|
|
|
2016-05-03 03:35:20 +02:00
|
|
|
return next_state(peer, cstatus, STATE_CLOSE_WAIT_THEIRCOMMIT);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2016-03-24 02:39:41 +01:00
|
|
|
start_clearing:
|
2015-09-25 04:21:18 +02:00
|
|
|
/*
|
2016-03-24 02:39:41 +01:00
|
|
|
* Start a mutual close: tell them we want to clear.
|
2015-09-25 04:21:18 +02:00
|
|
|
*/
|
2016-03-30 08:27:18 +02:00
|
|
|
queue_pkt_close_clearing(peer);
|
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);
|
|
|
|
|
2016-03-24 02:39:41 +01:00
|
|
|
return next_state(peer, cstatus, STATE_US_CLEARING);
|
|
|
|
|
|
|
|
start_closing_cleared:
|
2015-09-25 04:21:18 +02:00
|
|
|
/* As soon as we send packet, they could close. */
|
2016-03-24 02:39:41 +01:00
|
|
|
peer_calculate_close_fee(peer);
|
|
|
|
peer_watch_close(peer, BITCOIN_CLOSE_DONE, INPUT_CLOSE_COMPLETE_TIMEOUT);
|
2016-03-30 08:27:18 +02:00
|
|
|
queue_pkt_close_signature(peer);
|
2016-03-24 02:39:41 +01:00
|
|
|
return next_state(peer, cstatus, STATE_WAIT_FOR_CLOSE_SIG);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2016-03-24 02:39:41 +01:00
|
|
|
accept_clearing:
|
2016-03-30 08:27:18 +02:00
|
|
|
err = accept_pkt_close_clearing(peer, idata->pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
if (err)
|
|
|
|
goto err_start_unilateral_close;
|
2016-03-24 02:39:41 +01:00
|
|
|
|
|
|
|
/* Notify us when there are no more htlcs in either commit tx */
|
|
|
|
peer_watch_htlcs_cleared(peer, INPUT_HTLCS_CLEARED);
|
|
|
|
|
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-03-24 02:39:41 +01:00
|
|
|
|
|
|
|
/* Tell them we're clearing too. */
|
2016-03-30 08:27:18 +02:00
|
|
|
queue_pkt_close_clearing(peer);
|
2016-03-24 02:39:41 +01:00
|
|
|
|
|
|
|
return next_state(peer, cstatus, STATE_BOTH_CLEARING);
|
|
|
|
|
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);
|
2016-03-31 08:43:20 +02:00
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
return next_state(peer, cstatus, STATE_CLOSED);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
old_commit_spotted:
|
|
|
|
/*
|
|
|
|
* bitcoind reported a broadcast of the not-latest commit tx.
|
|
|
|
*/
|
2016-03-30 08:27:18 +02:00
|
|
|
queue_pkt_err(peer, pkt_err(peer, "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-05-03 03:36:20 +02:00
|
|
|
tx = bitcoin_steal(peer, idata->ci);
|
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
|
|
|
}
|