From bbfc970c0e1d574d23a42d46fe8944fd8c6d7a75 Mon Sep 17 00:00:00 2001 From: Dheeraj Manjunath Date: Thu, 6 Jan 2022 12:12:30 -0500 Subject: [PATCH] Revert "Retry getting blocks if no transactions (#2208)" (#2231) This reverts commit 8177e77d0fc140823806e2cfe6ac1111f5c7f7b8. --- discovery-provider/src/tasks/index.py | 67 +++++++++------------------ 1 file changed, 22 insertions(+), 45 deletions(-) diff --git a/discovery-provider/src/tasks/index.py b/discovery-provider/src/tasks/index.py index 058678cbdf1..3494e09c481 100644 --- a/discovery-provider/src/tasks/index.py +++ b/discovery-provider/src/tasks/index.py @@ -1,7 +1,6 @@ # pylint: disable=C0302 import concurrent.futures import logging -import time from sqlalchemy import func from src.app import get_contract_addresses @@ -126,59 +125,36 @@ def initialize_blocks_table_if_necessary(db: SessionManager): return target_blockhash -def get_latest_block(db: SessionManager, is_retry: bool = False): +def get_latest_block(db: SessionManager): + latest_block = None block_processing_window = int( update_task.shared_config["discprov"]["block_processing_window"] ) - try: - with db.scoped_session() as session: - current_block_query = session.query(Block).filter_by(is_current=True) - assert ( - current_block_query.count() == 1 - ), "Expected SINGLE row marked as current" - - current_block_query_results = current_block_query.all() - current_block = current_block_query_results[0] - current_block_number = current_block.number - - if current_block_number == None: - current_block_number = 0 + with db.scoped_session() as session: + current_block_query = session.query(Block).filter_by(is_current=True) + assert current_block_query.count() == 1, "Expected SINGLE row marked as current" - target_latest_block_number = current_block_number + block_processing_window + current_block_query_results = current_block_query.all() + current_block = current_block_query_results[0] + current_block_number = current_block.number - latest_block_from_chain = update_task.web3.eth.getBlock("latest", True) - latest_block_number_from_chain = latest_block_from_chain.number + if current_block_number == None: + current_block_number = 0 - target_latest_block_number = min( - target_latest_block_number, latest_block_number_from_chain - ) - - logger.info( - f"index.py | get_latest_block | current={current_block_number} target={target_latest_block_number}" - ) - latest_block = update_task.web3.eth.getBlock( - target_latest_block_number, True - ) + target_latest_block_number = current_block_number + block_processing_window - # we've seen potential instances of blocks returning no transactions - # if the block had no transactions and this is the first call to the gatewway - # retry after small delay to confirm the block is actually empty - if len(latest_block.transactions) == 0 and not is_retry: - logger.info( - f"index.py | get_latest_block | target={target_latest_block_number} | target block has 0 transactions, retrying to confirm" - ) - time.sleep(0.5) - return get_latest_block(db, True) + latest_block_from_chain = update_task.web3.eth.getBlock("latest", True) + latest_block_number_from_chain = latest_block_from_chain.number - # if it retries getting the block and this time it has transactions when it didn't previously - if len(latest_block.transactions) > 0 and is_retry: - logger.info( - f"index.py | get_latest_block | target={target_latest_block_number} | target block got transactions after retrying, got 0 initially" - ) + target_latest_block_number = min( + target_latest_block_number, latest_block_number_from_chain + ) - return latest_block - except Exception as e: - raise Exception(f"index.py | get_latest_block | got exception {e}") + logger.info( + f"index.py | get_latest_block | current={current_block_number} target={target_latest_block_number}" + ) + latest_block = update_task.web3.eth.getBlock(target_latest_block_number, True) + return latest_block def update_latest_block_redis(): @@ -420,6 +396,7 @@ def index_blocks(self, db, blocks_list): playlist_factory_txs = [] user_library_factory_txs = [] user_replica_set_manager_txs = [] + tx_receipt_dict = fetch_tx_receipts(self, block.transactions) # Sort transactions by hash