p2w_autoattest.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_ms: 10000
  102. symbol_groups:
  103. - group_name: fast_interval_rate_limited
  104. conditions:
  105. min_interval_ms: 1000
  106. rate_limit_interval_secs: 2
  107. symbols:
  108. """
  109. # integer-divide the symbols in ~half for two test
  110. # groups. Assumes arr[:idx] is exclusive, and arr[idx:] is
  111. # inclusive
  112. third_len = len(pyth_accounts) // 3;
  113. for thing in pyth_accounts[:third_len]:
  114. name = thing["name"]
  115. price = thing["price"]
  116. product = thing["product"]
  117. cfg_yaml += f"""
  118. - type: key
  119. name: {name}
  120. price: {price}
  121. product: {product}"""
  122. # End of fast_interval_only
  123. cfg_yaml += f"""
  124. - group_name: longer_interval_sensitive_changes
  125. conditions:
  126. min_interval_ms: 3000
  127. price_changed_bps: 300
  128. symbols:
  129. """
  130. for stuff in pyth_accounts[third_len:-third_len]:
  131. name = stuff["name"]
  132. price = stuff["price"]
  133. product = stuff["product"]
  134. cfg_yaml += f"""
  135. - type: key
  136. name: {name}
  137. price: {price}
  138. product: {product}"""
  139. with open(P2W_ATTESTATION_CFG, "w") as f:
  140. f.write(cfg_yaml)
  141. f.flush()
  142. # Set helpfully chatty logging default, filtering especially annoying
  143. # modules like async HTTP requests and tokio runtime logs
  144. os.environ["RUST_LOG"] = os.environ.get("RUST_LOG", "info")
  145. # Do not exit this script if a continuous attestation stops for
  146. # whatever reason (this avoids k8s restart penalty)
  147. while True:
  148. # Start the child process in daemon mode
  149. pwhac_process = Popen(
  150. [
  151. "pwhac",
  152. "--commitment",
  153. "confirmed",
  154. "--p2w-addr",
  155. P2W_SOL_ADDRESS,
  156. "--rpc-url",
  157. SOL_RPC_URL,
  158. "--payer",
  159. SOL_PAYER_KEYPAIR,
  160. "attest",
  161. "-f",
  162. P2W_ATTESTATION_CFG,
  163. "--timeout",
  164. P2W_RPC_TIMEOUT_SECS,
  165. ]
  166. )
  167. # Wait for an unexpected process exit
  168. retcode = pwhac_process.wait()
  169. # Yell if the supposedly non-stop attestation process exits
  170. logging.warn(f"pwhac stopped unexpectedly with code {retcode}")