truly public forum: auto-discover other instances via RNS announce handler

This commit is contained in:
lichenblankie 2026-06-05 00:55:03 +00:00
parent 0ed4ec82ba
commit 3b3e807518
2 changed files with 80 additions and 1 deletions

View file

@ -378,6 +378,57 @@ def test_sync_peer_discovery():
shutil.rmtree(dir_c)
def test_announce_handler():
"""Announce handler should auto-discover peers."""
dir_a = tempfile.mkdtemp()
try:
fdb = ForumDB(dir_a)
from tinyweb_forum.sync import _ForumAnnounceHandler
# Mock identity — RNS identity.hash returns bytes
class MockIdentity:
def __init__(self, h):
self._hash_hex = h
@property
def hash(self):
return bytes.fromhex(self._hash_hex)
local_hex = "aa" * 16
peer_hex = "bb" * 16
handler = _ForumAnnounceHandler(fdb, MockIdentity(local_hex))
class MockAnnouncedIdentity:
def __init__(self, h):
self._hash_hex = h
@property
def hash(self):
return bytes.fromhex(self._hash_hex)
# Announce from a peer
handler.received_announce(
destination_hash=bytes.fromhex(peer_hex),
announced_identity=MockAnnouncedIdentity(peer_hex),
app_data=b"tinyweb-forum",
)
known = [r["instance_hash"] for r in fdb.get_synced_instances()]
assert peer_hex in known, "peer should be added to sync list"
# Announce from ourselves should be ignored
handler.received_announce(
destination_hash=bytes.fromhex(local_hex),
announced_identity=MockAnnouncedIdentity(local_hex),
app_data=b"tinyweb-forum",
)
known = [r["instance_hash"] for r in fdb.get_synced_instances()]
assert known.count(local_hex) == 0, "own announce should be ignored"
finally:
import shutil
shutil.rmtree(dir_a)
if __name__ == "__main__":
test_sync_thread()
test_sync_reply()
@ -388,4 +439,5 @@ if __name__ == "__main__":
test_sync_block_gossip()
test_sync_updated_content()
test_sync_peer_discovery()
test_announce_handler()
print("all sync tests passed")