Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit 7348b26

Browse files
authored
chore: make pwm internals private (#315)
This makes it easier to tell where module boundaries are.
1 parent 7888679 commit 7348b26

File tree

4 files changed

+54
-54
lines changed

4 files changed

+54
-54
lines changed

internal/peermanager/peermanager.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ func (pm *PeerManager) Connected(p peer.ID, initialWantHaves []cid.Cid) {
9494
// If this is the first connection to the peer
9595
if pq.refcnt == 1 {
9696
// Inform the peer want manager that there's a new peer
97-
pm.pwm.AddPeer(p)
97+
pm.pwm.addPeer(p)
9898
// Record that the want-haves are being sent to the peer
99-
pm.pwm.PrepareSendWants(p, nil, initialWantHaves)
99+
_, wantHaves := pm.pwm.prepareSendWants(p, nil, initialWantHaves)
100100
// Broadcast any live want-haves to the newly connected peers
101-
pq.pq.AddBroadcastWantHaves(initialWantHaves)
101+
pq.pq.AddBroadcastWantHaves(wantHaves)
102102
// Inform the sessions that the peer has connected
103103
pm.signalAvailability(p, true)
104104
}
@@ -126,7 +126,7 @@ func (pm *PeerManager) Disconnected(p peer.ID) {
126126
// Clean up the peer
127127
delete(pm.peerQueues, p)
128128
pq.pq.Shutdown()
129-
pm.pwm.RemovePeer(p)
129+
pm.pwm.removePeer(p)
130130
}
131131

132132
// BroadcastWantHaves broadcasts want-haves to all peers (used by the session
@@ -137,7 +137,7 @@ func (pm *PeerManager) BroadcastWantHaves(ctx context.Context, wantHaves []cid.C
137137
pm.pqLk.Lock()
138138
defer pm.pqLk.Unlock()
139139

140-
for p, ks := range pm.pwm.PrepareBroadcastWantHaves(wantHaves) {
140+
for p, ks := range pm.pwm.prepareBroadcastWantHaves(wantHaves) {
141141
if pqi, ok := pm.peerQueues[p]; ok {
142142
pqi.pq.AddBroadcastWantHaves(ks)
143143
}
@@ -151,7 +151,7 @@ func (pm *PeerManager) SendWants(ctx context.Context, p peer.ID, wantBlocks []ci
151151
defer pm.pqLk.Unlock()
152152

153153
if pqi, ok := pm.peerQueues[p]; ok {
154-
wblks, whvs := pm.pwm.PrepareSendWants(p, wantBlocks, wantHaves)
154+
wblks, whvs := pm.pwm.prepareSendWants(p, wantBlocks, wantHaves)
155155
pqi.pq.AddWants(wblks, whvs)
156156
}
157157
}
@@ -163,7 +163,7 @@ func (pm *PeerManager) SendCancels(ctx context.Context, cancelKs []cid.Cid) {
163163
defer pm.pqLk.Unlock()
164164

165165
// Send a CANCEL to each peer that has been sent a want-block or want-have
166-
for p, ks := range pm.pwm.PrepareSendCancels(cancelKs) {
166+
for p, ks := range pm.pwm.prepareSendCancels(cancelKs) {
167167
if pqi, ok := pm.peerQueues[p]; ok {
168168
pqi.pq.AddCancels(ks)
169169
}
@@ -175,23 +175,23 @@ func (pm *PeerManager) CurrentWants() []cid.Cid {
175175
pm.pqLk.RLock()
176176
defer pm.pqLk.RUnlock()
177177

178-
return pm.pwm.GetWants()
178+
return pm.pwm.getWants()
179179
}
180180

181181
// CurrentWantBlocks returns the list of pending want-blocks
182182
func (pm *PeerManager) CurrentWantBlocks() []cid.Cid {
183183
pm.pqLk.RLock()
184184
defer pm.pqLk.RUnlock()
185185

186-
return pm.pwm.GetWantBlocks()
186+
return pm.pwm.getWantBlocks()
187187
}
188188

189189
// CurrentWantHaves returns the list of pending want-haves
190190
func (pm *PeerManager) CurrentWantHaves() []cid.Cid {
191191
pm.pqLk.RLock()
192192
defer pm.pqLk.RUnlock()
193193

194-
return pm.pwm.GetWantHaves()
194+
return pm.pwm.getWantHaves()
195195
}
196196

197197
func (pm *PeerManager) getOrCreate(p peer.ID) *peerQueueInstance {

internal/peermanager/peerwantmanager.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func newPeerWantManager(wantBlockGauge Gauge) *peerWantManager {
3939
}
4040

4141
// AddPeer adds a peer whose wants we need to keep track of
42-
func (pwm *peerWantManager) AddPeer(p peer.ID) {
42+
func (pwm *peerWantManager) addPeer(p peer.ID) {
4343
if _, ok := pwm.peerWants[p]; !ok {
4444
pwm.peerWants[p] = &peerWant{
4545
wantBlocks: cid.NewSet(),
@@ -49,13 +49,13 @@ func (pwm *peerWantManager) AddPeer(p peer.ID) {
4949
}
5050

5151
// RemovePeer removes a peer and its associated wants from tracking
52-
func (pwm *peerWantManager) RemovePeer(p peer.ID) {
52+
func (pwm *peerWantManager) removePeer(p peer.ID) {
5353
delete(pwm.peerWants, p)
5454
}
5555

5656
// PrepareBroadcastWantHaves filters the list of want-haves for each peer,
5757
// returning a map of peers to the want-haves they have not yet been sent.
58-
func (pwm *peerWantManager) PrepareBroadcastWantHaves(wantHaves []cid.Cid) map[peer.ID][]cid.Cid {
58+
func (pwm *peerWantManager) prepareBroadcastWantHaves(wantHaves []cid.Cid) map[peer.ID][]cid.Cid {
5959
res := make(map[peer.ID][]cid.Cid)
6060

6161
// Iterate over all known peers
@@ -81,7 +81,7 @@ func (pwm *peerWantManager) PrepareBroadcastWantHaves(wantHaves []cid.Cid) map[p
8181

8282
// PrepareSendWants filters the list of want-blocks and want-haves such that
8383
// it only contains wants that have not already been sent to the peer.
84-
func (pwm *peerWantManager) PrepareSendWants(p peer.ID, wantBlocks []cid.Cid, wantHaves []cid.Cid) ([]cid.Cid, []cid.Cid) {
84+
func (pwm *peerWantManager) prepareSendWants(p peer.ID, wantBlocks []cid.Cid, wantHaves []cid.Cid) ([]cid.Cid, []cid.Cid) {
8585
resWantBlks := make([]cid.Cid, 0)
8686
resWantHvs := make([]cid.Cid, 0)
8787

@@ -124,7 +124,7 @@ func (pwm *peerWantManager) PrepareSendWants(p peer.ID, wantBlocks []cid.Cid, wa
124124
// PrepareSendCancels filters the list of cancels for each peer,
125125
// returning a map of peers which only contains cancels for wants that have
126126
// been sent to the peer.
127-
func (pwm *peerWantManager) PrepareSendCancels(cancelKs []cid.Cid) map[peer.ID][]cid.Cid {
127+
func (pwm *peerWantManager) prepareSendCancels(cancelKs []cid.Cid) map[peer.ID][]cid.Cid {
128128
res := make(map[peer.ID][]cid.Cid)
129129

130130
// Iterate over all known peers
@@ -158,7 +158,7 @@ func (pwm *peerWantManager) PrepareSendCancels(cancelKs []cid.Cid) map[peer.ID][
158158
}
159159

160160
// GetWantBlocks returns the set of all want-blocks sent to all peers
161-
func (pwm *peerWantManager) GetWantBlocks() []cid.Cid {
161+
func (pwm *peerWantManager) getWantBlocks() []cid.Cid {
162162
res := cid.NewSet()
163163

164164
// Iterate over all known peers
@@ -174,7 +174,7 @@ func (pwm *peerWantManager) GetWantBlocks() []cid.Cid {
174174
}
175175

176176
// GetWantHaves returns the set of all want-haves sent to all peers
177-
func (pwm *peerWantManager) GetWantHaves() []cid.Cid {
177+
func (pwm *peerWantManager) getWantHaves() []cid.Cid {
178178
res := cid.NewSet()
179179

180180
// Iterate over all known peers
@@ -190,7 +190,7 @@ func (pwm *peerWantManager) GetWantHaves() []cid.Cid {
190190
}
191191

192192
// GetWants returns the set of all wants (both want-blocks and want-haves).
193-
func (pwm *peerWantManager) GetWants() []cid.Cid {
193+
func (pwm *peerWantManager) getWants() []cid.Cid {
194194
res := cid.NewSet()
195195

196196
// Iterate over all known peers

internal/peermanager/peerwantmanager_test.go

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ func (g *gauge) Dec() {
2222
func TestEmpty(t *testing.T) {
2323
pwm := newPeerWantManager(&gauge{})
2424

25-
if len(pwm.GetWantBlocks()) > 0 {
25+
if len(pwm.getWantBlocks()) > 0 {
2626
t.Fatal("Expected GetWantBlocks() to have length 0")
2727
}
28-
if len(pwm.GetWantHaves()) > 0 {
28+
if len(pwm.getWantHaves()) > 0 {
2929
t.Fatal("Expected GetWantHaves() to have length 0")
3030
}
3131
}
@@ -38,11 +38,11 @@ func TestPrepareBroadcastWantHaves(t *testing.T) {
3838
cids2 := testutil.GenerateCids(2)
3939
cids3 := testutil.GenerateCids(2)
4040

41-
pwm.AddPeer(peers[0])
42-
pwm.AddPeer(peers[1])
41+
pwm.addPeer(peers[0])
42+
pwm.addPeer(peers[1])
4343

4444
// Broadcast 2 cids to 2 peers
45-
bcst := pwm.PrepareBroadcastWantHaves(cids)
45+
bcst := pwm.prepareBroadcastWantHaves(cids)
4646
if len(bcst) != 2 {
4747
t.Fatal("Expected 2 peers")
4848
}
@@ -53,13 +53,13 @@ func TestPrepareBroadcastWantHaves(t *testing.T) {
5353
}
5454

5555
// Broadcasting same cids should have no effect
56-
bcst2 := pwm.PrepareBroadcastWantHaves(cids)
56+
bcst2 := pwm.prepareBroadcastWantHaves(cids)
5757
if len(bcst2) != 0 {
5858
t.Fatal("Expected 0 peers")
5959
}
6060

6161
// Broadcast 2 other cids
62-
bcst3 := pwm.PrepareBroadcastWantHaves(cids2)
62+
bcst3 := pwm.prepareBroadcastWantHaves(cids2)
6363
if len(bcst3) != 2 {
6464
t.Fatal("Expected 2 peers")
6565
}
@@ -70,7 +70,7 @@ func TestPrepareBroadcastWantHaves(t *testing.T) {
7070
}
7171

7272
// Broadcast mix of old and new cids
73-
bcst4 := pwm.PrepareBroadcastWantHaves(append(cids, cids3...))
73+
bcst4 := pwm.prepareBroadcastWantHaves(append(cids, cids3...))
7474
if len(bcst4) != 2 {
7575
t.Fatal("Expected 2 peers")
7676
}
@@ -84,9 +84,9 @@ func TestPrepareBroadcastWantHaves(t *testing.T) {
8484
// Sending want-block for a cid should prevent broadcast to that peer
8585
cids4 := testutil.GenerateCids(4)
8686
wantBlocks := []cid.Cid{cids4[0], cids4[2]}
87-
pwm.PrepareSendWants(peers[0], wantBlocks, []cid.Cid{})
87+
pwm.prepareSendWants(peers[0], wantBlocks, []cid.Cid{})
8888

89-
bcst5 := pwm.PrepareBroadcastWantHaves(cids4)
89+
bcst5 := pwm.prepareBroadcastWantHaves(cids4)
9090
if len(bcst4) != 2 {
9191
t.Fatal("Expected 2 peers")
9292
}
@@ -105,8 +105,8 @@ func TestPrepareBroadcastWantHaves(t *testing.T) {
105105
}
106106

107107
// Add another peer
108-
pwm.AddPeer(peers[2])
109-
bcst6 := pwm.PrepareBroadcastWantHaves(cids)
108+
pwm.addPeer(peers[2])
109+
bcst6 := pwm.prepareBroadcastWantHaves(cids)
110110
if len(bcst6) != 1 {
111111
t.Fatal("Expected 1 peer")
112112
}
@@ -126,11 +126,11 @@ func TestPrepareSendWants(t *testing.T) {
126126
cids := testutil.GenerateCids(2)
127127
cids2 := testutil.GenerateCids(2)
128128

129-
pwm.AddPeer(p0)
130-
pwm.AddPeer(p1)
129+
pwm.addPeer(p0)
130+
pwm.addPeer(p1)
131131

132132
// Send 2 want-blocks and 2 want-haves to p0
133-
wb, wh := pwm.PrepareSendWants(p0, cids, cids2)
133+
wb, wh := pwm.prepareSendWants(p0, cids, cids2)
134134
if !testutil.MatchKeysIgnoreOrder(wb, cids) {
135135
t.Fatal("Expected 2 want-blocks")
136136
}
@@ -143,7 +143,7 @@ func TestPrepareSendWants(t *testing.T) {
143143
// - 1 old want-have and 2 new want-haves
144144
cids3 := testutil.GenerateCids(2)
145145
cids4 := testutil.GenerateCids(2)
146-
wb2, wh2 := pwm.PrepareSendWants(p0, append(cids3, cids[0]), append(cids4, cids2[0]))
146+
wb2, wh2 := pwm.prepareSendWants(p0, append(cids3, cids[0]), append(cids4, cids2[0]))
147147
if !testutil.MatchKeysIgnoreOrder(wb2, cids3) {
148148
t.Fatal("Expected 2 want-blocks")
149149
}
@@ -154,7 +154,7 @@ func TestPrepareSendWants(t *testing.T) {
154154
// Send to p0 as want-blocks: 1 new want-block, 1 old want-have
155155
cids5 := testutil.GenerateCids(1)
156156
newWantBlockOldWantHave := append(cids5, cids2[0])
157-
wb3, wh3 := pwm.PrepareSendWants(p0, newWantBlockOldWantHave, []cid.Cid{})
157+
wb3, wh3 := pwm.prepareSendWants(p0, newWantBlockOldWantHave, []cid.Cid{})
158158
// If a want was sent as a want-have, it should be ok to now send it as a
159159
// want-block
160160
if !testutil.MatchKeysIgnoreOrder(wb3, newWantBlockOldWantHave) {
@@ -167,7 +167,7 @@ func TestPrepareSendWants(t *testing.T) {
167167
// Send to p0 as want-haves: 1 new want-have, 1 old want-block
168168
cids6 := testutil.GenerateCids(1)
169169
newWantHaveOldWantBlock := append(cids6, cids[0])
170-
wb4, wh4 := pwm.PrepareSendWants(p0, []cid.Cid{}, newWantHaveOldWantBlock)
170+
wb4, wh4 := pwm.prepareSendWants(p0, []cid.Cid{}, newWantHaveOldWantBlock)
171171
// If a want was previously sent as a want-block, it should not be
172172
// possible to now send it as a want-have
173173
if !testutil.MatchKeysIgnoreOrder(wh4, cids6) {
@@ -178,7 +178,7 @@ func TestPrepareSendWants(t *testing.T) {
178178
}
179179

180180
// Send 2 want-blocks and 2 want-haves to p1
181-
wb5, wh5 := pwm.PrepareSendWants(p1, cids, cids2)
181+
wb5, wh5 := pwm.prepareSendWants(p1, cids, cids2)
182182
if !testutil.MatchKeysIgnoreOrder(wb5, cids) {
183183
t.Fatal("Expected 2 want-blocks")
184184
}
@@ -200,41 +200,41 @@ func TestPrepareSendCancels(t *testing.T) {
200200
allwb := append(wb1, wb2...)
201201
allwh := append(wh1, wh2...)
202202

203-
pwm.AddPeer(p0)
204-
pwm.AddPeer(p1)
203+
pwm.addPeer(p0)
204+
pwm.addPeer(p1)
205205

206206
// Send 2 want-blocks and 2 want-haves to p0
207-
pwm.PrepareSendWants(p0, wb1, wh1)
207+
pwm.prepareSendWants(p0, wb1, wh1)
208208
// Send 3 want-blocks and 3 want-haves to p1
209209
// (1 overlapping want-block / want-have with p0)
210-
pwm.PrepareSendWants(p1, append(wb2, wb1[1]), append(wh2, wh1[1]))
210+
pwm.prepareSendWants(p1, append(wb2, wb1[1]), append(wh2, wh1[1]))
211211

212-
if !testutil.MatchKeysIgnoreOrder(pwm.GetWantBlocks(), allwb) {
212+
if !testutil.MatchKeysIgnoreOrder(pwm.getWantBlocks(), allwb) {
213213
t.Fatal("Expected 4 cids to be wanted")
214214
}
215-
if !testutil.MatchKeysIgnoreOrder(pwm.GetWantHaves(), allwh) {
215+
if !testutil.MatchKeysIgnoreOrder(pwm.getWantHaves(), allwh) {
216216
t.Fatal("Expected 4 cids to be wanted")
217217
}
218218

219219
// Cancel 1 want-block and 1 want-have that were sent to p0
220-
res := pwm.PrepareSendCancels([]cid.Cid{wb1[0], wh1[0]})
220+
res := pwm.prepareSendCancels([]cid.Cid{wb1[0], wh1[0]})
221221
// Should cancel the want-block and want-have
222222
if len(res) != 1 {
223223
t.Fatal("Expected 1 peer")
224224
}
225225
if !testutil.MatchKeysIgnoreOrder(res[p0], []cid.Cid{wb1[0], wh1[0]}) {
226226
t.Fatal("Expected 2 cids to be cancelled")
227227
}
228-
if !testutil.MatchKeysIgnoreOrder(pwm.GetWantBlocks(), append(wb2, wb1[1])) {
228+
if !testutil.MatchKeysIgnoreOrder(pwm.getWantBlocks(), append(wb2, wb1[1])) {
229229
t.Fatal("Expected 3 want-blocks")
230230
}
231-
if !testutil.MatchKeysIgnoreOrder(pwm.GetWantHaves(), append(wh2, wh1[1])) {
231+
if !testutil.MatchKeysIgnoreOrder(pwm.getWantHaves(), append(wh2, wh1[1])) {
232232
t.Fatal("Expected 3 want-haves")
233233
}
234234

235235
// Cancel everything
236236
allCids := append(allwb, allwh...)
237-
res2 := pwm.PrepareSendCancels(allCids)
237+
res2 := pwm.prepareSendCancels(allCids)
238238
// Should cancel the remaining want-blocks and want-haves
239239
if len(res2) != 2 {
240240
t.Fatal("Expected 2 peers", len(res2))
@@ -247,10 +247,10 @@ func TestPrepareSendCancels(t *testing.T) {
247247
if !testutil.MatchKeysIgnoreOrder(res2[p1], remainingP2) {
248248
t.Fatal("Expected un-cancelled cids to be cancelled")
249249
}
250-
if len(pwm.GetWantBlocks()) != 0 {
250+
if len(pwm.getWantBlocks()) != 0 {
251251
t.Fatal("Expected 0 want-blocks")
252252
}
253-
if len(pwm.GetWantHaves()) != 0 {
253+
if len(pwm.getWantHaves()) != 0 {
254254
t.Fatal("Expected 0 want-haves")
255255
}
256256
}
@@ -264,18 +264,18 @@ func TestStats(t *testing.T) {
264264
cids := testutil.GenerateCids(2)
265265
cids2 := testutil.GenerateCids(2)
266266

267-
pwm.AddPeer(p0)
267+
pwm.addPeer(p0)
268268

269269
// Send 2 want-blocks and 2 want-haves to p0
270-
pwm.PrepareSendWants(p0, cids, cids2)
270+
pwm.prepareSendWants(p0, cids, cids2)
271271

272272
if g.count != 2 {
273273
t.Fatal("Expected 2 want-blocks")
274274
}
275275

276276
// Send 1 old want-block and 2 new want-blocks to p0
277277
cids3 := testutil.GenerateCids(2)
278-
pwm.PrepareSendWants(p0, append(cids3, cids[0]), []cid.Cid{})
278+
pwm.prepareSendWants(p0, append(cids3, cids[0]), []cid.Cid{})
279279

280280
if g.count != 4 {
281281
t.Fatal("Expected 4 want-blocks")
@@ -284,7 +284,7 @@ func TestStats(t *testing.T) {
284284
// Cancel 1 want-block that was sent to p0
285285
// and 1 want-block that was not sent
286286
cids4 := testutil.GenerateCids(1)
287-
pwm.PrepareSendCancels(append(cids4, cids[0]))
287+
pwm.prepareSendCancels(append(cids4, cids[0]))
288288

289289
if g.count != 3 {
290290
t.Fatal("Expected 3 want-blocks", g.count)

internal/wantmanager/wantmanager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (wm *WantManager) BroadcastWantHaves(ctx context.Context, ses uint64, wantH
8989

9090
// RemoveSession is called when the session is shut down
9191
func (wm *WantManager) RemoveSession(ctx context.Context, ses uint64) {
92-
// Remove session's interest in the given blocks
92+
// Remove session's interest in the given blocks.
9393
cancelKs := wm.sim.RemoveSessionInterest(ses)
9494

9595
// Remove broadcast want-haves for session

0 commit comments

Comments
 (0)