2015-09-25 04:21:18 +02:00
|
|
|
#ifndef LIGHTNING_STATE_H
|
|
|
|
#define LIGHTNING_STATE_H
|
2016-01-21 21:08:08 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <ccan/tal/tal.h>
|
2015-09-25 04:21:18 +02:00
|
|
|
#include <state_types.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the core state machine.
|
|
|
|
*
|
2016-01-21 21:11:47 +01:00
|
|
|
* Calling the state machine updates updates peer->state, and may call
|
|
|
|
* various peer_ callbacks. It also returns the status of the current
|
|
|
|
* command.
|
2015-09-25 04:21:18 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
static inline bool state_is_error(enum state s)
|
|
|
|
{
|
2016-05-26 07:55:25 +02:00
|
|
|
return s >= STATE_ERR_BREAKDOWN && s <= STATE_ERR_INTERNAL;
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
static inline bool state_is_clearing(enum state s)
|
2016-05-26 07:55:24 +02:00
|
|
|
{
|
2016-05-26 07:55:25 +02:00
|
|
|
return s == STATE_CLEARING || s == STATE_CLEARING_COMMITTING;
|
2016-05-26 07:55:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
static inline bool state_is_onchain(enum state s)
|
|
|
|
{
|
|
|
|
return s >= STATE_CLOSE_ONCHAIN_CHEATED
|
|
|
|
&& s <= STATE_CLOSE_ONCHAIN_MUTUAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool state_is_normal(enum state s)
|
|
|
|
{
|
|
|
|
return s == STATE_NORMAL || s == STATE_NORMAL_COMMITTING;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool state_is_opening(enum state s)
|
|
|
|
{
|
|
|
|
return s < STATE_NORMAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool state_can_io(enum state s)
|
|
|
|
{
|
|
|
|
if (state_is_error(s))
|
|
|
|
return false;
|
|
|
|
if (s == STATE_CLOSED)
|
|
|
|
return false;
|
|
|
|
if (state_is_onchain(s))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool state_can_commit(enum state s)
|
|
|
|
{
|
|
|
|
return s == STATE_NORMAL || s == STATE_CLEARING;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A node MUST NOT send a `update_add_htlc` after a `close_clearing`
|
|
|
|
*/
|
|
|
|
static inline bool state_can_add_htlc(enum state s)
|
|
|
|
{
|
|
|
|
return state_is_normal(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool state_can_remove_htlc(enum state s)
|
|
|
|
{
|
|
|
|
return state_is_normal(s) || state_is_clearing(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-21 21:11:45 +01:00
|
|
|
struct peer;
|
2016-01-21 21:11:47 +01:00
|
|
|
struct bitcoin_tx;
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
static inline bool input_is_pkt(enum state_input input)
|
2015-09-25 04:21:18 +02:00
|
|
|
{
|
|
|
|
return input <= PKT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
union input {
|
2016-06-28 23:19:20 +02:00
|
|
|
/* For PKT_* */
|
2015-09-25 04:21:18 +02:00
|
|
|
Pkt *pkt;
|
2016-06-28 23:19:20 +02:00
|
|
|
/* For CMD_SEND_HTLC_ADD, CMD_SEND_HTLC_FULFILL, CMD_SEND_HTLC_FAIL */
|
|
|
|
union htlc_staging *stage;
|
2015-09-25 04:21:18 +02:00
|
|
|
};
|
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
enum state state(struct peer *peer,
|
|
|
|
const enum state_input input,
|
|
|
|
const union input *idata,
|
|
|
|
const struct bitcoin_tx **broadcast);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2015-09-25 04:21:18 +02:00
|
|
|
/* Any CMD_SEND_HTLC_* */
|
2015-09-25 04:21:18 +02:00
|
|
|
#define CMD_SEND_UPDATE_ANY INPUT_MAX
|
|
|
|
|
|
|
|
/* a == b? (or one of several for CMD_SEND_UPDATE_ANY) */
|
|
|
|
static inline bool input_is(enum state_input a, enum state_input b)
|
|
|
|
{
|
|
|
|
if (b == CMD_SEND_UPDATE_ANY) {
|
|
|
|
/* Single | here, we want to record all. */
|
2016-03-08 01:12:15 +01:00
|
|
|
return input_is(a, CMD_SEND_HTLC_ADD)
|
2015-09-25 04:21:18 +02:00
|
|
|
| input_is(a, CMD_SEND_HTLC_FULFILL)
|
2016-03-08 01:11:15 +01:00
|
|
|
| input_is(a, CMD_SEND_HTLC_FAIL);
|
2015-09-25 04:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* For test_state_coverate to make the states. */
|
|
|
|
#ifdef MAPPING_INPUTS
|
|
|
|
MAPPING_INPUTS(b);
|
|
|
|
#endif
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
|
2015-09-25 04:21:18 +02:00
|
|
|
struct signature;
|
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
/* Inform peer have an unexpected packet. */
|
|
|
|
void peer_unexpected_pkt(struct peer *peer, const Pkt *pkt);
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
/* Send various kinds of packets */
|
|
|
|
void queue_pkt_open(struct peer *peer, OpenChannel__AnchorOffer anchor);
|
|
|
|
void queue_pkt_anchor(struct peer *peer);
|
|
|
|
void queue_pkt_open_commit_sig(struct peer *peer);
|
|
|
|
void queue_pkt_open_complete(struct peer *peer);
|
|
|
|
void queue_pkt_htlc_add(struct peer *peer,
|
2016-06-28 23:19:20 +02:00
|
|
|
const struct channel_htlc *htlc);
|
2016-05-26 07:55:24 +02:00
|
|
|
void queue_pkt_htlc_fulfill(struct peer *peer, u64 id, const struct sha256 *r);
|
|
|
|
void queue_pkt_htlc_fail(struct peer *peer, u64 id);
|
2016-03-31 08:43:20 +02:00
|
|
|
void queue_pkt_commit(struct peer *peer);
|
|
|
|
void queue_pkt_revocation(struct peer *peer);
|
2016-03-30 08:27:18 +02:00
|
|
|
void queue_pkt_close_clearing(struct peer *peer);
|
|
|
|
void queue_pkt_close_signature(struct peer *peer);
|
|
|
|
|
|
|
|
Pkt *pkt_err(struct peer *peer, const char *msg, ...);
|
|
|
|
void queue_pkt_err(struct peer *peer, Pkt *err);
|
|
|
|
Pkt *pkt_err_unexpected(struct peer *peer, const Pkt *pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/* Process various packets: return an error packet on failure. */
|
2016-03-30 08:27:18 +02:00
|
|
|
Pkt *accept_pkt_open(struct peer *peer, const Pkt *pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
Pkt *accept_pkt_anchor(struct peer *peer, const Pkt *pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
Pkt *accept_pkt_open_commit_sig(struct peer *peer, const Pkt *pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
Pkt *accept_pkt_open_complete(struct peer *peer, const Pkt *pkt);
|
2016-03-08 01:09:15 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
Pkt *accept_pkt_htlc_add(struct peer *peer, const Pkt *pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
Pkt *accept_pkt_htlc_fail(struct peer *peer, const Pkt *pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
Pkt *accept_pkt_htlc_fulfill(struct peer *peer, const Pkt *pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
Pkt *accept_pkt_update_accept(struct peer *peer, const Pkt *pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
Pkt *accept_pkt_commit(struct peer *peer, const Pkt *pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
Pkt *accept_pkt_revocation(struct peer *peer, const Pkt *pkt);
|
2016-03-30 08:27:18 +02:00
|
|
|
Pkt *accept_pkt_close_clearing(struct peer *peer, const Pkt *pkt);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/**
|
2016-01-21 21:11:47 +01:00
|
|
|
* peer_watch_anchor: create a watch for the anchor transaction.
|
2016-01-21 21:11:45 +01:00
|
|
|
* @peer: the state data for this peer.
|
2015-09-25 04:21:18 +02:00
|
|
|
* @depthok: the input to give when anchor reaches expected depth.
|
|
|
|
* @timeout: the input to give if anchor doesn't reach depth in time.
|
|
|
|
*
|
|
|
|
* @depthok can be INPUT_NONE if it's our anchor (we don't time
|
|
|
|
* ourselves out).
|
|
|
|
*/
|
2016-01-21 21:11:47 +01:00
|
|
|
void peer_watch_anchor(struct peer *peer,
|
|
|
|
enum state_input depthok,
|
2016-05-04 08:44:22 +02:00
|
|
|
enum state_input timeout);
|
2015-09-25 04:21:18 +02:00
|
|
|
/**
|
2016-01-21 21:11:47 +01:00
|
|
|
* peer_unwatch_anchor_depth: remove depth watch for the anchor.
|
2016-01-21 21:11:45 +01:00
|
|
|
* @peer: the state data for this peer.
|
2015-09-25 04:21:18 +02:00
|
|
|
* @depthok: the input to give when anchor reaches expected depth.
|
|
|
|
* @timeout: the input to give if anchor doesn't reach depth in time.
|
|
|
|
*
|
|
|
|
* @depthok and @timeout must match bitcoin_watch_anchor() call.
|
|
|
|
*/
|
2016-01-21 21:11:47 +01:00
|
|
|
void peer_unwatch_anchor_depth(struct peer *peer,
|
|
|
|
enum state_input depthok,
|
|
|
|
enum state_input timeout);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
2016-03-24 02:39:41 +01:00
|
|
|
/**
|
|
|
|
* peer_calculate_close_fee: figure out what the fee for closing is.
|
|
|
|
* @peer: the state data for this peer.
|
|
|
|
*/
|
|
|
|
void peer_calculate_close_fee(struct peer *peer);
|
|
|
|
|
2016-01-21 21:11:47 +01:00
|
|
|
/* Start creation of the bitcoin anchor tx. */
|
|
|
|
void bitcoin_create_anchor(struct peer *peer, enum state_input done);
|
|
|
|
|
|
|
|
/* We didn't end up broadcasting the anchor: release the utxos.
|
|
|
|
* If done != INPUT_NONE, remove existing create_anchor too. */
|
|
|
|
void bitcoin_release_anchor(struct peer *peer, enum state_input done);
|
|
|
|
|
|
|
|
/* Get the bitcoin anchor tx. */
|
2016-03-30 08:27:18 +02:00
|
|
|
const struct bitcoin_tx *bitcoin_anchor(struct peer *peer);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/* Create a bitcoin close tx. */
|
2016-03-30 08:27:18 +02:00
|
|
|
const struct bitcoin_tx *bitcoin_close(struct peer *peer);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/* Create a bitcoin spend tx (to spend our commit's outputs) */
|
2016-03-30 08:27:18 +02:00
|
|
|
const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
/* Create our commit tx */
|
2016-03-30 08:27:18 +02:00
|
|
|
const struct bitcoin_tx *bitcoin_commit(struct peer *peer);
|
2015-09-25 04:21:18 +02:00
|
|
|
|
|
|
|
#endif /* LIGHTNING_STATE_H */
|