Skip to content

Commit

Permalink
fix: handle timeout on request dry-runs
Browse files Browse the repository at this point in the history
  • Loading branch information
guidiaz committed Dec 27, 2021
1 parent a57e269 commit 8bf6781
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions price_feed_poller.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from config import load_config
from contract import pf_contracts, pf_thresholds, wrb
from threading import Timer
from web3 import Web3, exceptions

# Post a data request to the post_dr method of the WRB contract
Expand Down Expand Up @@ -164,9 +165,18 @@ def dry_run_request(bytecode):
cmdline = "npx witnet-toolkit try-data-request --hex "
cmdline += bytecode.hex()
cmdline += " | tail -n 2 | head -n 1 | awk -F: '{ print $2 }' | sed 's/ //g' | tr -d \"\""

process = subprocess.Popen(cmdline, stdout=subprocess.PIPE, shell=True)
process.wait()
output, error = process.communicate()
timer = Timer(15, process.kill)
try:
timer.start()
process.wait()
output, error = process.communicate()
finally:
timer.cancel()

if len(output) == 0:
raise Exception("Timeout while trying data request")
if error is not None:
raise Exception(error)
return int(output)
Expand Down Expand Up @@ -266,9 +276,10 @@ def log_loop(
# If thresholds is configured, evaluate actual price deviation
try:
next_price = dry_run_request(element['feed'].functions.bytecode().call())
except:
except Exception as ex:
# If dry run fails, assume 0 deviation as to, at least, guarantee the heartbeat periodicity is met
print("Cannot dry run request:", element['feed'].functions.bytecode().call().hex())
print("Cannot dry run request:", ex)
print(element['feed'].functions.bytecode().call().hex())
next_price = last_price
deviation = round(100 * ((next_price - last_price) / last_price), 2)
if abs(deviation) < thresholds[index] and elapsed_secs < max_secs_between_request_updates:
Expand Down

0 comments on commit 8bf6781

Please sign in to comment.