bitswap_test.go 15.4 KB
Newer Older
1
package bitswap
Brian Tiger Chow's avatar
Brian Tiger Chow committed
2 3

import (
4
	"bytes"
5
	"context"
Jeromy's avatar
Jeromy committed
6
	"fmt"
7
	"sync"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
8 9 10
	"testing"
	"time"

11
	blocks "github.com/ipfs/go-ipfs/blocks"
jbenet's avatar
jbenet committed
12
	blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
13
	blocksutil "github.com/ipfs/go-ipfs/blocks/blocksutil"
14
	decision "github.com/ipfs/go-ipfs/exchange/bitswap/decision"
15 16 17
	tn "github.com/ipfs/go-ipfs/exchange/bitswap/testnet"
	mockrouting "github.com/ipfs/go-ipfs/routing/mock"
	delay "github.com/ipfs/go-ipfs/thirdparty/delay"
18 19 20
	travis "github.com/ipfs/go-ipfs/thirdparty/testutil/ci/travis"

	detectrace "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-detect-race"
21

22 23
	cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
	p2ptestutil "gx/ipfs/QmcCgouQ5iXfmxmVNc1fpXLacRSPMNHx4tzqDpou6XNvvd/go-libp2p-netutil"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
24 25
)

Brian Tiger Chow's avatar
Brian Tiger Chow committed
26 27
// FIXME the tests are really sensitive to the network delay. fix them to work
// well under varying conditions
28 29
const kNetworkDelay = 0 * time.Millisecond

30 31 32 33
func getVirtualNetwork() tn.Network {
	return tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
}

34
func TestClose(t *testing.T) {
35
	vnet := getVirtualNetwork()
36
	sesgen := NewTestSessionGenerator(vnet)
Jeromy's avatar
Jeromy committed
37
	defer sesgen.Close()
38
	bgen := blocksutil.NewBlockGenerator()
39 40 41 42

	block := bgen.Next()
	bitswap := sesgen.Next()

43
	bitswap.Exchange.Close()
44
	bitswap.Exchange.GetBlock(context.Background(), block.Cid())
45 46
}

Jeromy's avatar
Jeromy committed
47 48 49 50 51 52 53 54 55
func TestProviderForKeyButNetworkCannotFind(t *testing.T) { // TODO revisit this

	rs := mockrouting.NewServer()
	net := tn.VirtualNetwork(rs, delay.Fixed(kNetworkDelay))
	g := NewTestSessionGenerator(net)
	defer g.Close()

	block := blocks.NewBlock([]byte("block"))
	pinfo := p2ptestutil.RandTestBogusIdentityOrFatal(t)
56
	rs.Client(pinfo).Provide(context.Background(), block.Cid(), true) // but not on network
Jeromy's avatar
Jeromy committed
57 58 59 60

	solo := g.Next()
	defer solo.Exchange.Close()

rht's avatar
rht committed
61 62
	ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
	defer cancel()
63
	_, err := solo.Exchange.GetBlock(ctx, block.Cid())
Jeromy's avatar
Jeromy committed
64 65 66 67 68 69

	if err != context.DeadlineExceeded {
		t.Fatal("Expected DeadlineExceeded error")
	}
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
70 71
func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {

72
	net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
73
	block := blocks.NewBlock([]byte("block"))
74
	g := NewTestSessionGenerator(net)
Jeromy's avatar
Jeromy committed
75
	defer g.Close()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
76

77 78
	peers := g.Instances(2)
	hasBlock := peers[0]
Jeromy's avatar
Jeromy committed
79
	defer hasBlock.Exchange.Close()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
80

81
	if err := hasBlock.Exchange.HasBlock(block); err != nil {
82 83
		t.Fatal(err)
	}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
84

85
	wantsBlock := peers[1]
Jeromy's avatar
Jeromy committed
86
	defer wantsBlock.Exchange.Close()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
87

rht's avatar
rht committed
88 89
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
90
	received, err := wantsBlock.Exchange.GetBlock(ctx, block.Cid())
Brian Tiger Chow's avatar
Brian Tiger Chow committed
91 92 93 94
	if err != nil {
		t.Log(err)
		t.Fatal("Expected to succeed")
	}
95

Jeromy's avatar
Jeromy committed
96
	if !bytes.Equal(block.RawData(), received.RawData()) {
97 98
		t.Fatal("Data doesn't match")
	}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
99 100
}

101
func TestLargeSwarm(t *testing.T) {
102 103 104
	if testing.Short() {
		t.SkipNow()
	}
105
	numInstances := 100
106
	numBlocks := 2
107 108 109 110
	if detectrace.WithRace() {
		// when running with the race detector, 500 instances launches
		// well over 8k goroutines. This hits a race detector limit.
		numInstances = 100
111 112
	} else if travis.IsRunning() {
		numInstances = 200
113 114 115
	} else {
		t.Parallel()
	}
116 117
	PerformDistributionTest(t, numInstances, numBlocks)
}
118

119 120 121
func TestLargeFile(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
122
	}
123 124 125 126 127

	if !travis.IsRunning() {
		t.Parallel()
	}

128 129 130
	numInstances := 10
	numBlocks := 100
	PerformDistributionTest(t, numInstances, numBlocks)
131 132
}

133 134 135 136 137 138 139 140 141 142 143 144
func TestLargeFileNoRebroadcast(t *testing.T) {
	rbd := rebroadcastDelay.Get()
	rebroadcastDelay.Set(time.Hour * 24 * 365 * 10) // ten years should be long enough
	if testing.Short() {
		t.SkipNow()
	}
	numInstances := 10
	numBlocks := 100
	PerformDistributionTest(t, numInstances, numBlocks)
	rebroadcastDelay.Set(rbd)
}

145 146 147 148 149 150 151 152 153
func TestLargeFileTwoPeers(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	numInstances := 2
	numBlocks := 100
	PerformDistributionTest(t, numInstances, numBlocks)
}

154
func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) {
155
	ctx := context.Background()
156 157 158
	if testing.Short() {
		t.SkipNow()
	}
159
	net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
160
	sg := NewTestSessionGenerator(net)
Jeromy's avatar
Jeromy committed
161
	defer sg.Close()
162
	bg := blocksutil.NewBlockGenerator()
163 164 165 166 167 168

	instances := sg.Instances(numInstances)
	blocks := bg.Blocks(numBlocks)

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

169 170 171 172 173 174 175 176 177 178 179 180 181
	nump := len(instances) - 1
	// assert we're properly connected
	for _, inst := range instances {
		peers := inst.Exchange.wm.ConnectedPeers()
		for i := 0; i < 10 && len(peers) != nump; i++ {
			time.Sleep(time.Millisecond * 50)
			peers = inst.Exchange.wm.ConnectedPeers()
		}
		if len(peers) != nump {
			t.Fatal("not enough peers connected to instance")
		}
	}

182
	var blkeys []*cid.Cid
183 184
	first := instances[0]
	for _, b := range blocks {
185
		blkeys = append(blkeys, b.Cid())
186
		first.Exchange.HasBlock(b)
187 188 189 190
	}

	t.Log("Distribute!")

Jeromy's avatar
Jeromy committed
191
	wg := sync.WaitGroup{}
192 193
	errs := make(chan error)

194
	for _, inst := range instances[1:] {
Jeromy's avatar
Jeromy committed
195 196 197
		wg.Add(1)
		go func(inst Instance) {
			defer wg.Done()
198
			outch, err := inst.Exchange.GetBlocks(ctx, blkeys)
Jeromy's avatar
Jeromy committed
199
			if err != nil {
200
				errs <- err
Jeromy's avatar
Jeromy committed
201 202 203 204
			}
			for _ = range outch {
			}
		}(inst)
205
	}
206 207 208 209 210 211 212 213 214 215 216

	go func() {
		wg.Wait()
		close(errs)
	}()

	for err := range errs {
		if err != nil {
			t.Fatal(err)
		}
	}
217 218 219 220 221

	t.Log("Verify!")

	for _, inst := range instances {
		for _, b := range blocks {
222
			if _, err := inst.Blockstore().Get(b.Cid()); err != nil {
223 224 225 226 227 228
				t.Fatal(err)
			}
		}
	}
}

229
func getOrFail(bitswap Instance, b blocks.Block, t *testing.T, wg *sync.WaitGroup) {
230 231
	if _, err := bitswap.Blockstore().Get(b.Cid()); err != nil {
		_, err := bitswap.Exchange.GetBlock(context.Background(), b.Cid())
232 233 234 235 236 237 238
		if err != nil {
			t.Fatal(err)
		}
	}
	wg.Done()
}

239
// TODO simplify this test. get to the _essence_!
240
func TestSendToWantingPeer(t *testing.T) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
241 242 243 244
	if testing.Short() {
		t.SkipNow()
	}

245
	net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
246
	sg := NewTestSessionGenerator(net)
Jeromy's avatar
Jeromy committed
247
	defer sg.Close()
