Skip to content

Commit 88beadf

Browse files
authored
Stop using go-ipfs-blocksutil (#656)
- Replace use of go-ipfs-blocksutil with go-test/random - Remove remaining references to go-blockservice
1 parent 635e175 commit 88beadf

File tree

13 files changed

+75
-110
lines changed

13 files changed

+75
-110
lines changed

bitswap/bitswap_test.go

+15-23
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ import (
1919
blocks "github.com/ipfs/go-block-format"
2020
cid "github.com/ipfs/go-cid"
2121
detectrace "github.com/ipfs/go-detect-race"
22-
blocksutil "github.com/ipfs/go-ipfs-blocksutil"
2322
delay "github.com/ipfs/go-ipfs-delay"
2423
ipld "github.com/ipfs/go-ipld-format"
24+
"github.com/ipfs/go-test/random"
2525
tu "github.com/libp2p/go-libp2p-testing/etc"
2626
p2ptestutil "github.com/libp2p/go-libp2p-testing/netutil"
2727
peer "github.com/libp2p/go-libp2p/core/peer"
2828
)
2929

30+
const blockSize = 4
31+
3032
func isCI() bool {
3133
// https://github.blog/changelog/2020-04-15-github-actions-sets-the-ci-environment-variable-to-true/
3234
return os.Getenv("CI") != ""
@@ -52,9 +54,7 @@ func TestClose(t *testing.T) {
5254
vnet := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
5355
ig := testinstance.NewTestInstanceGenerator(vnet, nil, nil)
5456
defer ig.Close()
55-
bgen := blocksutil.NewBlockGenerator()
56-
57-
block := bgen.Next()
57+
block := random.BlocksOfSize(1, blockSize)[0]
5858
bitswap := ig.Next()
5959

6060
bitswap.Exchange.Close()
@@ -187,7 +187,6 @@ func TestUnwantedBlockNotAdded(t *testing.T) {
187187
func TestPendingBlockAdded(t *testing.T) {
188188
ctx := context.Background()
189189
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
190-
bg := blocksutil.NewBlockGenerator()
191190
sessionBroadcastWantCapacity := 4
192191

193192
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
@@ -202,7 +201,7 @@ func TestPendingBlockAdded(t *testing.T) {
202201
// Request enough blocks to exceed the session's broadcast want list
203202
// capacity (by one block). The session will put the remaining block
204203
// into the "tofetch" queue
205-
blks := bg.Blocks(sessionBroadcastWantCapacity + 1)
204+
blks := random.BlocksOfSize(sessionBroadcastWantCapacity+1, blockSize)
206205
ks := make([]cid.Cid, 0, len(blks))
207206
for _, b := range blks {
208207
ks = append(ks, b.Cid())
@@ -285,10 +284,9 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) {
285284
bitswap.MaxOutstandingBytesPerPeer(1 << 20),
286285
})
287286
defer ig.Close()
288-
bg := blocksutil.NewBlockGenerator()
289287

290288
instances := ig.Instances(numInstances)
291-
blocks := bg.Blocks(numBlocks)
289+
blocks := random.BlocksOfSize(numBlocks, blockSize)
292290

293291
t.Log("Give the blocks to the first instance")
294292

@@ -338,7 +336,6 @@ func TestSendToWantingPeer(t *testing.T) {
338336
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
339337
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
340338
defer ig.Close()
341-
bg := blocksutil.NewBlockGenerator()
342339

343340
peers := ig.Instances(2)
344341
peerA := peers[0]
@@ -349,7 +346,7 @@ func TestSendToWantingPeer(t *testing.T) {
349346

350347
waitTime := time.Second * 5
351348

352-
alpha := bg.Next()
349+
alpha := random.BlocksOfSize(1, blockSize)[0]
353350
// peerA requests and waits for block alpha
354351
ctx, cancel := context.WithTimeout(context.Background(), waitTime)
355352
defer cancel()
@@ -409,12 +406,11 @@ func TestBasicBitswap(t *testing.T) {
409406
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
410407
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
411408
defer ig.Close()
412-
bg := blocksutil.NewBlockGenerator()
413409

414410
t.Log("Test a one node trying to get one block from another")
415411

416412
instances := ig.Instances(3)
417-
blocks := bg.Blocks(1)
413+
blocks := random.BlocksOfSize(1, blockSize)
418414

419415
// First peer has block
420416
addBlock(t, context.Background(), instances[0], blocks[0])
@@ -481,12 +477,11 @@ func TestDoubleGet(t *testing.T) {
481477
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
482478
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
483479
defer ig.Close()
484-
bg := blocksutil.NewBlockGenerator()
485480

486481
t.Log("Test a one node trying to get one block from another")
487482

488483
instances := ig.Instances(2)
489-
blocks := bg.Blocks(1)
484+
blocks := random.BlocksOfSize(1, blockSize)
490485

491486
// NOTE: A race condition can happen here where these GetBlocks requests go
492487
// through before the peers even get connected. This is okay, bitswap
@@ -546,12 +541,11 @@ func TestWantlistCleanup(t *testing.T) {
546541
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
547542
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
548543
defer ig.Close()
549-
bg := blocksutil.NewBlockGenerator()
550544

551545
instances := ig.Instances(2)
552546
instance := instances[0]
553547
bswap := instance.Exchange
554-
blocks := bg.Blocks(20)
548+
blocks := random.BlocksOfSize(20, blockSize)
555549

556550
var keys []cid.Cid
557551
for _, b := range blocks {
@@ -668,12 +662,11 @@ func TestBitswapLedgerOneWay(t *testing.T) {
668662
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
669663
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
670664
defer ig.Close()
671-
bg := blocksutil.NewBlockGenerator()
672665

673666
t.Log("Test ledgers match when one peer sends block to another")
674667

675668
instances := ig.Instances(2)
676-
blocks := bg.Blocks(1)
669+
blocks := random.BlocksOfSize(1, blockSize)
677670
addBlock(t, context.Background(), instances[0], blocks[0])
678671

679672
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
@@ -693,12 +686,12 @@ func TestBitswapLedgerOneWay(t *testing.T) {
693686
}
694687

695688
// check that receipts have intended values
696-
ratest := newReceipt(1, 0, 1)
689+
ratest := newReceipt(blockSize, 0, 1)
697690
err = assertLedgerEqual(ratest, ra)
698691
if err != nil {
699692
t.Fatal(err)
700693
}
701-
rbtest := newReceipt(0, 1, 1)
694+
rbtest := newReceipt(0, blockSize, 1)
702695
err = assertLedgerEqual(rbtest, rb)
703696
if err != nil {
704697
t.Fatal(err)
@@ -717,12 +710,11 @@ func TestBitswapLedgerTwoWay(t *testing.T) {
717710
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
718711
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
719712
defer ig.Close()
720-
bg := blocksutil.NewBlockGenerator()
721713

722714
t.Log("Test ledgers match when two peers send one block to each other")
723715

724716
instances := ig.Instances(2)
725-
blocks := bg.Blocks(2)
717+
blocks := random.BlocksOfSize(2, blockSize)
726718
addBlock(t, context.Background(), instances[0], blocks[0])
727719
addBlock(t, context.Background(), instances[1], blocks[1])
728720

@@ -750,7 +742,7 @@ func TestBitswapLedgerTwoWay(t *testing.T) {
750742
}
751743

752744
// check that receipts have intended values
753-
rtest := newReceipt(1, 1, 2)
745+
rtest := newReceipt(blockSize, blockSize, 2)
754746
err = assertLedgerEqual(rtest, ra)
755747
if err != nil {
756748
t.Fatal(err)

bitswap/client/bitswap_with_sessions_test.go

+12-19
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ import (
1515
mockrouting "github.com/ipfs/boxo/routing/mock"
1616
blocks "github.com/ipfs/go-block-format"
1717
cid "github.com/ipfs/go-cid"
18-
blocksutil "github.com/ipfs/go-ipfs-blocksutil"
1918
delay "github.com/ipfs/go-ipfs-delay"
19+
"github.com/ipfs/go-test/random"
2020
tu "github.com/libp2p/go-libp2p-testing/etc"
2121
"github.com/libp2p/go-libp2p/core/peer"
2222
)
2323

24+
const blockSize = 4
25+
2426
func getVirtualNetwork() tn.Network {
2527
// FIXME: the tests are really sensitive to the network delay. fix them to work
2628
// well under varying conditions
@@ -46,9 +48,8 @@ func TestBasicSessions(t *testing.T) {
4648
vnet := getVirtualNetwork()
4749
ig := testinstance.NewTestInstanceGenerator(vnet, nil, nil)
4850
defer ig.Close()
49-
bgen := blocksutil.NewBlockGenerator()
5051

51-
block := bgen.Next()
52+
block := random.BlocksOfSize(1, blockSize)[0]
5253
inst := ig.Instances(2)
5354

5455
a := inst[0]
@@ -113,12 +114,11 @@ func TestSessionBetweenPeers(t *testing.T) {
113114
vnet := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(time.Millisecond))
114115
ig := testinstance.NewTestInstanceGenerator(vnet, nil, []bitswap.Option{bitswap.SetSimulateDontHavesOnTimeout(false)})
115116
defer ig.Close()
116-
bgen := blocksutil.NewBlockGenerator()
117117

118118
inst := ig.Instances(10)
119119

120120
// Add 101 blocks to Peer A
121-
blks := bgen.Blocks(101)
121+
blks := random.BlocksOfSize(101, blockSize)
122122
if err := inst[0].Blockstore().PutMany(ctx, blks); err != nil {
123123
t.Fatal(err)
124124
}
@@ -173,12 +173,11 @@ func TestSessionSplitFetch(t *testing.T) {
173173
vnet := getVirtualNetwork()
174174
ig := testinstance.NewTestInstanceGenerator(vnet, nil, nil)
175175
defer ig.Close()
176-
bgen := blocksutil.NewBlockGenerator()
177176

178177
inst := ig.Instances(11)
179178

180179
// Add 10 distinct blocks to each of 10 peers
181-
blks := bgen.Blocks(100)
180+
blks := random.BlocksOfSize(100, blockSize)
182181
for i := 0; i < 10; i++ {
183182
if err := inst[i].Blockstore().PutMany(ctx, blks[i*10:(i+1)*10]); err != nil {
184183
t.Fatal(err)
@@ -217,12 +216,11 @@ func TestFetchNotConnected(t *testing.T) {
217216
vnet := getVirtualNetwork()
218217
ig := testinstance.NewTestInstanceGenerator(vnet, nil, []bitswap.Option{bitswap.ProviderSearchDelay(10 * time.Millisecond)})
219218
defer ig.Close()
220-
bgen := blocksutil.NewBlockGenerator()
221219

222220
other := ig.Next()
223221

224222
// Provide 10 blocks on Peer A
225-
blks := bgen.Blocks(10)
223+
blks := random.BlocksOfSize(10, blockSize)
226224
for _, block := range blks {
227225
addBlock(t, ctx, other, block)
228226
}
@@ -263,14 +261,13 @@ func TestFetchAfterDisconnect(t *testing.T) {
263261
bitswap.RebroadcastDelay(delay.Fixed(15 * time.Millisecond)),
264262
})
265263
defer ig.Close()
266-
bgen := blocksutil.NewBlockGenerator()
267264

268265
inst := ig.Instances(2)
269266
peerA := inst[0]
270267
peerB := inst[1]
271268

272269
// Provide 5 blocks on Peer A
273-
blks := bgen.Blocks(10)
270+
blks := random.BlocksOfSize(10, blockSize)
274271
var cids []cid.Cid
275272
for _, blk := range blks {
276273
cids = append(cids, blk.Cid())
@@ -338,9 +335,8 @@ func TestInterestCacheOverflow(t *testing.T) {
338335
vnet := getVirtualNetwork()
339336
ig := testinstance.NewTestInstanceGenerator(vnet, nil, nil)
340337
defer ig.Close()
341-
bgen := blocksutil.NewBlockGenerator()
342338

343-
blks := bgen.Blocks(2049)
339+
blks := random.BlocksOfSize(2049, blockSize)
344340
inst := ig.Instances(2)
345341

346342
a := inst[0]
@@ -388,9 +384,8 @@ func TestPutAfterSessionCacheEvict(t *testing.T) {
388384
vnet := getVirtualNetwork()
389385
ig := testinstance.NewTestInstanceGenerator(vnet, nil, nil)
390386
defer ig.Close()
391-
bgen := blocksutil.NewBlockGenerator()
392387

393-
blks := bgen.Blocks(2500)
388+
blks := random.BlocksOfSize(2500, blockSize)
394389
inst := ig.Instances(1)
395390

396391
a := inst[0]
@@ -426,9 +421,8 @@ func TestMultipleSessions(t *testing.T) {
426421
vnet := getVirtualNetwork()
427422
ig := testinstance.NewTestInstanceGenerator(vnet, nil, nil)
428423
defer ig.Close()
429-
bgen := blocksutil.NewBlockGenerator()
430424

431-
blk := bgen.Blocks(1)[0]
425+
blk := random.BlocksOfSize(1, blockSize)[0]
432426
inst := ig.Instances(2)
433427

434428
a := inst[0]
@@ -467,9 +461,8 @@ func TestWantlistClearsOnCancel(t *testing.T) {
467461
vnet := getVirtualNetwork()
468462
ig := testinstance.NewTestInstanceGenerator(vnet, nil, nil)
469463
defer ig.Close()
470-
bgen := blocksutil.NewBlockGenerator()
471464

472-
blks := bgen.Blocks(10)
465+
blks := random.BlocksOfSize(10, blockSize)
473466
var cids []cid.Cid
474467
for _, blk := range blks {
475468
cids = append(cids, blk.Cid())

bitswap/client/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ func (bs *Client) IsOnline() bool {
499499
// block requests in a row. The session returned will have it's own GetBlocks
500500
// method, but the session will use the fact that the requests are related to
501501
// be more efficient in its requests to peers. If you are using a session
502-
// from go-blockservice, it will create a bitswap session automatically.
502+
// from blockservice, it will create a bitswap session automatically.
503503
func (bs *Client) NewSession(ctx context.Context) exchange.Fetcher {
504504
ctx, span := internal.StartSpan(ctx, "NewSession")
505505
defer span.End()

bitswap/client/internal/notifications/notifications_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import (
88

99
blocks "github.com/ipfs/go-block-format"
1010
cid "github.com/ipfs/go-cid"
11-
blocksutil "github.com/ipfs/go-ipfs-blocksutil"
11+
"github.com/ipfs/go-test/random"
1212
"github.com/libp2p/go-libp2p/core/peer"
1313
)
1414

15+
const blockSize = 4
16+
1517
func TestDuplicates(t *testing.T) {
1618
var zero peer.ID // this test doesn't check the peer id
1719

@@ -152,13 +154,12 @@ func TestCarryOnWhenDeadlineExpires(t *testing.T) {
152154
func TestDoesNotDeadLockIfContextCancelledBeforePublish(t *testing.T) {
153155
var zero peer.ID // this test doesn't check the peer id
154156

155-
g := blocksutil.NewBlockGenerator()
156157
ctx, cancel := context.WithCancel(context.Background())
157158
n := New()
158159
defer n.Shutdown()
159160

160161
t.Log("generate a large number of blocks. exceed default buffer")
161-
bs := g.Blocks(1000)
162+
bs := random.BlocksOfSize(1000, blockSize)
162163
ks := func() []cid.Cid {
163164
var keys []cid.Cid
164165
for _, b := range bs {

0 commit comments

Comments
 (0)