Browse Source

Move get_channel_info and get_channel_policy code, so that routing

hints can be created without access to a ChannelDB instance.
patch-4
ThomasV 4 years ago
parent
commit
1161ce919f
  1. 53
      electrum/channel_db.py
  2. 6
      electrum/lnworker.py

53
electrum/channel_db.py

@ -220,6 +220,32 @@ class CategorizedChannelUpdates(NamedTuple):
good: List # good updates 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_channel_info = """
CREATE TABLE IF NOT EXISTS channel_info ( CREATE TABLE IF NOT EXISTS channel_info (
short_channel_id BLOB(8), short_channel_id BLOB(8),
@ -700,24 +726,8 @@ class ChannelDB(SqlDB):
if chan_upd_dict: if chan_upd_dict:
return Policy.from_msg(chan_upd_dict) return Policy.from_msg(chan_upd_dict)
# check if it's one of our own channels # check if it's one of our own channels
if not my_channels: if my_channels:
return return get_mychannel_policy(short_channel_id, node_id, my_channels)
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)
def get_channel_info(self, short_channel_id: ShortChannelID, *, def get_channel_info(self, short_channel_id: ShortChannelID, *,
my_channels: Dict[ShortChannelID, 'Channel'] = None) -> Optional[ChannelInfo]: my_channels: Dict[ShortChannelID, 'Channel'] = None) -> Optional[ChannelInfo]:
@ -725,11 +735,8 @@ class ChannelDB(SqlDB):
if ret: if ret:
return ret return ret
# check if it's one of our own channels # check if it's one of our own channels
if not my_channels: if my_channels:
return return get_mychannel_info(short_channel_id, my_channels)
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)
def get_channels_for_node(self, node_id: bytes, *, def get_channels_for_node(self, node_id: bytes, *,
my_channels: Dict[ShortChannelID, 'Channel'] = None) -> Set[bytes]: my_channels: Dict[ShortChannelID, 'Channel'] = None) -> Set[bytes]:

6
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 .lnutil import ChannelBackupStorage
from .lnchannel import ChannelBackup from .lnchannel import ChannelBackup
from .channel_db import UpdateStatus from .channel_db import UpdateStatus
from .channel_db import get_mychannel_info, get_mychannel_policy
from .submarine_swaps import SwapManager from .submarine_swaps import SwapManager
if TYPE_CHECKING: if TYPE_CHECKING:
@ -1323,7 +1324,7 @@ class LNWallet(LNWorker):
continue continue
chan_id = chan.short_channel_id chan_id = chan.short_channel_id
assert isinstance(chan_id, bytes), chan_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 # 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. # 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 # 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 cltv_expiry_delta = 1 # lnd won't even try with zero
missing_info = True missing_info = True
if channel_info: if channel_info:
policy = self.channel_db.get_policy_for_node(channel_info.short_channel_id, chan.node_id, policy = get_mychannel_policy(channel_info.short_channel_id, chan.node_id, scid_to_my_channels)
my_channels=scid_to_my_channels)
if policy: if policy:
fee_base_msat = policy.fee_base_msat fee_base_msat = policy.fee_base_msat
fee_proportional_millionths = policy.fee_proportional_millionths fee_proportional_millionths = policy.fee_proportional_millionths

Loading…
Cancel
Save