77import re
88import sys
99import threading
10- import time
1110from http .client import HTTPConnection
12- from http .server import BaseHTTPRequestHandler , HTTPServer
1311from subprocess import PIPE , STDOUT , Popen
1412
1513from pyth_utils import *
3634 "WORMHOLE_ADDRESS" , "Bridge1p5gheXUvJ6jGWGeCsgPKgnE3YgdGKRVCMY9o"
3735)
3836
39- P2W_MAX_LOG_LINES = int (os .environ .get ("P2W_MAX_LOG_LINES" , 1000 ))
40-
4137# attester needs string, but we validate as int first
4238P2W_RPC_TIMEOUT_SECS = str (int (os .environ .get ("P2W_RPC_TIMEOUT_SECS" , "20" )))
4339
44- ATTESTATIONS = {
45- "pendingSeqnos" : [],
46- }
47-
48- SEQNO_REGEX = re .compile (r"Sequence number: (\d+)" )
49-
50-
51-
52- class P2WAutoattestStatusEndpoint (BaseHTTPRequestHandler ):
53- """
54- A dumb endpoint for last attested price metadata.
55- """
56-
57- def do_GET (self ):
58- logging .info (f"Got path { self .path } " )
59- sys .stdout .flush ()
60- data = json .dumps (ATTESTATIONS ).encode ("utf-8" )
61- logging .debug (f"Sending: { data } " )
62-
63- ATTESTATIONS ["pendingSeqnos" ] = []
64-
65- self .send_response (200 )
66- self .send_header ("Content-Type" , "application/json" )
67- self .send_header ("Content-Length" , str (len (data )))
68- self .end_headers ()
69- self .wfile .write (data )
70- self .wfile .flush ()
71-
72-
73- def serve_attestations ():
74- """
75- Run a barebones HTTP server to share Pyth2wormhole attestation history
76- """
77- server_address = ("" , P2W_ATTESTATIONS_PORT )
78- httpd = HTTPServer (server_address , P2WAutoattestStatusEndpoint )
79- httpd .serve_forever ()
80-
81-
8240if SOL_AIRDROP_AMT > 0 :
8341 # Fund the p2w owner
8442 sol_run_or_die (
@@ -92,17 +50,6 @@ def serve_attestations():
9250 ],
9351 )
9452
95- def find_and_log_seqnos (s ):
96- # parse seqnos
97- matches = SEQNO_REGEX .findall (s )
98-
99- seqnos = list (map (lambda m : int (m ), matches ))
100-
101- ATTESTATIONS ["pendingSeqnos" ] += seqnos
102-
103- if len (seqnos ) > 0 :
104- logging .info (f"{ len (seqnos )} batch seqno(s) received: { seqnos } )" )
105-
10653if P2W_INITIALIZE_SOL_CONTRACT is not None :
10754 # Get actor pubkeys
10855 P2W_OWNER_ADDRESS = sol_run_or_die (
@@ -272,12 +219,6 @@ def find_and_log_seqnos(s):
272219
273220logging .info ("p2w_autoattest ready to roll!" )
274221
275- find_and_log_seqnos (first_attest_result .stdout )
276-
277- # Serve p2w endpoint
278- endpoint_thread = threading .Thread (target = serve_attestations , daemon = True )
279- endpoint_thread .start ()
280-
281222# Let k8s know the service is up
282223readiness_thread = threading .Thread (target = readiness , daemon = True )
283224readiness_thread .start ()
@@ -303,31 +244,12 @@ def find_and_log_seqnos(s):
303244 "-d" ,
304245 "--timeout" ,
305246 P2W_RPC_TIMEOUT_SECS ,
306- ],
307- stdout = PIPE ,
308- stderr = STDOUT ,
309- text = True ,
310- )
311-
312- saved_log_lines = []
247+ ]
248+ )
313249
314- # Keep listening for seqnos until the program exits
250+ # Wait for an unexpected process exit
315251 while p2w_client_process .poll () is None :
316- line = p2w_client_process .stdout .readline ()
317-
318- # Always pass output to the debug level
319- logging .debug (f"pyth2wormhole-client: { line } " )
320-
321- find_and_log_seqnos (line )
322-
323- # Extend with new line
324- saved_log_lines .append (line )
325-
326- # trim back to specified maximum
327- if len (saved_log_lines ) > P2W_MAX_LOG_LINES :
328- saved_log_lines .pop (0 )
329-
252+ pass
330253
331254 # Yell if the supposedly non-stop attestation process exits
332255 logging .warn (f"pyth2wormhole-client stopped unexpectedly with code { p2w_client_process .retcode } " )
333- logging .warn (f"Last { len (saved_log_lines )} log lines:\n { (saved_log_lines )} " )
0 commit comments