Skip to content

Commit 808b782

Browse files
JulesFiliotfelicio
andauthored
feat(status-js): publish & listen to shards 32, 128 and 256
* feat(status-js): publish & listen to shards 32, 128 and 256 * chore: add changeset for status-js * chore(status-js/chat): remove magic number * chore(status-js): remove deprecated shardInfo property for waku nodes - use networkConfig * chore(status-js): remove unnecessary try/catch --------- Co-authored-by: Felicio <[email protected]>
1 parent 8d4b40e commit 808b782

File tree

6 files changed

+223
-217
lines changed

6 files changed

+223
-217
lines changed

.changeset/happy-masks-train.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@status-im/js': patch
3+
---
4+
5+
feat: publish & listen to shards 32, 128 and 256

packages/status-js/src/client/chat.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { create, toBinary } from '@bufbuild/protobuf'
22
import { createDecoder } from '@waku/message-encryption/symmetric'
33

4+
import { getRoutingInfo, SHARDS } from '../consts/waku'
45
import { containsOnlyEmoji } from '../helpers/contains-only-emoji'
56
import { ApplicationMetadataMessage_Type } from '../protos/application-metadata-message_pb'
67
import {
@@ -49,6 +50,8 @@ export type ChatMessage = ChatMessageProto & {
4950

5051
type FetchedMessage = { messageId: string; timestamp?: Date }
5152

53+
const MESSAGES_PAGINATION_LIMIT = 50
54+
5255
export class Chat {
5356
private readonly client: Client
5457
#clock: bigint
@@ -211,31 +214,28 @@ export class Chat {
211214
endTime = new Date()
212215
}
213216

214-
await this.client.waku.store.queryWithOrderedCallback(
215-
[
216-
createDecoder(
217-
this.contentTopic,
218-
{
219-
clusterId: 16,
220-
shardId: 32,
221-
pubsubTopic: '/waku/2/rs/16/32',
222-
},
223-
this.symmetricKey,
224-
),
225-
],
226-
wakuMessage => {
227-
this.#fetchingMessages = true
228-
this.client.handleWakuMessage(wakuMessage)
229-
this.#fetchingMessages = false
230-
},
231-
{
232-
timeStart: startTime,
233-
timeEnd: endTime,
234-
paginationLimit: 50,
235-
// most recent page first
236-
paginationForward: false,
237-
},
238-
)
217+
for (const shardId of SHARDS) {
218+
const decoder = createDecoder(
219+
this.contentTopic,
220+
getRoutingInfo(shardId),
221+
this.symmetricKey,
222+
)
223+
await this.client.waku.store.queryWithOrderedCallback(
224+
[decoder],
225+
wakuMessage => {
226+
this.#fetchingMessages = true
227+
this.client.handleWakuMessage(wakuMessage)
228+
this.#fetchingMessages = false
229+
},
230+
{
231+
timeStart: startTime,
232+
timeEnd: endTime,
233+
paginationLimit: MESSAGES_PAGINATION_LIMIT,
234+
// most recent page first
235+
paginationForward: false,
236+
},
237+
)
238+
}
239239

240240
this.#previousFetchedStartTime = startTime
241241

packages/status-js/src/client/client.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { createLightNode, waitForRemotePeer } from '@waku/sdk'
1010
import { hexToBytes } from 'ethereum-cryptography/utils'
1111

1212
import { peers } from '../consts/peers'
13+
import { CLUSTER_ID, getRoutingInfo, SHARDS } from '../consts/waku'
1314
import { ApplicationMetadataMessageSchema } from '../protos/application-metadata-message_pb'
1415
import { Account } from './account'
1516
import { ActivityCenter } from './activityCenter'
@@ -153,6 +154,9 @@ class Client {
153154
* Note: Not supported in light mode.
154155
*/
155156
relayKeepAlive: 0,
157+
networkConfig: {
158+
clusterId: CLUSTER_ID,
159+
},
156160
libp2p: {
157161
peerDiscovery: [
158162
/**
@@ -169,10 +173,6 @@ class Client {
169173
}),
170174
],
171175
},
172-
shardInfo: {
173-
clusterId: 16,
174-
shards: [32],
175-
},
176176
})
177177
await waku.start()
178178
await waitForRemotePeer(
@@ -272,18 +272,18 @@ class Client {
272272
}),
273273
)
274274

275-
await this.waku.lightPush.send(
276-
createEncoder({
277-
contentTopic,
278-
symKey,
279-
sigPrivKey: hexToBytes(this.#account.privateKey),
280-
routingInfo: {
281-
clusterId: 16,
282-
shardId: 32,
283-
pubsubTopic: '/waku/2/rs/16/32',
284-
},
285-
}),
286-
{ payload: message },
275+
await Promise.all(
276+
SHARDS.map(shardId =>
277+
this.waku.lightPush.send(
278+
createEncoder({
279+
contentTopic,
280+
symKey,
281+
sigPrivKey: hexToBytes(this.#account!.privateKey),
282+
routingInfo: getRoutingInfo(shardId),
283+
}),
284+
{ payload: message },
285+
),
286+
),
287287
)
288288
}
289289

packages/status-js/src/client/community/community.ts

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { create, toBinary } from '@bufbuild/protobuf'
22
import { createDecoder } from '@waku/message-encryption/symmetric'
33
import { hexToBytes } from 'ethereum-cryptography/utils'
44

5+
import { getRoutingInfo, SHARDS } from '../../consts/waku'
56
import { getDifferenceByKeys } from '../../helpers/get-difference-by-keys'
67
import { getObjectsDifference } from '../../helpers/get-objects-difference'
78
import { ApplicationMetadataMessage_Type } from '../../protos/application-metadata-message_pb'
@@ -92,44 +93,42 @@ export class Community {
9293
}
9394

9495
public fetch = async () => {
95-
// most recent page first
96-
await this.client.waku.store.queryWithOrderedCallback(
97-
[
98-
createDecoder(
99-
this.contentTopic,
100-
{
101-
clusterId: 16,
102-
shardId: 32,
103-
pubsubTopic: '/waku/2/rs/16/32',
104-
},
105-
this.symmetricKey,
106-
),
107-
],
108-
wakuMessage => {
109-
this.client.handleWakuMessage(wakuMessage)
110-
111-
if (this.description) {
112-
return true
113-
}
114-
},
115-
)
96+
for (const shardId of SHARDS) {
97+
const decoder = createDecoder(
98+
this.contentTopic,
99+
getRoutingInfo(shardId),
100+
this.symmetricKey,
101+
)
102+
// most recent page first
103+
await this.client.waku.store.queryWithOrderedCallback(
104+
[decoder],
105+
wakuMessage => {
106+
this.client.handleWakuMessage(wakuMessage)
107+
108+
if (this.description) {
109+
return true
110+
}
111+
},
112+
)
113+
114+
if (this.description) {
115+
break
116+
}
117+
}
116118

117119
return this.description
118120
}
119121

120122
private observe = async () => {
123+
const decoders = SHARDS.map(shardId =>
124+
createDecoder(
125+
this.contentTopic,
126+
getRoutingInfo(shardId),
127+
this.symmetricKey,
128+
),
129+
)
121130
await this.client.waku.filter.subscribe(
122-
[
123-
createDecoder(
124-
this.contentTopic,
125-
{
126-
clusterId: 16,
127-
shardId: 32,
128-
pubsubTopic: '/waku/2/rs/16/32',
129-
},
130-
this.symmetricKey,
131-
),
132-
],
131+
decoders,
133132
this.client.handleWakuMessage,
134133
)
135134
}
@@ -149,23 +148,21 @@ export class Community {
149148

150149
this.chats.set(chatUuid, chat)
151150

152-
const decoder = createDecoder(
153-
chat.contentTopic,
154-
{
155-
clusterId: 16,
156-
shardId: 32,
157-
pubsubTopic: '/waku/2/rs/16/32',
158-
},
159-
chat.symmetricKey,
151+
const decoders = SHARDS.map(shardId =>
152+
createDecoder(
153+
chat.contentTopic,
154+
getRoutingInfo(shardId),
155+
chat.symmetricKey,
156+
),
160157
)
161158

162159
await this.client.waku.filter.subscribe(
163-
[decoder],
160+
decoders,
164161
this.client.handleWakuMessage,
165162
)
166163

167164
const unobserveFn = () => {
168-
this.client.waku.filter.unsubscribe([decoder])
165+
this.client.waku.filter.unsubscribe(decoders)
169166
}
170167

171168
this.#chatUnobserveFns.set(chat.contentTopic, unobserveFn)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const CLUSTER_ID = 16
2+
export const SHARDS = [32, 128, 256] as const
3+
4+
export const getRoutingInfo = (shardId: number) => ({
5+
clusterId: CLUSTER_ID,
6+
shardId,
7+
pubsubTopic: `/waku/2/rs/${CLUSTER_ID}/${shardId}`,
8+
})

0 commit comments

Comments
 (0)