-
Notifications
You must be signed in to change notification settings - Fork 0
/
hyperlane_network_exporter.py
215 lines (174 loc) · 6.43 KB
/
hyperlane_network_exporter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import argparse
import asyncio
import enum
import functools
import json
import logging
import pathlib
import sys
import typing
from aiohttp import client, web
from prometheus_async import aio
from prometheus_client import Gauge
from web3 import AsyncWeb3
from web3.contract import AsyncContract
from web3.eth import AsyncEth
from web3.providers.rpc import AsyncHTTPProvider
logger = logging.getLogger(__name__)
# #############
# Command line
DEFAULT_INTERVAL_MS = 30000
arg_parser = argparse.ArgumentParser("Hyperlane network metrics exporter.")
arg_parser.add_argument(
"-e",
"--ethereum-rpc",
help="Ethereum RPC base URL",
required=True,
)
arg_parser.add_argument(
"-i",
"--interval-ms",
type=int,
help="How often to update value from contract",
default=DEFAULT_INTERVAL_MS,
)
arg_parser.add_argument(
"-H", "--host", default="127.0.0.1", help="Listen on this host."
)
arg_parser.add_argument(
"-P", "--port", type=int, default=39339, help="Listen on this port."
)
# ############################
# Supported Ethereum networks
class SupportedNetworks(str, enum.Enum):
MAINNET = "mainnet"
HOLESKY = "holesky"
def __str__(self) -> str:
return self.value
def hyperlane_merkle_tree_hook_contract(self) -> str:
# See https://docs.hyperlane.xyz/docs/reference/contract-addresses#merkle-tree-hook
match self.value:
case SupportedNetworks.HOLESKY:
return "0x98AAE089CaD930C64a76dD2247a2aC5773a4B8cE"
case SupportedNetworks.MAINNET:
return "0x48e6c30B97748d1e2e03bf3e9FbE3890ca5f8CCA"
raise RuntimeError(
"Can not derive hyperlane merkle tree hook address for network"
)
# ########
# Metrics
hyperlane_contract_latest_checkpoint = Gauge(
name="hyperlane_contract_latest_checkpoint",
documentation="Latest checkpoint acknowledged by Hyperlane contract",
labelnames=["network"],
)
# ####################
# Aiohttp & web3 apps
async def start_exporter_app(app: web.Application) -> None:
exporter: HyperlaneContractExporter = app[exporter_app_key]
await exporter.w3.provider.cache_async_session(exporter.session) # type: ignore[attr-defined]
await exporter.init()
# Acquire data once to verify its working
await exporter.tick()
exporter.start()
async def stop_exporter_app(app: web.Application) -> None:
exporter: HyperlaneContractExporter = app[exporter_app_key]
await exporter.stop()
def get_application(exporter: "HyperlaneContractExporter") -> web.Application:
app = web.Application()
app[exporter_app_key] = exporter
app.router.add_get("/metrics", aio.web.server_stats)
app.on_startup.append(start_exporter_app)
app.on_shutdown.append(stop_exporter_app)
return app
def get_web3_provider(ethereum_rpc_url: str) -> AsyncWeb3:
return AsyncWeb3(
provider=AsyncHTTPProvider(ethereum_rpc_url),
modules={"eth": (AsyncEth,)},
)
def get_hyperlane_merkle_tree_hook_contract_abi() -> typing.Any:
contents = (
pathlib.Path(__file__).parent / "contract/MerkleTreeHook.json"
).read_text()
return json.loads(contents)
def get_hyperlane_merkle_tree_hook_contract(
web3: AsyncWeb3, network: SupportedNetworks
) -> AsyncContract:
abi = get_hyperlane_merkle_tree_hook_contract_abi()
address = network.hyperlane_merkle_tree_hook_contract()
contract: AsyncContract = web3.eth.contract(address=address, abi=abi) # type: ignore[call-overload]
return contract
class HyperlaneContractExporter:
def __init__(
self,
rpc_address: str,
interval_ms: int = DEFAULT_INTERVAL_MS,
*,
loop: asyncio.AbstractEventLoop,
):
self.rpc_address = rpc_address
self.interval_ms = interval_ms
self.loop = loop
self.session = client.ClientSession(loop=loop)
self.w3 = get_web3_provider(self.rpc_address)
self.stopping = False
self.stopped = asyncio.Event()
@functools.cached_property
def contract(self) -> AsyncContract:
return get_hyperlane_merkle_tree_hook_contract(self.w3, self.network)
def on_runner_task_done(self, *args: typing.Any) -> None:
self.stopped.set()
def start(self) -> None:
self._runner_task = self.loop.create_task(self.run())
# Raise event when task is stopped
self._runner_task.add_done_callback(self.on_runner_task_done)
async def stop(self) -> None:
logger.info("Gracefully shutting down application")
self.stopping = True
self._runner_task.cancel()
if not self.stopped.is_set():
await self.stopped.wait()
await self.session.close()
logger.info("Stopped components, will exit")
async def sleep(self) -> None:
await asyncio.sleep(self.interval_ms / 1000)
async def init(self) -> None:
# Resolve network from RPC
chain_id = await self.w3.eth.chain_id
if chain_id == 1:
logger.info("Discovered Ethereum network as MAINNET")
self.network = SupportedNetworks.MAINNET
elif chain_id == 17000:
logger.info("Discovered Ethereum network as HOLESKY")
self.network = SupportedNetworks.HOLESKY
else:
raise RuntimeError("Unsupported network with chain id %s", chain_id)
async def tick(self) -> None:
_, value = await self.contract.functions.latestCheckpoint().call()
logger.info("Fetched checkpoint index from contract: %s", value)
hyperlane_contract_latest_checkpoint.labels(self.network).set(value)
async def run(self) -> None:
"""Infinite loop that spawns checker tasks."""
while not self.stopping:
self.loop.create_task(self.tick())
await self.sleep()
self.stopped.set()
exporter_app_key: web.AppKey[HyperlaneContractExporter] = web.AppKey(
"exporter", HyperlaneContractExporter
)
def main() -> None:
args = arg_parser.parse_args()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
exporter = HyperlaneContractExporter(args.ethereum_rpc, args.interval_ms, loop=loop)
app = get_application(exporter)
web.run_app(
app, host=args.host, port=args.port, loop=loop, handler_cancellation=True
)
if __name__ == "__main__":
logging.basicConfig(
level=logging.INFO,
stream=sys.stderr,
format="%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s: %(message)s",
)
main()