From 55d0a9587ec1dbadbecd6b97662bda0a84cee565 Mon Sep 17 00:00:00 2001
From: ThomasV <thomasv@electrum.org>
Date: Mon, 6 Apr 2020 18:35:12 +0200
Subject: [PATCH] move maybe_save_short_chan_id to lnchannel

---
 electrum/lnchannel.py | 34 +++++++++++++++++++++++++++++++---
 electrum/lnworker.py  | 31 -------------------------------
 2 files changed, 31 insertions(+), 34 deletions(-)

diff --git a/electrum/lnchannel.py b/electrum/lnchannel.py
index 85ad99a46..ab28dad29 100644
--- a/electrum/lnchannel.py
+++ b/electrum/lnchannel.py
@@ -1105,6 +1105,33 @@ class Channel(Logger):
         else:
             self.update_closed_state(funding_txid, funding_height, closing_txid, closing_height, keep_watching)
 
+    def is_funding_tx_mined(self, funding_height):
+        """
+        Checks if Funding TX has been mined. If it has, save the short channel ID in chan;
+        if it's also deep enough, also save to disk.
+        Returns tuple (mined_deep_enough, num_confirmations).
+        """
+        funding_txid = self.funding_outpoint.txid
+        funding_idx = self.funding_outpoint.output_index
+        conf = funding_height.conf
+        if conf < self.constraints.funding_txn_minimum_depth:
+            self.logger.info(f"funding tx is still not at sufficient depth. actual depth: {conf}")
+            return False
+        assert conf > 0
+        # check funding_tx amount and script
+        funding_tx = self.lnworker.lnwatcher.db.get_transaction(funding_txid)
+        if not funding_tx:
+            self.logger.info(f"no funding_tx {funding_txid}")
+            return False
+        outp = funding_tx.outputs()[funding_idx]
+        redeem_script = funding_output_script(self.config[REMOTE], self.config[LOCAL])
+        funding_address = redeem_script_to_address('p2wsh', redeem_script)
+        funding_sat = self.constraints.capacity
+        if not (outp.address == funding_address and outp.value == funding_sat):
+            self.logger.info('funding outpoint mismatch')
+            return False
+        return True
+
     def update_unfunded_state(self):
         self.delete_funding_height()
         self.delete_closing_height()
@@ -1136,10 +1163,11 @@ class Channel(Logger):
         self.save_funding_height(funding_txid, funding_height.height, funding_height.timestamp)
         self.delete_closing_height()
         if self.get_state() == channel_states.OPENING:
-            if self.short_channel_id is None:
-                self.lnworker.maybe_save_short_chan_id(self, funding_height)
-            if self.short_channel_id:
+            if self.is_funding_tx_mined(funding_height):
                 self.set_state(channel_states.FUNDED)
+                self.set_short_channel_id(ShortChannelID.from_components(
+                    funding_height.height, funding_height.txpos, self.funding_outpoint.output_index))
+                self.logger.info(f"save_short_channel_id: {self.short_channel_id}")
 
     def update_closed_state(self, funding_txid, funding_height, closing_txid, closing_height, keep_watching):
         self.save_funding_height(funding_txid, funding_height.height, funding_height.timestamp)
diff --git a/electrum/lnworker.py b/electrum/lnworker.py
index 6da31e5c1..78a24a31d 100644
--- a/electrum/lnworker.py
+++ b/electrum/lnworker.py
@@ -659,36 +659,6 @@ class LNWallet(LNWorker):
         self.wallet.save_db()
         self.network.trigger_callback('channel', chan)
 
-    def maybe_save_short_chan_id(self, chan, funding_height):
-        """
-        Checks if Funding TX has been mined. If it has, save the short channel ID in chan;
-        if it's also deep enough, also save to disk.
-        Returns tuple (mined_deep_enough, num_confirmations).
-        """
-        funding_txid = chan.funding_outpoint.txid
-        funding_idx = chan.funding_outpoint.output_index
-        conf = funding_height.conf
-        if conf < chan.constraints.funding_txn_minimum_depth:
-            self.logger.info(f"funding tx is still not at sufficient depth. actual depth: {conf}")
-            return
-        assert conf > 0
-        # check funding_tx amount and script
-        funding_tx = self.lnwatcher.db.get_transaction(funding_txid)
-        if not funding_tx:
-            self.logger.info(f"no funding_tx {funding_txid}")
-            return
-        outp = funding_tx.outputs()[funding_idx]
-        redeem_script = funding_output_script(chan.config[REMOTE], chan.config[LOCAL])
-        funding_address = redeem_script_to_address('p2wsh', redeem_script)
-        funding_sat = chan.constraints.capacity
-        if not (outp.address == funding_address and outp.value == funding_sat):
-            self.logger.info('funding outpoint mismatch')
-            return
-        chan.set_short_channel_id(ShortChannelID.from_components(
-            funding_height.height, funding_height.txpos, chan.funding_outpoint.output_index))
-        self.logger.info(f"save_short_channel_id: {chan.short_channel_id}")
-        self.save_channel(chan)
-
     def channel_by_txo(self, txo):
         with self.lock:
             channels = list(self.channels.values())
@@ -696,7 +666,6 @@ class LNWallet(LNWorker):
             if chan.funding_outpoint.to_str() == txo:
                 return chan
 
-
     async def on_channel_update(self, chan):
 
         if chan.get_state() == channel_states.OPEN and chan.should_be_closed_due_to_expiring_htlcs(self.network.get_local_height()):