2017-08-28 18:02:01 +02:00
|
|
|
/* This is the full channel routines, with HTLC support. */
|
2018-03-22 11:36:25 +01:00
|
|
|
#ifndef LIGHTNING_CHANNELD_FULL_CHANNEL_H
|
|
|
|
#define LIGHTNING_CHANNELD_FULL_CHANNEL_H
|
2017-02-21 05:45:28 +01:00
|
|
|
#include "config.h"
|
2017-08-29 06:12:04 +02:00
|
|
|
#include <channeld/channeld_htlc.h>
|
2018-02-19 02:06:14 +01:00
|
|
|
#include <channeld/full_channel_error.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/initial_channel.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/sphinx.h>
|
2017-02-21 05:45:28 +01:00
|
|
|
|
2020-09-09 09:20:53 +02:00
|
|
|
struct channel_id;
|
2020-04-03 05:14:07 +02:00
|
|
|
struct existing_htlc;
|
|
|
|
|
2017-02-21 05:45:28 +01:00
|
|
|
/**
|
2018-02-11 12:02:51 +01:00
|
|
|
* new_full_channel: Given initial fees and funding, what is initial state?
|
2017-02-21 05:45:28 +01:00
|
|
|
* @ctx: tal context to allocate return value from.
|
2020-09-09 09:20:53 +02:00
|
|
|
* @cid: The channel id.
|
2021-10-13 05:45:36 +02:00
|
|
|
* @funding: The commitment transaction id/output number.
|
2019-02-26 17:57:19 +01:00
|
|
|
* @minimum_depth: The minimum confirmations needed for funding transaction.
|
2021-06-22 20:25:59 +02:00
|
|
|
* @blockheight_states: The blockheight update states.
|
2021-06-16 19:56:36 +02:00
|
|
|
* @lease_expiry: The block the lease on this channel expires at; 0 if no lease.
|
2021-10-13 05:45:36 +02:00
|
|
|
* @funding_sats: The commitment transaction amount.
|
2019-02-21 04:45:55 +01:00
|
|
|
* @local_msat: The amount for the local side (remainder goes to remote)
|
2019-12-12 18:18:25 +01:00
|
|
|
* @fee_states: The fee update states.
|
2017-02-21 05:45:28 +01:00
|
|
|
* @local: local channel configuration
|
|
|
|
* @remote: remote channel configuration
|
2017-03-07 02:07:06 +01:00
|
|
|
* @local_basepoints: local basepoints.
|
|
|
|
* @remote_basepoints: remote basepoints.
|
2017-03-29 12:58:15 +02:00
|
|
|
* @local_fundingkey: local funding key
|
|
|
|
* @remote_fundingkey: remote funding key
|
2021-09-09 07:25:23 +02:00
|
|
|
* @type: type for this channel
|
2021-09-08 02:06:14 +02:00
|
|
|
* @option_wumbo: large channel negotiated.
|
2019-09-09 18:11:24 +02:00
|
|
|
* @opener: which side initiated it.
|
2017-02-21 05:45:28 +01:00
|
|
|
*
|
|
|
|
* Returns state, or NULL if malformed.
|
|
|
|
*/
|
2018-02-11 12:02:51 +01:00
|
|
|
struct channel *new_full_channel(const tal_t *ctx,
|
2020-09-09 09:20:53 +02:00
|
|
|
const struct channel_id *cid,
|
2021-10-13 05:45:36 +02:00
|
|
|
const struct bitcoin_outpoint *funding,
|
2019-02-26 17:57:19 +01:00
|
|
|
u32 minimum_depth,
|
2021-06-22 20:25:59 +02:00
|
|
|
const struct height_states *blockheight_states,
|
2021-06-16 19:56:36 +02:00
|
|
|
u32 lease_expiry,
|
2021-10-13 05:45:36 +02:00
|
|
|
struct amount_sat funding_sats,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat local_msat,
|
2021-09-09 07:25:23 +02:00
|
|
|
const struct fee_states *fee_states TAKES,
|
2018-02-11 12:02:51 +01:00
|
|
|
const struct channel_config *local,
|
|
|
|
const struct channel_config *remote,
|
|
|
|
const struct basepoints *local_basepoints,
|
|
|
|
const struct basepoints *remote_basepoints,
|
|
|
|
const struct pubkey *local_funding_pubkey,
|
|
|
|
const struct pubkey *remote_funding_pubkey,
|
2021-09-09 07:25:23 +02:00
|
|
|
const struct channel_type *type TAKES,
|
2021-09-08 02:06:14 +02:00
|
|
|
bool option_wumbo,
|
2019-09-09 18:11:24 +02:00
|
|
|
enum side opener);
|
2017-03-29 12:58:15 +02:00
|
|
|
|
2017-02-21 05:45:28 +01:00
|
|
|
/**
|
2017-03-29 12:58:15 +02:00
|
|
|
* channel_txs: Get the current commitment and htlc txs for the channel.
|
2017-02-21 05:45:28 +01:00
|
|
|
* @ctx: tal context to allocate return value from.
|
|
|
|
* @channel: The channel to evaluate
|
2017-08-31 04:04:42 +02:00
|
|
|
* @htlc_map: Pointer to htlcs for each tx output (allocated off @ctx).
|
2020-05-07 02:43:34 +02:00
|
|
|
* @direct_outputs: If non-NULL, fill with pointers to the direct (non-HTLC) outputs (or NULL if none).
|
2020-02-18 04:50:18 +01:00
|
|
|
* @funding_wscript: Pointer to wscript for the funding tx output
|
2017-03-29 12:58:15 +02:00
|
|
|
* @per_commitment_point: Per-commitment point to determine keys
|
2017-06-20 08:10:03 +02:00
|
|
|
* @commitment_number: The index of this commitment.
|
2017-02-21 05:45:28 +01:00
|
|
|
* @side: which side to get the commitment transaction for
|
|
|
|
*
|
|
|
|
* Returns the unsigned commitment transaction for the committed state
|
2017-08-31 04:04:42 +02:00
|
|
|
* for @side, followed by the htlc transactions in output order and
|
|
|
|
* fills in @htlc_map, or NULL on key derivation failure.
|
2017-02-21 05:45:28 +01:00
|
|
|
*/
|
2017-03-29 12:58:15 +02:00
|
|
|
struct bitcoin_tx **channel_txs(const tal_t *ctx,
|
|
|
|
const struct htlc ***htlcmap,
|
2020-05-07 02:43:34 +02:00
|
|
|
struct wally_tx_output *direct_outputs[NUM_SIDES],
|
2020-02-18 04:50:18 +01:00
|
|
|
const u8 **funding_wscript,
|
2017-03-29 12:58:15 +02:00
|
|
|
const struct channel *channel,
|
|
|
|
const struct pubkey *per_commitment_point,
|
2017-06-20 08:10:03 +02:00
|
|
|
u64 commitment_number,
|
2017-03-29 12:58:15 +02:00
|
|
|
enum side side);
|
2017-02-21 05:45:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* actual_feerate: what is the actual feerate for the local side.
|
|
|
|
* @channel: The channel state
|
|
|
|
* @theirsig: The other side's signature
|
|
|
|
*
|
|
|
|
* The fee calculated on a commitment transaction is a worst-case
|
|
|
|
* approximation. It's also possible that the desired feerate is not
|
|
|
|
* met, because the initiator sets it while the other side is adding many
|
|
|
|
* htlcs.
|
|
|
|
*
|
|
|
|
* This is the fee rate we actually care about, if we're going to check
|
|
|
|
* whether it's actually too low.
|
|
|
|
*/
|
2017-11-21 04:33:22 +01:00
|
|
|
u32 actual_feerate(const struct channel *channel,
|
|
|
|
const struct signature *theirsig);
|
2017-02-21 05:45:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* channel_add_htlc: append an HTLC to channel if it can afford it
|
|
|
|
* @channel: The channel
|
|
|
|
* @offerer: the side offering the HTLC (to the other side).
|
|
|
|
* @id: unique HTLC id.
|
2019-02-21 04:45:55 +01:00
|
|
|
* @amount: amount in millisatoshi.
|
2017-03-29 12:55:15 +02:00
|
|
|
* @cltv_expiry: block number when HTLC can no longer be redeemed.
|
2017-02-21 05:45:28 +01:00
|
|
|
* @payment_hash: hash whose preimage can redeem HTLC.
|
|
|
|
* @routing: routing information (copied)
|
2020-04-11 05:22:40 +02:00
|
|
|
* @blinding: optional blinding information for this HTLC.
|
2018-01-29 05:46:54 +01:00
|
|
|
* @htlcp: optional pointer for resulting htlc: filled in if and only if CHANNEL_ERR_NONE.
|
2021-09-28 21:08:11 +02:00
|
|
|
* @err_immediate_failures: in some cases (dusty htlcs) we want to immediately
|
|
|
|
* fail the htlc; for peer incoming don't want to
|
|
|
|
* error, but rather mark it as failed and fail after
|
|
|
|
* it's been committed to (so set this to false)
|
2017-02-21 05:45:28 +01:00
|
|
|
*
|
|
|
|
* If this returns CHANNEL_ERR_NONE, the fee htlc was added and
|
|
|
|
* the output amounts adjusted accordingly. Otherwise nothing
|
|
|
|
* is changed.
|
|
|
|
*/
|
|
|
|
enum channel_add_err channel_add_htlc(struct channel *channel,
|
|
|
|
enum side sender,
|
|
|
|
u64 id,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat msatoshi,
|
2017-03-29 12:55:15 +02:00
|
|
|
u32 cltv_expiry,
|
2017-02-21 05:45:28 +01:00
|
|
|
const struct sha256 *payment_hash,
|
2020-12-08 07:48:53 +01:00
|
|
|
const u8 routing[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)],
|
2020-04-11 05:22:40 +02:00
|
|
|
const struct pubkey *blinding TAKES,
|
2019-05-30 13:36:35 +02:00
|
|
|
struct htlc **htlcp,
|
2021-09-28 21:08:11 +02:00
|
|
|
struct amount_sat *htlc_fee,
|
|
|
|
bool err_immediate_failures);
|
2017-02-21 05:45:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* channel_get_htlc: find an HTLC
|
|
|
|
* @channel: The channel
|
|
|
|
* @offerer: the side offering the HTLC.
|
|
|
|
* @id: unique HTLC id.
|
|
|
|
*/
|
|
|
|
struct htlc *channel_get_htlc(struct channel *channel, enum side sender, u64 id);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* channel_fail_htlc: remove an HTLC, funds to the side which offered it.
|
|
|
|
* @channel: The channel state
|
2017-04-01 13:01:11 +02:00
|
|
|
* @owner: the side who offered the HTLC (opposite to that failing it)
|
2017-02-21 05:45:28 +01:00
|
|
|
* @id: unique HTLC id.
|
2018-01-29 05:46:54 +01:00
|
|
|
* @htlcp: optional pointer for failed htlc: filled in if and only if CHANNEL_ERR_REMOVE_OK.
|
2017-02-21 05:45:28 +01:00
|
|
|
*
|
|
|
|
* This will remove the htlc and credit the value of the HTLC (back)
|
|
|
|
* to its offerer.
|
|
|
|
*/
|
|
|
|
enum channel_remove_err channel_fail_htlc(struct channel *channel,
|
2017-11-28 06:03:09 +01:00
|
|
|
enum side owner, u64 id,
|
|
|
|
struct htlc **htlcp);
|
2017-02-21 05:45:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* channel_fulfill_htlc: remove an HTLC, funds to side which accepted it.
|
|
|
|
* @channel: The channel state
|
2017-04-01 13:01:11 +02:00
|
|
|
* @owner: the side who offered the HTLC (opposite to that fulfilling it)
|
2017-02-21 05:45:28 +01:00
|
|
|
* @id: unique HTLC id.
|
2018-07-02 06:29:30 +02:00
|
|
|
* @htlcp: optional pointer for resulting htlc: filled in if and only if CHANNEL_ERR_FULFILL_OK.
|
2017-02-21 05:45:28 +01:00
|
|
|
*
|
|
|
|
* If the htlc exists, is not already fulfilled, the preimage is correct and
|
|
|
|
* HTLC committed at the recipient, this will add a pending change to
|
|
|
|
* remove the htlc and give the value of the HTLC to its recipient,
|
|
|
|
* and return CHANNEL_ERR_FULFILL_OK. Otherwise, it will return another error.
|
|
|
|
*/
|
|
|
|
enum channel_remove_err channel_fulfill_htlc(struct channel *channel,
|
2017-04-01 13:01:11 +02:00
|
|
|
enum side owner,
|
2017-02-21 05:45:28 +01:00
|
|
|
u64 id,
|
2018-07-02 06:29:30 +02:00
|
|
|
const struct preimage *preimage,
|
|
|
|
struct htlc **htlcp);
|
2017-02-21 05:45:28 +01:00
|
|
|
|
|
|
|
/**
|
2019-09-09 18:11:24 +02:00
|
|
|
* approx_max_feerate: what's the max opener could raise fee rate to?
|
2017-02-21 05:45:28 +01:00
|
|
|
* @channel: The channel state
|
|
|
|
*
|
2017-11-21 06:26:25 +01:00
|
|
|
* This is not exact! To check if their offer is valid, try
|
|
|
|
* channel_update_feerate.
|
2017-02-21 05:45:28 +01:00
|
|
|
*/
|
2017-11-21 04:33:22 +01:00
|
|
|
u32 approx_max_feerate(const struct channel *channel);
|
2017-02-21 05:45:28 +01:00
|
|
|
|
|
|
|
/**
|
2019-09-09 18:11:24 +02:00
|
|
|
* can_opener_afford_feerate: could the opener pay the fee?
|
2017-02-21 05:45:28 +01:00
|
|
|
* @channel: The channel state
|
2017-11-21 06:26:25 +01:00
|
|
|
* @feerate: The feerate in satoshi per 1000 bytes.
|
2017-02-21 05:45:28 +01:00
|
|
|
*/
|
2019-09-09 18:11:24 +02:00
|
|
|
bool can_opener_afford_feerate(const struct channel *channel, u32 feerate);
|
2017-02-21 05:45:28 +01:00
|
|
|
|
2021-09-28 21:12:01 +02:00
|
|
|
/**
|
|
|
|
* htlc_dust_ok: will this feerate keep our dusted htlc's beneath
|
|
|
|
* the updated feerate?
|
|
|
|
*
|
|
|
|
* @channel: The channel state
|
|
|
|
* @feerate_per_kw: new feerate to test ok'ness for
|
|
|
|
* @side: which side's htlcs to verify
|
|
|
|
*/
|
|
|
|
bool htlc_dust_ok(const struct channel *channel,
|
|
|
|
u32 feerate_per_kw,
|
|
|
|
enum side side);
|
|
|
|
|
2017-02-21 05:45:28 +01:00
|
|
|
/**
|
2019-09-09 18:11:24 +02:00
|
|
|
* channel_update_feerate: Change fee rate on non-opener side.
|
2017-11-21 06:26:25 +01:00
|
|
|
* @channel: The channel
|
2017-02-21 05:45:28 +01:00
|
|
|
* @feerate_per_kw: fee in satoshi per 1000 bytes.
|
2017-11-21 06:26:25 +01:00
|
|
|
*
|
|
|
|
* Returns true if it's affordable, otherwise does nothing.
|
|
|
|
*/
|
|
|
|
bool channel_update_feerate(struct channel *channel, u32 feerate_per_kw);
|
|
|
|
|
2021-06-22 20:25:59 +02:00
|
|
|
/*
|
|
|
|
* channel_update_blockheight: Change blockheight on non-opener side.
|
|
|
|
* @channel: The channel
|
|
|
|
* @blockheight: current blockheight
|
|
|
|
*/
|
|
|
|
void channel_update_blockheight(struct channel *channel, u32 blockheight);
|
|
|
|
|
2017-11-21 06:26:25 +01:00
|
|
|
/**
|
|
|
|
* channel_feerate: Get fee rate for this side of channel.
|
|
|
|
* @channel: The channel
|
|
|
|
* @side: the side
|
2017-02-21 05:45:28 +01:00
|
|
|
*/
|
2017-11-21 06:26:25 +01:00
|
|
|
u32 channel_feerate(const struct channel *channel, enum side side);
|
2017-02-21 05:45:28 +01:00
|
|
|
|
|
|
|
/**
|
2017-04-01 12:58:23 +02:00
|
|
|
* channel_sending_commit: commit all remote outstanding changes.
|
2017-02-21 05:45:28 +01:00
|
|
|
* @channel: the channel
|
2017-06-20 07:41:03 +02:00
|
|
|
* @htlcs: initially-empty tal_arr() for htlcs which changed state.
|
2017-02-21 05:45:28 +01:00
|
|
|
*
|
|
|
|
* This is where we commit to pending changes we've added; returns true if
|
2017-04-01 12:58:23 +02:00
|
|
|
* anything changed for the remote side (if not, don't send!) */
|
2017-06-20 07:41:03 +02:00
|
|
|
bool channel_sending_commit(struct channel *channel,
|
|
|
|
const struct htlc ***htlcs);
|
2017-02-21 05:45:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* channel_rcvd_revoke_and_ack: accept ack on remote committed changes.
|
|
|
|
* @channel: the channel
|
2017-06-20 07:41:03 +02:00
|
|
|
* @htlcs: initially-empty tal_arr() for htlcs which changed state.
|
2017-02-21 05:45:28 +01:00
|
|
|
*
|
|
|
|
* This is where we commit to pending changes we've added; returns true if
|
2017-04-01 12:58:23 +02:00
|
|
|
* anything changed for our local commitment (ie. we have pending changes).
|
2017-04-01 12:29:39 +02:00
|
|
|
*/
|
2017-06-20 07:41:03 +02:00
|
|
|
bool channel_rcvd_revoke_and_ack(struct channel *channel,
|
|
|
|
const struct htlc ***htlcs);
|
2017-02-21 05:45:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* channel_rcvd_commit: commit all local outstanding changes.
|
|
|
|
* @channel: the channel
|
2017-06-20 07:41:03 +02:00
|
|
|
* @htlcs: initially-empty tal_arr() for htlcs which changed state.
|
2017-02-21 05:45:28 +01:00
|
|
|
*
|
|
|
|
* This is where we commit to pending changes we've added; returns true if
|
2017-04-01 12:58:23 +02:00
|
|
|
* anything changed for our local commitment (ie. we had pending changes).
|
2017-04-01 12:29:39 +02:00
|
|
|
*/
|
2017-06-20 07:41:03 +02:00
|
|
|
bool channel_rcvd_commit(struct channel *channel,
|
|
|
|
const struct htlc ***htlcs);
|
2017-02-21 05:45:28 +01:00
|
|
|
|
|
|
|
/**
|
2017-04-01 12:58:23 +02:00
|
|
|
* channel_sending_revoke_and_ack: sending ack on local committed changes.
|
2017-02-21 05:45:28 +01:00
|
|
|
* @channel: the channel
|
|
|
|
*
|
2017-04-01 12:58:23 +02:00
|
|
|
* This is where we commit to pending changes we've added. Returns true if
|
|
|
|
* anything changed for the remote commitment (ie. send a new commit).*/
|
|
|
|
bool channel_sending_revoke_and_ack(struct channel *channel);
|
2017-02-21 05:45:28 +01:00
|
|
|
|
2017-07-04 02:47:32 +02:00
|
|
|
/**
|
2018-02-23 06:53:47 +01:00
|
|
|
* num_channel_htlcs: how many (live) HTLCs at all in channel?
|
2017-07-04 02:47:32 +02:00
|
|
|
* @channel: the channel
|
|
|
|
*/
|
2018-02-23 06:53:47 +01:00
|
|
|
size_t num_channel_htlcs(const struct channel *channel);
|
2017-07-04 02:47:32 +02:00
|
|
|
|
2017-06-20 08:05:03 +02:00
|
|
|
/**
|
|
|
|
* channel_force_htlcs: force these htlcs into the (new) channel
|
|
|
|
* @channel: the channel
|
2020-04-03 05:14:07 +02:00
|
|
|
* @htlcs: the htlcs to add (tal_arr) elements stolen.
|
2017-06-20 08:05:03 +02:00
|
|
|
*
|
|
|
|
* This is used for restoring a channel state.
|
|
|
|
*/
|
|
|
|
bool channel_force_htlcs(struct channel *channel,
|
2020-04-03 05:14:07 +02:00
|
|
|
const struct existing_htlc **htlcs);
|
2017-06-20 08:05:03 +02:00
|
|
|
|
2017-06-20 07:47:03 +02:00
|
|
|
/**
|
|
|
|
* dump_htlcs: debugging dump of all HTLCs
|
|
|
|
* @channel: the channel
|
|
|
|
* @prefix: the prefix to prepend to each line.
|
|
|
|
*
|
2019-09-08 18:39:26 +02:00
|
|
|
* Uses status_debug() on every HTLC.
|
2017-06-20 07:47:03 +02:00
|
|
|
*/
|
|
|
|
void dump_htlcs(const struct channel *channel, const char *prefix);
|
2018-02-19 02:06:14 +01:00
|
|
|
|
2021-05-31 05:08:04 +02:00
|
|
|
/**
|
|
|
|
* pending_updates: does this side have updates pending in channel?
|
|
|
|
* @channel: the channel
|
|
|
|
* @side: the side who is offering or failing/fulfilling HTLC, or feechange
|
2021-06-04 03:53:55 +02:00
|
|
|
* @uncommitted_ok: don't count uncommitted changes.
|
2021-05-31 05:08:04 +02:00
|
|
|
*/
|
2021-06-04 03:53:55 +02:00
|
|
|
bool pending_updates(const struct channel *channel, enum side side,
|
|
|
|
bool uncommitted_ok);
|
2021-05-31 05:08:04 +02:00
|
|
|
|
2018-02-19 02:06:14 +01:00
|
|
|
const char *channel_add_err_name(enum channel_add_err e);
|
|
|
|
const char *channel_remove_err_name(enum channel_remove_err e);
|
|
|
|
|
2018-03-22 11:36:25 +01:00
|
|
|
#endif /* LIGHTNING_CHANNELD_FULL_CHANNEL_H */
|