Skip to content

Commit

Permalink
funder: print reason that we don't contribute funds
Browse files Browse the repository at this point in the history
If we don't put funds into a channel, say why in the logs. Should make
it a bit easier to figure out what's going on.
  • Loading branch information
niftynei authored and rustyrussell committed May 22, 2021
1 parent c93bd5b commit 4778320
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 51 deletions.
17 changes: 10 additions & 7 deletions plugins/funder.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ listfunds_success(struct command *cmd,
const jsmntok_t *outputs_tok, *tok;
struct out_req *req;
size_t i;
const char *funding_err;

outputs_tok = json_get_member(buf, result, "outputs");
if (!outputs_tok)
Expand Down Expand Up @@ -410,16 +411,18 @@ listfunds_success(struct command *cmd,
"`listfunds` overflowed output values");
}

info->our_funding = calculate_our_funding(current_policy,
info->id,
info->their_funding,
available_funds,
info->channel_max);
funding_err = calculate_our_funding(current_policy,
info->id,
info->their_funding,
available_funds,
info->channel_max,
&info->our_funding);
plugin_log(cmd->plugin, LOG_DBG,
"Policy %s returned funding amount of %s",
"Policy %s returned funding amount of %s. %s",
funder_policy_desc(tmpctx, current_policy),
type_to_string(tmpctx, struct amount_sat,
&info->our_funding));
&info->our_funding),
funding_err ? funding_err : "");

if (amount_sat_eq(info->our_funding, AMOUNT_SAT(0)))
return command_hook_success(cmd);
Expand Down
97 changes: 72 additions & 25 deletions plugins/funder_policy.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,62 +166,109 @@ apply_policy(struct funder_policy policy,
abort();
}

struct amount_sat
const char *
calculate_our_funding(struct funder_policy policy,
struct node_id id,
struct amount_sat their_funding,
struct amount_sat available_funds,
struct amount_sat channel_max)
struct amount_sat channel_max,
struct amount_sat *our_funding)
{
struct amount_sat our_funding, avail_channel_space,
net_available_funds;
struct amount_sat avail_channel_space, net_available_funds;

/* Are we skipping this one? */
if (pseudorand(100) >= policy.fund_probability)
return AMOUNT_SAT(0);
if (pseudorand(100) >= policy.fund_probability) {
*our_funding = AMOUNT_SAT(0);
return tal_fmt(tmpctx,
"Skipping, failed fund_probability test");
}

/* Figure out amount of actual headroom we have */
if (!amount_sat_sub(&avail_channel_space, channel_max, their_funding))
return AMOUNT_SAT(0);
if (!amount_sat_sub(&avail_channel_space, channel_max, their_funding)
|| amount_sat_eq(avail_channel_space, AMOUNT_SAT(0))) {
*our_funding = AMOUNT_SAT(0);
return tal_fmt(tmpctx, "No space available in channel."
" channel_max %s, their_funding %s",
type_to_string(tmpctx, struct amount_sat,
&channel_max),
type_to_string(tmpctx, struct amount_sat,
&their_funding));
}

/* Figure out actual available funds, given our requested
* 'reserve_tank' */
if (!amount_sat_sub(&net_available_funds, available_funds,
policy.reserve_tank))
return AMOUNT_SAT(0);
policy.reserve_tank)
|| amount_sat_eq(net_available_funds, AMOUNT_SAT(0))) {
*our_funding = AMOUNT_SAT(0);
return tal_fmt(tmpctx, "Reserve tank too low."
" available_funds %s, reserve_tank requires %s",
type_to_string(tmpctx, struct amount_sat,
&available_funds),
type_to_string(tmpctx, struct amount_sat,
&policy.reserve_tank));
}

/* Are they funding enough ? */
if (amount_sat_less(their_funding, policy.min_their_funding))
return AMOUNT_SAT(0);
if (amount_sat_less(their_funding, policy.min_their_funding)) {
*our_funding = AMOUNT_SAT(0);
return tal_fmt(tmpctx, "Peer's funding too little."
" their_funding %s,"
" min_their_funding requires %s",
type_to_string(tmpctx, struct amount_sat,
&their_funding),
type_to_string(tmpctx, struct amount_sat,
&policy.min_their_funding));
}

/* Are they funding too much ? */
if (amount_sat_greater(their_funding, policy.max_their_funding))
return AMOUNT_SAT(0);
if (amount_sat_greater(their_funding, policy.max_their_funding)) {
*our_funding = AMOUNT_SAT(0);
return tal_fmt(tmpctx, "Peer's funding too much."
" their_funding %s,"
" max_their_funding requires %s",
type_to_string(tmpctx, struct amount_sat,
&their_funding),
type_to_string(tmpctx, struct amount_sat,
&policy.max_their_funding));
}

/* What's our amount, given our policy */
our_funding = apply_policy(policy, their_funding, available_funds);
*our_funding = apply_policy(policy, their_funding, available_funds);

/* Don't return an 'error' if we're already at 0 */
if (amount_sat_eq(*our_funding, AMOUNT_SAT(0)))
return NULL;

/* our_funding is probably sane, so let's fuzz this amount a bit */
our_funding = apply_fuzz(policy.fuzz_factor, our_funding);
*our_funding = apply_fuzz(policy.fuzz_factor, *our_funding);

/* Is our_funding more than we can fit? if so set to avail space */
if (amount_sat_greater(our_funding, avail_channel_space))
our_funding = avail_channel_space;
if (amount_sat_greater(*our_funding, avail_channel_space))
*our_funding = avail_channel_space;

/* Is our_funding more than we want to fund in a channel?
* if so set at our desired per-channel max */
if (amount_sat_greater(our_funding, policy.per_channel_max))
our_funding = policy.per_channel_max;
if (amount_sat_greater(*our_funding, policy.per_channel_max))
*our_funding = policy.per_channel_max;

/* Is our_funding more than we have available? if so
* set to max available */
if (amount_sat_greater(our_funding, net_available_funds))
our_funding = net_available_funds;
if (amount_sat_greater(*our_funding, net_available_funds))
*our_funding = net_available_funds;

/* Is our_funding less than our per-channel minimum?
* if so, don't fund */
if (amount_sat_less(our_funding, policy.per_channel_min))
return AMOUNT_SAT(0);
if (amount_sat_less(*our_funding, policy.per_channel_min)) {
*our_funding = AMOUNT_SAT(0);
return tal_fmt(tmpctx, "Can't meet our min channel requirement."
" our_funding %s,"
" per_channel_min requires %s",
type_to_string(tmpctx, struct amount_sat,
our_funding),
type_to_string(tmpctx, struct amount_sat,
&policy.per_channel_min));
}

return our_funding;
return NULL;
}
5 changes: 3 additions & 2 deletions plugins/funder_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ default_funder_policy(enum funder_opt policy,

/* Given the policy and this request's details, figure
* out how much we should contribute to this channel */
struct amount_sat
const char *
calculate_our_funding(struct funder_policy policy,
struct node_id id,
struct amount_sat their_funding,
struct amount_sat available_funds,
struct amount_sat channel_max);
struct amount_sat channel_max,
struct amount_sat *our_funding);

/* Get the name of this policy option */
const char *funder_opt_name(enum funder_opt opt);
Expand Down
Loading

0 comments on commit 4778320

Please sign in to comment.