auto peer discovery: gossip known peers during sync, discover from_hash automatically

This commit is contained in:
lichenblankie 2026-06-05 00:43:18 +00:00
parent d201fa0bc9
commit 0ed4ec82ba
4 changed files with 87 additions and 1 deletions

View file

@ -116,14 +116,18 @@ class ForumSync:
retracted = [{"id": cid, "type": ct, "author": ai, "at": ra}
for cid, ct, ai, ra in self.fdb.get_raw_retractions()]
my_hash = self.identity.hash.hex() if self.identity else "local"
known_peers = [h for h in self.fdb.get_all_known_hashes() if h != instance_hash and h != my_hash]
request_data = {
"query": {"since": [since]} if since else {},
"threads": threads,
"posts": posts,
"upvotes": upvotes,
"from_hash": self.identity.hash.hex() if self.identity else "local",
"from_hash": my_hash,
"blocks": {"mine": my_blocks, "peers": my_peer_blocks},
"retractions": retracted,
"known_peers": known_peers,
}
receipt = link.request("/forum", data=request_data, timeout=REQUEST_TIMEOUT)
@ -162,6 +166,10 @@ class ForumSync:
for r in data.get("retractions", []):
if r.get("id") and r.get("type") and r.get("author") and r.get("at"):
self.fdb.merge_retraction(r["id"], r["type"], r["author"], r["at"])
# Discover new peers from gossip
for peer_hash in data.get("known_peers", []):
if peer_hash and peer_hash != my_hash and peer_hash != instance_hash:
self.fdb.add_known_peer(peer_hash)
now = time.strftime("%Y-%m-%dT%H:%M:%S")
self.fdb.set_last_sync(instance_hash, now)
else: