Browse Source

fix #6125: detect self-payments

master
ThomasV 5 years ago
parent
commit
984da7515a
  1. 6
      electrum/gui/kivy/uix/dialogs/lightning_tx_dialog.py
  2. 4
      electrum/lnchannel.py
  3. 29
      electrum/lnworker.py

6
electrum/gui/kivy/uix/dialogs/lightning_tx_dialog.py

@ -98,7 +98,6 @@ class LightningTxDialog(Factory.Popup):
self.app = app # type: ElectrumWindow self.app = app # type: ElectrumWindow
self.wallet = self.app.wallet self.wallet = self.app.wallet
self._action_button_fn = lambda btn: None self._action_button_fn = lambda btn: None
self.is_sent = bool(tx_item['direction'] == 'sent')
self.description = tx_item['label'] self.description = tx_item['label']
self.timestamp = tx_item['timestamp'] self.timestamp = tx_item['timestamp']
self.date_str = datetime.fromtimestamp(self.timestamp).isoformat(' ')[:-3] self.date_str = datetime.fromtimestamp(self.timestamp).isoformat(' ')[:-3]
@ -106,6 +105,7 @@ class LightningTxDialog(Factory.Popup):
self.payment_hash = tx_item['payment_hash'] self.payment_hash = tx_item['payment_hash']
self.preimage = tx_item['preimage'] self.preimage = tx_item['preimage']
format_amount = self.app.format_amount_and_units format_amount = self.app.format_amount_and_units
self.amount_str = format_amount(self.amount) self.is_sent = self.amount < 0
if self.is_sent: self.amount_str = format_amount(-self.amount if self.is_sent else self.amount)
if tx_item.get('fee_msat'):
self.fee_str = format_amount(Decimal(tx_item['fee_msat']) / 1000) self.fee_str = format_amount(Decimal(tx_item['fee_msat']) / 1000)

4
electrum/lnchannel.py

@ -631,14 +631,14 @@ class Channel(AbstractChannel):
return out return out
def get_settled_payments(self): def get_settled_payments(self):
out = {} out = defaultdict(list)
for subject in LOCAL, REMOTE: for subject in LOCAL, REMOTE:
log = self.hm.log[subject] log = self.hm.log[subject]
for htlc_id, htlc in log.get('adds', {}).items(): for htlc_id, htlc in log.get('adds', {}).items():
if htlc_id in log.get('settles',{}): if htlc_id in log.get('settles',{}):
direction = SENT if subject is LOCAL else RECEIVED direction = SENT if subject is LOCAL else RECEIVED
rhash = bh2u(htlc.payment_hash) rhash = bh2u(htlc.payment_hash)
out[rhash] = (self.channel_id, htlc, direction) out[rhash].append((self.channel_id, htlc, direction))
return out return out
def open_with_first_pcp(self, remote_pcp: bytes, remote_sig: bytes) -> None: def open_with_first_pcp(self, remote_pcp: bytes, remote_sig: bytes) -> None:

29
electrum/lnworker.py

@ -593,7 +593,7 @@ class LNWallet(LNWorker):
for chan in self.channels.values(): for chan in self.channels.values():
d = chan.get_settled_payments() d = chan.get_settled_payments()
for k, v in d.items(): for k, v in d.items():
out[k].append(v) out[k] += v
return out return out
def get_lightning_history(self): def get_lightning_history(self):
@ -601,26 +601,21 @@ class LNWallet(LNWorker):
for key, plist in self.get_settled_payments().items(): for key, plist in self.get_settled_payments().items():
if len(plist) == 0: if len(plist) == 0:
continue continue
elif len(plist) == 1: payment_hash = bytes.fromhex(key)
chan_id, htlc, _direction = plist[0] info = self.get_payment_info(payment_hash)
direction = 'sent' if _direction == SENT else 'received' timestamp = min([htlc.timestamp for chan_id, htlc, _direction in plist])
amount_msat = int(_direction) * htlc.amount_msat amount_msat = 0
timestamp = htlc.timestamp
label = self.wallet.get_label(key)
if _direction == SENT:
info = self.get_payment_info(bfh(key))
fee_msat = - info.amount*1000 - amount_msat if info and info.amount else None
else:
fee_msat = None fee_msat = None
for chan_id, htlc, _direction in plist:
amount_msat += int(_direction) * htlc.amount_msat
if _direction == SENT and info and info.amount:
fee_msat = (fee_msat or 0) - info.amount*1000 - amount_msat
if info is not None:
label = self.wallet.get_label(key)
direction = ('sent' if info.direction == SENT else 'received') if len(plist)==1 else 'self-payment'
else: else:
# assume forwarding
direction = 'forwarding' direction = 'forwarding'
amount_msat = sum([int(_direction) * htlc.amount_msat for chan_id, htlc, _direction in plist])
label = _('Forwarding') label = _('Forwarding')
timestamp = min([htlc.timestamp for chan_id, htlc, _direction in plist])
fee_msat = None # fixme
payment_hash = bytes.fromhex(key)
preimage = self.get_preimage(payment_hash).hex() preimage = self.get_preimage(payment_hash).hex()
item = { item = {
'type': 'payment', 'type': 'payment',

Loading…
Cancel
Save