-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
110 lines (88 loc) · 3.43 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import test from 'ava'
import { createLibp2p } from 'libp2p'
import { webSockets } from '@libp2p/websockets'
import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { createBitswap } from 'ipfs-bitswap'
import { MemoryBlockstore } from 'blockstore-core/memory'
import { fromString, toString } from 'multiformats/bytes'
import * as raw from 'multiformats/codecs/raw'
import { sha256 } from 'multiformats/hashes/sha2'
import { blake2b256 } from '@multiformats/blake2/blake2b'
import { CID } from 'multiformats/cid'
import { multiaddr } from '@multiformats/multiaddr'
import { Miniswap, BITSWAP_PROTOCOL } from './index.js'
test('should bitswap a single CID', async t => {
const clientBlockstore = new MemoryBlockstore()
const client = await createLibp2p({
transports: [webSockets()],
streamMuxers: [yamux()],
connectionEncrypters: [noise()]
})
const bitswap = createBitswap(client, clientBlockstore)
console.log('starting client')
await client.start()
// create blockstore and add data
const serverBlockstore = new MemoryBlockstore()
const data = fromString(`TEST DATA ${Date.now()}`)
const hash = await sha256.digest(data)
const cid = CID.create(1, raw.code, hash)
await serverBlockstore.put(cid, data)
const serverAddr = '/ip4/127.0.0.1/tcp/1337/ws'
const server = await createLibp2p({
addresses: { listen: [serverAddr] },
transports: [webSockets()],
streamMuxers: [yamux()],
connectionEncrypters: [noise()]
})
const miniswap = new Miniswap(serverBlockstore)
server.handle(BITSWAP_PROTOCOL, miniswap.handler)
console.log('starting server')
await server.start()
console.log(`dialing ${serverAddr}/p2p/${server.peerId}`)
await client.dial(multiaddr(`${serverAddr}/p2p/${server.peerId}`))
console.log('starting bitswap')
bitswap.start()
console.log(`bitswapping ${cid}`)
const retrievedData = await bitswap.want(cid)
t.is(toString(retrievedData), toString(data))
await Promise.all([client.stop(), server.stop()])
})
test('should bitswap a CID that uses blake2b', async t => {
const clientBlockstore = new MemoryBlockstore()
const client = await createLibp2p({
transports: [webSockets()],
streamMuxers: [yamux()],
connectionEncrypters: [noise()]
})
const bitswap = createBitswap(client, clientBlockstore, {
hashLoader: { getHasher: async () => blake2b256 }
})
console.log('starting client')
await client.start()
// create blockstore and add data
const serverBlockstore = new MemoryBlockstore()
const data = fromString(`TEST DATA ${Date.now()}`)
const hash = await blake2b256.digest(data)
const cid = CID.create(1, raw.code, hash)
await serverBlockstore.put(cid, data)
const serverAddr = '/ip4/127.0.0.1/tcp/1338/ws'
const server = await createLibp2p({
addresses: { listen: [serverAddr] },
transports: [webSockets()],
streamMuxers: [yamux()],
connectionEncrypters: [noise()]
})
const miniswap = new Miniswap(serverBlockstore)
server.handle(BITSWAP_PROTOCOL, miniswap.handler)
console.log('starting server')
await server.start()
console.log(`dialing ${serverAddr}/p2p/${server.peerId}`)
await client.dial(multiaddr(`${serverAddr}/p2p/${server.peerId}`))
console.log('starting bitswap')
bitswap.start()
console.log(`bitswapping ${cid}`)
const retrievedData = await bitswap.want(cid)
t.is(toString(retrievedData), toString(data))
await Promise.all([client.stop(), server.stop()])
})