@@ -1112,6 +1112,88 @@ func TestTaskComparator(t *testing.T) {
1112
1112
}
1113
1113
}
1114
1114
1115
+ func TestPeerBlockFilter (t * testing.T ) {
1116
+ ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
1117
+ defer cancel ()
1118
+
1119
+ // Generate a few keys
1120
+ keys := []string {"a" , "b" , "c" }
1121
+ cids := make (map [cid.Cid ]int )
1122
+ blks := make ([]blocks.Block , 0 , len (keys ))
1123
+ for i , letter := range keys {
1124
+ block := blocks .NewBlock ([]byte (letter ))
1125
+ blks = append (blks , block )
1126
+ cids [block .Cid ()] = i
1127
+ }
1128
+
1129
+ // Generate a few peers
1130
+ peerIDs := make ([]peer.ID , len (keys ))
1131
+ for _ , i := range cids {
1132
+ peerID := libp2ptest .RandPeerIDFatal (t )
1133
+ peerIDs [i ] = peerID
1134
+ }
1135
+
1136
+ // Setup the peer
1137
+ fpt := & fakePeerTagger {}
1138
+ sl := NewTestScoreLedger (shortTerm , nil , clock .New ())
1139
+ bs := blockstore .NewBlockstore (dssync .MutexWrap (ds .NewMapDatastore ()))
1140
+ if err := bs .PutMany (ctx , blks ); err != nil {
1141
+ t .Fatal (err )
1142
+ }
1143
+
1144
+ // use a single task worker so that the order of outgoing messages is deterministic
1145
+ engineTaskWorkerCount := 1
1146
+ e := newEngineForTesting (ctx , bs , 4 , engineTaskWorkerCount , defaults .BitswapMaxOutstandingBytesPerPeer , fpt , "localhost" , 0 , sl ,
1147
+ // if this Option is omitted, the test fails
1148
+ WithPeerBlockRequestFilter (func (p peer.ID , c cid.Cid ) bool {
1149
+ // peer 0 has access to everything
1150
+ if p == peerIDs [0 ] {
1151
+ return true
1152
+ }
1153
+ // peer 1 has access to key b and c
1154
+ if p == peerIDs [1 ] {
1155
+ return blks [1 ].Cid ().Equals (c ) || blks [2 ].Cid ().Equals (c )
1156
+ }
1157
+ // peer 2 and other have access to key c
1158
+ return blks [2 ].Cid ().Equals (c )
1159
+ }),
1160
+ )
1161
+ e .StartWorkers (ctx , process .WithTeardown (func () error { return nil }))
1162
+
1163
+ // Create wants requests
1164
+ for _ , peerID := range peerIDs {
1165
+ partnerWantBlocks (e , keys , peerID )
1166
+ }
1167
+
1168
+ // check that outgoing messages are sent with the correct content
1169
+ checkPeer := func (peerIndex int , expectedBlocks []blocks.Block ) {
1170
+ next := <- e .Outbox ()
1171
+ envelope := <- next
1172
+
1173
+ peerID := peerIDs [peerIndex ]
1174
+ responseBlocks := envelope .Message .Blocks ()
1175
+
1176
+ if peerID != envelope .Peer {
1177
+ t .Errorf ("(Peer%v) expected message for peer ID %#v but instead got message for peer ID %#v" , peerIndex , peerID , envelope .Peer )
1178
+ }
1179
+
1180
+ if len (responseBlocks ) != len (expectedBlocks ) {
1181
+ t .Errorf ("(Peer%v) expected %v block in response but instead got %v" , peerIndex , len (expectedBlocks ), len (responseBlocks ))
1182
+ }
1183
+
1184
+ // TODO: figure out how to make this test deterministic (sort or use a set?)
1185
+ // for i, expectedBlock := range expectedBlocks {
1186
+ // if responseBlocks[i].Cid() != expectedBlock.Cid() {
1187
+ // t.Errorf("(Peer%v) expected block with CID %#v but instead got block with CID %#v", peerIndex, expectedBlock.Cid(), responseBlocks[i].Cid())
1188
+ // }
1189
+ // }
1190
+ }
1191
+
1192
+ checkPeer (0 , blks [0 :3 ])
1193
+ checkPeer (1 , blks [1 :3 ])
1194
+ checkPeer (2 , blks [2 :3 ])
1195
+ }
1196
+
1115
1197
func TestTaggingPeers (t * testing.T ) {
1116
1198
ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
1117
1199
defer cancel ()
0 commit comments