testutil.go 2.03 KB
Newer Older
1 2 3 4 5
package testutil

import (
	bsmsg "github.com/ipfs/go-bitswap/message"
	"github.com/ipfs/go-bitswap/wantlist"
hannahhoward's avatar
hannahhoward committed
6
	"github.com/ipfs/go-block-format"
7 8 9 10 11 12 13 14
	cid "github.com/ipfs/go-cid"
	blocksutil "github.com/ipfs/go-ipfs-blocksutil"
	peer "github.com/libp2p/go-libp2p-peer"
)

var blockGenerator = blocksutil.NewBlockGenerator()
var prioritySeq int

15
// GenerateCids produces n content identifiers.
16 17 18 19 20 21 22 23 24
func GenerateCids(n int) []cid.Cid {
	cids := make([]cid.Cid, 0, n)
	for i := 0; i < n; i++ {
		c := blockGenerator.Next().Cid()
		cids = append(cids, c)
	}
	return cids
}

25
// GenerateWantlist makes a populated wantlist.
26 27 28 29 30 31 32 33 34 35
func GenerateWantlist(n int, ses uint64) *wantlist.ThreadSafe {
	wl := wantlist.NewThreadSafe()
	for i := 0; i < n; i++ {
		prioritySeq++
		entry := wantlist.NewRefEntry(blockGenerator.Next().Cid(), prioritySeq)
		wl.AddEntry(entry, ses)
	}
	return wl
}

36
// GenerateMessageEntries makes fake bitswap message entries.
37
func GenerateMessageEntries(n int, isCancel bool) []*bsmsg.Entry {
38 39 40 41 42 43 44 45 46 47 48 49 50 51
	bsmsgs := make([]*bsmsg.Entry, 0, n)
	for i := 0; i < n; i++ {
		prioritySeq++
		msg := &bsmsg.Entry{
			Entry:  wantlist.NewRefEntry(blockGenerator.Next().Cid(), prioritySeq),
			Cancel: isCancel,
		}
		bsmsgs = append(bsmsgs, msg)
	}
	return bsmsgs
}

var peerSeq int

52
// GeneratePeers creates n peer ids.
53 54 55 56 57 58 59 60 61 62 63 64
func GeneratePeers(n int) []peer.ID {
	peerIds := make([]peer.ID, 0, n)
	for i := 0; i < n; i++ {
		peerSeq++
		p := peer.ID(peerSeq)
		peerIds = append(peerIds, p)
	}
	return peerIds
}

var nextSession uint64

65
// GenerateSessionID make a unit session identifier.
66 67 68 69 70
func GenerateSessionID() uint64 {
	nextSession++
	return uint64(nextSession)
}

71
// ContainsPeer returns true if a peer is found n a list of peers.
72 73 74 75 76 77 78 79
func ContainsPeer(peers []peer.ID, p peer.ID) bool {
	for _, n := range peers {
		if p == n {
			return true
		}
	}
	return false
}
hannahhoward's avatar
hannahhoward committed
80 81 82 83 84 85 86 87 88 89

// ContainsBlock returns true if a block is found n a list of blocks
func ContainsBlock(blks []blocks.Block, block blocks.Block) bool {
	for _, n := range blks {
		if block.Cid() == n.Cid() {
			return true
		}
	}
	return false
}