248
	bg := blocksutil.NewBlockGenerator()
249

Brian Tiger Chow's avatar
Brian Tiger Chow committed
250 251
	prev := rebroadcastDelay.Set(time.Second / 2)
	defer func() { rebroadcastDelay.Set(prev) }()
252

253 254 255
	peers := sg.Instances(2)
	peerA := peers[0]
	peerB := peers[1]
256

257 258
	t.Logf("Session %v\n", peerA.Peer)
	t.Logf("Session %v\n", peerB.Peer)
259

260
	waitTime := time.Second * 5
261

262 263
	alpha := bg.Next()
	// peerA requests and waits for block alpha
264
	ctx, cancel := context.WithTimeout(context.Background(), waitTime)
rht's avatar
rht committed
265
	defer cancel()
266
	alphaPromise, err := peerA.Exchange.GetBlocks(ctx, []*cid.Cid{alpha.Cid()})
267
	if err != nil {
268 269
		t.Fatal(err)
	}
270

271
	// peerB announces to the network that he has block alpha
272
	err = peerB.Exchange.HasBlock(alpha)
273
	if err != nil {
274 275
		t.Fatal(err)
	}
276

277 278 279 280
	// At some point, peerA should get alpha (or timeout)
	blkrecvd, ok := <-alphaPromise
	if !ok {
		t.Fatal("context timed out and broke promise channel!")
281
	}
282

283
	if !blkrecvd.Cid().Equals(alpha.Cid()) {
284
		t.Fatal("Wrong block!")
285
	}
286

287
}
Jeromy's avatar
Jeromy committed
288

jbenet's avatar
jbenet committed
289 290 291 292 293 294
func TestEmptyKey(t *testing.T) {
	net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
	sg := NewTestSessionGenerator(net)
	defer sg.Close()
	bs := sg.Instances(1)[0].Exchange

Jeromy's avatar
Jeromy committed
295 296 297
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	defer cancel()

298
	_, err := bs.GetBlock(ctx, nil)
jbenet's avatar
jbenet committed
299 300 301 302 303
	if err != blockstore.ErrNotFound {
		t.Error("empty str key should return ErrNotFound")
	}
}

Jeromy's avatar
Jeromy committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
func assertStat(st *Stat, sblks, rblks int, sdata, rdata uint64) error {
	if sblks != st.BlocksSent {
		return fmt.Errorf("mismatch in blocks sent: %d vs %d", sblks, st.BlocksSent)
	}

	if rblks != st.BlocksReceived {
		return fmt.Errorf("mismatch in blocks recvd: %d vs %d", rblks, st.BlocksReceived)
	}

	if sdata != st.DataSent {
		return fmt.Errorf("mismatch in data sent: %d vs %d", sdata, st.DataSent)
	}

	if rdata != st.DataReceived {
		return fmt.Errorf("mismatch in data recvd: %d vs %d", rdata, st.DataReceived)
	}
	return nil
}

Jeromy's avatar
Jeromy committed
323
func TestBasicBitswap(t *testing.T) {
324
	net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
325
	sg := NewTestSessionGenerator(net)
326
	defer sg.Close()
Jeromy's avatar
Jeromy committed
327 328
	bg := blocksutil.NewBlockGenerator()

329
	t.Log("Test a one node trying to get one block from another")
Jeromy's avatar
Jeromy committed
330 331 332

	instances := sg.Instances(2)
	blocks := bg.Blocks(1)
333
	err := instances[0].Exchange.HasBlock(blocks[0])
Jeromy's avatar
Jeromy committed
334 335 336 337
	if err != nil {
		t.Fatal(err)
	}

338
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
rht's avatar
rht committed
339
	defer cancel()
340
	blk, err := instances[1].Exchange.GetBlock(ctx, blocks[0].Cid())
Jeromy's avatar
Jeromy committed
341 342 343 344
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	st0, err := instances[0].Exchange.Stat()
	if err != nil {
		t.Fatal(err)
	}

	st1, err := instances[1].Exchange.Stat()
	if err != nil {
		t.Fatal(err)
	}

	if err := assertStat(st0, 1, 0, 1, 0); err != nil {
		t.Fatal(err)
	}

	if err := assertStat(st1, 0, 1, 0, 1); err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
363 364 365 366 367 368 369 370
	t.Log(blk)
	for _, inst := range instances {
		err := inst.Exchange.Close()
		if err != nil {
			t.Fatal(err)
		}
	}
}
Jeromy's avatar
Jeromy committed
371 372 373 374 375 376 377 378 379 380 381 382 383

