Skip to content

Commit

Permalink
pyth2wormhole: share pending attestation information over HTTP
Browse files Browse the repository at this point in the history
Change-Id: I585392f995f3d32b5e8152c08c79a012091c36ed
  • Loading branch information
Stan Drozd authored and Stanislaw Drozd committed Nov 15, 2021
1 parent a8465ef commit 19c475e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
54 changes: 54 additions & 0 deletions third_party/pyth/p2w_autoattest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
from pyth_utils import *

from http.client import HTTPConnection
from http.server import HTTPServer, BaseHTTPRequestHandler

import json
import os
import re
import subprocess
import time
import threading
Expand All @@ -16,12 +18,46 @@
P2W_ATTEST_INTERVAL = float(os.environ.get("P2W_ATTEST_INTERVAL", 5))
P2W_OWNER_KEYPAIR = os.environ.get(
"P2W_OWNER_KEYPAIR", f"/usr/src/solana/keys/p2w_owner.json")
P2W_ATTESTATIONS_PORT = int(os.environ.get("P2W_ATTESTATIONS_PORT", 4343))

PYTH_ACCOUNTS_HOST = "pyth"
PYTH_ACCOUNTS_PORT = 4242

WORMHOLE_ADDRESS = "Bridge1p5gheXUvJ6jGWGeCsgPKgnE3YgdGKRVCMY9o"

ATTESTATIONS = {
"pendingSeqnos": [],
}

class P2WAutoattestStatusEndpoint(BaseHTTPRequestHandler):
"""
A dumb endpoint for last attested price metadata.
"""

def do_GET(self):
print(f"Got path {self.path}")
sys.stdout.flush()
data = json.dumps(ATTESTATIONS).encode("utf-8")
print(f"Sending:\n{data}")

ATTESTATIONS["pendingSeqnos"] = []

self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
self.wfile.flush()

def serve_attestations():
"""
Run a barebones HTTP server to share Pyth2wormhole attestation history
"""
server_address = ('', P2W_ATTESTATIONS_PORT)
httpd = HTTPServer(server_address, P2WAutoattestStatusEndpoint)
httpd.serve_forever()


# Get actor pubkeys
P2W_OWNER_ADDRESS = sol_run_or_die(
"address", ["--keypair", P2W_OWNER_KEYPAIR], capture_output=True).stdout.strip()
Expand Down Expand Up @@ -99,10 +135,16 @@
print(f"ACCOUNTS: {pyth_accounts}")
print(f"Attest Interval: {P2W_ATTEST_INTERVAL}")

# Serve p2w endpoint
endpoint_thread = threading.Thread(target=serve_attestations, daemon=True)
endpoint_thread.start()

# Let k8s know the service is up
readiness_thread = threading.Thread(target=readiness, daemon=True)
readiness_thread.start()

seqno_regex = re.compile(r"^Sequence number: (\d+)")

nonce = 1
while True:
attest_result = run_or_die([
Expand All @@ -117,6 +159,18 @@
"--nonce", str(nonce),
], capture_output=True)
time.sleep(P2W_ATTEST_INTERVAL)

matches = seqno_regex.match(attest_result.stdout)

if matches is not None:
seqno = int(matches.group(1))
print(f"Got seqno {seqno}")

ATTESTATIONS["pendingSeqnos"].append(seqno)

else:
print(f"Warning: Could not get sequence number")

nonce += 1

readiness_thread.join()
6 changes: 3 additions & 3 deletions third_party/pyth/pyth_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import time


class P2WAccEndpoint(BaseHTTPRequestHandler):
class PythAccEndpoint(BaseHTTPRequestHandler):
"""
A dumb endpoint to respond with a JSON containing Pyth account addresses
"""
Expand All @@ -38,7 +38,7 @@ def publisher_random_update(price_pubkey):
Update the specified price with random values
"""
value = random.randrange(1024)
confidence = 1
confidence = 5
pyth_run_or_die("upd_price_val", args=[
price_pubkey, str(value), str(confidence), "trading"
])
Expand All @@ -51,7 +51,7 @@ def accounts_endpoint():
mapping/product/price account addresses
"""
server_address = ('', 4242)
httpd = HTTPServer(server_address, P2WAccEndpoint)
httpd = HTTPServer(server_address, PythAccEndpoint)
httpd.serve_forever()


Expand Down

0 comments on commit 19c475e

Please sign in to comment.