There might be some nuance around recursive pins and underlying blocks, but that should be enough for you to calculate the size.
Thatโs a tricky one, given that this isnโt tracked by Helia. There might be a clever way to hook into block requests via Bitswap and track this, but I donโt have enough knowledge of the js-bitswap implementation to answer this.
/* eslint-disable no-console */
import { noise } from '@chainsafe/libp2p-noise' // for encrypting connections
import { yamux } from '@chainsafe/libp2p-yamux'
import { unixfs } from '@helia/unixfs'
import { bootstrap } from '@libp2p/bootstrap'
import { identify } from '@libp2p/identify'
import { tcp } from '@libp2p/tcp' // Required to enable TCP connection for peer to peer communication
import { FsBlockstore } from 'blockstore-fs'
import { FsDatastore } from 'datastore-fs'
import { createHelia } from 'helia'
import { createLibp2p } from 'libp2p'
// the blockstore is where we store the blocks that make up files
const blockstore = new FsBlockstore('block-store')
// application-specific data lives in the datastore
const datastore = new FsDatastore('data-store')
// libp2p is the networking layer that underpins Helia
const libp2p = await createLibp2p({
datastore,
blockstore,
addresses: {
listen: [
// add a listen address (localhost) to accept TCP connections on a random port
'/ip4/127.0.0.1/tcp/0'
]
},
transports: [
tcp()
],
connectionEncryption: [
noise()
],
streamMuxers: [
yamux()
],
peerDiscovery: [
bootstrap({
list: [
'/ip4/********/tcp/4001/p2p/12D3KooWR6PtoaHyoQZ6tJmjEdGHCNA4LMXctrvnph4DNYYLPprb'
]
})
],
services: {
identify: identify()
}
})
console.log(`Node started with id ${libp2p.peerId.toString()}`)
// Save the generated peer ID
//await savePeerId(libp2p.peerId.toString());
// print out listening addresses
console.log('listening on addresses:')
libp2p.getMultiaddrs().forEach((addr) => {
console.log(addr.toString())
})
// Listen for new connections to peers
libp2p.addEventListener("peer:connect", (evt) => {
const connection = evt.detail;
console.log(`Connected to ${connection.toString()}`);
});
libp2p.addEventListener('peer:discovery', (peerId) => {
// No need to dial, autoDial is on
console.log('Discovered:', peerId.toString())
})
// Listen for peers disconnecting
libp2p.addEventListener("peer:disconnect", (evt) => {
const connection = evt.detail;
console.log(`Disconnected from ${connection.toCID().toString()}`);
});
const helia = await createHelia({
libp2p,
blockstore,
datastore
})
//console.log(await helia)
// create a filesystem on top of Helia, in this case it's UnixFS
const fs = unixfs(helia)
// we will use this TextEncoder to turn strings into Uint8Arrays
const encoder = new TextEncoder()
// add the bytes to your node and receive a unique content identifier
const cid = await fs.addBytes(encoder.encode('Hello Kava'))
console.log('Added file:', cid.toString())
It can be quite time consuming to create a reproduction for such errors. Given this, we ask for a reproduction repo to make it easier for us to debug the problem and find a resolution if needed.
Thanks so much. this made it really easy to see whatโs happening.
I think because youโre constructing libp2p yourself, some of the required loading of peerId is not being handled (talked about in the link I posted in my comment above)
Appreciate your reply, Sorry I couldnโt reply on time.
Below example provided by @SgtPooki helped me solve the peer Id persistent issue
Here is an extremely tiny example showing that libp2p and helia will persist a peerId if itโs saved to the datastore: https://codepen.io/SgtPooki/pen/VwNdWBQ?editors=0011