@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,196 @@
1
+ import type { RPC } from './message/rpc.js'
2
+ import type { MessageId, MsgIdStr, PeerIdStr, TopicStr, MsgIdToStrFn } from './types.js'
3
+
4
+ export type CacheEntry = MessageId & {
5
+ topic: TopicStr
6
+ }
7
+
8
+ export type MessageCacheRecord = Pick<MessageCacheEntry, 'message' | 'originatingPeers'>
9
+
10
+ interface MessageCacheEntry {
11
+ message: RPC.Message
12
+ /**
13
+ * Tracks if the message has been validated by the app layer and thus forwarded
14
+ */
15
+ validated: boolean
16
+ /**
17
+ * Tracks peers that sent this message before it has been validated by the app layer
18
+ */
19
+ originatingPeers: Set<PeerIdStr>
20
+ /**
21
+ * For every message and peer the number of times this peer asked for the message
22
+ */
23
+ iwantCounts: Map<PeerIdStr, number>
24
+ }
25
+
26
+ export class MessageCache {
27
+ msgs = new Map<MsgIdStr, MessageCacheEntry>()
28
+
29
+ msgIdToStrFn: MsgIdToStrFn
30
+
31
+ history: CacheEntry[][] = []
32
+
33
+ /** Track with accounting of messages in the mcache that are not yet validated */
34
+ notValidatedCount = 0
35
+
36
+ /**
37
+ * Holds history of messages in timebounded history arrays
38
+ */
39
+ constructor (
40
+ /**
41
+ * The number of indices in the cache history used for gossiping. That means that a message
42
+ * won't get gossiped anymore when shift got called `gossip` many times after inserting the
43
+ * message in the cache.
44
+ */
45
+ private readonly gossip: number,
46
+ historyCapacity: number,
47
+ msgIdToStrFn: MsgIdToStrFn
48
+ ) {
49
+ this.msgIdToStrFn = msgIdToStrFn
50
+ for (let i = 0; i < historyCapacity; i++) {
51
+ this.history[i] = []
52
+ }
53
+ }
54
+
55
+ get size (): number {
56
+ return this.msgs.size
57
+ }
58
+
59
+ /**
60
+ * Adds a message to the current window and the cache
61
+ * Returns true if the message is not known and is inserted in the cache
62
+ */
63
+ put (messageId: MessageId, msg: RPC.Message, validated = false): boolean {
64
+ const { msgIdStr } = messageId
65
+ // Don't add duplicate entries to the cache.
66
+ if (this.msgs.has(msgIdStr)) {
67
+ return false
68
+ }
69
+
70
+ this.msgs.set(msgIdStr, {
71
+ message: msg,
72
+ validated,
73
+ originatingPeers: new Set(),
74
+ iwantCounts: new Map()
75
+ })
76
+
77
+ this.history[0].push({ ...messageId, topic: msg.topic })
78
+
79
+ if (!validated) {
80
+ this.notValidatedCount++
81
+ }
82
+
83
+ return true
84
+ }
85
+
86
+ observeDuplicate (msgId: MsgIdStr, fromPeerIdStr: PeerIdStr): void {
87
+ const entry = this.msgs.get(msgId)
88
+
89
+ if (
90
+ (entry != null) &&
91
+ // if the message is already validated, we don't need to store extra peers sending us
92
+ // duplicates as the message has already been forwarded
93
+ !entry.validated
94
+ ) {
95
+ entry.originatingPeers.add(fromPeerIdStr)
96
+ }
97
+ }
98
+
99
+ /**
100
+ * Retrieves a message from the cache by its ID, if it is still present
101
+ */
102
+ get (msgId: Uint8Array): RPC.Message | undefined {
103
+ return this.msgs.get(this.msgIdToStrFn(msgId))?.message
104
+ }
105
+
106
+ /**
107
+ * Increases the iwant count for the given message by one and returns the message together
108
+ * with the iwant if the message exists.
109
+ */
110
+ getWithIWantCount (msgIdStr: string, p: string): { msg: RPC.Message, count: number } | null {
111
+ const msg = this.msgs.get(msgIdStr)
112
+ if (msg == null) {
113
+ return null
114
+ }
115
+
116
+ const count = (msg.iwantCounts.get(p) ?? 0) + 1
117
+ msg.iwantCounts.set(p, count)
118
+
119
+ return { msg: msg.message, count }
120
+ }
121
+
122
+ /**
123
+ * Retrieves a list of message IDs for a set of topics
124
+ */
125
+ getGossipIDs (topics: Set<string>): Map<string, Uint8Array[]> {
126
+ const msgIdsByTopic = new Map<string, Uint8Array[]>()
127
+ for (let i = 0; i < this.gossip; i++) {
128
+ this.history[i].forEach((entry) => {
129
+ const msg = this.msgs.get(entry.msgIdStr)
130
+ if ((msg?.validated ?? false) && topics.has(entry.topic)) {
131
+ let msgIds = msgIdsByTopic.get(entry.topic)
132
+ if (msgIds == null) {
133
+ msgIds = []
134
+ msgIdsByTopic.set(entry.topic, msgIds)
135
+ }
136
+ msgIds.push(entry.msgId)
137
+ }
138
+ })
139
+ }
140
+
141
+ return msgIdsByTopic
142
+ }
143
+
144
+ /**
145
+ * Gets a message with msgId and tags it as validated.
146
+ * This function also returns the known peers that have sent us this message. This is used to
147
+ * prevent us sending redundant messages to peers who have already propagated it.
148
+ */
149
+ validate (msgId: MsgIdStr): MessageCacheRecord | null {
150
+ const entry = this.msgs.get(msgId)
151
+ if (entry == null) {
152
+ return null
153
+ }
154
+
155
+ if (!entry.validated) {
156
+ this.notValidatedCount--
157
+ }
158
+
159
+ const { message, originatingPeers } = entry
160
+ entry.validated = true
161
+ // Clear the known peers list (after a message is validated, it is forwarded and we no
162
+ // longer need to store the originating peers).
163
+ entry.originatingPeers = new Set()
164
+ return { message, originatingPeers }
165
+ }
166
+
167
+ /**
168
+ * Shifts the current window, discarding messages older than this.history.length of the cache
169
+ */
170
+ shift (): void {
171
+ const lastCacheEntries = this.history[this.history.length - 1]
172
+ lastCacheEntries.forEach((cacheEntry) => {
173
+ const entry = this.msgs.get(cacheEntry.msgIdStr)
174
+ if (entry != null) {
175
+ this.msgs.delete(cacheEntry.msgIdStr)
176
+ if (!entry.validated) {
177
+ this.notValidatedCount--
178
+ }
179
+ }
180
+ })
181
+
182
+ this.history.pop()
183
+ this.history.unshift([])
184
+ }
185
+
186
+ remove (msgId: MsgIdStr): MessageCacheRecord | null {
187
+ const entry = this.msgs.get(msgId)
188
+ if (entry == null) {
189
+ return null
190
+ }
191
+
192
+ // Keep the message on the history vector, it will be dropped on a shift()
193
+ this.msgs.delete(msgId)
194
+ return entry
195
+ }
196
+ }