ipfs / helia

An implementation of IPFS in TypeScript
https://helia.io
Other
954 stars 105 forks source link

fs.cat with no response or waiting long time #179

Closed BetterWorld-Liuser closed 1 year ago

BetterWorld-Liuser commented 1 year ago

this is createHelia function

async function createNode() {
  // the blockstore is where we store the blocks that make up files
  const blockstore = new FsBlockstore("./store");

  // application-specific data lives in the datastore
  const datastore = new MemoryDatastore();

  // libp2p is the networking layer that underpins Helia
  const libp2p = await createLibp2p({
    datastore,
    addresses: {
      listen: ["/ip4/127.0.0.1/tcp/0"],
    },
    transports: [tcp()],
    connectionEncryption: [noise()],
    streamMuxers: [yamux()],
    peerDiscovery: [
      bootstrap({
        list: [
          "/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
          "/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
          "/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
          "/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
        ],
      }),
    ],
    // dht:kadDHT(),
    services: {
      identify: identifyService(),
    //   dht: kadDHT(),
    },
  });

//   console.log(libp2p.getMultiaddrs());
  libp2p.addEventListener("peer:connect", (evt) => {
    console.log("Connected to %s", evt.detail); // Log connected peer
  });

  return await createHelia({
    datastore,
    blockstore,
    libp2p,
  });
}

and i start a ipfs desktop and get libp2p nodeid: /ip4/127.0.0.1/tcp/4001/p2p/12D3KooWPLGk8jupG9qVprUpB3Pv9XBdHJrvLPcy1h4Kv1U8B9x5

finally i fs.cat file pinned in ipfs desktop which cid is QmV3aSkBAXZtj2JNW6s5wEdq1QjLcW9mFP5xmNvPp634j4

// create two helia nodes
const node = await createNode();
const ma = multiaddr(
  "/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWPLGk8jupG9qVprUpB3Pv9XBdHJrvLPcy1h4Kv1U8B9x5"
);
await node.libp2p.dial(ma)

// create a filesystem on top of the second Helia node
const fs = unixfs(node);

let bufferarray = Buffer.alloc(0)
for await (const buf of fs.cat(
  "QmV3aSkBAXZtj2JNW6s5wEdq1QjLcW9mFP5xmNvPp634j4"
)) {
  bufferarray = Buffer.concat([bufferarray, buf]);
  console.log("get chunck")
}
nodefs.writeFile("./1.png", bufferarray, { flag: "a" },(a)=>{});

it just costs a long time or even no response and no file downloaded. i am sure it connects to desktop ipfs because it print

Connected to 12D3KooWPLGk8jupG9qVprUpB3Pv9XBdHJrvLPcy1h4Kv1U8B9x5

BetterWorld-Liuser commented 1 year ago

I have debug for a while, finding out that ipfs desktop seems to response wantlist not in time or even randomly which cause the node client sluggishing.

but it still needs an option to put timeout config to control cat time.

SgtPooki commented 1 year ago

@BetterWorld-Liuser you can use an AbortSignal to timeout the cat method:

fs.cat(cid, { signal: AbortSignal.timeout(30000) })

Most methods in the Helia ecosystem support a signal config option where you can pass an AbortSignal.

For support inquiries, you may want to reach out on discuss.ipfs.tech.

github-actions[bot] commented 1 year ago

Oops, seems like we needed more information for this issue, please comment with more details or this issue will be closed in 7 days.

github-actions[bot] commented 1 year ago

This issue was closed because it is missing author input.