Skip to content

Commit

Permalink
bitswap: filter interests
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumemichel committed Jan 29, 2025
1 parent e41fd53 commit fd5f484
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,26 @@ func (sim *SessionInterestManager) InterestedSessions(keySets ...[]cid.Cid) []ui
}
return ses
}

// Filters only the keys that are wanted by at least one session
func (sim *SessionInterestManager) FilterInterests(keySets ...[]cid.Cid) [][]cid.Cid {
sim.lk.RLock()
defer sim.lk.RUnlock()

result := make([][]cid.Cid, len(keySets))
// For each set of keys
for i, ks := range keySets {
// The set of keys that at least one session is interested in
wanted := make([]cid.Cid, 0, len(ks))

// For each key in the set
for _, c := range ks {
// If there are any sessions interested in this key
if _, ok := sim.wants[c]; ok {
wanted = append(wanted, c)
}
}
result[i] = wanted
}
return result
}
5 changes: 5 additions & 0 deletions bitswap/client/internal/sessionmanager/sessionmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ func (sm *SessionManager) GetNextSessionID() uint64 {

// ReceiveFrom is called when a new message is received
func (sm *SessionManager) ReceiveFrom(ctx context.Context, p peer.ID, blks []cid.Cid, haves []cid.Cid, dontHaves []cid.Cid) {
// Keep only the keys that at least one session is interested in
keys := sm.sessionInterestManager.FilterInterests(blks, haves, dontHaves)
blks = keys[0]
haves = keys[1]
dontHaves = keys[2]
// Record block presence for HAVE / DONT_HAVE
sm.blockPresenceManager.ReceiveFrom(p, haves, dontHaves)

Expand Down

0 comments on commit fd5f484

Please sign in to comment.