Skip to content

Instantly share code, notes, and snippets.

@vasco-santos
Created March 2, 2023 14:29
Show Gist options
  • Save vasco-santos/84ce21ac5bf9ca9a8e19549bae336325 to your computer and use it in GitHub Desktop.
Save vasco-santos/84ce21ac5bf9ca9a8e19549bae336325 to your computer and use it in GitHub Desktop.
import { webSockets } from '@libp2p/websockets'
import { mplex } from '@libp2p/mplex'
import { noise } from '@chainsafe/libp2p-noise'
import { createLibp2p } from 'libp2p'
import { MemoryBlockstore } from 'blockstore-core/memory'
import { createBitswap } from 'ipfs-bitswap'
import { CID } from 'multiformats/cid'
import { multiaddr } from '@multiformats/multiaddr'
const cid = CID.parse('kreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq')
const node = await createLibp2p({
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0/ws']
},
transports: [
webSockets()
],
streamMuxers: [
mplex()
],
connectionEncryption: [
noise()
],
})
await node.start()
await new Promise((resolve) => {
setTimeout(() => resolve(), 500)
})
console.log('node', node.peerId)
await node.dial(multiaddr('/dns4/elastic-staging.dag.house/tcp/443/wss/p2p/bafzbeigjqot6fm3i3yv37wiyybsfblrlsmib7bzlbnkpjxde6fw6b4fvei'))
const bitswap = await createBitswap(node, new MemoryBlockstore())
await bitswap.start()
// Let ready
await new Promise((resolve) => {
setTimeout(() => resolve(), 2000)
})
console.log('bitswap started', bitswap.isStarted())
await Promise.race([
(async function () {
for (let i = 0; i < 20; i++) {
const list = Array.from(bitswap.getWantlist())
console.log('wantlinst', list.length)
await new Promise((resolve) => {
setTimeout(() => resolve(), 200)
})
}
await new Promise((resolve) => {
setTimeout(() => resolve(), 10000)
})
})(),
(async function () {
let block
try {
block = await bitswap.get(cid)
} catch (e) {
console.log('failed')
}
console.log('block', block)
})(),
(async function () {
await new Promise((resolve) => {
setTimeout(() => resolve(), 800)
})
// unwant
bitswap.cancelWants([cid])
console.log('unwant')
await new Promise((resolve) => {
setTimeout(() => resolve(), 15000)
})
})(),
])
console.log('end wantlinst', bitswap.getWantlist())
await node.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment