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-08-18 06:53:46 +02:00
|
|
|
static inline bool state_is_shutdown(enum state s)
|
2016-05-26 07:55:24 +02:00
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
return s == STATE_SHUTDOWN || s == STATE_SHUTDOWN_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;
|
|
|
|
}
|
|
|
|
|
2016-11-08 22:34:26 +01:00
|
|
|
static inline bool state_is_waiting_for_anchor(enum state s)
|
2016-05-26 07:55:25 +02:00
|
|
|
{
|
2016-11-08 22:34:26 +01:00
|
|
|
return s == STATE_OPEN_WAIT_ANCHORDEPTH_AND_THEIRCOMPLETE
|
|
|
|
|| s == STATE_OPEN_WAIT_ANCHORDEPTH;
|
2016-05-26 07:55:25 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 22:34:26 +01:00
|
|
|
static inline bool state_is_openwait(enum state s)
|
|
|
|
{
|
|
|
|
return s == STATE_OPEN_WAIT_ANCHORDEPTH_AND_THEIRCOMPLETE
|
|
|
|
|| s == STATE_OPEN_WAIT_ANCHORDEPTH
|
|
|
|
|| s == STATE_OPEN_WAIT_THEIRCOMPLETE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool state_is_opening(enum state s)
|
2016-09-13 07:53:37 +02:00
|
|
|
{
|
2016-11-08 22:34:26 +01:00
|
|
|
return s <= STATE_OPEN_WAIT_THEIRCOMPLETE;
|
2016-09-13 07:53:37 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 07:55:25 +02:00
|
|
|
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)
|
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
return s == STATE_NORMAL || s == STATE_SHUTDOWN;
|
2016-05-26 07:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2016-08-18 06:53:46 +02:00
|
|
|
* A node MUST NOT send a `update_add_htlc` after a `close_shutdown`
|
2016-05-26 07:55:25 +02:00
|
|
|
*/
|
|
|
|
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)
|
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
return state_is_normal(s) || state_is_shutdown(s);
|
2016-05-26 07:55:25 +02:00
|
|
|
}
|
|
|
|
|
2015-09-25 04:21:18 +02:00
|
|
|
#endif /* LIGHTNING_STATE_H */
|