electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 1161ce919f59110973f59bf23175a19a759cc517
parent 4bd4fc7697e118755af4d00579b1f2d1b08a63c9
Author: ThomasV <thomasv@electrum.org>
Date:   Mon, 23 Nov 2020 14:57:14 +0100

Move get_channel_info and get_channel_policy code, so that routing
hints can be created without access to a ChannelDB instance.

Diffstat:
Melectrum/channel_db.py | 53++++++++++++++++++++++++++++++-----------------------
Melectrum/lnworker.py | 6+++---
2 files changed, 33 insertions(+), 26 deletions(-)

diff --git a/electrum/channel_db.py b/electrum/channel_db.py @@ -220,6 +220,32 @@ class CategorizedChannelUpdates(NamedTuple): good: List # good updates +def get_mychannel_info(short_channel_id: ShortChannelID, + my_channels: Dict[ShortChannelID, 'Channel']) -> Optional[ChannelInfo]: + chan = my_channels.get(short_channel_id) + ci = ChannelInfo.from_raw_msg(chan.construct_channel_announcement_without_sigs()) + return ci._replace(capacity_sat=chan.constraints.capacity) + +def get_mychannel_policy(short_channel_id: bytes, node_id: bytes, + my_channels: Dict[ShortChannelID, 'Channel']) -> Optional[Policy]: + chan = my_channels.get(short_channel_id) # type: Optional[Channel] + if not chan: + return + if node_id == chan.node_id: # incoming direction (to us) + remote_update_raw = chan.get_remote_update() + if not remote_update_raw: + return + now = int(time.time()) + remote_update_decoded = decode_msg(remote_update_raw)[1] + remote_update_decoded['timestamp'] = now + remote_update_decoded['start_node'] = node_id + return Policy.from_msg(remote_update_decoded) + elif node_id == chan.get_local_pubkey(): # outgoing direction (from us) + local_update_decoded = decode_msg(chan.get_outgoing_gossip_channel_update())[1] + local_update_decoded['start_node'] = node_id + return Policy.from_msg(local_update_decoded) + + create_channel_info = """ CREATE TABLE IF NOT EXISTS channel_info ( short_channel_id BLOB(8), @@ -700,24 +726,8 @@ class ChannelDB(SqlDB): if chan_upd_dict: return Policy.from_msg(chan_upd_dict) # check if it's one of our own channels - if not my_channels: - return - chan = my_channels.get(short_channel_id) # type: Optional[Channel] - if not chan: - return - if node_id == chan.node_id: # incoming direction (to us) - remote_update_raw = chan.get_remote_update() - if not remote_update_raw: - return - now = int(time.time()) - remote_update_decoded = decode_msg(remote_update_raw)[1] - remote_update_decoded['timestamp'] = now - remote_update_decoded['start_node'] = node_id - return Policy.from_msg(remote_update_decoded) - elif node_id == chan.get_local_pubkey(): # outgoing direction (from us) - local_update_decoded = decode_msg(chan.get_outgoing_gossip_channel_update())[1] - local_update_decoded['start_node'] = node_id - return Policy.from_msg(local_update_decoded) + if my_channels: + return get_mychannel_policy(short_channel_id, node_id, my_channels) def get_channel_info(self, short_channel_id: ShortChannelID, *, my_channels: Dict[ShortChannelID, 'Channel'] = None) -> Optional[ChannelInfo]: @@ -725,11 +735,8 @@ class ChannelDB(SqlDB): if ret: return ret # check if it's one of our own channels - if not my_channels: - return - chan = my_channels.get(short_channel_id) # type: Optional[Channel] - ci = ChannelInfo.from_raw_msg(chan.construct_channel_announcement_without_sigs()) - return ci._replace(capacity_sat=chan.constraints.capacity) + if my_channels: + return get_mychannel_info(short_channel_id, my_channels) def get_channels_for_node(self, node_id: bytes, *, my_channels: Dict[ShortChannelID, 'Channel'] = None) -> Set[bytes]: diff --git a/electrum/lnworker.py b/electrum/lnworker.py @@ -72,6 +72,7 @@ from .crypto import pw_encode_with_version_and_mac, pw_decode_with_version_and_m from .lnutil import ChannelBackupStorage from .lnchannel import ChannelBackup from .channel_db import UpdateStatus +from .channel_db import get_mychannel_info, get_mychannel_policy from .submarine_swaps import SwapManager if TYPE_CHECKING: @@ -1323,7 +1324,7 @@ class LNWallet(LNWorker): continue chan_id = chan.short_channel_id assert isinstance(chan_id, bytes), chan_id - channel_info = self.channel_db.get_channel_info(chan_id, my_channels=scid_to_my_channels) + channel_info = get_mychannel_info(chan_id, scid_to_my_channels) # note: as a fallback, if we don't have a channel update for the # incoming direction of our private channel, we fill the invoice with garbage. # the sender should still be able to pay us, but will incur an extra round trip @@ -1333,8 +1334,7 @@ class LNWallet(LNWorker): cltv_expiry_delta = 1 # lnd won't even try with zero missing_info = True if channel_info: - policy = self.channel_db.get_policy_for_node(channel_info.short_channel_id, chan.node_id, - my_channels=scid_to_my_channels) + policy = get_mychannel_policy(channel_info.short_channel_id, chan.node_id, scid_to_my_channels) if policy: fee_base_msat = policy.fee_base_msat fee_proportional_millionths = policy.fee_proportional_millionths