@@ -29,68 +29,6 @@ import (
29
29
"github.com/ipfs/go-graphsync/testutil"
30
30
)
31
31
32
- type requestRecord struct {
33
- gsr gsmsg.GraphSyncRequest
34
- p peer.ID
35
- }
36
-
37
- type fakePeerHandler struct {
38
- requestRecordChan chan requestRecord
39
- }
40
-
41
- func (fph * fakePeerHandler ) AllocateAndBuildMessage (p peer.ID , blkSize uint64 ,
42
- requestBuilder func (b * gsmsg.Builder ), notifees []notifications.Notifee ) {
43
- builder := gsmsg .NewBuilder (gsmsg .Topic (0 ))
44
- requestBuilder (builder )
45
- message , err := builder .Build ()
46
- if err != nil {
47
- panic (err )
48
- }
49
- fph .requestRecordChan <- requestRecord {
50
- gsr : message .Requests ()[0 ],
51
- p : p ,
52
- }
53
- }
54
-
55
- func readNNetworkRequests (ctx context.Context ,
56
- t * testing.T ,
57
- requestRecordChan <- chan requestRecord ,
58
- count int ) []requestRecord {
59
- requestRecords := make ([]requestRecord , 0 , count )
60
- for i := 0 ; i < count ; i ++ {
61
- var rr requestRecord
62
- testutil .AssertReceive (ctx , t , requestRecordChan , & rr , fmt .Sprintf ("did not receive request %d" , i ))
63
- requestRecords = append (requestRecords , rr )
64
- }
65
- // because of the simultaneous request queues it's possible for the requests to go to the network layer out of order
66
- // if the requests are queued at a near identical time
67
- sort .Slice (requestRecords , func (i , j int ) bool {
68
- return requestRecords [i ].gsr .ID () < requestRecords [j ].gsr .ID ()
69
- })
70
- return requestRecords
71
- }
72
-
73
- func metadataForBlocks (blks []blocks.Block , present bool ) metadata.Metadata {
74
- md := make (metadata.Metadata , 0 , len (blks ))
75
- for _ , block := range blks {
76
- md = append (md , metadata.Item {
77
- Link : block .Cid (),
78
- BlockPresent : present ,
79
- })
80
- }
81
- return md
82
- }
83
-
84
- func encodedMetadataForBlocks (t * testing.T , blks []blocks.Block , present bool ) graphsync.ExtensionData {
85
- md := metadataForBlocks (blks , present )
86
- metadataEncoded , err := metadata .EncodeMetadata (md )
87
- require .NoError (t , err , "did not encode metadata" )
88
- return graphsync.ExtensionData {
89
- Name : graphsync .ExtensionMetadata ,
90
- Data : metadataEncoded ,
91
- }
92
- }
93
-
94
32
func TestNormalSimultaneousFetch (t * testing.T ) {
95
33
ctx := context .Background ()
96
34
td := newTestData (ctx , t )
@@ -106,6 +44,8 @@ func TestNormalSimultaneousFetch(t *testing.T) {
106
44
107
45
requestRecords := readNNetworkRequests (requestCtx , t , td .requestRecordChan , 2 )
108
46
47
+ td .tcm .AssertProtected (t , peers [0 ])
48
+ td .tcm .AssertProtectedWithTags (t , peers [0 ], requestRecords [0 ].gsr .ID ().Tag (), requestRecords [1 ].gsr .ID ().Tag ())
109
49
require .Equal (t , peers [0 ], requestRecords [0 ].p )
110
50
require .Equal (t , peers [0 ], requestRecords [1 ].p )
111
51
require .False (t , requestRecords [0 ].gsr .IsCancel ())
@@ -148,6 +88,10 @@ func TestNormalSimultaneousFetch(t *testing.T) {
148
88
td .blockChain .VerifyWholeChain (requestCtx , returnedResponseChan1 )
149
89
blockChain2 .VerifyResponseRange (requestCtx , returnedResponseChan2 , 0 , 3 )
150
90
91
+ td .tcm .AssertProtected (t , peers [0 ])
92
+ td .tcm .RefuteProtectedWithTags (t , peers [0 ], requestRecords [0 ].gsr .ID ().Tag ())
93
+ td .tcm .AssertProtectedWithTags (t , peers [0 ], requestRecords [1 ].gsr .ID ().Tag ())
94
+
151
95
moreBlocks := blockChain2 .RemainderBlocks (3 )
152
96
moreMetadata := metadataForBlocks (moreBlocks , true )
153
97
moreMetadataEncoded , err := metadata .EncodeMetadata (moreMetadata )
@@ -170,6 +114,8 @@ func TestNormalSimultaneousFetch(t *testing.T) {
170
114
blockChain2 .VerifyRemainder (requestCtx , returnedResponseChan2 , 3 )
171
115
testutil .VerifyEmptyErrors (requestCtx , t , returnedErrorChan1 )
172
116
testutil .VerifyEmptyErrors (requestCtx , t , returnedErrorChan2 )
117
+
118
+ td .tcm .RefuteProtected (t , peers [0 ])
173
119
}
174
120
175
121
func TestCancelRequestInProgress (t * testing.T ) {
@@ -187,6 +133,9 @@ func TestCancelRequestInProgress(t *testing.T) {
187
133
188
134
requestRecords := readNNetworkRequests (requestCtx , t , td .requestRecordChan , 2 )
189
135
136
+ td .tcm .AssertProtected (t , peers [0 ])
137
+ td .tcm .AssertProtectedWithTags (t , peers [0 ], requestRecords [0 ].gsr .ID ().Tag (), requestRecords [1 ].gsr .ID ().Tag ())
138
+
190
139
firstBlocks := td .blockChain .Blocks (0 , 3 )
191
140
firstMetadata := encodedMetadataForBlocks (t , firstBlocks , true )
192
141
firstResponses := []gsmsg.GraphSyncResponse {
@@ -224,6 +173,8 @@ func TestCancelRequestInProgress(t *testing.T) {
224
173
require .Len (t , errors , 1 )
225
174
_ , ok := errors [0 ].(graphsync.RequestClientCancelledErr )
226
175
require .True (t , ok )
176
+
177
+ td .tcm .RefuteProtected (t , peers [0 ])
227
178
}
228
179
func TestCancelRequestImperativeNoMoreBlocks (t * testing.T ) {
229
180
ctx := context .Background ()
@@ -246,6 +197,9 @@ func TestCancelRequestImperativeNoMoreBlocks(t *testing.T) {
246
197
247
198
requestRecords := readNNetworkRequests (requestCtx , t , td .requestRecordChan , 1 )
248
199
200
+ td .tcm .AssertProtected (t , peers [0 ])
201
+ td .tcm .AssertProtectedWithTags (t , peers [0 ], requestRecords [0 ].gsr .ID ().Tag ())
202
+
249
203
go func () {
250
204
firstBlocks := td .blockChain .Blocks (0 , 3 )
251
205
firstMetadata := encodedMetadataForBlocks (t , firstBlocks , true )
@@ -267,6 +221,8 @@ func TestCancelRequestImperativeNoMoreBlocks(t *testing.T) {
267
221
require .True (t , rr .gsr .IsCancel ())
268
222
require .Equal (t , requestRecords [0 ].gsr .ID (), rr .gsr .ID ())
269
223
224
+ td .tcm .RefuteProtected (t , peers [0 ])
225
+
270
226
errors := testutil .CollectErrors (requestCtx , t , returnedErrorChan1 )
271
227
require .Len (t , errors , 1 )
272
228
_ , ok := errors [0 ].(graphsync.RequestClientCancelledErr )
@@ -321,13 +277,17 @@ func TestFailedRequest(t *testing.T) {
321
277
returnedResponseChan , returnedErrorChan := td .requestManager .NewRequest (requestCtx , peers [0 ], td .blockChain .TipLink , td .blockChain .Selector ())
322
278
323
279
rr := readNNetworkRequests (requestCtx , t , td .requestRecordChan , 1 )[0 ]
280
+ td .tcm .AssertProtected (t , peers [0 ])
281
+ td .tcm .AssertProtectedWithTags (t , peers [0 ], rr .gsr .ID ().Tag ())
282
+
324
283
failedResponses := []gsmsg.GraphSyncResponse {
325
284
gsmsg .NewResponse (rr .gsr .ID (), graphsync .RequestFailedContentNotFound ),
326
285
}
327
286
td .requestManager .ProcessResponses (peers [0 ], failedResponses , nil )
328
287
329
288
testutil .VerifySingleTerminalError (requestCtx , t , returnedErrorChan )
330
289
testutil .VerifyEmptyResponse (requestCtx , t , returnedResponseChan )
290
+ td .tcm .RefuteProtected (t , peers [0 ])
331
291
}
332
292
333
293
func TestLocallyFulfilledFirstRequestFailsLater (t * testing.T ) {
@@ -962,10 +922,73 @@ func TestPauseResumeExternal(t *testing.T) {
962
922
testutil .VerifyEmptyErrors (ctx , t , returnedErrorChan )
963
923
}
964
924
925
+ type requestRecord struct {
926
+ gsr gsmsg.GraphSyncRequest
927
+ p peer.ID
928
+ }
929
+
930
+ type fakePeerHandler struct {
931
+ requestRecordChan chan requestRecord
932
+ }
933
+
934
+ func (fph * fakePeerHandler ) AllocateAndBuildMessage (p peer.ID , blkSize uint64 ,
935
+ requestBuilder func (b * gsmsg.Builder ), notifees []notifications.Notifee ) {
936
+ builder := gsmsg .NewBuilder (gsmsg .Topic (0 ))
937
+ requestBuilder (builder )
938
+ message , err := builder .Build ()
939
+ if err != nil {
940
+ panic (err )
941
+ }
942
+ fph .requestRecordChan <- requestRecord {
943
+ gsr : message .Requests ()[0 ],
944
+ p : p ,
945
+ }
946
+ }
947
+
948
+ func readNNetworkRequests (ctx context.Context ,
949
+ t * testing.T ,
950
+ requestRecordChan <- chan requestRecord ,
951
+ count int ) []requestRecord {
952
+ requestRecords := make ([]requestRecord , 0 , count )
953
+ for i := 0 ; i < count ; i ++ {
954
+ var rr requestRecord
955
+ testutil .AssertReceive (ctx , t , requestRecordChan , & rr , fmt .Sprintf ("did not receive request %d" , i ))
956
+ requestRecords = append (requestRecords , rr )
957
+ }
958
+ // because of the simultaneous request queues it's possible for the requests to go to the network layer out of order
959
+ // if the requests are queued at a near identical time
960
+ sort .Slice (requestRecords , func (i , j int ) bool {
961
+ return requestRecords [i ].gsr .ID () < requestRecords [j ].gsr .ID ()
962
+ })
963
+ return requestRecords
964
+ }
965
+
966
+ func metadataForBlocks (blks []blocks.Block , present bool ) metadata.Metadata {
967
+ md := make (metadata.Metadata , 0 , len (blks ))
968
+ for _ , block := range blks {
969
+ md = append (md , metadata.Item {
970
+ Link : block .Cid (),
971
+ BlockPresent : present ,
972
+ })
973
+ }
974
+ return md
975
+ }
976
+
977
+ func encodedMetadataForBlocks (t * testing.T , blks []blocks.Block , present bool ) graphsync.ExtensionData {
978
+ md := metadataForBlocks (blks , present )
979
+ metadataEncoded , err := metadata .EncodeMetadata (md )
980
+ require .NoError (t , err , "did not encode metadata" )
981
+ return graphsync.ExtensionData {
982
+ Name : graphsync .ExtensionMetadata ,
983
+ Data : metadataEncoded ,
984
+ }
985
+ }
986
+
965
987
type testData struct {
966
988
requestRecordChan chan requestRecord
967
989
fph * fakePeerHandler
968
990
fal * testloader.FakeAsyncLoader
991
+ tcm * testutil.TestConnManager
969
992
requestHooks * hooks.OutgoingRequestHooks
970
993
responseHooks * hooks.IncomingResponseHooks
971
994
blockHooks * hooks.IncomingBlockHooks
@@ -989,13 +1012,14 @@ func newTestData(ctx context.Context, t *testing.T) *testData {
989
1012
td .requestRecordChan = make (chan requestRecord , 3 )
990
1013
td .fph = & fakePeerHandler {td .requestRecordChan }
991
1014
td .fal = testloader .NewFakeAsyncLoader ()
1015
+ td .tcm = testutil .NewTestConnManager ()
992
1016
td .requestHooks = hooks .NewRequestHooks ()
993
1017
td .responseHooks = hooks .NewResponseHooks ()
994
1018
td .blockHooks = hooks .NewBlockHooks ()
995
1019
td .networkErrorListeners = listeners .NewNetworkErrorListeners ()
996
1020
td .taskqueue = taskqueue .NewTaskQueue (ctx )
997
1021
lsys := cidlink .DefaultLinkSystem ()
998
- td .requestManager = New (ctx , td .fal , lsys , td .requestHooks , td .responseHooks , td .networkErrorListeners , td .taskqueue )
1022
+ td .requestManager = New (ctx , td .fal , lsys , td .requestHooks , td .responseHooks , td .networkErrorListeners , td .taskqueue , td . tcm )
999
1023
td .executor = executor .NewExecutor (td .requestManager , td .blockHooks , td .fal .AsyncLoad )
1000
1024
td .requestManager .SetDelegate (td .fph )
1001
1025
td .requestManager .Startup ()
0 commit comments