OverviewFilesCommitsIssuesDocsReleasesPeers

apds commits / 84823e0

add a gossip schedule for blobs we do not have
84823e08d9a776fb2c81a54069f960d461206b0b
by Everett Bogue <ev@evbogue.com> · 2025-12-19T22:30:57.000Z

Changed files (2) +75 -2

gossip.js +53 -0
……@@ -0,0 +1,53 @@
1export const createGossip = (options = {}) => {
2 const {
3 getPeers = () => new Set(),
4 has = async () => false,
5 request = () => {},
6 intervalMs = 10000,
7 } = options
8
9 const missing = new Set()
10 let timer = null
11
12 const enqueue = async (hash) => {
13 if (!hash) return
14 if (await has(hash)) return
15 missing.add(hash)
16 }
17
18 const resolve = (hash) => {
19 if (hash) missing.delete(hash)
20 }
21
22 const tick = async () => {
23 if (!missing.size) return
24 const peers = getPeers() || []
25 const list = peers.size !== undefined ? peers : Array.from(peers)
26 if (!list.length) return
27
28 for (const hash of missing) {
29 if (await has(hash)) {
30 missing.delete(hash)
31 continue
32 }
33 for (const peer of list) {
34 try { request(peer, hash) } catch (_err) {}
35 }
36 }
37 }
38
39 const start = () => {
40 if (timer) return
41 timer = setInterval(() => {
42 tick().catch(() => {})
43 }, intervalMs)
44 }
45
46 const stop = () => {
47 if (!timer) return
48 clearInterval(timer)
49 timer = null
50 }
51
52 return { enqueue, resolve, start, stop }
53}
serve.js +22 -2
……@@ -1,6 +1,7 @@
11import db from './db.json' with { type: 'json'}
22import { serveDir } from 'https://deno.land/std/http/file_server.ts'
33import { apds } from './apds.js'
4import { createGossip } from './gossip.js'
45
56await apds.start('apdsv1')
67
……@@ -12,7 +13,19 @@ if (!await apds.pubkey()) {
1213 console.log('apdsbot started, your pubkey: ' + await apds.pubkey())
1314}
1415
16const sockets = new Set()
17const gossipQueue = createGossip({
18 getPeers: () => sockets,
19 has: async (hash) => !!(await apds.get(hash)),
20 request: (peer, hash) => {
21 if (peer.readyState === 1) peer.send(hash)
22 },
23 intervalMs: 10000,
24})
25gossipQueue.start()
26
1527const apdsbot = async (ws) => {
28 sockets.add(ws)
1629 ws.onopen = async () => {
1730 setTimeout(async () => {
1831 const q = await apds.query()
……@@ -38,10 +51,16 @@ const apdsbot = async (ws) => {
3851 const latest = await apds.getLatest(m.data)
3952 if (latest) { ws.send(latest.sig) }
4053 const got = await apds.get(m.data)
41 if (got) { ws.send(got) }
54 if (got) {
55 ws.send(got)
56 gossipQueue.resolve(m.data)
57 } else {
58 await gossipQueue.enqueue(m.data)
59 }
4260 }
4361 if (m.data.length != 44) {
44 await apds.make(m.data)
62 const storedHash = await apds.make(m.data)
63 gossipQueue.resolve(storedHash)
4564 await apds.add(m.data)
4665 const opened = await apds.open(m.data)
4766 if (opened) {
……@@ -62,6 +81,7 @@ const apdsbot = async (ws) => {
6281 }
6382 }
6483 ws.onclose = () => {
84 sockets.delete(ws)
6585 console.log('DISCONNECTED!')
6686 }
6787}