OverviewFilesCommitsIssuesDocsReleasesPeers

wirecore commits / 16be8f4

Fix P2P replication and add sync improvements
Bug fixes: - Fixed ELOCKED error in loadState() by checking if storage exists before opening Corestore - Fixed race condition where cmdInit would try to open Corestore twice P2P improvements: - Add feed.update() to sync remote feed length before reading - Increase connection wait time from 2s to 8s for DHT discovery - Increase block fetch timeout from 2s to 5s for better reliability - Add connection counter and detailed logging - Use wait:true for block fetching to download missing blocks Testing results: - Successfully tested bidirectional P2P replication between two nodes - Local node and ashatron.local can see each other's messages - DHT discovery and Hyperswarm connections working - Sparse replication functioning correctly Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
16be8f4ca2dd7f817de074890aed3dc60c3143b3
by plankatron <fleeky@gmail.com> · 2026-01-20T20:13:42.000Z

Changed files (1) +36 -6

pear-app/index.js +36 -6
……@@ -37,7 +37,16 @@ let follows = new Map() // discoveryKey -> { feed, publicKey, name }
3737 * Initialize Corestore and load state (without starting swarm)
3838 */
3939async function loadState() {
40 if (store) return // Already loaded
40 if (store) return true // Already loaded
41
42 // Check if storage directory exists first
43 const fs = await import('fs')
44 const path = await import('path')
45 const identityPath = path.join(STORAGE_PATH, 'cores')
46
47 if (!fs.existsSync(identityPath)) {
48 return null // Not initialized yet
49 }
4150
4251 store = new Corestore(STORAGE_PATH)
4352 await store.ready()
……@@ -47,6 +56,8 @@ async function loadState() {
4756 await identityCore.ready()
4857
4958 if (identityCore.length === 0) {
59 await store.close()
60 store = null
5061 return null // Not initialized
5162 }
5263
……@@ -75,17 +86,21 @@ async function startSwarm() {
7586
7687 swarm = new Hyperswarm()
7788
89 let connectionCount = 0
7890 swarm.on('connection', (socket, peerInfo) => {
79 console.log('🔗 Connected to peer')
91 connectionCount++
92 console.log(`🔗 Connected to peer ${connectionCount}`)
8093 store.replicate(socket)
8194 })
8295
8396 // Announce our feed
8497 const discovery = swarm.join(myFeed.discoveryKey, { server: true, client: false })
8598 await discovery.flushed()
99 console.log(` 📢 Announced feed: ${b4a.toString(myFeed.discoveryKey, 'hex').substring(0, 16)}...`)
86100
87101 // Load and join followed feeds
88102 await loadFollows()
103 console.log(` 📥 Joined ${follows.size} follow topics`)
89104
90105 // Graceful shutdown
91106 goodbye(() => cleanup())
……@@ -283,9 +298,23 @@ async function cmdTimeline(limit = 20) {
283298 // Start swarm to fetch remote data
284299 console.log('📡 Connecting to network...')
285300 await startSwarm()
301 console.log(` Following ${follows.size} users`)
286302
287 // Wait a bit for connections
288 await new Promise(resolve => setTimeout(resolve, 2000))
303 // Wait for DHT discovery and connections
304 console.log('⏳ Waiting for peer connections (8 seconds)...')
305 await new Promise(resolve => setTimeout(resolve, 8000))
306
307 // Download latest blocks from followed feeds
308 console.log('📥 Syncing feeds...')
309 for (const [discoveryKey, { feed, name }] of follows.entries()) {
310 try {
311 // Update feed length (fetch remote length)
312 await feed.update()
313 console.log(` ${name}: ${feed.length} blocks`)
314 } catch (err) {
315 console.log(` ${name}: sync failed`)
316 }
317 }
289318
290319 const messages = []
291320
……@@ -306,12 +335,13 @@ async function cmdTimeline(limit = 20) {
306335 const feedLimit = Math.min(limit, feed.length)
307336 for (let i = Math.max(0, feed.length - feedLimit); i < feed.length; i++) {
308337 try {
309 const block = await feed.get(i, { wait: false, timeout: 1000 })
338 const block = await feed.get(i, { wait: true, timeout: 5000 })
310339 if (!block) continue
311340 const entry = JSON.parse(block.toString())
312341 messages.push({ ...entry, author: name || publicKey.substring(0, 16), isOwn: false })
313342 } catch (err) {
314 // Sparse replication may not have all blocks yet
343 // Skip blocks that can't be fetched
344 console.log(` ⚠️ Couldn't fetch block ${i} from ${name}: ${err.message}`)
315345 continue
316346 }
317347 }