channel: approx_max_feerate and can_afford_feerate

Routines for getting maximum feerate we should offer, and checking if their
offer is valid.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-08-23 10:12:00 +09:30
parent c7f8ce9d3b
commit 24298a4278
2 changed files with 52 additions and 13 deletions

View File

@ -18,23 +18,23 @@ uint64_t fee_by_feerate(size_t txsize, uint64_t fee_rate)
return txsize * fee_rate / 2000 * 2;
}
/* BOLT #2:
*
* A node MUST use the formula 338 + 32 bytes for every non-dust HTLC
* as the bytecount for calculating commitment transaction fees. Note
* that the fee requirement is unchanged, even if the elimination of
* dust HTLC outputs has caused a non-zero fee already.
*/
static size_t tx_bytes(size_t num_nondust_htlcs)
{
return 338 + 32 * num_nondust_htlcs;
}
static uint64_t calculate_fee_msat(size_t num_nondust_htlcs,
uint64_t fee_rate)
{
uint64_t bytes;
/* BOLT #2:
*
* A node MUST use the formula 338 + 32 bytes for every
* non-dust HTLC as the bytecount for calculating commitment
* transaction fees. Note that the fee requirement is
* unchanged, even if the elimination of dust HTLC outputs has
* caused a non-zero fee already.
*/
bytes = 338 + 32 * num_nondust_htlcs;
/* milli-satoshis */
return fee_by_feerate(bytes, fee_rate) * 1000;
return fee_by_feerate(tx_bytes(num_nondust_htlcs), fee_rate) * 1000;
}
/* Pay this much fee, if possible. Return amount unpaid. */
@ -155,6 +155,26 @@ struct channel_state *initial_cstate(const tal_t *ctx,
return cstate;
}
/* FIXME: Write exact variant! */
uint64_t approx_max_feerate(const struct channel_state *cstate,
enum channel_side side)
{
uint64_t max_funds;
max_funds = cstate->side[side].pay_msat + cstate->side[side].fee_msat;
return max_funds * 1000 / tx_bytes(cstate->num_nondust);
}
bool can_afford_feerate(const struct channel_state *cstate, uint64_t fee_rate,
enum channel_side side)
{
u64 fee_msat = calculate_fee_msat(cstate->num_nondust, fee_rate);
return cstate->side[side].pay_msat + cstate->side[side].fee_msat
>= fee_msat;
}
void adjust_fee(struct channel_state *cstate, uint64_t fee_rate)
{
uint64_t fee_msat;

View File

@ -84,6 +84,25 @@ void cstate_fail_htlc(struct channel_state *cstate, const struct htlc *htlc);
*/
void cstate_fulfill_htlc(struct channel_state *cstate, const struct htlc *htlc);
/**
* approx_max_feerate: what's the most side could raise fee rate to?
* @cstate: The channel state
* @side: OURS or THEIRS
*
* This is not exact! To check if their offer is valid, use can_afford_feerate.
*/
uint64_t approx_max_feerate(const struct channel_state *cstate,
enum channel_side side);
/**
* can_afford_feerate: could this side pay for the fee if changed to fee_rate?
* @cstate: The channel state
* @fee_rate: the new fee rate proposed
* @side: OURS or THEIRS
*/
bool can_afford_feerate(const struct channel_state *cstate, uint64_t fee_rate,
enum channel_side side);
/**
* adjust_fee: Change fee rate.
* @cstate: The channel state