|
@ -32,7 +32,7 @@ import traceback |
|
|
import aiorpcx |
|
|
import aiorpcx |
|
|
import asyncio |
|
|
import asyncio |
|
|
import concurrent.futures |
|
|
import concurrent.futures |
|
|
from aiorpcx import ClientSession, Notification |
|
|
from aiorpcx import ClientSession, Notification, TaskGroup |
|
|
|
|
|
|
|
|
import requests |
|
|
import requests |
|
|
|
|
|
|
|
@ -82,11 +82,15 @@ class Interface(PrintError): |
|
|
self.port = int(self.port) |
|
|
self.port = int(self.port) |
|
|
self.config_path = config_path |
|
|
self.config_path = config_path |
|
|
self.cert_path = os.path.join(self.config_path, 'certs', self.host) |
|
|
self.cert_path = os.path.join(self.config_path, 'certs', self.host) |
|
|
self.fut = asyncio.get_event_loop().create_task(self.run()) |
|
|
|
|
|
self.tip_header = None |
|
|
self.tip_header = None |
|
|
self.tip = 0 |
|
|
self.tip = 0 |
|
|
self.blockchain = None |
|
|
self.blockchain = None |
|
|
self.network = network |
|
|
self.network = network |
|
|
|
|
|
|
|
|
|
|
|
# TODO combine? |
|
|
|
|
|
self.fut = asyncio.get_event_loop().create_task(self.run()) |
|
|
|
|
|
self.group = TaskGroup() |
|
|
|
|
|
|
|
|
if proxy: |
|
|
if proxy: |
|
|
username, pw = proxy.get('user'), proxy.get('password') |
|
|
username, pw = proxy.get('user'), proxy.get('password') |
|
|
if not username or not pw: |
|
|
if not username or not pw: |
|
@ -231,18 +235,24 @@ class Interface(PrintError): |
|
|
self.tip = subscription_res['height'] |
|
|
self.tip = subscription_res['height'] |
|
|
self.mark_ready() |
|
|
self.mark_ready() |
|
|
copy_header_queue = asyncio.Queue() |
|
|
copy_header_queue = asyncio.Queue() |
|
|
block_retriever = asyncio.get_event_loop().create_task(self.run_fetch_blocks(subscription_res, copy_header_queue)) |
|
|
async with self.group as group: |
|
|
while True: |
|
|
await group.spawn(self.run_fetch_blocks(subscription_res, copy_header_queue)) |
|
|
try: |
|
|
await group.spawn(self.subscribe_to_headers(header_queue, copy_header_queue)) |
|
|
new_header = await asyncio.wait_for(header_queue.get(), 300) |
|
|
# NOTE: group.__aexit__ will be called here; this is needed to notice exceptions in the group! |
|
|
self.tip_header = new_header |
|
|
|
|
|
self.tip = new_header['block_height'] |
|
|
async def subscribe_to_headers(self, header_queue, copy_header_queue): |
|
|
await copy_header_queue.put(new_header) |
|
|
while True: |
|
|
except concurrent.futures.TimeoutError: |
|
|
try: |
|
|
await asyncio.wait_for(session.send_request('server.ping'), 5) |
|
|
new_header = await asyncio.wait_for(header_queue.get(), 300) |
|
|
|
|
|
self.tip_header = new_header |
|
|
|
|
|
self.tip = new_header['block_height'] |
|
|
|
|
|
await copy_header_queue.put(new_header) |
|
|
|
|
|
except concurrent.futures.TimeoutError: |
|
|
|
|
|
await asyncio.wait_for(self.session.send_request('server.ping'), 5) |
|
|
|
|
|
|
|
|
def close(self): |
|
|
def close(self): |
|
|
self.fut.cancel() |
|
|
self.fut.cancel() |
|
|
|
|
|
asyncio.get_event_loop().create_task(self.group.cancel_remaining()) |
|
|
|
|
|
|
|
|
@aiosafe |
|
|
@aiosafe |
|
|
async def run_fetch_blocks(self, sub_reply, replies): |
|
|
async def run_fetch_blocks(self, sub_reply, replies): |
|
|