Skip to content

Commit

Permalink
lnworker: store onchain default labels in a cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ecdsa committed Jan 23, 2023
1 parent 5d9678a commit 062051b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 10 additions & 4 deletions electrum/lnworker.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def __init__(self, xprv, features: LnFeatures):
self.network = None # type: Optional[Network]
self.config = None # type: Optional[SimpleConfig]
self.stopping_soon = False # whether we are being shut down

self._labels_cache = {} # txid -> str
self.register_callbacks()

@property
Expand Down Expand Up @@ -876,6 +876,9 @@ def get_lightning_history(self):
out[payment_hash] = item
return out

def get_label_for_txid(self, txid: str) -> str:
return self._labels_cache.get(txid)

def get_onchain_history(self):
current_height = self.wallet.adb.get_local_height()
out = {}
Expand All @@ -886,10 +889,11 @@ def get_onchain_history(self):
continue
funding_txid, funding_height, funding_timestamp = item
tx_height = self.wallet.adb.get_tx_height(funding_txid)
self._labels_cache[funding_txid] = _('Open channel') + ' ' + chan.get_id_for_log()
item = {
'channel_id': bh2u(chan.channel_id),
'type': 'channel_opening',
'label': self.wallet.get_label_for_txid(funding_txid) or (_('Open channel') + ' ' + chan.get_id_for_log()),
'label': self.get_label_for_txid(funding_txid),
'txid': funding_txid,
'amount_msat': chan.balance(LOCAL, ctn=0),
'direction': PaymentDirection.RECEIVED,
Expand All @@ -906,10 +910,11 @@ def get_onchain_history(self):
continue
closing_txid, closing_height, closing_timestamp = item
tx_height = self.wallet.adb.get_tx_height(closing_txid)
self._labels_cache[closing_txid] = _('Close channel') + ' ' + chan.get_id_for_log()
item = {
'channel_id': bh2u(chan.channel_id),
'txid': closing_txid,
'label': self.wallet.get_label_for_txid(closing_txid) or (_('Close channel') + ' ' + chan.get_id_for_log()),
'label': self.get_label_for_txid(closing_txid),
'type': 'channel_closure',
'amount_msat': -chan.balance_minus_outgoing_htlcs(LOCAL),
'direction': PaymentDirection.SENT,
Expand Down Expand Up @@ -942,13 +947,14 @@ def get_onchain_history(self):
label += ' (%s)' % _('waiting for funding tx confirmation')
if not swap.is_reverse and not swap.is_redeemed and swap.spending_txid is None and delta < 0:
label += f' (refundable in {-delta} blocks)' # fixme: only if unspent
self._labels_cache[txid] = label
out[txid] = {
'txid': txid,
'group_id': txid,
'amount_msat': 0,
#'amount_msat': amount_msat, # must not be added
'type': 'swap',
'label': self.wallet.get_label_for_txid(txid) or label,
'label': self.get_label_for_txid(txid),
}
return out

Expand Down
2 changes: 2 additions & 0 deletions electrum/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,8 @@ def get_label_for_txid(self, tx_hash: str) -> str:
return self._labels.get(tx_hash) or self._get_default_label_for_txid(tx_hash)

def _get_default_label_for_txid(self, tx_hash: str) -> str:
if self.lnworker and (label:= self.lnworker.get_label_for_txid(tx_hash)):
return label
# note: we don't deserialize tx as the history calls us for every tx, and that would be slow
if not self.db.get_txi_addresses(tx_hash):
# no inputs are ismine -> likely incoming payment -> concat labels of output addresses
Expand Down

0 comments on commit 062051b

Please sign in to comment.