func TestDoubleGet(t *testing.T) {
	net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
	sg := NewTestSessionGenerator(net)
	defer sg.Close()
	bg := blocksutil.NewBlockGenerator()

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

	instances := sg.Instances(2)
	blocks := bg.Blocks(1)

	ctx1, cancel1 := context.WithCancel(context.Background())
384
	blkch1, err := instances[1].Exchange.GetBlocks(ctx1, []*cid.Cid{blocks[0].Cid()})
Jeromy's avatar
Jeromy committed
385 386 387 388 389 390 391
	if err != nil {
		t.Fatal(err)
	}

	ctx2, cancel2 := context.WithCancel(context.Background())
	defer cancel2()

392
	blkch2, err := instances[1].Exchange.GetBlocks(ctx2, []*cid.Cid{blocks[0].Cid()})
Jeromy's avatar
Jeromy committed
393 394 395 396
	if err != nil {
		t.Fatal(err)
	}

397 398
	// ensure both requests make it into the wantlist at the same time
	time.Sleep(time.Millisecond * 100)
Jeromy's avatar
Jeromy committed
399 400 401 402 403 404 405 406 407 408 409 410
	cancel1()

	_, ok := <-blkch1
	if ok {
		t.Fatal("expected channel to be closed")
	}

	err = instances[0].Exchange.HasBlock(blocks[0])
	if err != nil {
		t.Fatal(err)
	}

411 412 413 414 415 416 417 418
	select {
	case blk, ok := <-blkch2:
		if !ok {
			t.Fatal("expected to get the block here")
		}
		t.Log(blk)
	case <-time.After(time.Second * 5):
		t.Fatal("timed out waiting on block")
Jeromy's avatar
Jeromy committed
419 420 421 422 423 424 425 426 427
	}

	for _, inst := range instances {
		err := inst.Exchange.Close()
		if err != nil {
			t.Fatal(err)
		}
	}
}
428 429 430 431 432 433 434 435 436 437 438

func TestWantlistCleanup(t *testing.T) {
	net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
	sg := NewTestSessionGenerator(net)
	defer sg.Close()
	bg := blocksutil.NewBlockGenerator()

	instances := sg.Instances(1)[0]
	bswap := instances.Exchange
	blocks := bg.Blocks(20)

439
	var keys []*cid.Cid
440
	for _, b := range blocks {
441
		keys = append(keys, b.Cid())
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*50)
	defer cancel()
	_, err := bswap.GetBlock(ctx, keys[0])
	if err != context.DeadlineExceeded {
		t.Fatal("shouldnt have fetched any blocks")
	}

	time.Sleep(time.Millisecond * 50)

	if len(bswap.GetWantlist()) > 0 {
		t.Fatal("should not have anyting in wantlist")
	}

	ctx, cancel = context.WithTimeout(context.Background(), time.Millisecond*50)
	defer cancel()
	_, err = bswap.GetBlocks(ctx, keys[:10])
	if err != nil {
		t.Fatal(err)
	}

	<-ctx.Done()
	time.Sleep(time.Millisecond * 50)

	if len(bswap.GetWantlist()) > 0 {
		t.Fatal("should not have anyting in wantlist")
	}

	_, err = bswap.GetBlocks(context.Background(), keys[:1])
	if err != nil {
		t.Fatal(err)
	}

	ctx, cancel = context.WithCancel(context.Background())
	_, err = bswap.GetBlocks(ctx, keys[10:])
	if err != nil {
		t.Fatal(err)
	}

	time.Sleep(time.Millisecond * 50)
	if len(bswap.GetWantlist()) != 11 {
		t.Fatal("should have 11 keys in wantlist")
	}

	cancel()
	time.Sleep(time.Millisecond * 50)
	if !(len(bswap.GetWantlist()) == 1 && bswap.GetWantlist()[0] == keys[0]) {
		t.Fatal("should only have keys[0] in wantlist")
	}
}
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

func assertLedgerMatch(ra, rb *decision.Receipt) error {
	if ra.Sent != rb.Recv {
		return fmt.Errorf("mismatch in ledgers (exchanged bytes): %d sent vs %d recvd", ra.Sent, rb.Recv)
	}

	if ra.Recv != rb.Sent {
		return fmt.Errorf("mismatch in ledgers (exchanged bytes): %d recvd vs %d sent", ra.Recv, rb.Sent)
	}

	if ra.Exchanged != rb.Exchanged {
		return fmt.Errorf("mismatch in ledgers (exchanged blocks): %d vs %d ", ra.Exchanged, rb.Exchanged)
	}

	return nil
}

