2016-06-30 01:38:10 +02:00
|
|
|
#ifndef LIGHTNING_DAEMON_CHANNEL_H
|
|
|
|
#define LIGHTNING_DAEMON_CHANNEL_H
|
2016-01-21 21:08:08 +01:00
|
|
|
#include "config.h"
|
2016-01-21 21:11:47 +01:00
|
|
|
#include "bitcoin/locktime.h"
|
|
|
|
#include <ccan/crypto/sha256/sha256.h>
|
2015-08-07 05:15:30 +02:00
|
|
|
#include <ccan/tal/tal.h>
|
2015-07-29 08:44:28 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
struct htlc;
|
|
|
|
|
2015-08-07 05:15:30 +02:00
|
|
|
struct channel_oneside {
|
2015-09-24 07:30:47 +02:00
|
|
|
/* Payment and fee is in millisatoshi. */
|
|
|
|
uint32_t pay_msat, fee_msat;
|
2016-08-18 06:53:46 +02:00
|
|
|
/* Number of HTLCs (required for limiting total number) */
|
|
|
|
unsigned int num_htlcs;
|
2015-08-07 05:15:30 +02:00
|
|
|
};
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
enum channel_side {
|
|
|
|
/* Output for us, htlcs we offered to them. */
|
|
|
|
OURS,
|
|
|
|
/* Output for them, htlcs they offered to us. */
|
|
|
|
THEIRS
|
|
|
|
};
|
|
|
|
|
2015-08-07 05:15:30 +02:00
|
|
|
struct channel_state {
|
2016-03-24 02:42:43 +01:00
|
|
|
/* Satoshis paid by anchor. */
|
|
|
|
uint64_t anchor;
|
|
|
|
/* Satoshis per 1000 bytes. */
|
2016-08-18 06:55:13 +02:00
|
|
|
uint64_t fee_rate;
|
2016-08-18 06:53:46 +02:00
|
|
|
/* Number of non-dust htlcs (to calculate txsize) */
|
|
|
|
unsigned int num_nondust;
|
2016-05-26 07:55:24 +02:00
|
|
|
struct channel_oneside side[2];
|
2015-08-07 05:15:30 +02:00
|
|
|
};
|
|
|
|
|
2015-07-29 08:44:28 +02:00
|
|
|
/**
|
2016-06-30 01:38:10 +02:00
|
|
|
* initial_cstate: Given initial fees and funding anchor, what is initial state?
|
2015-08-07 05:15:30 +02:00
|
|
|
* @ctx: tal context to allocate return value from.
|
2016-01-21 21:11:47 +01:00
|
|
|
* @anchor_satoshis: The anchor amount.
|
2016-03-24 02:42:43 +01:00
|
|
|
* @fee_rate: amount to pay in fees per kb (in satoshi).
|
2016-05-26 07:55:24 +02:00
|
|
|
* @dir: which side paid for the anchor.
|
2015-08-07 05:15:30 +02:00
|
|
|
*
|
|
|
|
* Returns state, or NULL if malformed.
|
2015-07-29 08:44:28 +02:00
|
|
|
*/
|
2016-06-30 01:38:10 +02:00
|
|
|
struct channel_state *initial_cstate(const tal_t *ctx,
|
|
|
|
uint64_t anchor_satoshis,
|
2016-08-18 06:55:13 +02:00
|
|
|
uint64_t fee_rate,
|
2016-06-30 01:38:10 +02:00
|
|
|
enum channel_side side);
|
2015-07-29 08:44:28 +02:00
|
|
|
|
2016-01-21 21:11:47 +01:00
|
|
|
/**
|
2016-06-30 01:38:10 +02:00
|
|
|
* copy_cstate: Make a deep copy of channel_state
|
2016-01-21 21:11:47 +01:00
|
|
|
* @ctx: tal context to allocate return value from.
|
|
|
|
* @cstate: state to copy.
|
|
|
|
*/
|
2016-06-30 01:38:10 +02:00
|
|
|
struct channel_state *copy_cstate(const tal_t *ctx,
|
|
|
|
const struct channel_state *cstate);
|
2016-01-21 21:11:47 +01:00
|
|
|
|
2015-07-29 08:44:28 +02:00
|
|
|
/**
|
2016-06-30 01:38:10 +02:00
|
|
|
* cstate_add_htlc: append an HTLC to cstate if it can afford it
|
2016-03-24 02:42:43 +01:00
|
|
|
* @cstate: The channel state
|
2016-06-30 01:38:11 +02:00
|
|
|
* @htlc: the htlc pointer.
|
2016-03-24 02:42:43 +01:00
|
|
|
*
|
2016-05-26 07:55:24 +02:00
|
|
|
* If that direction can't afford the HTLC (or still owes its half of the fees),
|
2016-08-18 06:53:46 +02:00
|
|
|
* this will return false and leave @cstate unchanged. Otherwise, pay_msat and
|
2016-06-30 01:38:11 +02:00
|
|
|
* fee_msat are adjusted accordingly; true is returned.
|
2015-07-29 08:44:28 +02:00
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
bool cstate_add_htlc(struct channel_state *cstate, const struct htlc *htlc);
|
2016-01-21 21:11:47 +01:00
|
|
|
/**
|
2016-06-30 01:38:10 +02:00
|
|
|
* cstate_fail_htlc: remove an HTLC, funds to the side which offered it.
|
2016-03-24 02:42:43 +01:00
|
|
|
* @cstate: The channel state
|
2016-08-18 06:53:46 +02:00
|
|
|
* @htlc: the htlc to remove.
|
2016-03-24 02:42:43 +01:00
|
|
|
*
|
2016-05-26 07:55:24 +02:00
|
|
|
* This will remove the @index'th entry in cstate->side[dir].htlcs[], and credit
|
|
|
|
* the value of the HTLC (back) to cstate->side[dir].
|
2016-01-21 21:11:47 +01:00
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
void cstate_fail_htlc(struct channel_state *cstate, const struct htlc *htlc);
|
2016-01-21 21:11:47 +01:00
|
|
|
|
2015-07-29 08:46:24 +02:00
|
|
|
/**
|
2016-06-30 01:38:10 +02:00
|
|
|
* cstate_fulfill_htlc: remove an HTLC, funds to side which accepted it.
|
2016-03-24 02:42:43 +01:00
|
|
|
* @cstate: The channel state
|
2016-08-18 06:53:46 +02:00
|
|
|
* @htlc: the htlc to remove
|
2016-03-24 02:42:43 +01:00
|
|
|
*
|
2016-05-26 07:55:24 +02:00
|
|
|
* This will remove the @index'th entry in cstate->side[dir].htlcs[], and credit
|
|
|
|
* the value of the HTLC to cstate->side[!dir].
|
2015-07-29 08:46:24 +02:00
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
void cstate_fulfill_htlc(struct channel_state *cstate, const struct htlc *htlc);
|
2015-08-07 05:15:30 +02:00
|
|
|
|
|
|
|
/**
|
2016-03-24 02:42:43 +01:00
|
|
|
* adjust_fee: Change fee rate.
|
|
|
|
* @cstate: The channel state
|
|
|
|
* @fee_rate: fee in satoshi per 1000 bytes.
|
2015-08-07 05:15:30 +02:00
|
|
|
*/
|
2016-08-18 06:55:13 +02:00
|
|
|
void adjust_fee(struct channel_state *cstate, uint64_t fee_rate);
|
2015-08-07 05:15:30 +02:00
|
|
|
|
2016-01-21 21:11:47 +01:00
|
|
|
/**
|
2016-03-24 02:42:43 +01:00
|
|
|
* force_fee: Change fee to a specific value.
|
|
|
|
* @cstate: The channel state
|
|
|
|
* @fee: fee in satoshi.
|
|
|
|
*
|
|
|
|
* This is used for the close transaction, which specifies an exact fee.
|
|
|
|
* If the fee cannot be paid in full, this return false (but cstate will
|
|
|
|
* still be altered).
|
2016-01-21 21:11:47 +01:00
|
|
|
*/
|
2016-03-24 02:42:43 +01:00
|
|
|
bool force_fee(struct channel_state *cstate, uint64_t fee);
|
|
|
|
|
2016-01-21 21:11:47 +01:00
|
|
|
/**
|
2016-03-24 02:42:43 +01:00
|
|
|
* fee_for_feerate: calculate the fee (in satoshi) for a given fee_rate.
|
|
|
|
* @txsize: transaction size in bytes.
|
|
|
|
* @fee_rate: satoshi per 1000 bytes.
|
|
|
|
*/
|
2016-08-18 06:55:13 +02:00
|
|
|
uint64_t fee_by_feerate(size_t txsize, uint64_t fee_rate);
|
2016-03-24 02:42:43 +01:00
|
|
|
|
2016-06-30 01:38:10 +02:00
|
|
|
#endif /* LIGHTNING_DAEMON_CHANNEL_H */
|