auto peer discovery: gossip known peers during sync, discover from_hash automatically
This commit is contained in:
parent
d201fa0bc9
commit
0ed4ec82ba
4 changed files with 87 additions and 1 deletions
|
|
@ -246,6 +246,28 @@ class ForumDB:
|
|||
finally:
|
||||
self.return_db(db)
|
||||
|
||||
def add_known_peer(self, instance_hash):
|
||||
"""Add a discovered peer to the sync list (auto-discovery)."""
|
||||
db = self.get_db()
|
||||
try:
|
||||
db.execute(
|
||||
"INSERT OR IGNORE INTO synced_instances (instance_hash) VALUES (?)",
|
||||
(instance_hash,),
|
||||
)
|
||||
db.commit()
|
||||
finally:
|
||||
self.return_db(db)
|
||||
|
||||
def get_all_known_hashes(self):
|
||||
"""Get all known instance hashes for peer discovery gossip."""
|
||||
db = self.get_db()
|
||||
try:
|
||||
return [r["instance_hash"] for r in db.execute(
|
||||
"SELECT instance_hash FROM synced_instances"
|
||||
).fetchall()]
|
||||
finally:
|
||||
self.return_db(db)
|
||||
|
||||
def upsert_synced_instance(self, instance_hash, name=""):
|
||||
db = self.get_db()
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -604,6 +604,14 @@ class ForumHandlers:
|
|||
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"])
|
||||
|
||||
# Auto-discover the peer that synced with us and their known peers
|
||||
from_hash = data.get("from_hash", "")
|
||||
if from_hash and from_hash not in blocked:
|
||||
self.fdb.add_known_peer(from_hash)
|
||||
for peer_hash in data.get("known_peers", []):
|
||||
if peer_hash and peer_hash != from_hash and peer_hash not in blocked:
|
||||
self.fdb.add_known_peer(peer_hash)
|
||||
|
||||
my_blocks = list(blocked)
|
||||
my_peer_blocks = self.fdb.get_peer_block_list()
|
||||
threads, posts, upvote_threads = [], [], []
|
||||
|
|
@ -616,6 +624,8 @@ class ForumHandlers:
|
|||
retracted = [{"id": cid, "type": ct, "author": ai, "at": ra}
|
||||
for cid, ct, ai, ra in self.fdb.get_raw_retractions()]
|
||||
|
||||
known_peers = [h for h in self.fdb.get_all_known_hashes() if h != from_hash]
|
||||
|
||||
return {
|
||||
"status": 200,
|
||||
"content_type": "application/json",
|
||||
|
|
@ -625,6 +635,7 @@ class ForumHandlers:
|
|||
"upvote_threads": upvote_threads,
|
||||
"blocks": {"mine": my_blocks, "peers": my_peer_blocks},
|
||||
"retractions": retracted,
|
||||
"known_peers": known_peers,
|
||||
}),
|
||||
"headers": {},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue