Browse Source

lnchannel: save htlc preimages as soon as possible but horribly hacky

will properly clean this up...
regtest_lnd
SomberNight 6 years ago
parent
commit
ebd1f83d33
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 19
      electrum/lnchannel.py
  2. 2
      electrum/lnpeer.py
  3. 8
      electrum/lnworker.py

19
electrum/lnchannel.py

@ -28,6 +28,7 @@ import binascii
import json import json
from enum import Enum, auto from enum import Enum, auto
from typing import Optional, Dict, List, Tuple, NamedTuple, Set, Callable, Iterable, Sequence from typing import Optional, Dict, List, Tuple, NamedTuple, Set, Callable, Iterable, Sequence
import time
from . import ecc from . import ecc
from .util import bfh, PrintError, bh2u from .util import bfh, PrintError, bh2u
@ -120,10 +121,9 @@ class Channel(PrintError):
return super().diagnostic_name() return super().diagnostic_name()
def __init__(self, state, *, sweep_address=None, name=None, def __init__(self, state, *, sweep_address=None, name=None,
payment_completed: Optional[Callable[['Channel', Direction, UpdateAddHtlc, bytes], None]] = None): payment_completed: Optional[Callable[['Channel', Direction, UpdateAddHtlc], None]] = None):
self.preimages = {}
if not payment_completed: if not payment_completed:
payment_completed = lambda this, x, y, z: None payment_completed = lambda this, x, y: None
self.sweep_address = sweep_address self.sweep_address = sweep_address
self.payment_completed = payment_completed self.payment_completed = payment_completed
assert 'local_state' not in state assert 'local_state' not in state
@ -438,10 +438,9 @@ class Channel(PrintError):
received = self.hm.received_in_ctn(self.config[LOCAL].ctn) received = self.hm.received_in_ctn(self.config[LOCAL].ctn)
sent = self.hm.sent_in_ctn(self.config[LOCAL].ctn) sent = self.hm.sent_in_ctn(self.config[LOCAL].ctn)
for htlc in received: for htlc in received:
self.payment_completed(self, RECEIVED, htlc, None) self.payment_completed(self, RECEIVED, htlc)
for htlc in sent: for htlc in sent:
preimage = self.preimages.pop(htlc.htlc_id) self.payment_completed(self, SENT, htlc)
self.payment_completed(self, SENT, htlc, preimage)
received_this_batch = htlcsum(received) received_this_batch = htlcsum(received)
sent_this_batch = htlcsum(sent) sent_this_batch = htlcsum(sent)
@ -625,8 +624,12 @@ class Channel(PrintError):
assert htlc.payment_hash == sha256(preimage) assert htlc.payment_hash == sha256(preimage)
assert htlc_id not in log['settles'] assert htlc_id not in log['settles']
self.hm.recv_settle(htlc_id) self.hm.recv_settle(htlc_id)
self.preimages[htlc_id] = preimage try:
# we don't save the preimage because we don't need to forward it anyway self.save_preimage(htlc.payment_hash, preimage, timestamp=int(time.time()))
except AttributeError as e:
# save_preimage is not defined in the unit tests... this is ugly as hell. FIXME
import traceback
traceback.print_exc()
def fail_htlc(self, htlc_id): def fail_htlc(self, htlc_id):
self.print_error("fail_htlc") self.print_error("fail_htlc")

2
electrum/lnpeer.py

@ -379,6 +379,7 @@ class Peer(PrintError):
payment_completed=self.lnworker.payment_completed) payment_completed=self.lnworker.payment_completed)
chan.lnwatcher = self.lnwatcher chan.lnwatcher = self.lnwatcher
chan.get_preimage = self.lnworker.get_preimage # FIXME hack. chan.get_preimage = self.lnworker.get_preimage # FIXME hack.
chan.save_preimage = self.lnworker.save_preimage # FIXME hack.
sig_64, _ = chan.sign_next_commitment() sig_64, _ = chan.sign_next_commitment()
self.send_message("funding_created", self.send_message("funding_created",
temporary_channel_id=temp_channel_id, temporary_channel_id=temp_channel_id,
@ -472,6 +473,7 @@ class Peer(PrintError):
payment_completed=self.lnworker.payment_completed) payment_completed=self.lnworker.payment_completed)
chan.lnwatcher = self.lnwatcher chan.lnwatcher = self.lnwatcher
chan.get_preimage = self.lnworker.get_preimage # FIXME hack. chan.get_preimage = self.lnworker.get_preimage # FIXME hack.
chan.save_preimage = self.lnworker.save_preimage # FIXME hack.
remote_sig = funding_created['signature'] remote_sig = funding_created['signature']
chan.receive_new_commitment(remote_sig, []) chan.receive_new_commitment(remote_sig, [])
sig_64, _ = chan.sign_next_commitment() sig_64, _ = chan.sign_next_commitment()

8
electrum/lnworker.py

@ -79,7 +79,8 @@ class LNWorker(PrintError):
self.channels = {} # type: Dict[bytes, Channel] self.channels = {} # type: Dict[bytes, Channel]
for x in wallet.storage.get("channels", []): for x in wallet.storage.get("channels", []):
c = Channel(x, sweep_address=self.sweep_address, payment_completed=self.payment_completed) c = Channel(x, sweep_address=self.sweep_address, payment_completed=self.payment_completed)
c.get_preimage = self.get_preimage c.get_preimage = self.get_preimage # FIXME hack.
c.save_preimage = self.save_preimage # FIXME hack.
self.channels[c.channel_id] = c self.channels[c.channel_id] = c
c.set_remote_commitment() c.set_remote_commitment()
c.set_local_commitment(c.current_commitment(LOCAL)) c.set_local_commitment(c.current_commitment(LOCAL))
@ -126,11 +127,10 @@ class LNWorker(PrintError):
self.print_error('saved lightning gossip timestamp') self.print_error('saved lightning gossip timestamp')
def payment_completed(self, chan: Channel, direction: Direction, def payment_completed(self, chan: Channel, direction: Direction,
htlc: UpdateAddHtlc, preimage: Optional[bytes]): htlc: UpdateAddHtlc):
chan_id = chan.channel_id chan_id = chan.channel_id
preimage = preimage if preimage else self.get_preimage(htlc.payment_hash) preimage = self.get_preimage(htlc.payment_hash)
timestamp = int(time.time()) timestamp = int(time.time())
self.save_preimage(htlc.payment_hash, preimage, timestamp=timestamp)
self.network.trigger_callback('ln_payment_completed', timestamp, direction, htlc, preimage, chan_id) self.network.trigger_callback('ln_payment_completed', timestamp, direction, htlc, preimage, chan_id)
def get_invoice_status(self, payment_hash): def get_invoice_status(self, payment_hash):

Loading…
Cancel
Save