@hive-p2p/server 1.0.87 → 1.0.89
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/crypto-codex.mjs +4 -2
- package/core/gossip.mjs +4 -4
- package/package.json +1 -1
package/core/crypto-codex.mjs
CHANGED
|
@@ -223,9 +223,10 @@ export class CryptoCodex {
|
|
|
223
223
|
readGossipMessage(serialized) {
|
|
224
224
|
if (this.verbose > 3) console.log(`%creadGossipMessage ${serialized.byteLength} bytes`, LOG_CSS.CRYPTO_CODEX);
|
|
225
225
|
if (this.verbose > 4) console.log(`%c${serialized}`, LOG_CSS.CRYPTO_CODEX);
|
|
226
|
+
let topic;
|
|
226
227
|
try { // 1, 1, 1, 8, 4, 32, X, 64, 1
|
|
227
228
|
const { marker, dataCode, neighLength, timestamp, dataLength, pubkey, associatedId } = this.readBufferHeader(serialized);
|
|
228
|
-
|
|
229
|
+
topic = GOSSIP.MARKERS_BYTES[marker];
|
|
229
230
|
if (topic === undefined) throw new Error(`Failed to deserialize gossip message: unknown marker byte ${d[0]}.`);
|
|
230
231
|
const NDBL = neighLength + dataLength;
|
|
231
232
|
const neighbors = this.#bytesToIds(serialized.slice(47, 47 + neighLength));
|
|
@@ -243,9 +244,10 @@ export class CryptoCodex {
|
|
|
243
244
|
readUnicastMessage(serialized, peerStore) {
|
|
244
245
|
if (this.verbose > 3) console.log(`%creadUnicastMessage ${serialized.byteLength} bytes`, LOG_CSS.CRYPTO_CODEX);
|
|
245
246
|
if (this.verbose > 4) console.log(`%c${serialized}`, LOG_CSS.CRYPTO_CODEX);
|
|
247
|
+
let type;
|
|
246
248
|
try { // 1, 1, 1, 8, 4, 32, X, 1, X, 64
|
|
247
249
|
const { marker, dataCode, neighLength, timestamp, dataLength, pubkey } = this.readBufferHeader(serialized, false);
|
|
248
|
-
|
|
250
|
+
type = UNICAST.MARKERS_BYTES[marker];
|
|
249
251
|
if (type === undefined) throw new Error(`Failed to deserialize unicast message: unknown marker byte ${d[0]}.`);
|
|
250
252
|
const NDBL = neighLength + dataLength;
|
|
251
253
|
const neighbors = this.#bytesToIds(serialized.slice(47, 47 + neighLength));
|
package/core/gossip.mjs
CHANGED
|
@@ -108,10 +108,10 @@ export class Gossip {
|
|
|
108
108
|
const serializedMessage = this.cryptoCodex.createGossipMessage(topic, data, hops, this.peerStore.neighborsList);
|
|
109
109
|
if (!this.bloomFilter.addMessage(serializedMessage)) return; // avoid sending duplicate messages
|
|
110
110
|
if (this.verbose > 3) console.log(`(${this.id}) Gossip ${topic}, to ${JSON.stringify(this.peerStore.neighborsList)}: ${data}`);
|
|
111
|
-
for (const peerId of this.peerStore.neighborsList) this
|
|
111
|
+
for (const peerId of this.peerStore.neighborsList) this.#broadcastSerializedToPeer(peerId, serializedMessage);
|
|
112
112
|
}
|
|
113
113
|
/** @param {string} targetId @param {any} serializedMessage */
|
|
114
|
-
|
|
114
|
+
#broadcastSerializedToPeer(targetId, serializedMessage) {
|
|
115
115
|
if (targetId === this.id) throw new Error(`Refusing to send a gossip message to self (${this.id}).`);
|
|
116
116
|
const transportInstance = this.peerStore.connected[targetId]?.transportInstance;
|
|
117
117
|
if (!transportInstance) return { success: false, reason: `Transport instance is not available for peer ${targetId}.` };
|
|
@@ -120,7 +120,7 @@ export class Gossip {
|
|
|
120
120
|
}
|
|
121
121
|
sendGossipHistoryToPeer(peerId) {
|
|
122
122
|
const gossipHistory = this.bloomFilter.getGossipHistoryByTime('asc');
|
|
123
|
-
for (const entry of gossipHistory) this
|
|
123
|
+
for (const entry of gossipHistory) this.#broadcastSerializedToPeer(peerId, entry.data);
|
|
124
124
|
}
|
|
125
125
|
/** @param {string} from @param {Uint8Array} serialized @returns {void} */
|
|
126
126
|
async handleGossipMessage(from, serialized) {
|
|
@@ -156,6 +156,6 @@ export class Gossip {
|
|
|
156
156
|
for (const peerId of this.peerStore.neighborsList)
|
|
157
157
|
if (peerId === from) continue; // avoid sending back to sender
|
|
158
158
|
else if (!avoidTransmissionRate && Math.random() > transmissionRate) continue; // apply gossip transmission rate
|
|
159
|
-
else this
|
|
159
|
+
else this.#broadcastSerializedToPeer(peerId, serializedToTransmit);
|
|
160
160
|
}
|
|
161
161
|
}
|