electrum

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

commit 4ed8787433ddfffc1bc358f652e3373927cd4898
parent 8f2a730b3b39f2ba663c320cdb10025678a17c4c
Author: ThomasV <thomasv@electrum.org>
Date:   Tue,  5 Feb 2019 20:33:50 +0100

remove 'util.py' from scripts

Diffstat:
Melectrum/network.py | 30++++++++++++++++++++++++++++++
Melectrum/scripts/estimate_fee.py | 6++----
Melectrum/scripts/peers.py | 9+++------
Melectrum/scripts/servers.py | 4+---
Melectrum/scripts/txradar.py | 6++----
Delectrum/scripts/util.py | 47-----------------------------------------------
6 files changed, 38 insertions(+), 64 deletions(-)

diff --git a/electrum/network.py b/electrum/network.py @@ -1134,3 +1134,33 @@ class Network(PrintError): assert network._loop_thread is not threading.currentThread() coro = asyncio.run_coroutine_threadsafe(network._send_http_on_proxy(method, url, **kwargs), network.asyncio_loop) return coro.result(5) + + + + # methods used in scripts + async def get_peers(self): + while not self.is_connected(): + await asyncio.sleep(1) + session = self.interface.session + return parse_servers(await session.send_request('server.peers.subscribe')) + + async def send_multiple_requests(self, servers: List[str], method: str, params: Sequence): + num_connecting = len(self.connecting) + for server in servers: + self._start_interface(server) + # sleep a bit + for _ in range(10): + if len(self.connecting) < num_connecting: + break + await asyncio.sleep(1) + responses = dict() + async def get_response(iface: Interface): + try: + res = await iface.session.send_request(method, params, timeout=10) + except Exception as e: + res = e + responses[iface.server] = res + async with TaskGroup() as group: + for interface in self.interfaces.values(): + await group.spawn(get_response(interface)) + return responses diff --git a/electrum/scripts/estimate_fee.py b/electrum/scripts/estimate_fee.py @@ -7,8 +7,6 @@ from numbers import Number from electrum.network import filter_protocol, Network from electrum.util import create_and_start_event_loop, log_exceptions -import util - loop, stopping_fut, loop_thread = create_and_start_event_loop() network = Network() @@ -17,9 +15,9 @@ network.start() @log_exceptions async def f(): try: - peers = await util.get_peers(network) + peers = await network.get_peers() peers = filter_protocol(peers) - results = await util.send_request(network, peers, 'blockchain.estimatefee', [2]) + results = await network.send_multiple_requests(peers, 'blockchain.estimatefee', [2]) print(json.dumps(results, indent=4)) feerate_estimates = filter(lambda x: isinstance(x, Number), results.values()) print(f"median feerate: {median(feerate_estimates)}") diff --git a/electrum/scripts/peers.py b/electrum/scripts/peers.py @@ -5,9 +5,6 @@ from electrum.network import filter_protocol, Network from electrum.util import create_and_start_event_loop, log_exceptions from electrum.blockchain import hash_raw_header -import util - - loop, stopping_fut, loop_thread = create_and_start_event_loop() network = Network() network.start() @@ -15,13 +12,13 @@ network.start() @log_exceptions async def f(): try: - peers = await util.get_peers(network) + peers = await network.get_peers() peers = filter_protocol(peers, 's') - results = await util.send_request(network, peers, 'blockchain.headers.subscribe', []) + results = await network.send_multiple_requests(peers, 'blockchain.headers.subscribe', []) for server, header in sorted(results.items(), key=lambda x: x[1].get('height')): height = header.get('height') blockhash = hash_raw_header(header.get('hex')) - print("%60s" % server, height, blockhash) + print(server, height, blockhash) finally: stopping_fut.set_result(1) diff --git a/electrum/scripts/servers.py b/electrum/scripts/servers.py @@ -7,8 +7,6 @@ from electrum.network import filter_version, Network from electrum.util import create_and_start_event_loop, log_exceptions from electrum import constants -import util - # testnet? #constants.set_testnet() config = SimpleConfig({'testnet': False}) @@ -20,7 +18,7 @@ network.start() @log_exceptions async def f(): try: - peers = await util.get_peers(network) + peers = await network.get_peers() peers = filter_version(peers) print(json.dumps(peers, sort_keys=True, indent=4)) finally: diff --git a/electrum/scripts/txradar.py b/electrum/scripts/txradar.py @@ -5,8 +5,6 @@ import asyncio from electrum.network import filter_protocol, Network from electrum.util import create_and_start_event_loop, log_exceptions -import util - try: txid = sys.argv[1] @@ -22,9 +20,9 @@ network.start() @log_exceptions async def f(): try: - peers = await util.get_peers(network) + peers = await network.get_peers() peers = filter_protocol(peers, 's') - results = await util.send_request(network, peers, 'blockchain.transaction.get', [txid]) + results = await network.send_multiple_requests(peers, 'blockchain.transaction.get', [txid]) r1, r2 = [], [] for k, v in results.items(): (r1 if not isinstance(v, Exception) else r2).append(k) diff --git a/electrum/scripts/util.py b/electrum/scripts/util.py @@ -1,47 +0,0 @@ -import asyncio -from typing import List, Sequence - -from aiorpcx import TaskGroup - -from electrum.network import parse_servers, Network -from electrum.interface import Interface - - -#electrum.util.set_verbosity(True) - -async def get_peers(network: Network): - while not network.is_connected(): - await asyncio.sleep(1) - print("waiting for network to get connected...") - interface = network.interface - session = interface.session - print(f"asking server {interface.server} for its peers") - peers = parse_servers(await session.send_request('server.peers.subscribe')) - print(f"got {len(peers)} servers") - return peers - - -async def send_request(network: Network, servers: List[str], method: str, params: Sequence): - print(f"contacting {len(servers)} servers") - num_connecting = len(network.connecting) - for server in servers: - network._start_interface(server) - # sleep a bit - for _ in range(10): - if len(network.connecting) < num_connecting: - break - await asyncio.sleep(1) - print(f"connected to {len(network.interfaces)} servers. sending request to all.") - responses = dict() - async def get_response(iface: Interface): - try: - res = await iface.session.send_request(method, params, timeout=10) - except Exception as e: - print(f"server {iface.server} errored or timed out: ({repr(e)})") - res = e - responses[iface.server] = res - async with TaskGroup() as group: - for interface in network.interfaces.values(): - await group.spawn(get_response(interface)) - print("%d answers" % len(responses)) - return responses