|
| 1 | +import {expect} from "aegir/utils/chai.js"; |
| 2 | +import {create as createLibp2p, Libp2p} from "libp2p"; |
| 3 | +import PeerId from "peer-id"; |
| 4 | +import Mplex from "libp2p-mplex"; |
| 5 | +import TCP from "libp2p-tcp"; |
| 6 | +import {NOISE} from "@chainsafe/libp2p-noise"; |
| 7 | +import {MemoryBlockstore} from "blockstore-core/memory"; |
| 8 | +import {importer} from "ipfs-unixfs-importer"; |
| 9 | +import {GraphSync} from "../src/graphsync"; |
| 10 | +import {unixfsPathSelector, resolve} from "../src/resolver"; |
| 11 | +import {concatChunkIterator} from "./mock-libp2p"; |
| 12 | + |
| 13 | +async function createNode(): Promise<Libp2p> { |
| 14 | + const node = await createLibp2p({ |
| 15 | + addresses: { |
| 16 | + listen: ["/ip4/0.0.0.0/tcp/0"], |
| 17 | + }, |
| 18 | + modules: { |
| 19 | + transport: [TCP], |
| 20 | + connEncryption: [NOISE], |
| 21 | + streamMuxer: [Mplex], |
| 22 | + }, |
| 23 | + }); |
| 24 | + await node.start(); |
| 25 | + return node; |
| 26 | +} |
| 27 | + |
| 28 | +describe("listening", () => { |
| 29 | + it("reified transfer", async () => { |
| 30 | + const store1 = new MemoryBlockstore(); |
| 31 | + const net1 = await createNode(); |
| 32 | + |
| 33 | + const first = new Uint8Array(5 * 256); |
| 34 | + const second = new Uint8Array(3 * 256); |
| 35 | + |
| 36 | + // chunk and dagify it then get the root cid |
| 37 | + let cid; |
| 38 | + for await (const chunk of importer( |
| 39 | + [ |
| 40 | + {path: "first", content: first}, |
| 41 | + {path: "second", content: second}, |
| 42 | + ], |
| 43 | + store1, |
| 44 | + { |
| 45 | + cidVersion: 1, |
| 46 | + maxChunkSize: 256, |
| 47 | + rawLeaves: true, |
| 48 | + wrapWithDirectory: true, |
| 49 | + } |
| 50 | + )) { |
| 51 | + if (chunk.path === "") { |
| 52 | + cid = chunk.cid; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if (!cid) { |
| 57 | + throw new Error("failed to add DAG"); |
| 58 | + } |
| 59 | + |
| 60 | + const store2 = new MemoryBlockstore(); |
| 61 | + const net2 = await createNode(); |
| 62 | + |
| 63 | + net2.peerStore.addressBook.add(net1.peerId, net1.multiaddrs); |
| 64 | + |
| 65 | + const provider = new GraphSync(net1, store1); |
| 66 | + provider.start(); |
| 67 | + |
| 68 | + const client = new GraphSync(net2, store2); |
| 69 | + client.start(); |
| 70 | + const {root, selector} = unixfsPathSelector(cid.toString() + "/first"); |
| 71 | + |
| 72 | + const request = client.request(root, selector); |
| 73 | + request.open(net1.peerId); |
| 74 | + |
| 75 | + const buf = await concatChunkIterator(resolve(cid, selector, request)); |
| 76 | + expect(buf).to.deep.equal(first); |
| 77 | + }); |
| 78 | +}); |
0 commit comments