@@ -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" ])
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" ]) if channels else None
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 ,
0 commit comments