URI: 
       tadd watchtower class, send encumbered tx as json - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit e761f5b876785cb2f6b8329ea84454491677fc75
   DIR parent 7589bdc6a953598d081ff81d518277866b931c9e
  HTML Author: ThomasV <thomasv@electrum.org>
       Date:   Sat, 13 Oct 2018 12:11:10 +0200
       
       add watchtower class, send encumbered tx as json
       
       Diffstat:
         M electrum/daemon.py                  |      26 ++++++++++++++++++--------
         M electrum/lnchan.py                  |       4 ++--
         M electrum/lnwatcher.py               |       6 ++++--
       
       3 files changed, 24 insertions(+), 12 deletions(-)
       ---
   DIR diff --git a/electrum/daemon.py b/electrum/daemon.py
       t@@ -122,6 +122,23 @@ def get_rpc_credentials(config: SimpleConfig) -> Tuple[str, str]:
            return rpc_user, rpc_password
        
        
       +class WatchTower(DaemonThread):
       +
       +    def __init__(self, config, lnwatcher):
       +        DaemonThread.__init__(self)
       +        self.config = config
       +        self.lnwatcher = lnwatcher
       +        self.start()
       +
       +    def run(self):
       +        host = self.config.get('watchtower_host')
       +        port = self.config.get('watchtower_port', 12345)
       +        server = SimpleJSONRPCServer((host, port), logRequests=True)
       +        server.register_function(self.lnwatcher.add_sweep_tx, 'add_sweep_tx')
       +        server.timeout = 0.1
       +        while self.is_running():
       +            server.handle_request()
       +
        class Daemon(DaemonThread):
        
            @profiler
       t@@ -147,8 +164,7 @@ class Daemon(DaemonThread):
                self.server = None
                if listen_jsonrpc:
                    self.init_server(config, fd)
       -        if config.get('watchtower_host'):
       -            self.init_watchtower()
       +        self.watchtower = WatchTower(self.config, self.network.lnwatcher) if self.config.get('watchtower_host') else None
                self.start()
        
            def init_server(self, config: SimpleConfig, fd):
       t@@ -176,12 +192,6 @@ class Daemon(DaemonThread):
                    server.register_function(getattr(self.cmd_runner, cmdname), cmdname)
                server.register_function(self.run_cmdline, 'run_cmdline')
        
       -    def init_watchtower(self):
       -        host = self.config.get('watchtower_host')
       -        port = self.config.get('watchtower_port', 12345)
       -        server = SimpleJSONRPCServer((host, port), logRequests=False)
       -        server.register_function(self.network.lnwatcher, 'add_sweep_tx')
       -
            def ping(self):
                return True
        
   DIR diff --git a/electrum/lnchan.py b/electrum/lnchan.py
       t@@ -343,7 +343,7 @@ class Channel(PrintError):
                else:
                    their_cur_pcp = self.config[REMOTE].next_per_commitment_point
                    encumbered_sweeptx = maybe_create_sweeptx_for_their_ctx_to_remote(self, ctx, their_cur_pcp, self.sweep_address)
       -        self.lnwatcher.add_sweep_tx(outpoint, ctx.txid(), encumbered_sweeptx)
       +        self.lnwatcher.add_sweep_tx(outpoint, ctx.txid(), encumbered_sweeptx.to_json())
        
            def process_new_revocation_secret(self, per_commitment_secret: bytes):
                if not self.lnwatcher:
       t@@ -351,7 +351,7 @@ class Channel(PrintError):
                outpoint = self.funding_outpoint.to_str()
                ctx = self.remote_commitment_to_be_revoked
                encumbered_sweeptx = maybe_create_sweeptx_for_their_ctx_to_local(self, ctx, per_commitment_secret, self.sweep_address)
       -        self.lnwatcher.add_sweep_tx(outpoint, ctx.txid(), encumbered_sweeptx)
       +        self.lnwatcher.add_sweep_tx(outpoint, ctx.txid(), encumbered_sweeptx.to_json())
        
            def receive_revocation(self, revocation):
                """
   DIR diff --git a/electrum/lnwatcher.py b/electrum/lnwatcher.py
       t@@ -63,7 +63,8 @@ class LNWatcher(PrintError):
                    name, args, kwargs = await self.watchtower_queue.get()
                    func = getattr(self.watchtower, name)
                    try:
       -                func(*args, **kwargs)
       +                r = func(*args, **kwargs)
       +                self.print_error("watchtower answer", r)
                    except:
                        self.print_error('could not reach watchtower, will retry in 5s', name, args)
                        await asyncio.sleep(5)
       t@@ -179,7 +180,8 @@ class LNWatcher(PrintError):
                return keep_watching_this
        
            @with_watchtower
       -    def add_sweep_tx(self, funding_outpoint: str, ctx_txid: str, encumbered_sweeptx: EncumberedTransaction):
       +    def add_sweep_tx(self, funding_outpoint: str, ctx_txid: str, sweeptx):
       +        encumbered_sweeptx = EncumberedTransaction.from_json(sweeptx)
                if encumbered_sweeptx is None:
                    return
                with self.lock: