Skip to content

Commit 96db61e

Browse files
committed
Add basic dev docs for replicator logic
1 parent 9398c92 commit 96db61e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

lib/replicator.js

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/* DEV DOCS
2+
Every hypercore has one Replicator object managing its connections to other peers.
3+
There is one Peer object per peer connected to the Hypercore.
4+
Hypercores do not know about other hypercores, so when a peer is connected to multiple cores, there exists one Peer object per core.
5+
6+
Hypercore indicates block should be downloaded through methods like Replicator.addRange or Replicator.addBlock
7+
Hypercore calls Replicator.updateActivity every time a hypercore session opens/closes
8+
Replicator.updateActivity ensures the Hypercore is downloading blocks as expected
9+
Replicator keeps track of:
10+
- Which blocks need to be downloaded (Replicator._blocks)
11+
- Which blocks currently have inflight requests (Replicator._inflight)
12+
13+
Blocks are requested from remote peers by Peer objects. The flow is:
14+
- The replicator's updatePeer method gets called
15+
- The replicator detects whether the Peer can accept more requests (for example by checking if it's maxed out on inflight blocks)
16+
- The replicator then tells the Peer what to request (e.g. Peer_requestRange or Peer._requestBlock methods)
17+
18+
The Peer object is responsible for tracking
19+
- Which blocks does the Peer have available (tracked in remoteBitfield)
20+
- Which blocks are you actively looking for from this peer (tracked in missingBlocks)
21+
- How many blocks are currently inflight (tracked in inflight)
22+
The Peer uses this information to decide which blocks to request form the peer in response to _requestRange requests and the like.
23+
*/
24+
125
const b4a = require('b4a')
226
const safetyCatch = require('safety-catch')
327
const RandomIterator = require('random-array-iterator')

0 commit comments

Comments
 (0)