Browse Source

path finding: minor clean-up

dependabot/pip/contrib/deterministic-build/ecdsa-0.13.3
SomberNight 6 years ago
committed by ThomasV
parent
commit
a91e244a05
  1. 5
      electrum/lnrouter.py
  2. 12
      electrum/lnworker.py

5
electrum/lnrouter.py

@ -519,7 +519,7 @@ class LNPathFinder(PrintError):
@profiler @profiler
def find_path_for_payment(self, from_node_id: bytes, to_node_id: bytes, def find_path_for_payment(self, from_node_id: bytes, to_node_id: bytes,
amount_msat: int=None, my_channels: dict={}) -> Sequence[Tuple[bytes, bytes]]: amount_msat: int=None, my_channels: List=None) -> Sequence[Tuple[bytes, bytes]]:
"""Return a path between from_node_id and to_node_id. """Return a path between from_node_id and to_node_id.
Returns a list of (node_id, short_channel_id) representing a path. Returns a list of (node_id, short_channel_id) representing a path.
@ -527,7 +527,8 @@ class LNPathFinder(PrintError):
i.e. an element reads as, "to get to node_id, travel through short_channel_id" i.e. an element reads as, "to get to node_id, travel through short_channel_id"
""" """
if amount_msat is not None: assert type(amount_msat) is int if amount_msat is not None: assert type(amount_msat) is int
unable_channels = set(map(lambda x: x.short_channel_id, filter(lambda x: not x.can_pay(amount_msat), my_channels.values()))) if my_channels is None: my_channels = []
unable_channels = set(map(lambda x: x.short_channel_id, filter(lambda x: not x.can_pay(amount_msat), my_channels)))
# TODO find multiple paths?? # TODO find multiple paths??

12
electrum/lnworker.py

@ -54,7 +54,7 @@ class LNWorker(PrintError):
self.node_keypair = generate_keypair(self.ln_keystore, LnKeyFamily.NODE_KEY, 0) self.node_keypair = generate_keypair(self.ln_keystore, LnKeyFamily.NODE_KEY, 0)
self.config = network.config self.config = network.config
self.peers = {} # type: Dict[bytes, Peer] # pubkey -> Peer self.peers = {} # type: Dict[bytes, Peer] # pubkey -> Peer
self.channels = {x.channel_id: x for x in map(Channel, wallet.storage.get("channels", []))} # type: Dict[bytes, HTLCStateMachine] self.channels = {x.channel_id: x for x in map(Channel, wallet.storage.get("channels", []))} # type: Dict[bytes, Channel]
for c in self.channels.values(): for c in self.channels.values():
c.lnwatcher = network.lnwatcher c.lnwatcher = network.lnwatcher
c.sweep_address = self.sweep_address c.sweep_address = self.sweep_address
@ -277,11 +277,13 @@ class LNWorker(PrintError):
# if there are multiple hints, we will use the first one that works, # if there are multiple hints, we will use the first one that works,
# from a random permutation # from a random permutation
random.shuffle(r_tags) random.shuffle(r_tags)
with self.lock:
channels = list(self.channels.values())
for private_route in r_tags: for private_route in r_tags:
if len(private_route) == 0: continue if len(private_route) == 0: continue
border_node_pubkey = private_route[0][0] border_node_pubkey = private_route[0][0]
path = self.network.path_finder.find_path_for_payment(self.node_keypair.pubkey, border_node_pubkey, amount_msat, self.channels) path = self.network.path_finder.find_path_for_payment(self.node_keypair.pubkey, border_node_pubkey, amount_msat, channels)
if path is None: continue if not path: continue
route = self.network.path_finder.create_route_from_path(path, self.node_keypair.pubkey) route = self.network.path_finder.create_route_from_path(path, self.node_keypair.pubkey)
# 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_route_nodes = [edge[0] for edge in private_route][1:] + [invoice_pubkey]
@ -293,8 +295,8 @@ class LNWorker(PrintError):
break break
# if could not find route using any hint; try without hint now # if could not find route using any hint; try without hint now
if route is None: if route is None:
path = self.network.path_finder.find_path_for_payment(self.node_keypair.pubkey, invoice_pubkey, amount_msat, self.channels) path = self.network.path_finder.find_path_for_payment(self.node_keypair.pubkey, invoice_pubkey, amount_msat, channels)
if path is None: if not path:
raise PaymentFailure(_("No path found")) raise PaymentFailure(_("No path found"))
route = self.network.path_finder.create_route_from_path(path, self.node_keypair.pubkey) route = self.network.path_finder.create_route_from_path(path, self.node_keypair.pubkey)
return route return route

Loading…
Cancel
Save