Skip to content

Commit b3dbc96

Browse files
author
Stanisław Drozd
authored
pyth_publisher.py: Add a new symbol every 2 minutes (#331)
This change will help us verify the correctness of the mapping crawling logic in p2w-attest.
1 parent 6407eaa commit b3dbc96

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

third_party/pyth/pyth_publisher.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import threading
1313
import time
1414

15-
PYTH_TEST_SYMBOL_COUNT = int(os.environ.get("PYTH_TEST_SYMBOL_COUNT", "9"))
16-
1715
class PythAccEndpoint(BaseHTTPRequestHandler):
1816
"""
1917
A dumb endpoint to respond with a JSON containing Pyth symbol and mapping addresses
@@ -60,6 +58,9 @@ def accounts_endpoint():
6058

6159

6260
def add_symbol(num: int):
61+
"""
62+
NOTE: Updates HTTP_ENDPOINT_DATA
63+
"""
6364
symbol_name = f"Test symbol {num}"
6465
# Add a product
6566
prod_pubkey = pyth_admin_run_or_die(
@@ -96,6 +97,8 @@ def add_symbol(num: int):
9697

9798
sys.stdout.flush()
9899

100+
print(f"New symbol: {num}")
101+
99102
return num
100103

101104
# Fund the publisher
@@ -122,14 +125,14 @@ def add_symbol(num: int):
122125
"--keypair", PYTH_PUBLISHER_KEYPAIR
123126
], capture_output=True).stdout.strip()
124127

125-
with ThreadPoolExecutor(max_workers=10) as executor:
128+
with ThreadPoolExecutor(max_workers=PYTH_TEST_SYMBOL_COUNT) as executor:
126129
add_symbol_futures = {executor.submit(add_symbol, sym_id) for sym_id in range(PYTH_TEST_SYMBOL_COUNT)}
127130

128131
for future in as_completed(add_symbol_futures):
129132
print(f"Completed {future.result()}")
130133

131134
print(
132-
f"Mock updates ready to roll. Updating every {str(PYTH_PUBLISHER_INTERVAL)} seconds")
135+
f"Mock updates ready to roll. Updating every {str(PYTH_PUBLISHER_INTERVAL_SECS)} seconds")
133136

134137
# Spin off the readiness probe endpoint into a separate thread
135138
readiness_thread = threading.Thread(target=readiness, daemon=True)
@@ -140,12 +143,26 @@ def add_symbol(num: int):
140143
readiness_thread.start()
141144
http_service.start()
142145

143-
while True:
144-
for sym in HTTP_ENDPOINT_DATA["symbols"]:
145-
publisher_random_update(sym["price"])
146+
next_new_symbol_id = PYTH_TEST_SYMBOL_COUNT
147+
last_new_sym_added_at = time.monotonic()
148+
149+
with ThreadPoolExecutor() as executor: # Used for async adding of products and prices
150+
while True:
151+
for sym in HTTP_ENDPOINT_DATA["symbols"]:
152+
publisher_random_update(sym["price"])
153+
154+
# Add a symbol if new symbol interval configured
155+
if PYTH_NEW_SYMBOL_INTERVAL_SECS > 0:
156+
# Do it if enough time passed
157+
now = time.monotonic()
158+
if (now - last_new_sym_added_at) >= PYTH_NEW_SYMBOL_INTERVAL_SECS:
159+
executor.submit(add_symbol, next_new_symbol_id) # Returns immediately, runs in background
160+
last_sym_added_at = now
161+
next_new_symbol_id += 1
162+
163+
time.sleep(PYTH_PUBLISHER_INTERVAL_SECS)
164+
sys.stdout.flush()
146165

147-
time.sleep(PYTH_PUBLISHER_INTERVAL)
148-
sys.stdout.flush()
149166

150167
readiness_thread.join()
151168
http_service.join()

third_party/pyth/pyth_utils.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,18 @@
1414
PYTH_PUBLISHER_KEYPAIR = os.environ.get(
1515
"PYTH_PUBLISHER_KEYPAIR", f"{PYTH_KEY_STORE}/publish_key_pair.json"
1616
)
17-
PYTH_PUBLISHER_INTERVAL = float(os.environ.get("PYTH_PUBLISHER_INTERVAL", "5"))
17+
# How long to sleep between mock Pyth price updates
18+
PYTH_PUBLISHER_INTERVAL_SECS = float(os.environ.get("PYTH_PUBLISHER_INTERVAL_SECS", "5"))
19+
PYTH_TEST_SYMBOL_COUNT = int(os.environ.get("PYTH_TEST_SYMBOL_COUNT", "9"))
20+
21+
# If above 0, adds a new test symbol periodically, waiting at least
22+
# the given number of seconds in between
23+
#
24+
# NOTE: the new symbols are added in the HTTP endpoint used by the
25+
# p2w-attest service in Tilt. You may need to wait to see p2w-attest
26+
# pick up brand new symbols
27+
PYTH_NEW_SYMBOL_INTERVAL_SECS = int(os.environ.get("PYTH_NEW_SYMBOL_INTERVAL_SECS", "120"))
28+
1829
PYTH_MAPPING_KEYPAIR = os.environ.get(
1930
"PYTH_MAPPING_KEYPAIR", f"{PYTH_KEY_STORE}/mapping_key_pair.json"
2031
)

0 commit comments

Comments
 (0)