|
1 | 1 | import logging |
2 | 2 | import time |
| 3 | +from collections.abc import Sequence |
3 | 4 | from typing import Self |
4 | 5 |
|
5 | 6 | from eth_account._utils.signing import to_standard_v |
|
19 | 20 | Configuration, |
20 | 21 | ) |
21 | 22 | from observer.reward_epoch_manager import ( |
| 23 | + Entity, |
22 | 24 | SigningPolicy, |
23 | 25 | ) |
24 | 26 | from observer.types import ( |
@@ -190,6 +192,32 @@ def log_message(config: Configuration, message: Message): |
190 | 192 | notify_generic(n.generic, message) |
191 | 193 |
|
192 | 194 |
|
| 195 | +async def cron(config: Configuration, w: AsyncWeb3, e: Entity) -> Sequence[Message]: |
| 196 | + mb = Message.builder() |
| 197 | + messages = [] |
| 198 | + |
| 199 | + addrs = ( |
| 200 | + ("submit", e.submit_address), |
| 201 | + ("submit signatures", e.submit_signatures_address), |
| 202 | + ("signing policy", e.signing_policy_address), |
| 203 | + ) |
| 204 | + |
| 205 | + for name, addr in addrs: |
| 206 | + balance = await w.eth.get_balance(addr, "latest") |
| 207 | + if balance < config.fee_threshold * 1e18: |
| 208 | + level = MessageLevel.WARNING |
| 209 | + if balance <= 5e18: |
| 210 | + level = MessageLevel.ERROR |
| 211 | + |
| 212 | + messages.append( |
| 213 | + mb.build( |
| 214 | + level, f"low balance for {name} address ({balance / 1e18:.4f} NAT)" |
| 215 | + ) |
| 216 | + ) |
| 217 | + |
| 218 | + return messages |
| 219 | + |
| 220 | + |
193 | 221 | async def observer_loop(config: Configuration) -> None: |
194 | 222 | w = AsyncWeb3( |
195 | 223 | AsyncWeb3.AsyncHTTPProvider(config.rpc_url), |
@@ -270,6 +298,8 @@ async def observer_loop(config: Configuration) -> None: |
270 | 298 | # # f"current reward epoch: {current_rid}", |
271 | 299 | # ) |
272 | 300 |
|
| 301 | + cron_time = time.time() |
| 302 | + |
273 | 303 | # wait until next voting epoch |
274 | 304 | block_number = block["number"] |
275 | 305 | while True: |
@@ -453,10 +483,17 @@ async def observer_loop(config: Configuration) -> None: |
453 | 483 | except Exception: |
454 | 484 | pass |
455 | 485 |
|
| 486 | + messages: list[Message] = [] |
| 487 | + entity = signing_policy.entity_mapper.by_identity_address[tia] |
| 488 | + |
| 489 | + if int(time.time() - cron_time) < 60 * 60: |
| 490 | + messages.extend(await cron(config, w, entity)) |
| 491 | + |
456 | 492 | rounds = vrm.finalize(block_data) |
457 | 493 | for r in rounds: |
458 | | - entity = signing_policy.entity_mapper.by_identity_address[tia] |
459 | | - for message in validate_round(r, signing_policy, entity, config): |
460 | | - log_message(config, message) |
| 494 | + messages.extend(validate_round(r, signing_policy, entity, config)) |
| 495 | + |
| 496 | + for m in messages: |
| 497 | + log_message(config, m) |
461 | 498 |
|
462 | 499 | block_number = latest_block |
0 commit comments