From a32cb7784f55e0ef4abbc009a5116056063103b5 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Tue, 9 Jun 2020 17:50:06 +0200 Subject: [PATCH] myAiohttpClient: add id counter, and rename to JsonRPCClient --- electrum/daemon.py | 2 +- electrum/lnworker.py | 4 ++-- electrum/util.py | 9 ++++++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/electrum/daemon.py b/electrum/daemon.py index e2a227476..821a09864 100644 --- a/electrum/daemon.py +++ b/electrum/daemon.py @@ -104,7 +104,7 @@ def request(config: SimpleConfig, endpoint, args=(), timeout=60): loop = asyncio.get_event_loop() async def request_coroutine(): async with aiohttp.ClientSession(auth=auth) as session: - c = util.myAiohttpClient(session, server_url) + c = util.JsonRPCClient(session, server_url) return await c.request(endpoint, *args) try: fut = asyncio.run_coroutine_threadsafe(request_coroutine(), loop) diff --git a/electrum/lnworker.py b/electrum/lnworker.py index d7e718914..02cd671ce 100644 --- a/electrum/lnworker.py +++ b/electrum/lnworker.py @@ -26,7 +26,7 @@ from . import constants, util from . import keystore from .util import profiler from .invoices import PR_TYPE_LN, PR_UNPAID, PR_EXPIRED, PR_PAID, PR_INFLIGHT, PR_FAILED, PR_ROUTING, LNInvoice, LN_EXPIRY_NEVER -from .util import NetworkRetryManager, myAiohttpClient +from .util import NetworkRetryManager, JsonRPCClient from .lnutil import LN_MAX_FUNDING_SAT from .keystore import BIP32_KeyStore from .bitcoin import COIN @@ -533,7 +533,7 @@ class LNWallet(LNWorker): continue try: async with make_aiohttp_session(proxy=self.network.proxy) as session: - watchtower = myAiohttpClient(session, watchtower_url) + watchtower = JsonRPCClient(session, watchtower_url) watchtower.add_method('get_ctn') watchtower.add_method('add_sweep_tx') for chan in self.channels.values(): diff --git a/electrum/util.py b/electrum/util.py index b8401543b..36296ff3c 100644 --- a/electrum/util.py +++ b/electrum/util.py @@ -1370,14 +1370,17 @@ class MySocksProxy(aiorpcx.SOCKSProxy): return ret -class myAiohttpClient: +class JsonRPCClient: - def __init__(self, session, url): + def __init__(self, session: aiohttp.ClientSession, url: str): self.session = session self.url = url + self._id = 0 async def request(self, endpoint, *args): - data = '{"jsonrpc": "2.0", "id":"0", "method":"%s", "params": %s }' %(endpoint, json.dumps(args)) + self._id += 1 + data = ('{"jsonrpc": "2.0", "id":"%d", "method": "%s", "params": %s }' + % (self._id, endpoint, json.dumps(args))) async with self.session.post(self.url, data=data) as resp: if resp.status == 200: r = await resp.json()