electrum

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

commit 4aa9d7ea0d660d706e3fb7d29eba942773ee68a3
parent a4809245b3618fb1644a8f4f2a944b6bbfcdb43b
Author: SomberNight <somber.night@protonmail.com>
Date:   Thu, 19 Apr 2018 15:05:38 +0200

channel_establishment_flow: wait for confirmations of funding txn

Diffstat:
Mlib/lnbase.py | 27+++++++++++++++++++++++++--
Mlib/tests/test_lnbase_online.py | 3++-
2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/lib/lnbase.py b/lib/lnbase.py @@ -449,11 +449,12 @@ def make_commitment(ctn, local_funding_pubkey, remote_funding_pubkey, remotepubk class Peer(PrintError): - def __init__(self, host, port, pubkey, request_initial_sync=True): + def __init__(self, host, port, pubkey, request_initial_sync=True, network=None): self.host = host self.port = port self.privkey = os.urandom(32) + b"\x01" self.pubkey = pubkey + self.network = network self.read_buffer = b'' self.ping_time = 0 self.channel_accepted = {} @@ -706,6 +707,7 @@ class Peer(PrintError): remote_revocation_basepoint = payload['revocation_basepoint'] remote_payment_basepoint = payload['payment_basepoint'] remote_delayed_payment_basepoint = payload['delayed_payment_basepoint'] + funding_txn_minimum_depth = int.from_bytes(payload['minimum_depth'], byteorder="big") self.print_error('remote dust limit', remote_dust_limit_satoshis) self.print_error('remote delay', remote_delay) # create funding tx @@ -772,6 +774,22 @@ class Peer(PrintError): self.remote_funding_locked[channel_id] = asyncio.Future() self.network.broadcast(funding_tx) # wait until we see confirmations + + def on_network_update(event, *args): + if event == 'updated': + conf = wallet.get_tx_height(funding_txid)[1] + if conf >= funding_txn_minimum_depth: + try: + self.local_funding_locked[channel_id].set_result(1) + except (asyncio.InvalidStateError, KeyError) as e: + # FIXME race condition if updates come in quickly, set_result might be called multiple times + # or self.local_funding_locked[channel_id] might be deleted already + self.print_error('local_funding_locked.set_result error for channel {}: {}'.format(channel_id, e)) + self.network.unregister_callback(on_network_update, ['updated']) + else: + self.print_error("unexpected network message:", event, args) + self.network.register_callback(on_network_update, ['updated']) + try: await self.local_funding_locked[channel_id] finally: @@ -874,7 +892,12 @@ class ChannelDB(PrintError): def on_channel_update(self, msg_payload): short_channel_id = msg_payload['short_channel_id'] - self._id_to_channel_info[short_channel_id].on_channel_update(msg_payload) + try: + channel_info = self._id_to_channel_info[short_channel_id] + except KeyError: + pass # ignore channel update + else: + channel_info.on_channel_update(msg_payload) def remove_channel(self, short_channel_id): try: diff --git a/lib/tests/test_lnbase_online.py b/lib/tests/test_lnbase_online.py @@ -34,8 +34,9 @@ if __name__ == "__main__": # wallet storage = WalletStorage(config.get_wallet_path()) wallet = Wallet(storage) + wallet.start_threads(network) # start peer - peer = Peer(host, port, pubkey, request_initial_sync=False) + peer = Peer(host, port, pubkey, request_initial_sync=False, network=network) network.futures.append(asyncio.run_coroutine_threadsafe(peer.main_loop(), network.asyncio_loop)) # run blocking test start = time.time()