@@ -44,18 +44,38 @@ def route_get_msat(r):
44
44
45
45
def setup_routing_fees (route , msat ):
46
46
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 ]:
48
65
route_set_msat (r , msat )
49
66
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" ])
52
69
fee = Millisatoshi (ch ["base_fee_millisatoshi" ])
53
70
# BOLT #7 requires fee >= fee_base_msat + ( amount_to_forward * fee_proportional_millionths / 1000000 )
54
71
fee += (
55
72
msat * ch ["fee_per_millionth" ] + 10 ** 6 - 1
56
73
) // 10 ** 6 # integer math trick to round up
57
74
msat += fee
58
75
delay += ch ["delay" ]
76
+ r = route [0 ]
77
+ route_set_msat (r , msat )
78
+ r ["delay" ] = delay
59
79
60
80
61
81
def get_channel (payload , peer_id , scid , check_state : bool = False ):
@@ -109,10 +129,16 @@ def amounts_from_scid(scid):
109
129
110
130
111
131
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" ]
116
142
raise RpcError (
117
143
"rebalance" ,
118
144
payload ,
@@ -329,8 +355,10 @@ def rebalance(
329
355
my_node_id = plugin .getinfo .get ("id" )
330
356
outgoing_node_id = peer_from_scid (outgoing_scid , my_node_id , payload )
331
357
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
334
362
out_ours , out_total = amounts_from_scid (outgoing_scid )
335
363
in_ours , in_total = amounts_from_scid (incoming_scid )
336
364
@@ -350,12 +378,12 @@ def rebalance(
350
378
351
379
route_out = {
352
380
"id" : outgoing_node_id ,
353
- "channel" : outgoing_scid ,
381
+ "channel" : out_alias ,
354
382
"direction" : int (not my_node_id < outgoing_node_id ),
355
383
}
356
384
route_in = {
357
385
"id" : my_node_id ,
358
- "channel" : incoming_scid ,
386
+ "channel" : in_alias ,
359
387
"direction" : int (not incoming_node_id < my_node_id ),
360
388
}
361
389
start_ts = int (time .time ())
0 commit comments