Skip to content

Commit aa77a05

Browse files
authored
rebalance.py: Fixing setup_routing_fees() and peer_from_scid() for unannounced channels (#670)
* -Fixing setup_routing_fees() and peer_from_scid() to properly handle unannounced channels, as the listchannels command does not lists them. * -Removing unnecessary condition * rebalance.py: Aliases were not used for route_in and route_out. Some peers that do support aliases generated an error when trying to send the rebalancing payment.
1 parent 041fecc commit aa77a05

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

rebalance/rebalance.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,38 @@ def route_get_msat(r):
4444

4545
def setup_routing_fees(route, msat):
4646
delay = plugin.cltv_final
47-
for r in reversed(route):
47+
if plugin.listpeerchannels:
48+
loop_first_channel = -2
49+
r = route[-1]
50+
route_set_msat(r, msat)
51+
r["delay"] = delay
52+
channels = plugin.rpc.listpeerchannels(route[-2]["id"]).get("channels")
53+
ch = next(c["updates"]["remote"] for c in channels if c["short_channel_id"] == r["channel"] or c["alias"].get("remote") == r["channel"])
54+
fee = Millisatoshi(ch["fee_base_msat"])
55+
# BOLT #7 requires fee >= fee_base_msat + ( amount_to_forward * fee_proportional_millionths / 1000000 )
56+
fee += (
57+
msat * ch["fee_proportional_millionths"] + 10**6 - 1
58+
) // 10**6 # integer math trick to round up
59+
msat += fee
60+
delay += ch["cltv_expiry_delta"]
61+
else:
62+
loop_first_channel = -1
63+
64+
for r in route[loop_first_channel:0:-1]:
4865
route_set_msat(r, msat)
4966
r["delay"] = delay
50-
channels = plugin.rpc.listchannels(r["channel"])
51-
ch = next(c for c in channels.get("channels") if c["destination"] == r["id"])
67+
channels = plugin.rpc.listchannels(r["channel"]).get("channels")
68+
ch = next(c for c in channels if c["destination"] == r["id"])
5269
fee = Millisatoshi(ch["base_fee_millisatoshi"])
5370
# BOLT #7 requires fee >= fee_base_msat + ( amount_to_forward * fee_proportional_millionths / 1000000 )
5471
fee += (
5572
msat * ch["fee_per_millionth"] + 10**6 - 1
5673
) // 10**6 # integer math trick to round up
5774
msat += fee
5875
delay += ch["delay"]
76+
r = route[0]
77+
route_set_msat(r, msat)
78+
r["delay"] = delay
5979

6080

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

110130

111131
def peer_from_scid(short_channel_id, my_node_id, payload):
112-
channels = plugin.rpc.listchannels(short_channel_id).get("channels")
113-
for ch in channels:
114-
if ch["source"] == my_node_id:
115-
return ch["destination"]
132+
if plugin.listpeerchannels:
133+
channels = plugin.rpc.listpeerchannels().get("channels")
134+
for ch in channels:
135+
if ch["short_channel_id"] == short_channel_id:
136+
return ch["peer_id"]
137+
else:
138+
channels = plugin.rpc.listchannels(short_channel_id).get("channels")
139+
for ch in channels:
140+
if ch["source"] == my_node_id:
141+
return ch["destination"]
116142
raise RpcError(
117143
"rebalance",
118144
payload,
@@ -329,8 +355,10 @@ def rebalance(
329355
my_node_id = plugin.getinfo.get("id")
330356
outgoing_node_id = peer_from_scid(outgoing_scid, my_node_id, payload)
331357
incoming_node_id = peer_from_scid(incoming_scid, my_node_id, payload)
332-
get_channel(payload, outgoing_node_id, outgoing_scid, True)
333-
get_channel(payload, incoming_node_id, incoming_scid, True)
358+
out_aliases = get_channel(payload, outgoing_node_id, outgoing_scid, True).get("alias")
359+
out_alias = out_aliases.get("local") if (out_aliases and out_aliases.get("local")) else outgoing_scid
360+
in_aliases = get_channel(payload, incoming_node_id, incoming_scid, True).get("alias")
361+
in_alias = in_aliases.get("remote") if (in_aliases and in_aliases.get("remote")) else incoming_scid
334362
out_ours, out_total = amounts_from_scid(outgoing_scid)
335363
in_ours, in_total = amounts_from_scid(incoming_scid)
336364

@@ -350,12 +378,12 @@ def rebalance(
350378

351379
route_out = {
352380
"id": outgoing_node_id,
353-
"channel": outgoing_scid,
381+
"channel": out_alias,
354382
"direction": int(not my_node_id < outgoing_node_id),
355383
}
356384
route_in = {
357385
"id": my_node_id,
358-
"channel": incoming_scid,
386+
"channel": in_alias,
359387
"direction": int(not incoming_node_id < my_node_id),
360388
}
361389
start_ts = int(time.time())

0 commit comments

Comments
 (0)