@hive-p2p/server 1.0.56 → 1.0.57
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 +12 -7
- package/package.json +1 -1
package/core/arbiter.mjs
CHANGED
|
@@ -6,7 +6,8 @@ import { GOSSIP, UNICAST, LOG_CSS } from './config.mjs';
|
|
|
6
6
|
// Lowered each second by 100ms until 0 (avoid attacker growing balances on multiple disconnected peers)
|
|
7
7
|
|
|
8
8
|
const BYTES_COUNT_PERIOD = 10_000; // 10 seconds
|
|
9
|
-
const
|
|
9
|
+
const MAX_UNICAST_BYTES_PER_PERIOD = 1_000_000; // 1MB per period
|
|
10
|
+
const MAX_GOSSIP_BYTES_PER_PERIOD = 100_000; // 100KB per period
|
|
10
11
|
|
|
11
12
|
const MAX_TRUST = 3_600_000; // +3600 seconds = 1 hour of good behavior
|
|
12
13
|
export const TRUST_VALUES = {
|
|
@@ -37,7 +38,7 @@ export class Arbiter {
|
|
|
37
38
|
* - trustBalance = milliseconds of ban if negative
|
|
38
39
|
* @type {Record<string, number>} */
|
|
39
40
|
trustBalances = {};
|
|
40
|
-
bytesCounters = { gossip:
|
|
41
|
+
bytesCounters = { gossip: {}, unicast: {} };
|
|
41
42
|
bytesCounterResetIn = 0;
|
|
42
43
|
|
|
43
44
|
/** @param {string} selfId @param {import('./crypto-codex.mjs').CryptoCodex} cryptoCodex @param {number} verbose */
|
|
@@ -55,7 +56,7 @@ export class Arbiter {
|
|
|
55
56
|
// RESET GOSSIP BYTES COUNTER
|
|
56
57
|
if (this.bytesCounterResetIn - 1_000 > 0) return;
|
|
57
58
|
this.bytesCounterResetIn = BYTES_COUNT_PERIOD;
|
|
58
|
-
this.bytesCounters = { gossip:
|
|
59
|
+
this.bytesCounters = { gossip: {}, unicast: {} };
|
|
59
60
|
}
|
|
60
61
|
|
|
61
62
|
/** Call from HiveP2P module only!
|
|
@@ -76,10 +77,14 @@ export class Arbiter {
|
|
|
76
77
|
// MESSAGE VERIFICATION
|
|
77
78
|
/** @param {string} peerId @param {number} byteLength @param {'gossip' | 'unicast'} type */
|
|
78
79
|
countMessageBytes(peerId, byteLength, type) {
|
|
79
|
-
if (!this.bytesCounters[type]) this.bytesCounters[type] = 0;
|
|
80
|
-
this.bytesCounters[type] += byteLength;
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
if (!this.bytesCounters[type][peerId]) this.bytesCounters[type][peerId] = 0;
|
|
81
|
+
this.bytesCounters[type][peerId] += byteLength;
|
|
82
|
+
const [maxByte, penality] = type === 'gossip'
|
|
83
|
+
? [MAX_GOSSIP_BYTES_PER_PERIOD, TRUST_VALUES.GOSSIP_FLOOD]
|
|
84
|
+
: [MAX_UNICAST_BYTES_PER_PERIOD, TRUST_VALUES.UNICAST_FLOOD];
|
|
85
|
+
// If under the limit, return true -> else apply penality and return undefined
|
|
86
|
+
if (this.bytesCounters[type][peerId] < maxByte) return true;
|
|
87
|
+
return this.adjustTrust(peerId, penality, `Message ${type} flood detected`);
|
|
83
88
|
}
|
|
84
89
|
/** Call from HiveP2P module only! @param {string} from @param {any} message @param {Uint8Array} serialized @param {number} [powCheckFactor] default: 0.01 (1%) */
|
|
85
90
|
async digestMessage(from, message, serialized, powCheckFactor = .01) {
|