electrum

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

commit 9de6028fb56db9fc3bc39631f99c21ec9b24e7ed
parent 25c265768025e5d01aaab26a84a767726bbb6607
Author: SomberNight <somber.night@protonmail.com>
Date:   Wed, 17 Oct 2018 01:50:36 +0200

clean-up Peer init

Diffstat:
Melectrum/lnbase.py | 10++++++++--
Melectrum/lntransport.py | 3++-
Melectrum/lnworker.py | 16++++++----------
3 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/electrum/lnbase.py b/electrum/lnbase.py @@ -34,6 +34,7 @@ from .lnutil import (Outpoint, LocalConfig, ChannelConfig, get_ln_flag_pair_of_bit, privkey_to_pubkey) from .lnutil import LightningPeerConnectionClosed, HandshakeFailed from .lnrouter import NotFoundChanAnnouncementForUpdate, RouteEdge +from .lntransport import LNTransport def channel_id_from_funding_tx(funding_txid, funding_index): @@ -190,9 +191,9 @@ def gen_msg(msg_type: str, **kwargs) -> bytes: class Peer(PrintError): - def __init__(self, lnworker, peer_addr, request_initial_sync=False): + def __init__(self, lnworker, peer_addr, request_initial_sync=False, transport=None): self.initialized = asyncio.Future() - self.transport = None + self.transport = transport self.peer_addr = peer_addr self.lnworker = lnworker self.privkey = lnworker.node_keypair.privkey @@ -222,6 +223,11 @@ class Peer(PrintError): self.transport.send_bytes(gen_msg(message_name, **kwargs)) async def initialize(self): + if not self.transport: + reader, writer = await asyncio.open_connection(self.peer_addr.host, self.peer_addr.port) + transport = LNTransport(self.privkey, self.peer_addr.pubkey, reader, writer) + await transport.handshake() + self.transport = transport self.send_message("init", gflen=0, lflen=1, localfeatures=self.localfeatures) self.initialized.set_result(True) diff --git a/electrum/lntransport.py b/electrum/lntransport.py @@ -6,6 +6,7 @@ from .crypto import sha256 from .lnutil import get_ecdh, privkey_to_pubkey from .lnutil import LightningPeerConnectionClosed, HandshakeFailed from . import ecc +from .util import bh2u class HandshakeState(object): prologue = b"lightning" @@ -203,7 +204,7 @@ class LNTransport(LNTransportBase): self.writer.write(msg) rspns = await self.reader.read(2**10) if len(rspns) != 50: - raise HandshakeFailed("Lightning handshake act 1 response has bad length, are you sure this is the right pubkey? " + str(bh2u(self.pubkey))) + raise HandshakeFailed(f"Lightning handshake act 1 response has bad length, are you sure this is the right pubkey? {bh2u(self.remote_pubkey)}") hver, alice_epub, tag = rspns[0], rspns[1:34], rspns[34:] if bytes([hver]) != hs.handshake_version: raise HandshakeFailed("unexpected handshake version: {}".format(hver)) diff --git a/electrum/lnworker.py b/electrum/lnworker.py @@ -16,7 +16,7 @@ from . import bitcoin from .keystore import BIP32_KeyStore from .bitcoin import sha256, COIN from .util import bh2u, bfh, PrintError, InvoiceError, resolve_dns_srv, is_ip_address, log_exceptions -from .lntransport import LNTransport, LNResponderTransport +from .lntransport import LNResponderTransport from .lnbase import Peer from .lnaddr import lnencode, LnAddr, lndecode from .ecc import der_sig_from_sig_string @@ -116,13 +116,7 @@ class LNWorker(PrintError): self._last_tried_peer[peer_addr] = time.time() self.print_error("adding peer", peer_addr) peer = Peer(self, peer_addr, request_initial_sync=self.config.get("request_initial_sync", True)) - async def _init_peer(): - reader, writer = await asyncio.open_connection(peer_addr.host, peer_addr.port) - transport = LNTransport(self.node_keypair.privkey, node_id, reader, writer) - await transport.handshake() - peer.transport = transport - await self.network.main_taskgroup.spawn(peer.main_loop()) - asyncio.ensure_future(_init_peer()) + await self.network.main_taskgroup.spawn(peer.main_loop()) self.peers[node_id] = peer self.network.trigger_callback('ln_status') return peer @@ -525,8 +519,10 @@ class LNWorker(PrintError): async def cb(reader, writer): t = LNResponderTransport(self.node_keypair.privkey, reader, writer) node_id = await t.handshake() - peer = Peer(self, LNPeerAddr("bogus", 1337, node_id), request_initial_sync=self.config.get("request_initial_sync", True)) - peer.transport = t + # FIXME extract host and port from transport + peer = Peer(self, LNPeerAddr("bogus", 1337, node_id), + request_initial_sync=self.config.get("request_initial_sync", True), + transport=t) self.peers[node_id] = peer await self.network.main_taskgroup.spawn(peer.main_loop()) self.network.trigger_callback('ln_status')