510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
func assertLedgerEqual(ra, rb *decision.Receipt) error {
	if ra.Value != rb.Value {
		return fmt.Errorf("mismatch in ledgers (value/debt ratio): %f vs %f ", ra.Value, rb.Value)
	}

	if ra.Sent != rb.Sent {
		return fmt.Errorf("mismatch in ledgers (sent bytes): %d vs %d", ra.Sent, rb.Sent)
	}

	if ra.Recv != rb.Recv {
		return fmt.Errorf("mismatch in ledgers (recvd bytes): %d vs %d", ra.Recv, rb.Recv)
	}

	if ra.Exchanged != rb.Exchanged {
		return fmt.Errorf("mismatch in ledgers (exchanged blocks): %d vs %d ", ra.Exchanged, rb.Exchanged)
	}

	return nil
}

func newReceipt(sent, recv, exchanged uint64) *decision.Receipt {
	return &decision.Receipt{
		Peer:      "test",
		Value:     float64(sent) / (1 + float64(recv)),
		Sent:      sent,
		Recv:      recv,
		Exchanged: exchanged,
	}
}

func TestBitswapLedgerOneWay(t *testing.T) {
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
	net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
	sg := NewTestSessionGenerator(net)
	defer sg.Close()
	bg := blocksutil.NewBlockGenerator()

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

	instances := sg.Instances(2)
	blocks := bg.Blocks(1)
	err := instances[0].Exchange.HasBlock(blocks[0])
	if err != nil {
		t.Fatal(err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	defer cancel()
	blk, err := instances[1].Exchange.GetBlock(ctx, blocks[0].Cid())
	if err != nil {
		t.Fatal(err)
	}

	ra := instances[0].Exchange.LedgerForPeer(instances[1].Peer)
	rb := instances[1].Exchange.LedgerForPeer(instances[0].Peer)

565
	// compare peer ledger receipts
566 567 568 569 570
	err = assertLedgerMatch(ra, rb)
	if err != nil {
		t.Fatal(err)
	}

571 572 573 574 575 576 577 578 579 580 581 582
	// check that receipts have intended values
	ratest := newReceipt(1, 0, 1)
	err = assertLedgerEqual(ratest, ra)
	if err != nil {
		t.Fatal(err)
	}
	rbtest := newReceipt(0, 1, 1)
	err = assertLedgerEqual(rbtest, rb)
	if err != nil {
		t.Fatal(err)
	}

583 584 585 586 587 588 589 590 591
	t.Log(blk)
	for _, inst := range instances {
		err := inst.Exchange.Close()
		if err != nil {
			t.Fatal(err)
		}
	}
}

592
func TestBitswapLedgerTwoWay(t *testing.T) {
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
	net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
	sg := NewTestSessionGenerator(net)
	defer sg.Close()
	bg := blocksutil.NewBlockGenerator()

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

	instances := sg.Instances(2)
	blocks := bg.Blocks(2)
	err := instances[0].Exchange.HasBlock(blocks[0])
	if err != nil {
		t.Fatal(err)
	}

	err = instances[1].Exchange.HasBlock(blocks[1])
	if err != nil {
		t.Fatal(err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	defer cancel()
	blk, err := instances[1].Exchange.GetBlock(ctx, blocks[0].Cid())
	if err != nil {
		t.Fatal(err)
	}

	ctx, cancel = context.WithTimeout(context.Background(), time.Second*5)
	defer cancel()
	blk, err = instances[0].Exchange.GetBlock(ctx, blocks[1].Cid())
	if err != nil {
		t.Fatal(err)
	}

	ra := instances[0].Exchange.LedgerForPeer(instances[1].Peer)
	rb := instances[1].Exchange.LedgerForPeer(instances[0].Peer)

629
	// compare peer ledger receipts
630 631 632 633 634
	err = assertLedgerMatch(ra, rb)
	if err != nil {
		t.Fatal(err)
	}

635 636 637 638 639 640 641 642 643 644 645 646
	// check that receipts have intended values
	rtest := newReceipt(1, 1, 2)
	err = assertLedgerEqual(rtest, ra)
	if err != nil {
		t.Fatal(err)
	}

	err = assertLedgerEqual(rtest, rb)
	if err != nil {
		t.Fatal(err)
	}

647 648 649 650 651 652 653 654
	t.Log(blk)
	for _, inst := range instances {
		err := inst.Exchange.Close()
		if err != nil {
			t.Fatal(err)
		}
	}
}