fix: fix object references

This commit is contained in:
Derrick Hammer 2023-08-31 06:53:25 -04:00
parent 7afc759ece
commit e342982163
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 21 additions and 20 deletions

View File

@ -10,6 +10,7 @@ export default async function (
data: Unpacker, data: Unpacker,
rawData: Uint8Array, rawData: Uint8Array,
) { ) {
const p2p = node.services.p2p;
const p = new Packer(); const p = new Packer();
p.packInt(protocolMethodHandshakeDone); p.packInt(protocolMethodHandshakeDone);
p.packBinary(data.unpackBinary()); p.packBinary(data.unpackBinary());
@ -18,16 +19,16 @@ export default async function (
peerNetworkId = data.unpackString(); peerNetworkId = data.unpackString();
} catch {} } catch {}
if (this.networkId && peerNetworkId !== this.networkId) { if (node.services.p2p.networkId && peerNetworkId !== p2p.networkId) {
throw `Peer is in different network: ${peerNetworkId}`; throw `Peer is in different network: ${peerNetworkId}`;
} }
p.packInt(supportedFeatures); p.packInt(supportedFeatures);
p.packInt(node.services.p2p.selfConnectionUris.length); p.packInt(p2p.selfConnectionUris.length);
for (const uri of this.selfConnectionUris) { for (const uri of p2p.selfConnectionUris) {
p.packString(uri.toString()); p.packString(uri.toString());
} }
// TODO Protocol version // TODO Protocol version
// p.packInt(protocolVersion); // p.packInt(protocolVersion);
peer.sendMessage(await this.signMessageSimple(p.takeBytes())); peer.sendMessage(await p2p.signMessageSimple(p.takeBytes()));
} }

View File

@ -14,6 +14,7 @@ export default async function (
) { ) {
const length = data.unpackInt() as number; const length = data.unpackInt() as number;
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
const p2p = node.services.p2p;
const peerIdBinary = data.unpackBinary(); const peerIdBinary = data.unpackBinary();
const id = new NodeId(peerIdBinary); const id = new NodeId(peerIdBinary);
@ -31,8 +32,8 @@ export default async function (
// TODO Fully support multiple connection uris // TODO Fully support multiple connection uris
const uri = new URL(connectionUris[0].toString()); const uri = new URL(connectionUris[0].toString());
uri.username = id.toBase58(); uri.username = id.toBase58();
if (!this.reconnectDelay.has(NodeId.decode(uri.username).toString())) { if (!p2p.reconnectDelay.has(NodeId.decode(uri.username).toString())) {
node.services.p2p.connectToNode([uri]); p2p.connectToNode([uri]);
} }
} }
} }

View File

@ -11,6 +11,7 @@ export default async function (
message: SignedMessage, message: SignedMessage,
verifyId: boolean, verifyId: boolean,
) { ) {
const p2p = node.services.p2p;
const challenge = data.unpackBinary(); const challenge = data.unpackBinary();
if (!equalBytes(peer.challenge, challenge)) { if (!equalBytes(peer.challenge, challenge)) {
@ -35,8 +36,8 @@ export default async function (
throw "Remote node does not support required features"; throw "Remote node does not support required features";
} }
node.services.p2p.peers.set(peer.id.toString(), peer); p2p.peers.set(peer.id.toString(), peer);
node.services.p2p.reconnectDelay.set(peer.id.toString(), 1); p2p.reconnectDelay.set(peer.id.toString(), 1);
const connectionUrisCount = data.unpackInt() as number; const connectionUrisCount = data.unpackInt() as number;
@ -45,21 +46,18 @@ export default async function (
peer.connectionUris.push(new URL(data.unpackString() as string)); peer.connectionUris.push(new URL(data.unpackString() as string));
} }
this.logger.info( node.logger.info(
`[+] ${peer.id.toString()} (${peer.renderLocationUri().toString()})`, `[+] ${peer.id.toString()} (${peer.renderLocationUri().toString()})`,
); );
node.services.p2p.sendPublicPeersToPeer( p2p.sendPublicPeersToPeer(peer, Array.from(p2p.peers.values()));
peer, for (const p of p2p.peers.values()) {
Array.from(node.services.p2p.peers.values()),
);
for (const p of this._peers.values()) {
if (p.id.equals(peer.id)) { if (p.id.equals(peer.id)) {
continue; continue;
} }
if (p.isConnected) { if (p.isConnected) {
this.sendPublicPeersToPeer(p, [peer]); p2p.sendPublicPeersToPeer(p, [peer]);
} }
} }
} }

View File

@ -14,6 +14,7 @@ export default async function (
data: Unpacker, data: Unpacker,
rawData: Uint8Array, rawData: Uint8Array,
) { ) {
const p2p = node.services.p2p;
const hash = new Multihash(rawData.subarray(1, 34)); const hash = new Multihash(rawData.subarray(1, 34));
const type = rawData[34]; const type = rawData[34];
const expiry = decodeEndian(rawData.subarray(35, 39)); const expiry = decodeEndian(rawData.subarray(35, 39));
@ -53,10 +54,10 @@ export default async function (
nodeId, nodeId,
location: new StorageLocation(type, parts, expiry), location: new StorageLocation(type, parts, expiry),
message: rawData, message: rawData,
config: this.node.config, config: node.config,
}); });
const list = this.hashQueryRoutingTable.get(hash) || new Set<NodeId>(); const list = p2p.hashQueryRoutingTable.get(hash) || new Set<NodeId>();
for (const peerId of list) { for (const peerId of list) {
if (peerId.equals(nodeId)) { if (peerId.equals(nodeId)) {
continue; continue;
@ -65,11 +66,11 @@ export default async function (
continue; continue;
} }
if (this._peers.has(peerId.toString())) { if (p2p.peers.has(peerId.toString())) {
try { try {
this._peers.get(peerId.toString())?.sendMessage(event); p2p.peers.get(peerId.toString())?.sendMessage(rawData);
} catch (e) { } catch (e) {
this.logger.catched(e); node.logger.catched(e);
} }
} }
} }