Skip to content

Commit

Permalink
retry logic fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
crosschainer committed Apr 14, 2024
1 parent 71437c0 commit d46e51c
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/xian/query_eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def retry_logic(self, func, *args, retries=3, delay=1):
last_exception = None
for attempt in range(retries):
try:
return func(*args)
result = func(*args) # Attempt to call the function
return result, None # Return the result and None for exception if successful
except exceptions.ContractLogicError as e:
last_exception = e
print(f"Contract logic error on attempt {attempt+1}: {e}")
Expand All @@ -23,40 +24,40 @@ def retry_logic(self, func, *args, retries=3, delay=1):
except Exception as e:
last_exception = e
print(f"Unexpected error on attempt {attempt+1}: {e}")
time.sleep(delay)
return None, last_exception # Returns None and the last exception on failure
time.sleep(delay * (2 ** attempt)) # Exponential back-off
return None, last_exception # Only return None and the last exception after all retries fail

def call_contract(self, abi, contract_address, data):
func = lambda: self.web3.eth.contract(address=contract_address, abi=abi).functions[data].call()
result, exception = self.retry_logic(func)
if exception:
return f"Error after final attempt: {exception}"
return None # After all retries fail, return None
return result

def get_block(self, block_number):
func = lambda: self.web3.eth.get_block(block_number, full_transactions=True)
result, exception = self.retry_logic(func)
if exception:
return f"Error after final attempt: {exception}"
return None # After all retries fail, return None
return result

def get_block_by_hash(self, block_hash):
func = lambda: self.web3.eth.get_block(block_hash, full_transactions=True)
result, exception = self.retry_logic(func)
if exception:
return f"Error after final attempt: {exception}"
return None # After all retries fail, return None
return result

def get_transaction(self, tx_hash):
func = lambda: self.web3.eth.get_transaction(tx_hash)
result, exception = self.retry_logic(func)
if exception:
return f"Error after final attempt: {exception}"
return None # After all retries fail, return None
return result

def get_receipt(self, tx_hash):
func = lambda: self.web3.eth.get_transaction_receipt(tx_hash)
result, exception = self.retry_logic(func)
if exception:
return f"Error after final attempt: {exception}"
return None # After all retries fail, return None
return result

0 comments on commit d46e51c

Please sign in to comment.