2025-10-30 11:48:34 +01:00
|
|
|
from asyncio import sleep
|
2025-10-03 22:27:30 +02:00
|
|
|
from random import choices, random
|
2025-11-03 13:17:19 +01:00
|
|
|
from typing import TYPE_CHECKING, AsyncIterator, List
|
2025-10-03 22:27:30 +02:00
|
|
|
|
2025-10-30 11:48:34 +01:00
|
|
|
from rusty_results import Some
|
|
|
|
|
|
2025-10-03 22:27:30 +02:00
|
|
|
from node.api.base import NodeApi
|
2025-10-30 11:48:34 +01:00
|
|
|
from node.api.serializers.block import BlockSerializer
|
|
|
|
|
from node.api.serializers.health import HealthSerializer
|
2025-10-03 22:27:30 +02:00
|
|
|
|
2025-11-03 13:17:19 +01:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from core.app import NBESettings
|
|
|
|
|
|
2025-10-03 22:27:30 +02:00
|
|
|
|
|
|
|
|
def get_weighted_amount() -> int:
|
|
|
|
|
items = [1, 2, 3]
|
|
|
|
|
weights = [0.6, 0.3, 0.1]
|
|
|
|
|
return choices(items, weights=weights, k=1)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FakeNodeApi(NodeApi):
|
2025-11-03 13:17:19 +01:00
|
|
|
def __init__(self, _settings: "NBESettings"):
|
2025-10-30 11:48:34 +01:00
|
|
|
self.current_slot: int = 0
|
|
|
|
|
|
|
|
|
|
async def get_health(self) -> HealthSerializer:
|
2025-10-03 22:27:30 +02:00
|
|
|
if random() < 0.1:
|
2025-10-30 11:48:34 +01:00
|
|
|
return HealthSerializer.from_unhealthy()
|
2025-10-03 22:27:30 +02:00
|
|
|
else:
|
2025-10-30 11:48:34 +01:00
|
|
|
return HealthSerializer.from_healthy()
|
2025-10-03 22:27:30 +02:00
|
|
|
|
2025-10-30 11:48:34 +01:00
|
|
|
async def get_blocks(self, **kwargs) -> List[BlockSerializer]:
|
|
|
|
|
n = get_weighted_amount()
|
|
|
|
|
assert n >= 1
|
|
|
|
|
blocks = [BlockSerializer.from_random() for _ in range(n)]
|
|
|
|
|
self.current_slot = max(blocks, key=lambda block: block.slot).slot
|
|
|
|
|
return blocks
|
2025-10-15 20:53:52 +02:00
|
|
|
|
2025-10-30 11:48:34 +01:00
|
|
|
async def get_blocks_stream(self) -> AsyncIterator[BlockSerializer]:
|
2025-10-15 20:53:52 +02:00
|
|
|
while True:
|
2025-10-30 11:48:34 +01:00
|
|
|
yield BlockSerializer.from_random(slot=Some(self.current_slot))
|
|
|
|
|
self.current_slot += 1
|
|
|
|
|
await sleep(3)
|