From 1169f7e22b1b3cff5829a84a28f4106660037f41 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 19 Jun 2023 17:48:32 +0300 Subject: [PATCH] Add threadPoolExecutor. --- moonstreamapi/moonstreamapi/actions.py | 6 +++-- .../moonstreamapi/routes/subscriptions.py | 23 +++++++++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/moonstreamapi/moonstreamapi/actions.py b/moonstreamapi/moonstreamapi/actions.py index 420976b1..e767e1f8 100644 --- a/moonstreamapi/moonstreamapi/actions.py +++ b/moonstreamapi/moonstreamapi/actions.py @@ -869,8 +869,10 @@ def check_if_smartcontract( """ web3_client = connect(blockchain_type, access_id=uuid.UUID(access_id)) + is_contract = False + code = web3_client.eth.getCode(address) if code != b"": - return True + is_contract = True - return False + return blockchain_type, address, is_contract diff --git a/moonstreamapi/moonstreamapi/routes/subscriptions.py b/moonstreamapi/moonstreamapi/routes/subscriptions.py index 95500530..854e7c2d 100644 --- a/moonstreamapi/moonstreamapi/routes/subscriptions.py +++ b/moonstreamapi/moonstreamapi/routes/subscriptions.py @@ -1,6 +1,7 @@ """ The Moonstream subscriptions HTTP API """ +from concurrent.futures import as_completed, ProcessPoolExecutor, ThreadPoolExecutor import hashlib import json import logging @@ -613,12 +614,24 @@ async def address_info(request: Request, address: str): try: # connnect to blockchain - address_is_contract = check_if_smartcontract( - address=address, blockchain_type=blockchain_type, access_id=access_id - ) + futures = [] - if address_is_contract: - contract_info[blockchain_type.value] = address_is_contract + with ThreadPoolExecutor(max_workers=5) as executor: + futures.append( + executor.submit( + check_if_smartcontract, + address=address, + blockchain_type=blockchain_type, + access_id=access_id, + ) + ) + + for future in as_completed(futures): + + blockchain_type, address, is_contract = future.result() + + if is_contract: + contract_info[blockchain_type.value] = is_contract except Exception as e: logger.error(f"Error reading contract info from web3: {str(e)}")