electrum

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

commit ef7a59b4a97fddcdf354f8a3a599714fbe4e6284
parent 7292da24e6fa2813056f9d0843af568c6eb0a413
Author: SomberNight <somber.night@protonmail.com>
Date:   Tue, 26 Feb 2019 21:28:11 +0100

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

will properly clean this up...

Diffstat:
Melectrum/lnchannel.py | 19+++++++++++--------
Melectrum/lnpeer.py | 2++
Melectrum/lnworker.py | 8++++----
3 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/electrum/lnchannel.py b/electrum/lnchannel.py @@ -28,6 +28,7 @@ import binascii import json from enum import Enum, auto from typing import Optional, Dict, List, Tuple, NamedTuple, Set, Callable, Iterable, Sequence +import time from . import ecc from .util import bfh, PrintError, bh2u @@ -120,10 +121,9 @@ class Channel(PrintError): return super().diagnostic_name() def __init__(self, state, *, sweep_address=None, name=None, - payment_completed: Optional[Callable[['Channel', Direction, UpdateAddHtlc, bytes], None]] = None): - self.preimages = {} + payment_completed: Optional[Callable[['Channel', Direction, UpdateAddHtlc], None]] = None): 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.payment_completed = payment_completed assert 'local_state' not in state @@ -438,10 +438,9 @@ class Channel(PrintError): received = self.hm.received_in_ctn(self.config[LOCAL].ctn) sent = self.hm.sent_in_ctn(self.config[LOCAL].ctn) for htlc in received: - self.payment_completed(self, RECEIVED, htlc, None) + self.payment_completed(self, RECEIVED, htlc) for htlc in sent: - preimage = self.preimages.pop(htlc.htlc_id) - self.payment_completed(self, SENT, htlc, preimage) + self.payment_completed(self, SENT, htlc) received_this_batch = htlcsum(received) sent_this_batch = htlcsum(sent) @@ -625,8 +624,12 @@ class Channel(PrintError): assert htlc.payment_hash == sha256(preimage) assert htlc_id not in log['settles'] self.hm.recv_settle(htlc_id) - self.preimages[htlc_id] = preimage - # we don't save the preimage because we don't need to forward it anyway + try: + 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): self.print_error("fail_htlc") diff --git a/electrum/lnpeer.py b/electrum/lnpeer.py @@ -379,6 +379,7 @@ class Peer(PrintError): payment_completed=self.lnworker.payment_completed) chan.lnwatcher = self.lnwatcher chan.get_preimage = self.lnworker.get_preimage # FIXME hack. + chan.save_preimage = self.lnworker.save_preimage # FIXME hack. sig_64, _ = chan.sign_next_commitment() self.send_message("funding_created", temporary_channel_id=temp_channel_id, @@ -472,6 +473,7 @@ class Peer(PrintError): payment_completed=self.lnworker.payment_completed) chan.lnwatcher = self.lnwatcher chan.get_preimage = self.lnworker.get_preimage # FIXME hack. + chan.save_preimage = self.lnworker.save_preimage # FIXME hack. remote_sig = funding_created['signature'] chan.receive_new_commitment(remote_sig, []) sig_64, _ = chan.sign_next_commitment() diff --git a/electrum/lnworker.py b/electrum/lnworker.py @@ -79,7 +79,8 @@ class LNWorker(PrintError): self.channels = {} # type: Dict[bytes, Channel] for x in wallet.storage.get("channels", []): 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 c.set_remote_commitment() c.set_local_commitment(c.current_commitment(LOCAL)) @@ -126,11 +127,10 @@ class LNWorker(PrintError): self.print_error('saved lightning gossip timestamp') def payment_completed(self, chan: Channel, direction: Direction, - htlc: UpdateAddHtlc, preimage: Optional[bytes]): + htlc: UpdateAddHtlc): 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()) - self.save_preimage(htlc.payment_hash, preimage, timestamp=timestamp) self.network.trigger_callback('ln_payment_completed', timestamp, direction, htlc, preimage, chan_id) def get_invoice_status(self, payment_hash):