Skip to content

Commit 4c4d828

Browse files
authored
Merge pull request #6 from tryiou/main
Various small improvments to plugin_block_heights() , plugin_tx_fees() and logging.
2 parents f7573fa + 760bc10 commit 4c4d828

2 files changed

Lines changed: 42 additions & 16 deletions

File tree

plugin-adapter.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
logging.basicConfig(
23-
level=logging.DEBUG,
23+
level=logging.INFO,
2424
format="%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s",
2525
handlers=[
2626
logging.FileHandler("debug.log"),
@@ -80,6 +80,9 @@ async def connect(self):
8080
try:
8181
self.session = await connect_rs(self.host, self.port).__aenter__()
8282
self.session.transport._framer.max_size = 0
83+
except OSError as e:
84+
logger.error(f"[client] ERROR: Connection error to {self.host}:{self.port} - {e.strerror}")
85+
self.session = None
8386
except Exception as e:
8487
logger.error("[client] ERROR: Error connecting!", e)
8588
self.session = None
@@ -349,7 +352,7 @@ async def getblockcount(params):
349352
socket = coins[currency]['socket']
350353
res = {'result': None, 'error': None}
351354

352-
data = await socket.send_message("getblockcount", (), timeout=30)
355+
data = await socket.send_message("getblockcount", (), timeout=2)
353356

354357
if data == OS_ERROR or data == OTHER_EXCEPTION:
355358
logger.error("[server] ERROR: Error during getblockcount grabbing!")
@@ -520,27 +523,48 @@ async def ping():
520523

521524
async def plugin_block_heights():
522525
heights = {}
523-
for coin in coins:
524-
logger.info("[server] getting block_count for coin: " + coin)
525-
data = await get_block_count(coin)
526+
start_time = time.time()
527+
528+
# Create a list of coroutines for each coin
529+
coroutines = [get_block_count(coin) for coin in coins]
530+
531+
# Execute the coroutines concurrently
532+
results = await asyncio.gather(*coroutines)
533+
534+
for i, coin in enumerate(coins):
535+
data = results[i]
536+
537+
#logger.info("[server] getting block_count for coin: " + coin)
526538

527539
if data is None:
528540
heights[coin] = None
529541
continue
530542

531-
logger.info("[server] finished block_count, block# " + str(data))
543+
#logger.info("[server] finished block_count, block# " + str(data))
532544
heights[coin] = data
533-
545+
534546
res = {'result': heights, 'error': None}
535-
547+
548+
end_time = time.time()
549+
execution_time = end_time - start_time
550+
logger.info(f"[server] plugin_block_heights() {res}")
551+
#logger.info(f"[server] Execution time for 'plugin_block_heights': {execution_time} seconds")
536552
return json.dumps(res)
537553

538554

539555
async def plugin_tx_fees():
540556
fees = {}
541-
for coin in coins:
542-
logger.info("[server] getting fees for coin: " + coin)
543-
data = await get_plugin_fees(coin)
557+
558+
# Create a list of coroutines for each coin
559+
coroutines = [get_plugin_fees(coin) for coin in coins]
560+
561+
# Execute the coroutines concurrently
562+
results = await asyncio.gather(*coroutines)
563+
564+
for i, coin in enumerate(coins):
565+
data = results[i]
566+
567+
#logger.info("[server] getting fees for coin: " + coin)
544568

545569
if data is None:
546570
fees[coin] = None
@@ -549,7 +573,8 @@ async def plugin_tx_fees():
549573
fees[coin] = Decimal('{:.8f}'.format(data))
550574

551575
res = {'result': fees, 'error': None}
552-
576+
logger.info(f"[server] plugin_tx_fees() {res}")
577+
553578
return simplejson.dumps(res)
554579

555580

@@ -559,9 +584,11 @@ async def get_block_count(currency):
559584
return None
560585

561586
socket = coins[currency]['socket']
562-
587+
start_time = time.time()
563588
res = await socket.send_message("getblockcount", (), timeout=2)
564-
589+
end_time = time.time()
590+
execution_time = end_time - start_time
591+
#logger.info(f"[client] Execution time for 'get_block_count' {currency}: {execution_time} seconds")
565592
if res == OS_ERROR or res == OTHER_EXCEPTION:
566593
return None
567594

@@ -753,7 +780,6 @@ async def address_get_history(params):
753780

754781
return json.dumps(res)
755782

756-
757783
async def switchcase(requestjson):
758784
switcher = {
759785
'getutxos': getutxos(requestjson['params']),

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
requests==2.7.0
22
asyncio==3.4.3
3-
aiorpcX==0.18.4
3+
aiorpcX==0.18.7
44
aiohttp==3.6.2
55
#kubernetes==11.0.0
66
simplejson==3.17.0

0 commit comments

Comments
 (0)