diff --git a/src/messages/handshakeOpen.ts b/src/messages/handshakeOpen.ts index 3a18024..4138322 100644 --- a/src/messages/handshakeOpen.ts +++ b/src/messages/handshakeOpen.ts @@ -10,6 +10,7 @@ export default async function ( data: Unpacker, rawData: Uint8Array, ) { + const p2p = node.services.p2p; const p = new Packer(); p.packInt(protocolMethodHandshakeDone); p.packBinary(data.unpackBinary()); @@ -18,16 +19,16 @@ export default async function ( peerNetworkId = data.unpackString(); } catch {} - if (this.networkId && peerNetworkId !== this.networkId) { + if (node.services.p2p.networkId && peerNetworkId !== p2p.networkId) { throw `Peer is in different network: ${peerNetworkId}`; } p.packInt(supportedFeatures); - p.packInt(node.services.p2p.selfConnectionUris.length); - for (const uri of this.selfConnectionUris) { + p.packInt(p2p.selfConnectionUris.length); + for (const uri of p2p.selfConnectionUris) { p.packString(uri.toString()); } // TODO Protocol version // p.packInt(protocolVersion); - peer.sendMessage(await this.signMessageSimple(p.takeBytes())); + peer.sendMessage(await p2p.signMessageSimple(p.takeBytes())); } diff --git a/src/messages/signedMessages/announcePeers.ts b/src/messages/signedMessages/announcePeers.ts index b1a70d4..1448346 100644 --- a/src/messages/signedMessages/announcePeers.ts +++ b/src/messages/signedMessages/announcePeers.ts @@ -14,6 +14,7 @@ export default async function ( ) { const length = data.unpackInt() as number; for (let i = 0; i < length; i++) { + const p2p = node.services.p2p; const peerIdBinary = data.unpackBinary(); const id = new NodeId(peerIdBinary); @@ -31,8 +32,8 @@ export default async function ( // TODO Fully support multiple connection uris const uri = new URL(connectionUris[0].toString()); uri.username = id.toBase58(); - if (!this.reconnectDelay.has(NodeId.decode(uri.username).toString())) { - node.services.p2p.connectToNode([uri]); + if (!p2p.reconnectDelay.has(NodeId.decode(uri.username).toString())) { + p2p.connectToNode([uri]); } } } diff --git a/src/messages/signedMessages/handshakeDone.ts b/src/messages/signedMessages/handshakeDone.ts index 3e124f8..ae7f122 100644 --- a/src/messages/signedMessages/handshakeDone.ts +++ b/src/messages/signedMessages/handshakeDone.ts @@ -11,6 +11,7 @@ export default async function ( message: SignedMessage, verifyId: boolean, ) { + const p2p = node.services.p2p; const challenge = data.unpackBinary(); if (!equalBytes(peer.challenge, challenge)) { @@ -35,8 +36,8 @@ export default async function ( throw "Remote node does not support required features"; } - node.services.p2p.peers.set(peer.id.toString(), peer); - node.services.p2p.reconnectDelay.set(peer.id.toString(), 1); + p2p.peers.set(peer.id.toString(), peer); + p2p.reconnectDelay.set(peer.id.toString(), 1); const connectionUrisCount = data.unpackInt() as number; @@ -45,21 +46,18 @@ export default async function ( peer.connectionUris.push(new URL(data.unpackString() as string)); } - this.logger.info( + node.logger.info( `[+] ${peer.id.toString()} (${peer.renderLocationUri().toString()})`, ); - node.services.p2p.sendPublicPeersToPeer( - peer, - Array.from(node.services.p2p.peers.values()), - ); - for (const p of this._peers.values()) { + p2p.sendPublicPeersToPeer(peer, Array.from(p2p.peers.values())); + for (const p of p2p.peers.values()) { if (p.id.equals(peer.id)) { continue; } if (p.isConnected) { - this.sendPublicPeersToPeer(p, [peer]); + p2p.sendPublicPeersToPeer(p, [peer]); } } } diff --git a/src/messages/storageLocation.ts b/src/messages/storageLocation.ts index bca6f42..00c495f 100644 --- a/src/messages/storageLocation.ts +++ b/src/messages/storageLocation.ts @@ -14,6 +14,7 @@ export default async function ( data: Unpacker, rawData: Uint8Array, ) { + const p2p = node.services.p2p; const hash = new Multihash(rawData.subarray(1, 34)); const type = rawData[34]; const expiry = decodeEndian(rawData.subarray(35, 39)); @@ -53,10 +54,10 @@ export default async function ( nodeId, location: new StorageLocation(type, parts, expiry), message: rawData, - config: this.node.config, + config: node.config, }); - const list = this.hashQueryRoutingTable.get(hash) || new Set(); + const list = p2p.hashQueryRoutingTable.get(hash) || new Set(); for (const peerId of list) { if (peerId.equals(nodeId)) { continue; @@ -65,11 +66,11 @@ export default async function ( continue; } - if (this._peers.has(peerId.toString())) { + if (p2p.peers.has(peerId.toString())) { try { - this._peers.get(peerId.toString())?.sendMessage(event); + p2p.peers.get(peerId.toString())?.sendMessage(rawData); } catch (e) { - this.logger.catched(e); + node.logger.catched(e); } } }