nomos-specs/mixnet/mixnet.py

52 lines
1.5 KiB
Python
Raw Normal View History

from __future__ import annotations
import asyncio
from typing import Self
from cryptography.hazmat.primitives.asymmetric.x25519 import (
X25519PrivateKey,
)
from mixnet.client import MixClient
from mixnet.config import MixnetConfig
2024-01-23 01:29:14 +00:00
from mixnet.node import MixNode
class Mixnet:
__mixclient: MixClient
__mixnode: MixNode
2024-02-05 06:47:36 +00:00
@classmethod
async def new(
cls,
encryption_private_key: X25519PrivateKey,
config: MixnetConfig,
) -> Self:
self = cls()
self.__mixclient = await MixClient.new(config)
self.__mixnode = await MixNode.new(encryption_private_key, config)
return self
2024-02-05 06:47:36 +00:00
async def publish_message(self, msg: bytes) -> None:
await self.__mixclient.send_message(msg)
2024-02-05 06:47:36 +00:00
def subscribe_messages(self) -> "asyncio.Queue[bytes]":
return self.__mixclient.subscribe_messages()
2024-02-05 06:47:36 +00:00
def set_config(self, config: MixnetConfig) -> None:
2024-02-05 06:47:36 +00:00
"""
Replace the old config with the new config received.
In real implementations, this method should be a long-running task, accepting configs periodically.
Here in the spec, this method has been simplified as a setter, assuming the single-thread test environment.
2024-02-05 06:47:36 +00:00
"""
self.__mixclient.set_config(config)
self.__mixnode.set_config(config)
2024-02-05 06:47:36 +00:00
def get_config(self) -> MixnetConfig:
return self.__mixclient.get_config()
2024-02-05 06:47:36 +00:00
async def cancel(self) -> None:
await self.__mixclient.cancel()
await self.__mixnode.cancel()