Browse Source

remove 'util.py' from scripts

sqlite_db
ThomasV 6 years ago
parent
commit
4ed8787433
  1. 30
      electrum/network.py
  2. 6
      electrum/scripts/estimate_fee.py
  3. 9
      electrum/scripts/peers.py
  4. 4
      electrum/scripts/servers.py
  5. 6
      electrum/scripts/txradar.py
  6. 47
      electrum/scripts/util.py

30
electrum/network.py

@ -1134,3 +1134,33 @@ class Network(PrintError):
assert network._loop_thread is not threading.currentThread() assert network._loop_thread is not threading.currentThread()
coro = asyncio.run_coroutine_threadsafe(network._send_http_on_proxy(method, url, **kwargs), network.asyncio_loop) coro = asyncio.run_coroutine_threadsafe(network._send_http_on_proxy(method, url, **kwargs), network.asyncio_loop)
return coro.result(5) 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

6
electrum/scripts/estimate_fee.py

@ -7,8 +7,6 @@ from numbers import Number
from electrum.network import filter_protocol, Network from electrum.network import filter_protocol, Network
from electrum.util import create_and_start_event_loop, log_exceptions from electrum.util import create_and_start_event_loop, log_exceptions
import util
loop, stopping_fut, loop_thread = create_and_start_event_loop() loop, stopping_fut, loop_thread = create_and_start_event_loop()
network = Network() network = Network()
@ -17,9 +15,9 @@ network.start()
@log_exceptions @log_exceptions
async def f(): async def f():
try: try:
peers = await util.get_peers(network) peers = await network.get_peers()
peers = filter_protocol(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)) print(json.dumps(results, indent=4))
feerate_estimates = filter(lambda x: isinstance(x, Number), results.values()) feerate_estimates = filter(lambda x: isinstance(x, Number), results.values())
print(f"median feerate: {median(feerate_estimates)}") print(f"median feerate: {median(feerate_estimates)}")

9
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.util import create_and_start_event_loop, log_exceptions
from electrum.blockchain import hash_raw_header from electrum.blockchain import hash_raw_header
import util
loop, stopping_fut, loop_thread = create_and_start_event_loop() loop, stopping_fut, loop_thread = create_and_start_event_loop()
network = Network() network = Network()
network.start() network.start()
@ -15,13 +12,13 @@ network.start()
@log_exceptions @log_exceptions
async def f(): async def f():
try: try:
peers = await util.get_peers(network) peers = await network.get_peers()
peers = filter_protocol(peers, 's') 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')): for server, header in sorted(results.items(), key=lambda x: x[1].get('height')):
height = header.get('height') height = header.get('height')
blockhash = hash_raw_header(header.get('hex')) blockhash = hash_raw_header(header.get('hex'))
print("%60s" % server, height, blockhash) print(server, height, blockhash)
finally: finally:
stopping_fut.set_result(1) stopping_fut.set_result(1)

4
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.util import create_and_start_event_loop, log_exceptions
from electrum import constants from electrum import constants
import util
# testnet? # testnet?
#constants.set_testnet() #constants.set_testnet()
config = SimpleConfig({'testnet': False}) config = SimpleConfig({'testnet': False})
@ -20,7 +18,7 @@ network.start()
@log_exceptions @log_exceptions
async def f(): async def f():
try: try:
peers = await util.get_peers(network) peers = await network.get_peers()
peers = filter_version(peers) peers = filter_version(peers)
print(json.dumps(peers, sort_keys=True, indent=4)) print(json.dumps(peers, sort_keys=True, indent=4))
finally: finally:

6
electrum/scripts/txradar.py

@ -5,8 +5,6 @@ import asyncio
from electrum.network import filter_protocol, Network from electrum.network import filter_protocol, Network
from electrum.util import create_and_start_event_loop, log_exceptions from electrum.util import create_and_start_event_loop, log_exceptions
import util
try: try:
txid = sys.argv[1] txid = sys.argv[1]
@ -22,9 +20,9 @@ network.start()
@log_exceptions @log_exceptions
async def f(): async def f():
try: try:
peers = await util.get_peers(network) peers = await network.get_peers()
peers = filter_protocol(peers, 's') 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 = [], [] r1, r2 = [], []
for k, v in results.items(): for k, v in results.items():
(r1 if not isinstance(v, Exception) else r2).append(k) (r1 if not isinstance(v, Exception) else r2).append(k)

47
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
Loading…
Cancel
Save