@libp2p/gossipsub 14.1.1-6059227cb

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.
Files changed (160) hide show
  1. package/README.md +85 -0
  2. package/dist/index.min.js +19 -0
  3. package/dist/index.min.js.map +7 -0
  4. package/dist/src/config.d.ts +32 -0
  5. package/dist/src/config.d.ts.map +1 -0
  6. package/dist/src/config.js +2 -0
  7. package/dist/src/config.js.map +1 -0
  8. package/dist/src/constants.d.ts +213 -0
  9. package/dist/src/constants.d.ts.map +1 -0
  10. package/dist/src/constants.js +217 -0
  11. package/dist/src/constants.js.map +1 -0
  12. package/dist/src/errors.d.ts +9 -0
  13. package/dist/src/errors.d.ts.map +1 -0
  14. package/dist/src/errors.js +15 -0
  15. package/dist/src/errors.js.map +1 -0
  16. package/dist/src/gossipsub.d.ts +419 -0
  17. package/dist/src/gossipsub.d.ts.map +1 -0
  18. package/dist/src/gossipsub.js +2520 -0
  19. package/dist/src/gossipsub.js.map +1 -0
  20. package/dist/src/index.d.ts +344 -0
  21. package/dist/src/index.d.ts.map +1 -0
  22. package/dist/src/index.js +43 -0
  23. package/dist/src/index.js.map +1 -0
  24. package/dist/src/message/decodeRpc.d.ts +11 -0
  25. package/dist/src/message/decodeRpc.d.ts.map +1 -0
  26. package/dist/src/message/decodeRpc.js +10 -0
  27. package/dist/src/message/decodeRpc.js.map +1 -0
  28. package/dist/src/message/index.d.ts +2 -0
  29. package/dist/src/message/index.d.ts.map +1 -0
  30. package/dist/src/message/index.js +2 -0
  31. package/dist/src/message/index.js.map +1 -0
  32. package/dist/src/message/rpc.d.ts +99 -0
  33. package/dist/src/message/rpc.d.ts.map +1 -0
  34. package/dist/src/message/rpc.js +663 -0
  35. package/dist/src/message/rpc.js.map +1 -0
  36. package/dist/src/message-cache.d.ts +80 -0
  37. package/dist/src/message-cache.d.ts.map +1 -0
  38. package/dist/src/message-cache.js +144 -0
  39. package/dist/src/message-cache.js.map +1 -0
  40. package/dist/src/metrics.d.ts +467 -0
  41. package/dist/src/metrics.d.ts.map +1 -0
  42. package/dist/src/metrics.js +896 -0
  43. package/dist/src/metrics.js.map +1 -0
  44. package/dist/src/score/compute-score.d.ts +4 -0
  45. package/dist/src/score/compute-score.d.ts.map +1 -0
  46. package/dist/src/score/compute-score.js +75 -0
  47. package/dist/src/score/compute-score.js.map +1 -0
  48. package/dist/src/score/index.d.ts +4 -0
  49. package/dist/src/score/index.d.ts.map +1 -0
  50. package/dist/src/score/index.js +4 -0
  51. package/dist/src/score/index.js.map +1 -0
  52. package/dist/src/score/message-deliveries.d.ts +45 -0
  53. package/dist/src/score/message-deliveries.d.ts.map +1 -0
  54. package/dist/src/score/message-deliveries.js +75 -0
  55. package/dist/src/score/message-deliveries.js.map +1 -0
  56. package/dist/src/score/peer-score-params.d.ts +125 -0
  57. package/dist/src/score/peer-score-params.d.ts.map +1 -0
  58. package/dist/src/score/peer-score-params.js +159 -0
  59. package/dist/src/score/peer-score-params.js.map +1 -0
  60. package/dist/src/score/peer-score-thresholds.d.ts +31 -0
  61. package/dist/src/score/peer-score-thresholds.d.ts.map +1 -0
  62. package/dist/src/score/peer-score-thresholds.js +32 -0
  63. package/dist/src/score/peer-score-thresholds.js.map +1 -0
  64. package/dist/src/score/peer-score.d.ts +119 -0
  65. package/dist/src/score/peer-score.d.ts.map +1 -0
  66. package/dist/src/score/peer-score.js +459 -0
  67. package/dist/src/score/peer-score.js.map +1 -0
  68. package/dist/src/score/peer-stats.d.ts +32 -0
  69. package/dist/src/score/peer-stats.d.ts.map +1 -0
  70. package/dist/src/score/peer-stats.js +2 -0
  71. package/dist/src/score/peer-stats.js.map +1 -0
  72. package/dist/src/score/scoreMetrics.d.ts +23 -0
  73. package/dist/src/score/scoreMetrics.d.ts.map +1 -0
  74. package/dist/src/score/scoreMetrics.js +155 -0
  75. package/dist/src/score/scoreMetrics.js.map +1 -0
  76. package/dist/src/stream.d.ts +30 -0
  77. package/dist/src/stream.d.ts.map +1 -0
  78. package/dist/src/stream.js +55 -0
  79. package/dist/src/stream.js.map +1 -0
  80. package/dist/src/tracer.d.ts +53 -0
  81. package/dist/src/tracer.d.ts.map +1 -0
  82. package/dist/src/tracer.js +155 -0
  83. package/dist/src/tracer.js.map +1 -0
  84. package/dist/src/types.d.ts +148 -0
  85. package/dist/src/types.d.ts.map +1 -0
  86. package/dist/src/types.js +90 -0
  87. package/dist/src/types.js.map +1 -0
  88. package/dist/src/utils/buildRawMessage.d.ts +20 -0
  89. package/dist/src/utils/buildRawMessage.d.ts.map +1 -0
  90. package/dist/src/utils/buildRawMessage.js +151 -0
  91. package/dist/src/utils/buildRawMessage.js.map +1 -0
  92. package/dist/src/utils/create-gossip-rpc.d.ts +7 -0
  93. package/dist/src/utils/create-gossip-rpc.d.ts.map +1 -0
  94. package/dist/src/utils/create-gossip-rpc.js +31 -0
  95. package/dist/src/utils/create-gossip-rpc.js.map +1 -0
  96. package/dist/src/utils/index.d.ts +4 -0
  97. package/dist/src/utils/index.d.ts.map +1 -0
  98. package/dist/src/utils/index.js +4 -0
  99. package/dist/src/utils/index.js.map +1 -0
  100. package/dist/src/utils/messageIdToString.d.ts +5 -0
  101. package/dist/src/utils/messageIdToString.d.ts.map +1 -0
  102. package/dist/src/utils/messageIdToString.js +8 -0
  103. package/dist/src/utils/messageIdToString.js.map +1 -0
  104. package/dist/src/utils/msgIdFn.d.ts +10 -0
  105. package/dist/src/utils/msgIdFn.d.ts.map +1 -0
  106. package/dist/src/utils/msgIdFn.js +23 -0
  107. package/dist/src/utils/msgIdFn.js.map +1 -0
  108. package/dist/src/utils/multiaddr.d.ts +3 -0
  109. package/dist/src/utils/multiaddr.d.ts.map +1 -0
  110. package/dist/src/utils/multiaddr.js +15 -0
  111. package/dist/src/utils/multiaddr.js.map +1 -0
  112. package/dist/src/utils/publishConfig.d.ts +8 -0
  113. package/dist/src/utils/publishConfig.d.ts.map +1 -0
  114. package/dist/src/utils/publishConfig.js +25 -0
  115. package/dist/src/utils/publishConfig.js.map +1 -0
  116. package/dist/src/utils/set.d.ts +14 -0
  117. package/dist/src/utils/set.d.ts.map +1 -0
  118. package/dist/src/utils/set.js +41 -0
  119. package/dist/src/utils/set.js.map +1 -0
  120. package/dist/src/utils/shuffle.d.ts +7 -0
  121. package/dist/src/utils/shuffle.d.ts.map +1 -0
  122. package/dist/src/utils/shuffle.js +21 -0
  123. package/dist/src/utils/shuffle.js.map +1 -0
  124. package/dist/src/utils/time-cache.d.ts +22 -0
  125. package/dist/src/utils/time-cache.d.ts.map +1 -0
  126. package/dist/src/utils/time-cache.js +54 -0
  127. package/dist/src/utils/time-cache.js.map +1 -0
  128. package/package.json +142 -0
  129. package/src/config.ts +31 -0
  130. package/src/constants.ts +261 -0
  131. package/src/errors.ts +17 -0
  132. package/src/gossipsub.ts +3061 -0
  133. package/src/index.ts +404 -0
  134. package/src/message/decodeRpc.ts +19 -0
  135. package/src/message/index.ts +1 -0
  136. package/src/message/rpc.proto +58 -0
  137. package/src/message/rpc.ts +848 -0
  138. package/src/message-cache.ts +196 -0
  139. package/src/metrics.ts +1014 -0
  140. package/src/score/compute-score.ts +98 -0
  141. package/src/score/index.ts +3 -0
  142. package/src/score/message-deliveries.ts +95 -0
  143. package/src/score/peer-score-params.ts +316 -0
  144. package/src/score/peer-score-thresholds.ts +70 -0
  145. package/src/score/peer-score.ts +565 -0
  146. package/src/score/peer-stats.ts +33 -0
  147. package/src/score/scoreMetrics.ts +215 -0
  148. package/src/stream.ts +79 -0
  149. package/src/tracer.ts +177 -0
  150. package/src/types.ts +178 -0
  151. package/src/utils/buildRawMessage.ts +174 -0
  152. package/src/utils/create-gossip-rpc.ts +34 -0
  153. package/src/utils/index.ts +3 -0
  154. package/src/utils/messageIdToString.ts +8 -0
  155. package/src/utils/msgIdFn.ts +24 -0
  156. package/src/utils/multiaddr.ts +19 -0
  157. package/src/utils/publishConfig.ts +33 -0
  158. package/src/utils/set.ts +43 -0
  159. package/src/utils/shuffle.ts +21 -0
  160. package/src/utils/time-cache.ts +71 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"publishConfig.js","sourceRoot":"","sources":["../../../src/utils/publishConfig.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAA;AACzD,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAI/C;;GAEG;AACH,MAAM,UAAU,0BAA0B,CACxC,eAAwD,EACxD,MAAc,EACd,UAAsB;IAEtB,QAAQ,eAAe,EAAE,CAAC;QACxB,KAAK,UAAU,CAAC,CAAC,CAAC;YAChB,OAAO;gBACL,IAAI,EAAE,iBAAiB,CAAC,OAAO;gBAC/B,MAAM,EAAE,MAAM;gBACd,GAAG,EAAE,mBAAmB,CAAC,UAAU,CAAC,SAAS,CAAC;gBAC9C,UAAU;aACX,CAAA;QACH,CAAC;QAED,KAAK,YAAY;YACf,OAAO;gBACL,IAAI,EAAE,iBAAiB,CAAC,SAAS;aAClC,CAAA;QAEH;YACE,MAAM,IAAI,KAAK,CAAC,6BAA6B,eAAe,GAAG,CAAC,CAAA;IACpE,CAAC;AACH,CAAC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Exclude up to `ineed` items from a set if item meets condition `cond`
3
+ */
4
+ export declare function removeItemsFromSet<T>(superSet: Set<T>, ineed: number, cond?: (peer: T) => boolean): Set<T>;
5
+ /**
6
+ * Exclude up to `ineed` items from a set
7
+ */
8
+ export declare function removeFirstNItemsFromSet<T>(superSet: Set<T>, ineed: number): Set<T>;
9
+ export declare class MapDef<K, V> extends Map<K, V> {
10
+ private readonly getDefault;
11
+ constructor(getDefault: () => V);
12
+ getOrDefault(key: K): V;
13
+ }
14
+ //# sourceMappingURL=set.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"set.d.ts","sourceRoot":"","sources":["../../../src/utils/set.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAClC,QAAQ,EAAE,GAAG,CAAC,CAAC,CAAC,EAChB,KAAK,EAAE,MAAM,EACb,IAAI,GAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAoB,GACtC,GAAG,CAAC,CAAC,CAAC,CAaR;AAED;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,EAAG,QAAQ,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,GAAG,CAAC,CAAC,CAAC,CAEpF;AAED,qBAAa,MAAM,CAAC,CAAC,EAAE,CAAC,CAAE,SAAQ,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC;IAC5B,OAAO,CAAC,QAAQ,CAAC,UAAU;gBAAV,UAAU,EAAE,MAAM,CAAC;IAIjD,YAAY,CAAE,GAAG,EAAE,CAAC,GAAG,CAAC;CAQzB"}
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Exclude up to `ineed` items from a set if item meets condition `cond`
3
+ */
4
+ export function removeItemsFromSet(superSet, ineed, cond = () => true) {
5
+ const subset = new Set();
6
+ if (ineed <= 0) {
7
+ return subset;
8
+ }
9
+ for (const id of superSet) {
10
+ if (subset.size >= ineed) {
11
+ break;
12
+ }
13
+ if (cond(id)) {
14
+ subset.add(id);
15
+ superSet.delete(id);
16
+ }
17
+ }
18
+ return subset;
19
+ }
20
+ /**
21
+ * Exclude up to `ineed` items from a set
22
+ */
23
+ export function removeFirstNItemsFromSet(superSet, ineed) {
24
+ return removeItemsFromSet(superSet, ineed, () => true);
25
+ }
26
+ export class MapDef extends Map {
27
+ getDefault;
28
+ constructor(getDefault) {
29
+ super();
30
+ this.getDefault = getDefault;
31
+ }
32
+ getOrDefault(key) {
33
+ let value = super.get(key);
34
+ if (value === undefined) {
35
+ value = this.getDefault();
36
+ this.set(key, value);
37
+ }
38
+ return value;
39
+ }
40
+ }
41
+ //# sourceMappingURL=set.js.map
@@ -0,0 +1 @@
1
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
+ /**
2
+ * Pseudo-randomly shuffles an array
3
+ *
4
+ * Mutates the input array
5
+ */
6
+ export declare function shuffle<T>(arr: T[]): T[];
7
+ //# sourceMappingURL=shuffle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shuffle.d.ts","sourceRoot":"","sources":["../../../src/utils/shuffle.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAG,GAAG,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,CAezC"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Pseudo-randomly shuffles an array
3
+ *
4
+ * Mutates the input array
5
+ */
6
+ export function shuffle(arr) {
7
+ if (arr.length <= 1) {
8
+ return arr;
9
+ }
10
+ const randInt = () => {
11
+ return Math.floor(Math.random() * Math.floor(arr.length));
12
+ };
13
+ for (let i = 0; i < arr.length; i++) {
14
+ const j = randInt();
15
+ const tmp = arr[i];
16
+ arr[i] = arr[j];
17
+ arr[j] = tmp;
18
+ }
19
+ return arr;
20
+ }
21
+ //# sourceMappingURL=shuffle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shuffle.js","sourceRoot":"","sources":["../../../src/utils/shuffle.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,OAAO,CAAK,GAAQ;IAClC,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC;QACpB,OAAO,GAAG,CAAA;IACZ,CAAC;IACD,MAAM,OAAO,GAAG,GAAW,EAAE;QAC3B,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAA;IAC3D,CAAC,CAAA;IAED,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACpC,MAAM,CAAC,GAAG,OAAO,EAAE,CAAA;QACnB,MAAM,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC,CAAA;QAClB,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAA;QACf,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAA;IACd,CAAC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC"}
@@ -0,0 +1,22 @@
1
+ interface SimpleTimeCacheOpts {
2
+ validityMs: number;
3
+ }
4
+ /**
5
+ * This is similar to https://github.com/daviddias/time-cache/blob/master/src/index.js
6
+ * for our own need, we don't use lodash throttle to improve performance.
7
+ * This gives 4x - 5x performance gain compared to npm TimeCache
8
+ */
9
+ export declare class SimpleTimeCache<T> {
10
+ private readonly entries;
11
+ private readonly validityMs;
12
+ constructor(opts: SimpleTimeCacheOpts);
13
+ get size(): number;
14
+ /** Returns true if there was a key collision and the entry is dropped */
15
+ put(key: string | number, value: T): boolean;
16
+ prune(): void;
17
+ has(key: string): boolean;
18
+ get(key: string | number): T | undefined;
19
+ clear(): void;
20
+ }
21
+ export {};
22
+ //# sourceMappingURL=time-cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"time-cache.d.ts","sourceRoot":"","sources":["../../../src/utils/time-cache.ts"],"names":[],"mappings":"AAAA,UAAU,mBAAmB;IAC3B,UAAU,EAAE,MAAM,CAAA;CACnB;AAOD;;;;GAIG;AACH,qBAAa,eAAe,CAAC,CAAC;IAC5B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA4C;IACpE,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAQ;gBAEtB,IAAI,EAAE,mBAAmB;IAOtC,IAAI,IAAI,IAAK,MAAM,CAElB;IAED,yEAAyE;IACzE,GAAG,CAAE,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO;IAc7C,KAAK,IAAK,IAAI;IAcd,GAAG,CAAE,GAAG,EAAE,MAAM,GAAG,OAAO;IAI1B,GAAG,CAAE,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,GAAG,SAAS;IAKzC,KAAK,IAAK,IAAI;CAGf"}
@@ -0,0 +1,54 @@
1
+ /**
2
+ * This is similar to https://github.com/daviddias/time-cache/blob/master/src/index.js
3
+ * for our own need, we don't use lodash throttle to improve performance.
4
+ * This gives 4x - 5x performance gain compared to npm TimeCache
5
+ */
6
+ export class SimpleTimeCache {
7
+ entries = new Map();
8
+ validityMs;
9
+ constructor(opts) {
10
+ this.validityMs = opts.validityMs;
11
+ // allow negative validityMs so that this does not cache anything, spec test compliance.spec.js
12
+ // sends duplicate messages and expect peer to receive all. Application likely uses positive validityMs
13
+ }
14
+ get size() {
15
+ return this.entries.size;
16
+ }
17
+ /** Returns true if there was a key collision and the entry is dropped */
18
+ put(key, value) {
19
+ if (this.entries.has(key)) {
20
+ // Key collisions break insertion order in the entries cache, which break prune logic.
21
+ // prune relies on each iterated entry to have strictly ascending validUntilMs, else it
22
+ // won't prune expired entries and SimpleTimeCache will grow unexpectedly.
23
+ // As of Oct 2022 NodeJS v16, inserting the same key twice with different value does not
24
+ // change the key position in the iterator stream. A unit test asserts this behaviour.
25
+ return true;
26
+ }
27
+ this.entries.set(key, { value, validUntilMs: Date.now() + this.validityMs });
28
+ return false;
29
+ }
30
+ prune() {
31
+ const now = Date.now();
32
+ for (const [k, v] of this.entries.entries()) {
33
+ if (v.validUntilMs < now) {
34
+ this.entries.delete(k);
35
+ }
36
+ else {
37
+ // Entries are inserted with strictly ascending validUntilMs.
38
+ // Stop early to save iterations
39
+ break;
40
+ }
41
+ }
42
+ }
43
+ has(key) {
44
+ return this.entries.has(key);
45
+ }
46
+ get(key) {
47
+ const value = this.entries.get(key);
48
+ return (value != null) && value.validUntilMs >= Date.now() ? value.value : undefined;
49
+ }
50
+ clear() {
51
+ this.entries.clear();
52
+ }
53
+ }
54
+ //# sourceMappingURL=time-cache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"time-cache.js","sourceRoot":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B,CAAC;IAED,GAAG,CAAE,GAAoB;QACvB,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QACnC,OAAO,CAAC,KAAK,IAAI,IAAI,CAAC,IAAI,KAAK,CAAC,YAAY,IAAI,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;IACtF,CAAC;IAED,KAAK;QACH,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAA;IACtB,CAAC;CACF"}
package/package.json ADDED
@@ -0,0 +1,142 @@
1
+ {
2
+ "name": "@libp2p/gossipsub",
3
+ "version": "14.1.1-6059227cb",
4
+ "description": "A typescript implementation of gossipsub",
5
+ "files": [
6
+ "src",
7
+ "dist",
8
+ "!dist/test",
9
+ "!**/*.tsbuildinfo"
10
+ ],
11
+ "type": "module",
12
+ "types": "dist/src/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/src/index.d.ts",
16
+ "import": "./dist/src/index.js"
17
+ },
18
+ "./metrics": {
19
+ "types": "./dist/src/metrics.d.ts",
20
+ "import": "./dist/src/metrics.js"
21
+ },
22
+ "./message": {
23
+ "types": "./dist/src/message/index.d.ts",
24
+ "import": "./dist/src/message/index.js"
25
+ },
26
+ "./score": {
27
+ "types": "./dist/src/score/index.d.ts",
28
+ "import": "./dist/src/score/index.js"
29
+ },
30
+ "./types": {
31
+ "types": "./dist/src/types.d.ts",
32
+ "import": "./dist/src/types.js"
33
+ }
34
+ },
35
+ "typesVersions": {
36
+ "*": {
37
+ "*": [
38
+ "*",
39
+ "dist/src/*",
40
+ "dist/src/*/index"
41
+ ]
42
+ }
43
+ },
44
+ "scripts": {
45
+ "lint": "aegir lint",
46
+ "release": "aegir release --no-types",
47
+ "build": "aegir build",
48
+ "generate": "protons ./src/message/rpc.proto",
49
+ "pretest": "npm run build",
50
+ "pretest:e2e": "npm run build",
51
+ "benchmark": "yarn benchmark:files 'test/benchmark/**/*.test.ts'",
52
+ "benchmark:files": "NODE_OPTIONS='--max-old-space-size=4096 --loader=ts-node/esm' benchmark --config .benchrc.yaml --defaultBranch master",
53
+ "test": "aegir test -f './dist/test/*.spec.js'",
54
+ "test:unit": "aegir test -f './dist/test/unit/*.test.js' --target node",
55
+ "test:e2e": "aegir test -f './dist/test/e2e/*.spec.js'",
56
+ "test:browser": "npm run test -- --target browser"
57
+ },
58
+ "repository": {
59
+ "type": "git",
60
+ "url": "git+https://github.com/ChainSafe/js-libp2p-gossipsub.git"
61
+ },
62
+ "keywords": [
63
+ "libp2p",
64
+ "pubsub",
65
+ "gossip"
66
+ ],
67
+ "author": "Cayman Nava",
68
+ "license": "Apache-2.0",
69
+ "bugs": {
70
+ "url": "https://github.com/ChainSafe/js-libp2p-gossipsub/issues"
71
+ },
72
+ "homepage": "https://github.com/ChainSafe/js-libp2p-gossipsub#readme",
73
+ "dependencies": {
74
+ "@libp2p/crypto": "5.1.8-6059227cb",
75
+ "@libp2p/interface": "2.11.0-6059227cb",
76
+ "@libp2p/interface-internal": "2.3.19-6059227cb",
77
+ "@libp2p/peer-id": "5.1.9-6059227cb",
78
+ "@libp2p/pubsub": "^10.0.0",
79
+ "@multiformats/multiaddr": "^13.0.1",
80
+ "denque": "^2.1.0",
81
+ "it-length-prefixed": "^10.0.1",
82
+ "it-pipe": "^3.0.1",
83
+ "it-pushable": "^3.2.3",
84
+ "multiformats": "^13.0.1",
85
+ "protons-runtime": "^5.5.0",
86
+ "uint8arraylist": "^2.4.8",
87
+ "uint8arrays": "^5.0.1"
88
+ },
89
+ "devDependencies": {
90
+ "@chainsafe/as-sha256": "^1.2.0",
91
+ "@dapplion/benchmark": "^1.0.0",
92
+ "@libp2p/floodsub": "10.1.46-6059227cb",
93
+ "@libp2p/interface-compliance-tests": "6.5.0-6059227cb",
94
+ "@libp2p/logger": "5.2.0-6059227cb",
95
+ "@libp2p/peer-store": "11.2.7-6059227cb",
96
+ "@types/node": "^22.18.1",
97
+ "@types/sinon": "^17.0.3",
98
+ "abortable-iterator": "^5.1.0",
99
+ "aegir": "^47.0.21",
100
+ "datastore-core": "^10.0.0",
101
+ "delay": "^6.0.0",
102
+ "it-all": "^3.0.6",
103
+ "mkdirp": "^3.0.1",
104
+ "p-defer": "^4.0.0",
105
+ "p-event": "^6.0.0",
106
+ "p-retry": "^7.0.0",
107
+ "p-wait-for": "^5.0.2",
108
+ "protons": "^7.5.0",
109
+ "sinon": "^21.0.0",
110
+ "sinon-ts": "^2.0.0",
111
+ "time-cache": "^0.3.0"
112
+ },
113
+ "engines": {
114
+ "npm": ">=8.7.0"
115
+ },
116
+ "eslintConfig": {
117
+ "extends": "ipfs",
118
+ "ignorePatterns": [
119
+ "src/message/rpc*",
120
+ "!.aegir.js"
121
+ ]
122
+ },
123
+ "contributors": [
124
+ "Cayman <caymannava@gmail.com>",
125
+ "Vasco Santos <vasco.santos@moxy.studio>",
126
+ "Mikerah <mikerahqc@protonmail.com>",
127
+ "Tuyen Nguyen <vutuyen2636@gmail.com>",
128
+ "Alex Potsides <alex@achingbrain.net>",
129
+ "Marin Petrunić <marin.petrunic@gmail.com>",
130
+ "Lion - dapplion <35266934+dapplion@users.noreply.github.com>",
131
+ "Gregory Markou <16929357+GregTheGreek@users.noreply.github.com>",
132
+ "Alan Shaw <alan.shaw@protocol.ai>",
133
+ "Tuyen <tuyen@wetrust.io>",
134
+ "Jacob Heun <jacobheun@gmail.com>",
135
+ "Patrick Michot <k0m0d0tr0n@gmail.com>",
136
+ "chainsafe <aidan@chainsafe.io>",
137
+ "Hugo Dias <hugomrdias@gmail.com>",
138
+ "Franck Royer <franck@royer.one>",
139
+ "ChainSafe <superadmin@chainsafe.io>"
140
+ ],
141
+ "sideEffects": false
142
+ }
package/src/config.ts ADDED
@@ -0,0 +1,31 @@
1
+ export interface GossipsubOptsSpec {
2
+ /** D sets the optimal degree for a Gossipsub topic mesh. */
3
+ D: number
4
+ /** Dlo sets the lower bound on the number of peers we keep in a Gossipsub topic mesh. */
5
+ Dlo: number
6
+ /** Dhi sets the upper bound on the number of peers we keep in a Gossipsub topic mesh. */
7
+ Dhi: number
8
+ /** Dscore affects how peers are selected when pruning a mesh due to over subscription. */
9
+ Dscore: number
10
+ /** Dout sets the quota for the number of outbound connections to maintain in a topic mesh. */
11
+ Dout: number
12
+ /**
13
+ * Dlazy affects the minimum number of peers we will emit gossip to at each
14
+ * heartbeat.
15
+ */
16
+ Dlazy: number
17
+ /** heartbeatInterval is the time between heartbeats in milliseconds */
18
+ heartbeatInterval: number
19
+ /**
20
+ * fanoutTTL controls how long we keep track of the fanout state. If it's been
21
+ * fanoutTTL milliseconds since we've published to a topic that we're not subscribed to,
22
+ * we'll delete the fanout map for that topic.
23
+ */
24
+ fanoutTTL: number
25
+ /** mcacheLength is the number of windows to retain full messages for IWANT responses */
26
+ mcacheLength: number
27
+ /** mcacheGossip is the number of windows to gossip about */
28
+ mcacheGossip: number
29
+ /** seenTTL is the number of milliseconds to retain message IDs in the seen cache */
30
+ seenTTL: number
31
+ }
@@ -0,0 +1,261 @@
1
+ export const second = 1000
2
+ export const minute = 60 * second
3
+
4
+ // Protocol identifiers
5
+
6
+ export const FloodsubID = '/floodsub/1.0.0'
7
+
8
+ /**
9
+ * The protocol ID for version 1.0.0 of the Gossipsub protocol
10
+ * It is advertised along with GossipsubIDv11 for backwards compatability
11
+ */
12
+ export const GossipsubIDv10 = '/meshsub/1.0.0'
13
+
14
+ /**
15
+ * The protocol ID for version 1.1.0 of the Gossipsub protocol
16
+ * See the spec for details about how v1.1.0 compares to v1.0.0:
17
+ * https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.1.md
18
+ */
19
+ export const GossipsubIDv11 = '/meshsub/1.1.0'
20
+
21
+ /**
22
+ * The protocol ID for version 1.2.0 of the Gossipsub protocol
23
+ * See the spec for details about how v1.2.0 compares to v1.1.0:
24
+ * https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.2.md
25
+ */
26
+ export const GossipsubIDv12 = '/meshsub/1.2.0'
27
+
28
+ // Overlay parameters
29
+
30
+ /**
31
+ * GossipsubD sets the optimal degree for a Gossipsub topic mesh. For example, if GossipsubD == 6,
32
+ * each peer will want to have about six peers in their mesh for each topic they're subscribed to.
33
+ * GossipsubD should be set somewhere between GossipsubDlo and GossipsubDhi.
34
+ */
35
+ export const GossipsubD = 6
36
+
37
+ /**
38
+ * GossipsubDlo sets the lower bound on the number of peers we keep in a Gossipsub topic mesh.
39
+ * If we have fewer than GossipsubDlo peers, we will attempt to graft some more into the mesh at
40
+ * the next heartbeat.
41
+ */
42
+ export const GossipsubDlo = 4
43
+
44
+ /**
45
+ * GossipsubDhi sets the upper bound on the number of peers we keep in a Gossipsub topic mesh.
46
+ * If we have more than GossipsubDhi peers, we will select some to prune from the mesh at the next heartbeat.
47
+ */
48
+ export const GossipsubDhi = 12
49
+
50
+ /**
51
+ * GossipsubDscore affects how peers are selected when pruning a mesh due to over subscription.
52
+ * At least GossipsubDscore of the retained peers will be high-scoring, while the remainder are
53
+ * chosen randomly.
54
+ */
55
+ export const GossipsubDscore = 4
56
+
57
+ /**
58
+ * GossipsubDout sets the quota for the number of outbound connections to maintain in a topic mesh.
59
+ * When the mesh is pruned due to over subscription, we make sure that we have outbound connections
60
+ * to at least GossipsubDout of the survivor peers. This prevents sybil attackers from overwhelming
61
+ * our mesh with incoming connections.
62
+ *
63
+ * GossipsubDout must be set below GossipsubDlo, and must not exceed GossipsubD / 2.
64
+ */
65
+ export const GossipsubDout = 2
66
+
67
+ // Gossip parameters
68
+
69
+ /**
70
+ * GossipsubHistoryLength controls the size of the message cache used for gossip.
71
+ * The message cache will remember messages for GossipsubHistoryLength heartbeats.
72
+ */
73
+ export const GossipsubHistoryLength = 5
74
+
75
+ /**
76
+ * GossipsubHistoryGossip controls how many cached message ids we will advertise in
77
+ * IHAVE gossip messages. When asked for our seen message IDs, we will return
78
+ * only those from the most recent GossipsubHistoryGossip heartbeats. The slack between
79
+ * GossipsubHistoryGossip and GossipsubHistoryLength allows us to avoid advertising messages
80
+ * that will be expired by the time they're requested.
81
+ *
82
+ * GossipsubHistoryGossip must be less than or equal to GossipsubHistoryLength to
83
+ * avoid a runtime panic.
84
+ */
85
+ export const GossipsubHistoryGossip = 3
86
+
87
+ /**
88
+ * GossipsubDlazy affects how many peers we will emit gossip to at each heartbeat.
89
+ * We will send gossip to at least GossipsubDlazy peers outside our mesh. The actual
90
+ * number may be more, depending on GossipsubGossipFactor and how many peers we're
91
+ * connected to.
92
+ */
93
+ export const GossipsubDlazy = 6
94
+
95
+ /**
96
+ * GossipsubGossipFactor affects how many peers we will emit gossip to at each heartbeat.
97
+ * We will send gossip to GossipsubGossipFactor * (total number of non-mesh peers), or
98
+ * GossipsubDlazy, whichever is greater.
99
+ */
100
+ export const GossipsubGossipFactor = 0.25
101
+
102
+ /**
103
+ * GossipsubGossipRetransmission controls how many times we will allow a peer to request
104
+ * the same message id through IWANT gossip before we start ignoring them. This is designed
105
+ * to prevent peers from spamming us with requests and wasting our resources.
106
+ */
107
+ export const GossipsubGossipRetransmission = 3
108
+
109
+ // Heartbeat interval
110
+
111
+ /**
112
+ * GossipsubHeartbeatInitialDelay is the short delay before the heartbeat timer begins
113
+ * after the router is initialized.
114
+ */
115
+ export const GossipsubHeartbeatInitialDelay = 100
116
+
117
+ /**
118
+ * GossipsubHeartbeatInterval controls the time between heartbeats.
119
+ */
120
+ export const GossipsubHeartbeatInterval = second
121
+
122
+ /**
123
+ * GossipsubFanoutTTL controls how long we keep track of the fanout state. If it's been
124
+ * GossipsubFanoutTTL since we've published to a topic that we're not subscribed to,
125
+ * we'll delete the fanout map for that topic.
126
+ */
127
+ export const GossipsubFanoutTTL = minute
128
+
129
+ /**
130
+ * GossipsubPrunePeers controls the number of peers to include in prune Peer eXchange.
131
+ * When we prune a peer that's eligible for PX (has a good score, etc), we will try to
132
+ * send them signed peer records for up to GossipsubPrunePeers other peers that we
133
+ * know of.
134
+ */
135
+ export const GossipsubPrunePeers = 16
136
+
137
+ /**
138
+ * GossipsubPruneBackoff controls the backoff time for pruned peers. This is how long
139
+ * a peer must wait before attempting to graft into our mesh again after being pruned.
140
+ * When pruning a peer, we send them our value of GossipsubPruneBackoff so they know
141
+ * the minimum time to wait. Peers running older versions may not send a backoff time,
142
+ * so if we receive a prune message without one, we will wait at least GossipsubPruneBackoff
143
+ * before attempting to re-graft.
144
+ */
145
+ export const GossipsubPruneBackoff = minute
146
+
147
+ /**
148
+ * Backoff to use when unsuscribing from a topic. Should not resubscribe to this topic before it expired.
149
+ */
150
+ export const GossipsubUnsubscribeBackoff = 10 * second
151
+
152
+ /**
153
+ * GossipsubPruneBackoffTicks is the number of heartbeat ticks for attempting to prune expired
154
+ * backoff timers.
155
+ */
156
+ export const GossipsubPruneBackoffTicks = 15
157
+
158
+ /**
159
+ * GossipsubConnectors controls the number of active connection attempts for peers obtained through PX.
160
+ */
161
+ export const GossipsubConnectors = 8
162
+
163
+ /**
164
+ * GossipsubMaxPendingConnections sets the maximum number of pending connections for peers attempted through px.
165
+ */
166
+ export const GossipsubMaxPendingConnections = 128
167
+
168
+ /**
169
+ * GossipsubConnectionTimeout controls the timeout for connection attempts.
170
+ */
171
+ export const GossipsubConnectionTimeout = 30 * second
172
+
173
+ /**
174
+ * GossipsubDirectConnectTicks is the number of heartbeat ticks for attempting to reconnect direct peers
175
+ * that are not currently connected.
176
+ */
177
+ export const GossipsubDirectConnectTicks = 300
178
+
179
+ /**
180
+ * GossipsubDirectConnectInitialDelay is the initial delay before opening connections to direct peers
181
+ */
182
+ export const GossipsubDirectConnectInitialDelay = second
183
+
184
+ /**
185
+ * GossipsubOpportunisticGraftTicks is the number of heartbeat ticks for attempting to improve the mesh
186
+ * with opportunistic grafting. Every GossipsubOpportunisticGraftTicks we will attempt to select some
187
+ * high-scoring mesh peers to replace lower-scoring ones, if the median score of our mesh peers falls
188
+ * below a threshold
189
+ */
190
+ export const GossipsubOpportunisticGraftTicks = 60
191
+
192
+ /**
193
+ * GossipsubOpportunisticGraftPeers is the number of peers to opportunistically graft.
194
+ */
195
+ export const GossipsubOpportunisticGraftPeers = 2
196
+
197
+ /**
198
+ * If a GRAFT comes before GossipsubGraftFloodThreshold has elapsed since the last PRUNE,
199
+ * then there is an extra score penalty applied to the peer through P7.
200
+ */
201
+ export const GossipsubGraftFloodThreshold = 10 * second
202
+
203
+ /**
204
+ * GossipsubMaxIHaveLength is the maximum number of messages to include in an IHAVE message.
205
+ * Also controls the maximum number of IHAVE ids we will accept and request with IWANT from a
206
+ * peer within a heartbeat, to protect from IHAVE floods. You should adjust this value from the
207
+ * default if your system is pushing more than 5000 messages in GossipsubHistoryGossip heartbeats;
208
+ * with the defaults this is 1666 messages/s.
209
+ */
210
+ export const GossipsubMaxIHaveLength = 5000
211
+
212
+ /**
213
+ * GossipsubMaxIHaveMessages is the maximum number of IHAVE messages to accept from a peer within a heartbeat.
214
+ */
215
+ export const GossipsubMaxIHaveMessages = 10
216
+
217
+ /**
218
+ * Time to wait for a message requested through IWANT following an IHAVE advertisement.
219
+ * If the message is not received within this window, a broken promise is declared and
220
+ * the router may apply bahavioural penalties.
221
+ */
222
+ export const GossipsubIWantFollowupTime = 3 * second
223
+
224
+ /**
225
+ * Time in milliseconds to keep message ids in the seen cache
226
+ */
227
+ export const GossipsubSeenTTL = 2 * minute
228
+
229
+ export const TimeCacheDuration = 120 * 1000
230
+
231
+ export const ERR_TOPIC_VALIDATOR_REJECT = 'ERR_TOPIC_VALIDATOR_REJECT'
232
+ export const ERR_TOPIC_VALIDATOR_IGNORE = 'ERR_TOPIC_VALIDATOR_IGNORE'
233
+
234
+ /**
235
+ * If peer score is better than this, we accept messages from this peer
236
+ * within ACCEPT_FROM_WHITELIST_DURATION_MS from the last time computing score.
237
+ */
238
+ export const ACCEPT_FROM_WHITELIST_THRESHOLD_SCORE = 0
239
+
240
+ /**
241
+ * If peer score >= ACCEPT_FROM_WHITELIST_THRESHOLD_SCORE, accept up to this
242
+ * number of messages from that peer.
243
+ */
244
+ export const ACCEPT_FROM_WHITELIST_MAX_MESSAGES = 128
245
+
246
+ /**
247
+ * If peer score >= ACCEPT_FROM_WHITELIST_THRESHOLD_SCORE, accept messages from
248
+ * this peer up to this time duration.
249
+ */
250
+ export const ACCEPT_FROM_WHITELIST_DURATION_MS = 1000
251
+
252
+ /**
253
+ * The default MeshMessageDeliveriesWindow to be used in metrics.
254
+ */
255
+ export const DEFAULT_METRIC_MESH_MESSAGE_DELIVERIES_WINDOWS = 1000
256
+
257
+ /** Wait for 1 more heartbeats before clearing a backoff */
258
+ export const BACKOFF_SLACK = 1
259
+
260
+ export const GossipsubIdontwantMinDataSize = 512
261
+ export const GossipsubIdontwantMaxMessages = 512
package/src/errors.ts ADDED
@@ -0,0 +1,17 @@
1
+ export class InvalidPeerScoreParamsError extends Error {
2
+ static name = 'InvalidPeerScoreParamsError'
3
+
4
+ constructor (message = 'Invalid peer score params') {
5
+ super(message)
6
+ this.name = 'InvalidPeerScoreParamsError'
7
+ }
8
+ }
9
+
10
+ export class InvalidPeerScoreThresholdsError extends Error {
11
+ static name = 'InvalidPeerScoreThresholdsError'
12
+
13
+ constructor (message = 'Invalid peer score thresholds') {
14
+ super(message)
15
+ this.name = 'InvalidPeerScoreThresholdsError'
16
+ }
17
+ }