@hive-p2p/server 1.0.55 → 1.0.56

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/arbiter.mjs CHANGED
@@ -87,19 +87,14 @@ export class Arbiter {
87
87
  if (!this.#signatureControl(from, message, serialized)) return;
88
88
  if (!this.#lengthControl(from, topic ? 'gossip' : 'unicast', serialized, expectedEnd)) return;
89
89
 
90
- if (topic) this.#hopsControl(from, message);
91
- else this.#routeLengthControl(from, message);
90
+ const routeOrHopsOk = topic ? this.#hopsControl(from, message) : this.#routeLengthControl(from, message);
91
+ if (!routeOrHopsOk) return;
92
92
 
93
93
  if (this.isBanished(from) || this.isBanished(senderId)) return;
94
- if (this.trustBalances[senderId] > TRUST_VALUES.VALID_POW) return;
94
+ if (this.trustBalances[senderId] > TRUST_VALUES.VALID_POW) return true; // we check only low trust balances
95
95
  if (Math.random() < powCheckFactor) await this.#powControl(senderId, pubkey);
96
96
  return true;
97
97
  }
98
- /** @param {string} from @param {'gossip' | 'unicast'} type */
99
- #lengthControl(from, type, serialized, expectedEnd) {
100
- if (!expectedEnd || serialized.length === expectedEnd) return true;
101
- this.adjustTrust(from, TRUST_VALUES.WRONG_LENGTH, `${type} message length mismatch`);
102
- }
103
98
  /** @param {string} from @param {import('./gossip.mjs').GossipMessage} message @param {Uint8Array} serialized */
104
99
  #signatureControl(from, message, serialized) {
105
100
  try {
@@ -115,14 +110,19 @@ export class Arbiter {
115
110
  }
116
111
  this.adjustTrust(from, TRUST_VALUES.WRONG_SIGNATURE, 'Gossip signature invalid');
117
112
  }
113
+ /** @param {string} from @param {'gossip' | 'unicast'} type */
114
+ #lengthControl(from, type, serialized, expectedEnd) {
115
+ if (!expectedEnd || serialized.length === expectedEnd) return true;
116
+ this.adjustTrust(from, TRUST_VALUES.WRONG_LENGTH, `${type} message length mismatch`);
117
+ }
118
118
  /** GOSSIP only @param {string} from @param {import('./gossip.mjs').GossipMessage} message */
119
119
  #hopsControl(from, message) {
120
- if (message.HOPS <= (GOSSIP.HOPS[message.topic] || GOSSIP.HOPS.default)) return;
120
+ if (message.HOPS <= (GOSSIP.HOPS[message.topic] || GOSSIP.HOPS.default)) return true;
121
121
  this.adjustTrust(from, TRUST_VALUES.HOPS_EXCEEDED, 'Gossip HOPS exceeded');
122
122
  }
123
123
  /** UNICAST only @param {string} from @param {import('./unicast.mjs').DirectMessage} message */
124
124
  #routeLengthControl(from, message) {
125
- if (message.route.length <= UNICAST.MAX_HOPS) return;
125
+ if (message.route.length <= UNICAST.MAX_HOPS) return true;
126
126
  this.adjustTrust(from, TRUST_VALUES.HOPS_EXCEEDED, 'Unicast HOPS exceeded');
127
127
  }
128
128
  /** ONLY APPLY AFTER #signatureControl() - @param {string} senderId @param {Uint8Array} pubkey */
package/core/config.mjs CHANGED
@@ -45,7 +45,7 @@ export const SIMULATION = {
45
45
 
46
46
  export const NODE = {
47
47
  /** 0: none, 1: errors, 2: +important info, 3: +debug, 4: +everything | Can be bypass by some constructors */
48
- DEFAULT_VERBOSE: 1,
48
+ DEFAULT_VERBOSE: 2,
49
49
  /** Timeout for upgrading a "connecting" peer to "connected" | Default: 15_000 (15 seconds) */
50
50
  CONNECTION_UPGRADE_TIMEOUT: 15_000,
51
51
  /** Flag to indicate if we are running in a browser environment | DON'T MODIFY THIS VALUE */
package/core/gossip.mjs CHANGED
@@ -102,7 +102,7 @@ export class Gossip {
102
102
  else this.callbacks[callbackType].push(callback);
103
103
  }
104
104
  /** Gossip a message to all connected peers > will be forwarded to all peers
105
- * @param {string | Uint8Array | Object} data @param {string} topic @param {number} [HOPS] */
105
+ * @param {string | Uint8Array | Object} data @param {string} topic default: 'gossip' @param {number} [HOPS] */
106
106
  broadcastToAll(data, topic = 'gossip', HOPS) {
107
107
  const hops = HOPS || GOSSIP.HOPS[topic] || GOSSIP.HOPS.default;
108
108
  const serializedMessage = this.cryptoCodex.createGossipMessage(topic, data, hops, this.peerStore.neighborsList);
package/core/node.mjs CHANGED
@@ -160,7 +160,7 @@ export class Node {
160
160
  }
161
161
 
162
162
  /** Broadcast a message to all connected peers or to a specified peer
163
- * @param {string | Uint8Array | Object} data @param {string} topic @param {string} [targetId] default: broadcast to all
163
+ * @param {string | Uint8Array | Object} data @param {string} topic default: 'gossip' @param {string} [targetId] default: broadcast to all
164
164
  * @param {number} [timestamp] default: CLOCK.time @param {number} [HOPS] default: GOSSIP.HOPS[topic] || GOSSIP.HOPS.default */
165
165
  broadcast(data, topic, HOPS) { this.gossip.broadcastToAll(data, topic, HOPS); }
166
166
 
@@ -201,7 +201,7 @@ export class Node {
201
201
  onMessageData(callback) { this.messager.on('message', callback); }
202
202
 
203
203
  /** Triggered when a new gossip message is received.
204
- * @param {function} callback can use arguments: (senderId:string, topic:string, data:any) */
204
+ * @param {function} callback can use arguments: (senderId:string, data:any, HOPS:number) */
205
205
  onGossipData(callback) { this.gossip.on('gossip', callback); }
206
206
 
207
207
  /** Triggered when a new signal offer is received from another peer.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hive-p2p/server",
3
- "version": "1.0.55",
3
+ "version": "1.0.56",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },