2018-02-12 11:10:46 +01:00
|
|
|
#ifndef LIGHTNING_LIGHTNINGD_CHANNEL_H
|
|
|
|
#define LIGHTNING_LIGHTNINGD_CHANNEL_H
|
|
|
|
#include "config.h"
|
|
|
|
#include <ccan/list/list.h>
|
2018-02-19 02:06:14 +01:00
|
|
|
#include <lightningd/channel_state.h>
|
2018-02-19 02:06:12 +01:00
|
|
|
#include <lightningd/peer_htlcs.h>
|
2018-02-12 11:10:46 +01:00
|
|
|
#include <wallet/wallet.h>
|
|
|
|
|
2018-02-19 02:06:02 +01:00
|
|
|
struct uncommitted_channel;
|
|
|
|
|
2018-02-23 06:53:44 +01:00
|
|
|
struct billboard {
|
|
|
|
/* Status information to display on listpeers */
|
|
|
|
const char *permanent[CHANNEL_STATE_MAX+1];
|
|
|
|
const char *transient;
|
|
|
|
};
|
|
|
|
|
2018-02-12 11:10:46 +01:00
|
|
|
struct channel {
|
|
|
|
/* Inside peer->channels. */
|
|
|
|
struct list_node list;
|
|
|
|
|
|
|
|
/* Peer context */
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
/* Database ID: 0 == not in db yet */
|
|
|
|
u64 dbid;
|
|
|
|
|
|
|
|
/* Error message (iff in error state) */
|
|
|
|
u8 *error;
|
|
|
|
|
|
|
|
/* Their shachain. */
|
|
|
|
struct wallet_shachain their_shachain;
|
|
|
|
|
|
|
|
/* What's happening. */
|
2018-02-19 02:06:14 +01:00
|
|
|
enum channel_state state;
|
2018-02-12 11:10:46 +01:00
|
|
|
|
|
|
|
/* Which side offered channel? */
|
|
|
|
enum side funder;
|
|
|
|
|
|
|
|
/* Is there a single subdaemon responsible for us? */
|
|
|
|
struct subd *owner;
|
|
|
|
|
|
|
|
/* History */
|
|
|
|
struct log *log;
|
2018-02-23 06:53:44 +01:00
|
|
|
struct billboard billboard;
|
2018-02-12 11:10:46 +01:00
|
|
|
|
|
|
|
/* Channel flags from opening message. */
|
|
|
|
u8 channel_flags;
|
|
|
|
|
|
|
|
/* Our channel config. */
|
|
|
|
struct channel_config our_config;
|
|
|
|
|
|
|
|
/* Minimum funding depth (specified by us if they fund). */
|
|
|
|
u32 minimum_depth;
|
|
|
|
|
|
|
|
/* Tracking commitment transaction numbers. */
|
|
|
|
u64 next_index[NUM_SIDES];
|
|
|
|
u64 next_htlc_id;
|
|
|
|
|
2018-02-19 02:06:12 +01:00
|
|
|
/* Funding txid and amounts */
|
|
|
|
struct bitcoin_txid funding_txid;
|
2018-02-12 11:10:46 +01:00
|
|
|
u16 funding_outnum;
|
|
|
|
u64 funding_satoshi, push_msat;
|
|
|
|
bool remote_funding_locked;
|
|
|
|
/* Channel if locked locally. */
|
|
|
|
struct short_channel_id *scid;
|
|
|
|
|
|
|
|
/* Amount going to us, not counting unfinished HTLCs; if we have one. */
|
2018-02-19 02:06:12 +01:00
|
|
|
u64 our_msatoshi;
|
2018-03-31 02:21:13 +02:00
|
|
|
/* Statistics for min and max our_msatoshi. */
|
|
|
|
u64 msatoshi_to_us_min;
|
|
|
|
u64 msatoshi_to_us_max;
|
2018-02-12 11:10:46 +01:00
|
|
|
|
2018-02-19 02:06:12 +01:00
|
|
|
/* Last tx they gave us. */
|
2018-02-12 11:10:46 +01:00
|
|
|
struct bitcoin_tx *last_tx;
|
2018-02-19 02:06:12 +01:00
|
|
|
secp256k1_ecdsa_signature last_sig;
|
2018-02-12 11:10:46 +01:00
|
|
|
secp256k1_ecdsa_signature *last_htlc_sigs;
|
|
|
|
|
2018-02-19 02:06:12 +01:00
|
|
|
/* Keys for channel */
|
|
|
|
struct channel_info channel_info;
|
2018-02-12 11:10:46 +01:00
|
|
|
|
|
|
|
/* Secret seed (FIXME: Move to hsm!) */
|
|
|
|
struct privkey seed;
|
|
|
|
|
|
|
|
/* Their scriptpubkey if they sent shutdown. */
|
|
|
|
u8 *remote_shutdown_scriptpubkey;
|
2018-03-07 01:06:07 +01:00
|
|
|
/* Address for any final outputs */
|
|
|
|
u64 final_key_idx;
|
2018-02-12 11:10:46 +01:00
|
|
|
|
|
|
|
/* Reestablishment stuff: last sent commit and revocation details. */
|
|
|
|
bool last_was_revoke;
|
|
|
|
struct changed_htlc *last_sent_commit;
|
|
|
|
|
|
|
|
/* Blockheight at creation, scans for funding confirmations
|
|
|
|
* will start here */
|
|
|
|
u64 first_blocknum;
|
2018-04-03 09:19:39 +02:00
|
|
|
|
|
|
|
/* Feerate range */
|
|
|
|
u32 min_possible_feerate, max_possible_feerate;
|
2018-02-12 11:10:46 +01:00
|
|
|
};
|
|
|
|
|
2018-02-19 02:06:14 +01:00
|
|
|
struct channel *new_channel(struct peer *peer, u64 dbid,
|
|
|
|
/* NULL or stolen */
|
|
|
|
struct wallet_shachain *their_shachain,
|
2018-02-19 02:06:14 +01:00
|
|
|
enum channel_state state,
|
2018-02-19 02:06:14 +01:00
|
|
|
enum side funder,
|
|
|
|
/* NULL or stolen */
|
|
|
|
struct log *log,
|
2018-02-23 06:53:44 +01:00
|
|
|
const char *transient_billboard TAKES,
|
2018-02-19 02:06:14 +01:00
|
|
|
u8 channel_flags,
|
|
|
|
const struct channel_config *our_config,
|
|
|
|
u32 minimum_depth,
|
|
|
|
u64 next_index_local,
|
|
|
|
u64 next_index_remote,
|
|
|
|
u64 next_htlc_id,
|
|
|
|
const struct bitcoin_txid *funding_txid,
|
|
|
|
u16 funding_outnum,
|
|
|
|
u64 funding_satoshi,
|
|
|
|
u64 push_msat,
|
|
|
|
bool remote_funding_locked,
|
|
|
|
/* NULL or stolen */
|
|
|
|
struct short_channel_id *scid,
|
|
|
|
u64 our_msatoshi,
|
2018-03-31 02:21:13 +02:00
|
|
|
u64 msatoshi_to_us_min,
|
|
|
|
u64 msatoshi_to_us_max,
|
2018-02-19 02:06:14 +01:00
|
|
|
/* Stolen */
|
|
|
|
struct bitcoin_tx *last_tx,
|
|
|
|
const secp256k1_ecdsa_signature *last_sig,
|
|
|
|
/* NULL or stolen */
|
|
|
|
secp256k1_ecdsa_signature *last_htlc_sigs,
|
|
|
|
const struct channel_info *channel_info,
|
|
|
|
/* NULL or stolen */
|
|
|
|
u8 *remote_shutdown_scriptpubkey,
|
2018-03-07 01:06:07 +01:00
|
|
|
u64 final_key_idx,
|
2018-02-19 02:06:14 +01:00
|
|
|
bool last_was_revoke,
|
|
|
|
/* NULL or stolen */
|
|
|
|
struct changed_htlc *last_sent_commit,
|
2018-04-03 09:19:39 +02:00
|
|
|
u32 first_blocknum,
|
|
|
|
u32 min_possible_feerate,
|
|
|
|
u32 max_possible_feerate);
|
2018-02-14 02:53:04 +01:00
|
|
|
|
2018-02-21 16:50:49 +01:00
|
|
|
void delete_channel(struct channel *channel);
|
2018-02-12 11:10:46 +01:00
|
|
|
|
|
|
|
const char *channel_state_name(const struct channel *channel);
|
2018-02-19 02:06:14 +01:00
|
|
|
const char *channel_state_str(enum channel_state state);
|
2018-02-12 11:10:46 +01:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
void channel_set_owner(struct channel *channel, struct subd *owner);
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
/* Channel has failed, but can try again. */
|
|
|
|
PRINTF_FMT(2,3) void channel_fail_transient(struct channel *channel,
|
|
|
|
const char *fmt,...);
|
|
|
|
/* Channel has failed, give up on it. */
|
|
|
|
void channel_fail_permanent(struct channel *channel, const char *fmt, ...);
|
|
|
|
/* Permanent error, but due to internal problems, not peer. */
|
|
|
|
void channel_internal_error(struct channel *channel, const char *fmt, ...);
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
void channel_set_state(struct channel *channel,
|
2018-02-19 02:06:14 +01:00
|
|
|
enum channel_state old_state,
|
|
|
|
enum channel_state state);
|
2018-02-12 11:13:04 +01:00
|
|
|
|
2018-02-12 11:10:46 +01:00
|
|
|
/* Find a channel which is not onchain, if any */
|
|
|
|
struct channel *peer_active_channel(struct peer *peer);
|
|
|
|
|
2018-02-19 02:06:02 +01:00
|
|
|
/* Get active channel for peer, optionally any uncommitted_channel. */
|
2018-02-12 11:13:04 +01:00
|
|
|
struct channel *active_channel_by_id(struct lightningd *ld,
|
2018-02-19 02:06:02 +01:00
|
|
|
const struct pubkey *id,
|
|
|
|
struct uncommitted_channel **uc);
|
2018-02-12 11:13:04 +01:00
|
|
|
|
2018-04-05 18:33:14 +02:00
|
|
|
struct channel *channel_by_dbid(struct lightningd *ld, const u64 dbid);
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
void channel_set_last_tx(struct channel *channel,
|
|
|
|
struct bitcoin_tx *tx,
|
|
|
|
const secp256k1_ecdsa_signature *sig);
|
|
|
|
|
2018-02-12 11:10:46 +01:00
|
|
|
static inline bool channel_can_add_htlc(const struct channel *channel)
|
|
|
|
{
|
|
|
|
return channel->state == CHANNELD_NORMAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool channel_fees_can_change(const struct channel *channel)
|
|
|
|
{
|
|
|
|
return channel->state == CHANNELD_NORMAL
|
|
|
|
|| channel->state == CHANNELD_SHUTTING_DOWN;
|
|
|
|
}
|
|
|
|
|
2018-02-19 02:06:14 +01:00
|
|
|
static inline bool channel_state_on_chain(enum channel_state state)
|
2018-02-12 11:10:46 +01:00
|
|
|
{
|
2018-02-23 07:23:51 +01:00
|
|
|
return state == ONCHAIN;
|
2018-02-12 11:10:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool channel_on_chain(const struct channel *channel)
|
|
|
|
{
|
|
|
|
return channel_state_on_chain(channel->state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool channel_active(const struct channel *channel)
|
|
|
|
{
|
|
|
|
return channel->state != FUNDING_SPEND_SEEN
|
|
|
|
&& channel->state != CLOSINGD_COMPLETE
|
|
|
|
&& !channel_on_chain(channel);
|
|
|
|
}
|
|
|
|
|
2018-02-19 02:06:02 +01:00
|
|
|
void derive_channel_seed(struct lightningd *ld, struct privkey *seed,
|
|
|
|
const struct pubkey *peer_id,
|
|
|
|
const u64 dbid);
|
2018-02-23 06:53:44 +01:00
|
|
|
|
|
|
|
void channel_set_billboard(struct channel *channel, bool perm,
|
|
|
|
const char *str TAKES);
|
2018-02-28 23:23:45 +01:00
|
|
|
|
|
|
|
struct htlc_in *channel_has_htlc_in(struct channel *channel);
|
|
|
|
struct htlc_out *channel_has_htlc_out(struct channel *channel);
|
|
|
|
|
2018-02-12 11:10:46 +01:00
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_CHANNEL_H */
|