From 327f6adca904d3420ae5902aa83e4a6b7a0f963e Mon Sep 17 00:00:00 2001
From: ThomasV <thomasv@electrum.org>
Date: Thu, 18 Oct 2018 13:17:29 +0200
Subject: [PATCH] encapsulate get_invoice in lnworker

---
 electrum/lnbase.py   | 13 +++----------
 electrum/lnworker.py |  8 ++++++++
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/electrum/lnbase.py b/electrum/lnbase.py
index 4eb311eea..33ee43084 100644
--- a/electrum/lnbase.py
+++ b/electrum/lnbase.py
@@ -214,7 +214,6 @@ class Peer(PrintError):
         if request_initial_sync:
             self.localfeatures |= LnLocalFeatures.INITIAL_ROUTING_SYNC
         self.localfeatures |= LnLocalFeatures.OPTION_DATA_LOSS_PROTECT_OPT
-        self.invoices = lnworker.invoices
         self.attempted_route = {}
 
     def send_message(self, message_name, **kwargs):
@@ -996,16 +995,11 @@ class Peer(PrintError):
     @log_exceptions
     async def on_update_add_htlc(self, payload):
         # no onion routing for the moment: we assume we are the end node
-        self.print_error('on_update_add_htlc', payload)
+        self.print_error('on_update_add_htlc')
         # check if this in our list of requests
         payment_hash = payload["payment_hash"]
-        for k in self.invoices.keys():
-            preimage = bfh(k)
-            if sha256(preimage) == payment_hash:
-                break
-        else:
-            raise Exception('unknown payment hash')
-        request = lndecode(self.invoices[k], expected_hrp=constants.net.SEGWIT_HRP)
+        preimage, invoice = self.lnworker.get_invoice(payment_hash)
+        expected_received_msat = int(invoice.amount * bitcoin.COIN * 1000)
         channel_id = payload['channel_id']
         htlc_id = int.from_bytes(payload["id"], 'big')
         cltv_expiry = int.from_bytes(payload["cltv_expiry"], 'big')
@@ -1014,7 +1008,6 @@ class Peer(PrintError):
         assert htlc_id == chan.config[REMOTE].next_htlc_id, (htlc_id, chan.config[REMOTE].next_htlc_id)
         assert chan.get_state() == "OPEN"
         # TODO verify sanity of their cltv expiry
-        expected_received_msat = int(request.amount * bitcoin.COIN * 1000)
         assert amount_msat == expected_received_msat
         htlc = {'amount_msat': amount_msat, 'payment_hash':payment_hash, 'cltv_expiry':cltv_expiry}
         chan.receive_htlc(htlc)
diff --git a/electrum/lnworker.py b/electrum/lnworker.py
index 06f1baaf6..1a940b46f 100644
--- a/electrum/lnworker.py
+++ b/electrum/lnworker.py
@@ -325,6 +325,14 @@ class LNWorker(PrintError):
         self.wallet.storage.write()
         return pay_req
 
+    def get_invoice(self, payment_hash):
+        for k in self.invoices.keys():
+            preimage = bfh(k)
+            if sha256(preimage) == payment_hash:
+                return preimage, lndecode(self.invoices[k], expected_hrp=constants.net.SEGWIT_HRP)
+        else:
+            raise Exception('unknown payment hash')
+
     def _calc_routing_hints_for_invoice(self, amount_sat):
         """calculate routing hints (BOLT-11 'r' field)"""
         routing_hints = []