@@ -1279,6 +1279,173 @@ func TestPeerBlockFilter(t *testing.T) {
1279
1279
}
1280
1280
}
1281
1281
1282
+ func TestPeerBlockFilterMutability (t * testing.T ) {
1283
+ ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
1284
+ defer cancel ()
1285
+
1286
+ // Generate a few keys
1287
+ keys := []string {"a" , "b" , "c" }
1288
+ blks := make ([]blocks.Block , 0 , len (keys ))
1289
+ for _ , letter := range keys {
1290
+ block := blocks .NewBlock ([]byte (letter ))
1291
+ blks = append (blks , block )
1292
+ }
1293
+
1294
+ // Generate a few partner peers
1295
+ partnerID := libp2ptest .RandPeerIDFatal (t )
1296
+
1297
+ // Setup the main peer
1298
+ fpt := & fakePeerTagger {}
1299
+ sl := NewTestScoreLedger (shortTerm , nil , clock .New ())
1300
+ bs := blockstore .NewBlockstore (dssync .MutexWrap (ds .NewMapDatastore ()))
1301
+ if err := bs .PutMany (ctx , blks ); err != nil {
1302
+ t .Fatal (err )
1303
+ }
1304
+
1305
+ filterAllowList := make (map [cid.Cid ]bool )
1306
+
1307
+ // use a single task worker so that the order of outgoing messages is deterministic
1308
+ engineTaskWorkerCount := 1
1309
+ e := newEngineForTesting (ctx , bs , 4 , engineTaskWorkerCount , defaults .BitswapMaxOutstandingBytesPerPeer , fpt , "localhost" , 0 , sl ,
1310
+ WithPeerBlockRequestFilter (func (p peer.ID , c cid.Cid ) bool {
1311
+ return filterAllowList [c ]
1312
+ }),
1313
+ )
1314
+ e .StartWorkers (ctx , process .WithTeardown (func () error { return nil }))
1315
+
1316
+ // Setup the test
1317
+ type testCaseEntry struct {
1318
+ allowList string
1319
+ wantBlks string
1320
+ wantHaves string
1321
+ sendDontHave bool
1322
+ }
1323
+
1324
+ type testCaseExp struct {
1325
+ blks string
1326
+ haves string
1327
+ dontHaves string
1328
+ }
1329
+
1330
+ type testCase struct {
1331
+ only bool
1332
+ wls []testCaseEntry
1333
+ exps []testCaseExp
1334
+ }
1335
+
1336
+ testCases := []testCase {
1337
+ {
1338
+ wls : []testCaseEntry {
1339
+ {
1340
+ // Peer has no accesses
1341
+ allowList : "" ,
1342
+ wantBlks : "a" ,
1343
+ sendDontHave : true ,
1344
+ },
1345
+ {
1346
+ // Then Peer is allowed access to a
1347
+ allowList : "a" ,
1348
+ wantBlks : "a" ,
1349
+ sendDontHave : true ,
1350
+ },
1351
+ // If you run the code below, the test will fail with a sigsev,
1352
+ // because the response is nil, the node pretty much ignores the request.
1353
+ // {
1354
+ // // Peer has no accesses
1355
+ // allowList: "",
1356
+ // wantBlks: "a",
1357
+ // sendDontHave: true,
1358
+ // },
1359
+ },
1360
+ exps : []testCaseExp {
1361
+ {
1362
+ dontHaves : "a" ,
1363
+ },
1364
+ {
1365
+ blks : "a" ,
1366
+ },
1367
+ {
1368
+ dontHaves : "a" ,
1369
+ },
1370
+ },
1371
+ },
1372
+ {
1373
+ wls : []testCaseEntry {
1374
+ {
1375
+ // Peer has access to bc
1376
+ allowList : "bc" ,
1377
+ wantHaves : "bc" ,
1378
+ sendDontHave : true ,
1379
+ },
1380
+ {
1381
+ // Then Peer loses access to b
1382
+ allowList : "c" ,
1383
+ // Note: We request block here to force a response from the node
1384
+ wantBlks : "bc" ,
1385
+ sendDontHave : true ,
1386
+ },
1387
+ },
1388
+ exps : []testCaseExp {
1389
+ {
1390
+ haves : "bc" ,
1391
+ },
1392
+ {
1393
+ blks : "c" ,
1394
+ dontHaves : "b" ,
1395
+ },
1396
+ },
1397
+ },
1398
+ }
1399
+
1400
+ var onlyTestCases []testCase
1401
+ for _ , testCase := range testCases {
1402
+ if testCase .only {
1403
+ onlyTestCases = append (onlyTestCases , testCase )
1404
+ }
1405
+ }
1406
+ if len (onlyTestCases ) > 0 {
1407
+ testCases = onlyTestCases
1408
+ }
1409
+
1410
+ for i , testCase := range testCases {
1411
+ for j := range testCase .wls {
1412
+ wl := testCase .wls [j ]
1413
+ exp := testCase .exps [j ]
1414
+
1415
+ // Create wants requests
1416
+ t .Logf ("test case %v, %v: allow-list '%s' / want-blocks '%s' / want-haves '%s' / sendDontHave %t" ,
1417
+ i , j , wl .allowList , wl .wantBlks , wl .wantHaves , wl .sendDontHave )
1418
+
1419
+ allowList := strings .Split (wl .allowList , "" )
1420
+ wantBlks := strings .Split (wl .wantBlks , "" )
1421
+ wantHaves := strings .Split (wl .wantHaves , "" )
1422
+
1423
+ // Update the allow list
1424
+ filterAllowList = make (map [cid.Cid ]bool )
1425
+ for _ , letter := range allowList {
1426
+ block := blocks .NewBlock ([]byte (letter ))
1427
+ filterAllowList [block .Cid ()] = true
1428
+ }
1429
+
1430
+ // Send the request
1431
+ partnerWantBlocksHaves (e , wantBlks , wantHaves , wl .sendDontHave , partnerID )
1432
+
1433
+ // Check result
1434
+ next := <- e .Outbox ()
1435
+ envelope := <- next
1436
+
1437
+ expBlks := strings .Split (exp .blks , "" )
1438
+ expHaves := strings .Split (exp .haves , "" )
1439
+ expDontHaves := strings .Split (exp .dontHaves , "" )
1440
+
1441
+ err := checkOutput (t , e , envelope , expBlks , expHaves , expDontHaves )
1442
+ if err != nil {
1443
+ t .Fatal (err )
1444
+ }
1445
+ }
1446
+ }
1447
+ }
1448
+
1282
1449
func TestTaggingPeers (t * testing.T ) {
1283
1450
ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
1284
1451
defer cancel ()
0 commit comments