Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pay: respect maxfeepercent when choosing a shadow route #3693

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions plugins/pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ struct pay_command {

/* How much we're paying, and what riskfactor for routing. */
struct amount_msat msat;
/* Blank amount to pay, without fees and shadow route(s). */
struct amount_msat initial_msat;
/* riskfactor 12.345% -> riskfactor_millionths = 12345000 */
u64 riskfactor_millionths;
unsigned int final_cltv;
Expand Down Expand Up @@ -1046,6 +1048,12 @@ static struct command_result *add_shadow_route(struct command *cmd,
size_t i;
u64 sample = 0;
struct route_info *route = tal_arr(NULL, struct route_info, 1);
struct amount_msat fees, maxfees;
/* Don't go above this. Note how we use the initial amount to get the percentage
* of the fees, or it would increase with the addition of new shadow routes. */
if (!amount_msat_fee(&maxfees, pc->initial_msat, 0, pc->maxfee_pct_millionths))
plugin_err(cmd->plugin, "Overflow when computing maxfees for "
"shadow routes.");

json_for_each_arr(i, chan, channels) {
u64 v = pseudorand(UINT64_MAX);
Expand All @@ -1068,6 +1076,11 @@ static struct command_result *add_shadow_route(struct command *cmd,
json_to_number(buf, json_get_member(buf, chan, "fee_per_millionth"),
&route[0].fee_proportional_millionths);

if (!amount_msat_fee(&fees, pc->initial_msat, route[0].fee_base_msat,
route[0].fee_proportional_millionths)
|| amount_msat_greater_eq(fees, maxfees))
continue;

best = chan;
sample = v;
}
Expand Down Expand Up @@ -1313,13 +1326,13 @@ static struct command_result *json_pay(struct command *cmd,
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"msatoshi parameter unnecessary");
}
pc->msat = *b11->msat;
pc->msat = pc->initial_msat = *b11->msat;
} else {
if (!msat) {
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"msatoshi parameter required");
}
pc->msat = *msat;
pc->msat = pc->initial_msat = *msat;
}

/* Sanity check */
Expand Down
28 changes: 28 additions & 0 deletions tests/test_pay.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,34 @@ def test_payment_duplicate_uncommitted(node_factory, executor):
fut2.result(10)


@unittest.skipIf(not DEVELOPER, "Too slow without --dev-fast-gossip")
def test_pay_maxfee_shadow(node_factory):
"""Test that we respect maxfeepercent for shadow routing."""
l1, l2, l3 = node_factory.line_graph(3, fundchannel=True,
wait_for_announce=True)
# We use this to search for shadow routes
wait_for(
lambda: len(l1.rpc.listchannels(source=l2.info["id"])["channels"]) > 1
)

# shadow routes are random, so run multiple times.
for i in range(5):
# A tiny amount, we must not add the base_fee between l2 and l3
amount = 2
bolt11 = l2.rpc.invoice(amount, "tiny.{}".format(i), "tiny")["bolt11"]
pay_status = l1.rpc.pay(bolt11)
assert pay_status["amount_msat"] == Millisatoshi(amount)

# shadow routes are random, so run multiple times.
for i in range(5):
# A bigger amount, shadow routing could have been used but we set a low
# maxfeepercent.
amount = 20000
bolt11 = l2.rpc.invoice(amount, "big.{}".format(i), "bigger")["bolt11"]
pay_status = l1.rpc.pay(bolt11, maxfeepercent="0.000001")
assert pay_status["amount_msat"] == Millisatoshi(amount)


def test_sendpay(node_factory):
l1, l2 = node_factory.line_graph(2, fundamount=10**6)

Expand Down