Skip to content

Commit 8a7e333

Browse files
21M4TWchrisguida
authored andcommitted
-Fixing setup_routing_fees() and peer_from_scid() to properly handle
unannounced channels, as the listchannels command does not lists them.
1 parent e8a5a19 commit 8a7e333

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

rebalance/rebalance.py

Lines changed: 33 additions & 7 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"])
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"]) if channels else None
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,

0 commit comments

Comments
 (0)