Browse Source

Refactor find_route_for_payment

- remove duplicated code
 - rename variable names to be consistent with the
'path', 'route' terminology
 - compute private route before route
master
ThomasV 5 years ago
parent
commit
391dba7117
  1. 61
      electrum/lnworker.py

61
electrum/lnworker.py

@ -1470,33 +1470,21 @@ class LNWallet(LNWorker):
if chan.short_channel_id is not None if chan.short_channel_id is not None
} }
blacklist = self.network.channel_blacklist.get_current_list() blacklist = self.network.channel_blacklist.get_current_list()
for private_route in r_tags: # first try with routing hints, then without
if len(private_route) == 0: for private_path in r_tags + [[]]:
private_route = []
path = full_path
if len(private_path) > NUM_MAX_EDGES_IN_PAYMENT_PATH:
continue continue
if len(private_route) > NUM_MAX_EDGES_IN_PAYMENT_PATH: if len(private_path) == 0:
continue border_node_pubkey = invoice_pubkey
border_node_pubkey = private_route[0][0]
if full_path:
# user pre-selected path. check that end of given path coincides with private_route:
if [edge.short_channel_id for edge in full_path[-len(private_route):]] != [edge[1] for edge in private_route]:
continue
path = full_path[:-len(private_route)]
else: else:
# find path now on public graph, to border node border_node_pubkey = private_path[0][0]
path = None
try:
route = self.network.path_finder.find_route(
self.node_keypair.pubkey, border_node_pubkey, amount_msat,
path=path, my_channels=scid_to_my_channels, blacklist=blacklist)
except NoChannelPolicy:
continue
if not route:
continue
# we need to shift the node pubkey by one towards the destination: # we need to shift the node pubkey by one towards the destination:
private_route_nodes = [edge[0] for edge in private_route][1:] + [invoice_pubkey] private_path_nodes = [edge[0] for edge in private_path][1:] + [invoice_pubkey]
private_route_rest = [edge[1:] for edge in private_route] private_path_rest = [edge[1:] for edge in private_path]
prev_node_id = border_node_pubkey prev_node_id = border_node_pubkey
for node_pubkey, edge_rest in zip(private_route_nodes, private_route_rest): for node_pubkey, edge_rest in zip(private_path_nodes, private_path_rest):
short_channel_id, fee_base_msat, fee_proportional_millionths, cltv_expiry_delta = edge_rest short_channel_id, fee_base_msat, fee_proportional_millionths, cltv_expiry_delta = edge_rest
short_channel_id = ShortChannelID(short_channel_id) short_channel_id = ShortChannelID(short_channel_id)
# if we have a routing policy for this edge in the db, that takes precedence, # if we have a routing policy for this edge in the db, that takes precedence,
@ -1510,7 +1498,7 @@ class LNWallet(LNWorker):
fee_proportional_millionths = channel_policy.fee_proportional_millionths fee_proportional_millionths = channel_policy.fee_proportional_millionths
cltv_expiry_delta = channel_policy.cltv_expiry_delta cltv_expiry_delta = channel_policy.cltv_expiry_delta
node_info = self.channel_db.get_node_info_for_node_id(node_id=node_pubkey) node_info = self.channel_db.get_node_info_for_node_id(node_id=node_pubkey)
route.append( private_route.append(
RouteEdge( RouteEdge(
node_id=node_pubkey, node_id=node_pubkey,
short_channel_id=short_channel_id, short_channel_id=short_channel_id,
@ -1519,21 +1507,26 @@ class LNWallet(LNWorker):
cltv_expiry_delta=cltv_expiry_delta, cltv_expiry_delta=cltv_expiry_delta,
node_features=node_info.features if node_info else 0)) node_features=node_info.features if node_info else 0))
prev_node_id = node_pubkey prev_node_id = node_pubkey
# test sanity if full_path:
if not is_route_sane_to_use(route, amount_msat, min_cltv_expiry): # user pre-selected path. check that end of given path coincides with private_route:
self.logger.info(f"rejecting insane route {route}") if [edge.short_channel_id for edge in full_path[-len(private_path):]] != [edge[1] for edge in private_path]:
route = None
continue continue
break path = full_path[:-len(private_path)]
# if could not find route using any hint; try without hint now try:
if route is None:
route = self.network.path_finder.find_route( route = self.network.path_finder.find_route(
self.node_keypair.pubkey, invoice_pubkey, amount_msat, self.node_keypair.pubkey, border_node_pubkey, amount_msat,
path=full_path, my_channels=scid_to_my_channels, blacklist=blacklist) path=path, my_channels=scid_to_my_channels, blacklist=blacklist)
except NoChannelPolicy:
continue
if not route: if not route:
raise NoPathFound() continue
route = route + private_route
# test sanity
if not is_route_sane_to_use(route, amount_msat, min_cltv_expiry): if not is_route_sane_to_use(route, amount_msat, min_cltv_expiry):
self.logger.info(f"rejecting insane route {route}") self.logger.info(f"rejecting insane route {route}")
continue
break
else:
raise NoPathFound() raise NoPathFound()
assert len(route) > 0 assert len(route) > 0
if route[-1].node_id != invoice_pubkey: if route[-1].node_id != invoice_pubkey:

Loading…
Cancel
Save