p2w_autoattest.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #!/usr/bin/env python3
  2. # This script sets up a simple loop for periodical attestation of Pyth data
  3. import json
  4. import logging
  5. import os
  6. import re
  7. import sys
  8. import threading
  9. from http.client import HTTPConnection
  10. from subprocess import PIPE, STDOUT, Popen
  11. from pyth_utils import *
  12. logging.basicConfig(
  13. level=logging.DEBUG, format="%(asctime)s | %(module)s | %(levelname)s | %(message)s"
  14. )
  15. P2W_SOL_ADDRESS = os.environ.get(
  16. "P2W_SOL_ADDRESS", "P2WH424242424242424242424242424242424242424"
  17. )
  18. P2W_OWNER_KEYPAIR = os.environ.get(
  19. "P2W_OWNER_KEYPAIR", "/solana-secrets/p2w_owner.json"
  20. )
  21. P2W_ATTESTATIONS_PORT = int(os.environ.get("P2W_ATTESTATIONS_PORT", 4343))
  22. P2W_INITIALIZE_SOL_CONTRACT = os.environ.get("P2W_INITIALIZE_SOL_CONTRACT", None)
  23. PYTH_TEST_ACCOUNTS_HOST = "pyth"
  24. PYTH_TEST_ACCOUNTS_PORT = 4242
  25. P2W_ATTESTATION_CFG = os.environ.get("P2W_ATTESTATION_CFG", None)
  26. WORMHOLE_ADDRESS = os.environ.get(
  27. "WORMHOLE_ADDRESS", "Bridge1p5gheXUvJ6jGWGeCsgPKgnE3YgdGKRVCMY9o"
  28. )
  29. # attester needs string, but we validate as int first
  30. P2W_RPC_TIMEOUT_SECS = str(int(os.environ.get("P2W_RPC_TIMEOUT_SECS", "20")))
  31. if P2W_INITIALIZE_SOL_CONTRACT is not None:
  32. # Get actor pubkeys
  33. P2W_OWNER_ADDRESS = sol_run_or_die(
  34. "address", ["--keypair", P2W_OWNER_KEYPAIR], capture_output=True
  35. ).stdout.strip()
  36. PYTH_OWNER_ADDRESS = sol_run_or_die(
  37. "address", ["--keypair", PYTH_PROGRAM_KEYPAIR], capture_output=True,
  38. ).stdout.strip()
  39. init_result = run_or_die(
  40. [
  41. "pwhac",
  42. "--p2w-addr",
  43. P2W_SOL_ADDRESS,
  44. "--rpc-url",
  45. SOL_RPC_URL,
  46. "--payer",
  47. SOL_PAYER_KEYPAIR,
  48. "init",
  49. "--wh-prog",
  50. WORMHOLE_ADDRESS,
  51. "--owner",
  52. P2W_OWNER_ADDRESS,
  53. "--pyth-owner",
  54. PYTH_OWNER_ADDRESS,
  55. ],
  56. capture_output=True,
  57. debug=True,
  58. die=False,
  59. )
  60. if init_result.returncode != 0:
  61. logging.error(
  62. "NOTE: pwhac init failed, retrying with set_config"
  63. )
  64. run_or_die(
  65. [
  66. "pwhac",
  67. "--p2w-addr",
  68. P2W_SOL_ADDRESS,
  69. "--rpc-url",
  70. SOL_RPC_URL,
  71. "--payer",
  72. SOL_PAYER_KEYPAIR,
  73. "set-config",
  74. "--owner",
  75. P2W_OWNER_KEYPAIR,
  76. "--new-owner",
  77. P2W_OWNER_ADDRESS,
  78. "--new-wh-prog",
  79. WORMHOLE_ADDRESS,
  80. "--new-pyth-owner",
  81. PYTH_OWNER_ADDRESS,
  82. ],
  83. capture_output=True,
  84. )
  85. # Retrieve available symbols from the test pyth publisher if not provided in envs
  86. if P2W_ATTESTATION_CFG is None:
  87. P2W_ATTESTATION_CFG = "./attestation_cfg_test.yaml"
  88. publisher_state_map = get_pyth_accounts(PYTH_TEST_ACCOUNTS_HOST, PYTH_TEST_ACCOUNTS_PORT)
  89. pyth_accounts = publisher_state_map["symbols"]
  90. logging.info(
  91. f"Retrieved {len(pyth_accounts)} Pyth accounts from endpoint: {pyth_accounts}"
  92. )
  93. mapping_addr = publisher_state_map["mapping_addr"]
  94. cfg_yaml = f"""
  95. ---
  96. mapping_addr: {mapping_addr}
  97. mapping_reload_interval_mins: 1 # Very fast for testing purposes
  98. min_rpc_interval_ms: 0 # RIP RPC
  99. max_batch_jobs: 1000 # Where we're going there's no oomkiller
  100. default_attestation_conditions:
  101. min_interval_secs: 10
  102. symbol_groups:
  103. - group_name: fast_interval_only
  104. conditions:
  105. min_interval_secs: 1
  106. symbols:
  107. """
  108. # integer-divide the symbols in ~half for two test
  109. # groups. Assumes arr[:idx] is exclusive, and arr[idx:] is
  110. # inclusive
  111. third_len = len(pyth_accounts) // 3;
  112. for thing in pyth_accounts[:third_len]:
  113. name = thing["name"]
  114. price = thing["price"]
  115. product = thing["product"]
  116. cfg_yaml += f"""
  117. - type: key
  118. name: {name}
  119. price: {price}
  120. product: {product}"""
  121. # End of fast_interval_only
  122. cfg_yaml += f"""
  123. - group_name: longer_interval_sensitive_changes
  124. conditions:
  125. min_interval_secs: 3
  126. price_changed_bps: 300
  127. symbols:
  128. """
  129. for stuff in pyth_accounts[third_len:-third_len]:
  130. name = stuff["name"]
  131. price = stuff["price"]
  132. product = stuff["product"]
  133. cfg_yaml += f"""
  134. - type: key
  135. name: {name}
  136. price: {price}
  137. product: {product}"""
  138. with open(P2W_ATTESTATION_CFG, "w") as f:
  139. f.write(cfg_yaml)
  140. f.flush()
  141. # Set helpfully chatty logging default, filtering especially annoying
  142. # modules like async HTTP requests and tokio runtime logs
  143. os.environ["RUST_LOG"] = os.environ.get("RUST_LOG", "info")
  144. # Do not exit this script if a continuous attestation stops for
  145. # whatever reason (this avoids k8s restart penalty)
  146. while True:
  147. # Start the child process in daemon mode
  148. pwhac_process = Popen(
  149. [
  150. "pwhac",
  151. "--commitment",
  152. "confirmed",
  153. "--p2w-addr",
  154. P2W_SOL_ADDRESS,
  155. "--rpc-url",
  156. SOL_RPC_URL,
  157. "--payer",
  158. SOL_PAYER_KEYPAIR,
  159. "attest",
  160. "-f",
  161. P2W_ATTESTATION_CFG,
  162. "--timeout",
  163. P2W_RPC_TIMEOUT_SECS,
  164. ]
  165. )
  166. # Wait for an unexpected process exit
  167. retcode = pwhac_process.wait()
  168. # Yell if the supposedly non-stop attestation process exits
  169. logging.warn(f"pwhac stopped unexpectedly with code {retcode}")