2024-01-25 18:04:55 +09:00
|
|
|
import asyncio
|
2024-02-05 09:04:02 +01:00
|
|
|
from unittest import IsolatedAsyncioTestCase
|
2024-01-23 10:29:14 +09:00
|
|
|
|
2024-07-04 16:07:12 +09:00
|
|
|
import mixnet.framework.asyncio as asynciofw
|
|
|
|
from mixnet.connection import LocalSimplexConnection
|
2024-06-26 15:55:00 +09:00
|
|
|
from mixnet.node import Node
|
2024-02-05 09:04:02 +01:00
|
|
|
from mixnet.test_utils import (
|
2024-02-08 15:39:50 +09:00
|
|
|
init_mixnet_config,
|
2024-02-05 09:04:02 +01:00
|
|
|
)
|
2024-01-23 10:29:14 +09:00
|
|
|
|
|
|
|
|
2024-06-26 15:55:00 +09:00
|
|
|
class TestNode(IsolatedAsyncioTestCase):
|
|
|
|
async def test_node(self):
|
2024-07-04 16:07:12 +09:00
|
|
|
framework = asynciofw.Framework()
|
2024-06-27 18:04:51 +09:00
|
|
|
global_config, node_configs, _ = init_mixnet_config(10)
|
2024-07-04 16:07:12 +09:00
|
|
|
nodes = [
|
|
|
|
Node(framework, node_config, global_config) for node_config in node_configs
|
|
|
|
]
|
2024-06-26 15:55:00 +09:00
|
|
|
for i, node in enumerate(nodes):
|
2024-07-04 16:07:12 +09:00
|
|
|
node.connect(
|
|
|
|
nodes[(i + 1) % len(nodes)],
|
|
|
|
LocalSimplexConnection(framework),
|
|
|
|
LocalSimplexConnection(framework),
|
|
|
|
)
|
2024-06-26 15:55:00 +09:00
|
|
|
|
|
|
|
await nodes[0].send_message(b"block selection")
|
|
|
|
|
|
|
|
timeout = 15
|
|
|
|
for _ in range(timeout):
|
|
|
|
broadcasted_msgs = []
|
|
|
|
for node in nodes:
|
|
|
|
if not node.broadcast_channel.empty():
|
2024-07-04 16:07:12 +09:00
|
|
|
broadcasted_msgs.append(await node.broadcast_channel.get())
|
2024-06-26 15:55:00 +09:00
|
|
|
|
|
|
|
if len(broadcasted_msgs) == 0:
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
else:
|
|
|
|
# We expect only one node to broadcast the message.
|
|
|
|
assert len(broadcasted_msgs) == 1
|
|
|
|
self.assertEqual(b"block selection", broadcasted_msgs[0])
|
|
|
|
return
|
|
|
|
self.fail("timeout")
|
|
|
|
|
|
|
|
# TODO: check noise
|