Skip to content

rebalance.py: Fixing setup_routing_fees() and peer_from_scid() for unannounced channels #670

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
40 changes: 33 additions & 7 deletions rebalance/rebalance.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,38 @@ def route_get_msat(r):

def setup_routing_fees(route, msat):
delay = plugin.cltv_final
for r in reversed(route):
if plugin.listpeerchannels:
loop_first_channel = -2
r = route[-1]
route_set_msat(r, msat)
r["delay"] = delay
channels = plugin.rpc.listpeerchannels(route[-2]["id"]).get("channels")
ch = next(c["updates"]["remote"] for c in channels if c["short_channel_id"] == r["channel"])
fee = Millisatoshi(ch["fee_base_msat"])
# BOLT #7 requires fee >= fee_base_msat + ( amount_to_forward * fee_proportional_millionths / 1000000 )
fee += (
msat * ch["fee_proportional_millionths"] + 10**6 - 1
) // 10**6 # integer math trick to round up
msat += fee
delay += ch["cltv_expiry_delta"]
else:
loop_first_channel = -1

for r in route[loop_first_channel:0:-1]:
route_set_msat(r, msat)
r["delay"] = delay
channels = plugin.rpc.listchannels(r["channel"])
ch = next(c for c in channels.get("channels") if c["destination"] == r["id"])
channels = plugin.rpc.listchannels(r["channel"]).get("channels")
ch = next(c for c in channels if c["destination"] == r["id"])
fee = Millisatoshi(ch["base_fee_millisatoshi"])
# BOLT #7 requires fee >= fee_base_msat + ( amount_to_forward * fee_proportional_millionths / 1000000 )
fee += (
msat * ch["fee_per_millionth"] + 10**6 - 1
) // 10**6 # integer math trick to round up
msat += fee
delay += ch["delay"]
r = route[0]
route_set_msat(r, msat)
r["delay"] = delay


def get_channel(payload, peer_id, scid, check_state: bool = False):
Expand Down Expand Up @@ -109,10 +129,16 @@ def amounts_from_scid(scid):


def peer_from_scid(short_channel_id, my_node_id, payload):
channels = plugin.rpc.listchannels(short_channel_id).get("channels")
for ch in channels:
if ch["source"] == my_node_id:
return ch["destination"]
if plugin.listpeerchannels:
channels = plugin.rpc.listpeerchannels().get("channels")
for ch in channels:
if ch["short_channel_id"] == short_channel_id:
return ch["peer_id"]
else:
channels = plugin.rpc.listchannels(short_channel_id).get("channels")
for ch in channels:
if ch["source"] == my_node_id:
return ch["destination"]
raise RpcError(
"rebalance",
payload,
Expand Down