Skip to content

Commit

Permalink
chore: refactor and unify LOCAL/REMOTE helpers for receivable_msat
Browse files Browse the repository at this point in the history
  • Loading branch information
m-schmoock committed Mar 22, 2020
1 parent 99ef73d commit 784ed72
Showing 1 changed file with 21 additions and 66 deletions.
87 changes: 21 additions & 66 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -509,92 +509,46 @@ static void json_add_sat_only(struct json_stream *result,
type_to_string(tmpctx, struct amount_msat, &msat));
}

/* Fee a commitment transaction would currently cost when spending */
/* This is quite a lot of work to figure out what it would cost us! */
static struct amount_sat commit_txfee_spend(const struct channel *channel,
struct amount_msat spendable)
/* Fee a commitment transaction would currently cost */
static struct amount_sat commit_txfee(const struct channel *channel,
struct amount_msat amount,
enum side side)
{
/* FIXME: make per-channel htlc maps! */
const struct htlc_in *hin;
struct htlc_in_map_iter ini;
const struct htlc_out *hout;
struct htlc_out_map_iter outi;
struct lightningd *ld = channel->peer->ld;
u32 local_feerate = get_feerate(channel->channel_info.fee_states,
channel->funder, LOCAL);
size_t num_untrimmed_htlcs = 0;

/* Assume we tried to spend "spendable" */
if (!htlc_is_trimmed(LOCAL, spendable,
local_feerate, channel->our_config.dust_limit,
LOCAL))
num_untrimmed_htlcs++;

for (hin = htlc_in_map_first(&ld->htlcs_in, &ini);
hin;
hin = htlc_in_map_next(&ld->htlcs_in, &ini)) {
if (hin->key.channel != channel)
continue;
if (!htlc_is_trimmed(REMOTE, hin->msat, local_feerate,
channel->our_config.dust_limit,
LOCAL))
num_untrimmed_htlcs++;
}
for (hout = htlc_out_map_first(&ld->htlcs_out, &outi);
hout;
hout = htlc_out_map_next(&ld->htlcs_out, &outi)) {
if (hout->key.channel != channel)
continue;
if (!htlc_is_trimmed(LOCAL, hout->msat, local_feerate,
channel->our_config.dust_limit,
LOCAL))
num_untrimmed_htlcs++;
}

/* Funder is conservative: makes sure it allows an extra HTLC
* even if feerate increases 50% */
return commit_tx_base_fee(local_feerate + local_feerate / 2,
num_untrimmed_htlcs + 1);
}

/* Fee a commitment transaction would currently cost when receiving */
static struct amount_sat commit_txfee_recv(const struct channel *channel,
struct amount_msat receivable)
{
/* FIXME: make per-channel htlc maps! */
const struct htlc_in *hin;
struct htlc_in_map_iter ini;
const struct htlc_out *hout;
struct htlc_out_map_iter outi;
struct lightningd *ld = channel->peer->ld;
u32 feerate = get_feerate(channel->channel_info.fee_states,
channel->funder, REMOTE);
size_t num_untrimmed_htlcs = 0;

/* Assume we tried to receive "receivable" */
if (!htlc_is_trimmed(REMOTE, receivable, feerate,
channel->channel_info.their_config.dust_limit,
REMOTE))
channel->funder, side);
struct amount_sat dust_limit;
if (side == LOCAL)
dust_limit = channel->our_config.dust_limit;
if (side == REMOTE)
dust_limit = channel->channel_info.their_config.dust_limit;

/* Assume we tried to add "amount" */
if (!htlc_is_trimmed(side, amount, feerate, dust_limit, side))
num_untrimmed_htlcs++;

for (hin = htlc_in_map_first(&ld->htlcs_in, &ini);
hin;
hin = htlc_in_map_next(&ld->htlcs_in, &ini)) {
if (hin->key.channel != channel)
continue;
if (!htlc_is_trimmed(LOCAL, hin->msat, feerate,
channel->channel_info.their_config.dust_limit,
REMOTE))
if (!htlc_is_trimmed(!side, hin->msat, feerate, dust_limit,
side))
num_untrimmed_htlcs++;
}
for (hout = htlc_out_map_first(&ld->htlcs_out, &outi);
hout;
hout = htlc_out_map_next(&ld->htlcs_out, &outi)) {
if (hout->key.channel != channel)
continue;
if (!htlc_is_trimmed(REMOTE, hout->msat, feerate,
channel->channel_info.their_config.dust_limit,
REMOTE))
if (!htlc_is_trimmed(side, hout->msat, feerate, dust_limit,
side))
num_untrimmed_htlcs++;
}

Expand All @@ -604,7 +558,6 @@ static struct amount_sat commit_txfee_recv(const struct channel *channel,
num_untrimmed_htlcs + 1);
}


static void subtract_offered_htlcs(const struct channel *channel,
struct amount_msat *amount)
{
Expand Down Expand Up @@ -771,7 +724,8 @@ static void json_add_channel(struct lightningd *ld,
/* If we're funder, subtract txfees we'll need to spend this */
if (channel->funder == LOCAL) {
if (!amount_msat_sub_sat(&spendable, spendable,
commit_txfee_spend(channel, spendable)))
commit_txfee(channel, spendable,
LOCAL)))
spendable = AMOUNT_MSAT(0);
}

Expand Down Expand Up @@ -802,7 +756,8 @@ static void json_add_channel(struct lightningd *ld,
/* If they're funder, subtract txfees they'll need to spend this */
if (channel->funder == REMOTE) {
if (!amount_msat_sub_sat(&receivable, receivable,
commit_txfee_recv(channel, receivable)))
commit_txfee(channel,
receivable, REMOTE)))
receivable = AMOUNT_MSAT(0);
}

Expand Down

0 comments on commit 784ed72

Please sign in to comment.