Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Retry getting blocks if no transactions (#2208)" #2231

Merged
merged 2 commits into from Jan 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 22 additions & 45 deletions 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
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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
Expand Down