Long time listener, first time caller…would appreciate any help because I am at a loss!
I’m working on a node.js project using libp2p for peer discovery. I have my codebase running on 2 machines connected to my local network. They discover each other, connect to each other, and store the connection details in a Map object called discoveredPeers. I have several console log statements to track the value of discoveredPeers. The Map contains exactly what I would expect it to UNTIL I try to access that Map object from a secondary module. Then it immediately shows that it is empty, despite a log statement immediately prior showing that it holds 3 entries.
Modules in question:
peerState – Declares the discoveredPeers Map
console.log('Initializing peerState...');
export const discoveredPeers = new Map();
export const connectedPeers = new Map();
peerStateManager – Imports peerState where discoveredMaps object is declared. Contains various getter/setter methods for peerState
export const getAllDiscoveredPeers = async () => discoveredPeers;
export const updatePeerPublicKey = async (peerId, publicKey) => {
if (discoveredPeers.has(peerId)) {
discoveredPeers.get(peerId).publicKey = publicKey;
console.log('Updated peer publicKey');
console.log('Discovered Peers @ updatePeerPublicKey: ', discoveredPeers)
}
};
export const getPeerByPublicKey = async (publicKey) => {
console.log('Discovered Peers @ getPeerByPublicKey:', discoveredPeers);
for (const [peerId, peerData] of discoveredPeers) {
if (peerData.publicKey === publicKey) {
return {peerId, ...peerData}; // Return the whole peer object
}
}
return null; // Return null if no peer with that publicKey is found
};
messenger – Imports peerStateManager to access and update the values of peerState
case MESSAGE_TYPES.PEER.RESPONSE:
console.log('Received PEER_RESPONSE');
if (message.actionType === ACTION_TYPES.PEER.RESPONSE_PUBLIC_KEY) {
console.log('Received PEER_RESPONSE_PUBLIC_KEY: ', message.payload.publicKey);
const peerId = message.meta.sender;
const publicKey = message.payload.publicKey;
**await peerStateManager.updatePeerPublicKey(peerId, publicKey);**
}
break;
synapseController – Imports getAllDiscoveredPeers and getPeerByPublicKey from peerStateManager
exports.getSynapseUserPosts = async (req, res) => {
**console.log('Discovered Peers in getSynapseUserPosts: ', await getAllDiscoveredPeers());**
const handle = req.query.handle;
const synapsePublicKey = req.query.publicKey;
const {peerId} = await getPeerByPublicKey(synapsePublicKey);
...more definition
}
A call from messenger causes peerStateManager to update discoveredPeers. It logs 3 entries in discoveredPeers. Immediately after, a call via synapseController to getAllDiscoveredPeers() shows its empty, which subsequently causes getPeerByPublicKey() fail.
CONSOLE OUTPUT:
Updated peer publicKey
Discovered Peers @ updatePeerPublicKey: Map(3) {
'12D3KooWM2YGTyBb11aPdWHbDarzpmDLp3FpAPQbqFKHWjAtJrpm' => {
publicKey: '0315e1f282db55a243b70c8030dc4e4a1fe9fff45dbe82f1e5f1ae6638b6e3ff49',
multiaddrs: [
'/ip4/192.168.1.188/tcp/4001'
]
},
'12D3KooWJtLJ3Z5YM855gxsWDnEFHhATAPTGMBQDQGYjh3icFsbs' => {
publicKey: null,
multiaddrs: [
'/ip4/192.168.1.253/tcp/4001/p2p/12D3KooWJtLJ3Z5YM855gxsWDnEFHhATAPTGMBQDQGYjh3icFsbs'
]
},
'12D3KooWEESBm7xFdo35dGnbjY5LZVnWvbn1JS51Kc71ooGkcWNr' => {
publicKey: null,
multiaddrs: [
'/ip4/192.168.1.188/tcp/4001/p2p/12D3KooWEESBm7xFdo35dGnbjY5LZVnWvbn1JS51Kc71ooGkcWNr'
]
}
}
Discovered Peers in getSynapseUserPosts: Map(0) {}
Discovered Peers @ getPeerByPublicKey: Map(0) {}
/Users/enki/Developer/Repositories (Version Controlled)/meNexus-platform/synapse/api/src/controllers/synapseController.js:14
const {peerId} = await getPeerByPublicKey(synapsePublicKey);
^
TypeError: Cannot destructure property 'peerId' of '(intermediate value)' as it is null.
I tried declaring discoveredPeers in its own separate file that gets managed by a peerStateManager module because some of my reading suggested that discoveredPeers was being re-initialized when synapseController tried to access the Map because the file was imported again…so I tried to make sure that discoveredPeers was a global/singleton object. The same behavior persists where the Map shows empty as soon as synapseController tries to access it. I also have a log statement in peerState that shows (‘Initializing peerState…’) and that only fires once. I do not see that firing again when synapseController tries to access the discoveredPeers Map.
Any assistance would be greatly appreciated as I’m simply stuck on this one.
I am happy to answer any clarifying questions if needed. Struggling to ask my question gracefully so I apologize in advance if my post is gibberish. Thank you.