@hive-p2p/server 1.0.58 → 1.0.60

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
@@ -1,5 +1,5 @@
1
1
  import { CLOCK } from '../services/clock.mjs';
2
- import { GOSSIP, UNICAST, LOG_CSS } from './config.mjs';
2
+ import { NODE, GOSSIP, UNICAST, LOG_CSS } from './config.mjs';
3
3
 
4
4
  // TRUST_BALANCE = seconds of ban if negative - never exceed MAX_TRUST if positive
5
5
  // Growing each second by 1000ms until 0
@@ -38,6 +38,7 @@ export class Arbiter {
38
38
  * - trustBalance = milliseconds of ban if negative
39
39
  * @type {Record<string, number>} */
40
40
  trustBalances = {};
41
+ manualBanList = new Set();
41
42
  bytesCounters = { gossip: {}, unicast: {} };
42
43
  bytesCounterResetIn = 0;
43
44
 
@@ -73,7 +74,15 @@ export class Arbiter {
73
74
  if (delta && this.verbose > 3) console.log(`%c(Arbiter: ${this.id}) ${peerId} +${delta}ms (${reason}). Updated: ${this.trustBalances[peerId]}ms.`, LOG_CSS.ARBITER);
74
75
  if (this.isBanished(peerId) && this.verbose > 1) console.log(`%c(Arbiter: ${this.id}) Peer ${peerId} is now banished.`, LOG_CSS.ARBITER);
75
76
  }
76
- isBanished(peerId = 'toto') { return (this.trustBalances[peerId] || 0) < 0; }
77
+ /** MANUAL BAN (if enabled) @param {string} peerId */
78
+ setBanished(peerId) {
79
+ if (!this.manualBanList.has(peerId)) this.manualBanList.add(peerId);
80
+ }
81
+ /** Check if a peer is banished | based on trustBalances or manualBanList @param {string} peerId */
82
+ isBanished(peerId) {
83
+ if (!NODE.MANUAL_BAN_MODE) return (this.trustBalances[peerId] || 0) < 0;
84
+ else return this.manualBanList.has(peerId);
85
+ }
77
86
 
78
87
  // MESSAGE VERIFICATION
79
88
  /** @param {string} peerId @param {number} byteLength @param {'gossip' | 'unicast'} type */
package/core/config.mjs CHANGED
@@ -50,6 +50,8 @@ export const NODE = {
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 */
52
52
  IS_BROWSER: isNode ? false : true,
53
+ /** Enable manual banning of peers through the Arbiter module | Default: false (useful for consensus based ban, arbiter.trustBalances remain accessible) */
54
+ MANUAL_BAN_MODE: false
53
55
  }
54
56
 
55
57
  export const SERVICE = {
package/core/node.mjs CHANGED
@@ -168,12 +168,15 @@ export class Node {
168
168
  sendMessage(remoteId, data, type, spread = 1) { this.messager.sendUnicast(remoteId, data, type, spread); }
169
169
 
170
170
  /** Send a connection request to a peer */
171
- async tryConnectToPeer(targetId = 'toto', retry = 5) {
171
+ async tryConnectToPeer(targetId = 'toto', retry = 10) {
172
172
  if (this.peerStore.connected[targetId]) return; // already connected
173
173
  do {
174
174
  const { offerHash, readyOffer } = this.peerStore.offerManager.bestReadyOffer(100, false);
175
175
  if (!offerHash || !readyOffer) await new Promise(r => setTimeout(r, 1000)); // build in progress...
176
- else this.messager.sendUnicast(targetId, { signal: readyOffer.signal, offerHash }, 'signal_offer', 1);
176
+ else {
177
+ this.messager.sendUnicast(targetId, { signal: readyOffer.signal, offerHash }, 'signal_offer', 1);
178
+ return;
179
+ }
177
180
  } while (retry-- > 0);
178
181
  }
179
182
  destroy() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hive-p2p/server",
3
- "version": "1.0.58",
3
+ "version": "1.0.60",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },