Skip to content

Commit 7fcdb1f

Browse files
authored
Merge pull request #27 from Galeaf11/feat/SDK-1
chore: 🤖 update lib2p2 libs, add yamux
2 parents ca48726 + 1a871f1 commit 7fcdb1f

File tree

6 files changed

+456
-476
lines changed

6 files changed

+456
-476
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ temp
1212
coverage
1313
typedoc
1414
typechain
15+
16+
#ide
17+
.idea

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,12 @@
100100
"dotenv": "^16.1.4"
101101
},
102102
"dependencies": {
103-
"libp2p": "^0.43.3",
104-
"@chainsafe/libp2p-noise": "^11.0.4",
105-
"@chainsafe/libp2p-gossipsub": "^6.2.0",
106-
"@libp2p/mplex": "^7.1.3",
107-
"@libp2p/websockets": "^5.0.8",
103+
"libp2p": "^0.45.9",
104+
"@chainsafe/libp2p-gossipsub": "^8.0.0",
105+
"@chainsafe/libp2p-noise": "^12.0.1",
106+
"@chainsafe/libp2p-yamux": "^4.0.2",
107+
"@libp2p/mplex": "^8.0.3",
108+
"@libp2p/websockets": "^6.0.3",
108109
"ethers": "^6.4.0",
109110
"viem": "^1.0.7",
110111
"luxon": "^3.3.0",

src/client/index.ts

Lines changed: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
21
import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events';
32
import { createLibp2p, Libp2pOptions, Libp2p, Libp2pInit } from 'libp2p';
43
import { noise } from '@chainsafe/libp2p-noise';
@@ -9,18 +8,18 @@ import { multiaddr, Multiaddr } from '@multiformats/multiaddr';
98
import { peerIdFromString } from '@libp2p/peer-id';
109
import { PeerId } from '@libp2p/interface-peer-id';
1110
import { OPEN } from '@libp2p/interface-connection/status';
12-
import { Chain, stringify } from 'viem';
11+
import { stringify } from 'viem';
1312
import {
1413
OfferData,
1514
GenericOfferOptions,
1615
GenericQuery,
17-
Contracts,
1816
RequestData,
1917
} from '../shared/types.js';
2018
import { centerSub, CenterSub } from '../shared/pubsub.js';
21-
import { ChainsConfigOption, ServerAddressOption } from '../shared/options.js';
19+
import { ServerAddressOption } from '../shared/options.js';
2220
import { encodeText, decodeText } from '../utils/text.js';
2321
import { createLogger } from '../utils/logger.js';
22+
import { yamux } from '@chainsafe/libp2p-yamux';
2423

2524
const logger = createLogger('Client');
2625

@@ -168,7 +167,7 @@ export class Client<
168167
get connected(): boolean {
169168
return (
170169
!!this.libp2p &&
171-
(this.libp2p.pubsub as CenterSub).started &&
170+
(this.libp2p.services.pubsub as CenterSub).started &&
172171
this.libp2p.getPeers().length > 0 &&
173172
this.libp2p.getConnections(this.serverPeerId)[0]?.stat.status === OPEN
174173
);
@@ -183,23 +182,45 @@ export class Client<
183182
async start(): Promise<void> {
184183
const config: Libp2pOptions = {
185184
transports: [webSockets({ filter: all })],
186-
streamMuxers: [mplex()],
185+
streamMuxers: [yamux(), mplex()],
187186
connectionEncryption: [noise()],
188-
pubsub: centerSub({
189-
isClient: true,
190-
/** Client must be connected to the coordination server */
191-
directPeers: [
192-
{
193-
id: this.serverPeerId,
194-
addrs: [this.serverMultiaddr],
195-
},
196-
],
197-
}),
187+
services: {
188+
pubsub: centerSub({
189+
isClient: true,
190+
/** Client must be connected to the coordination server */
191+
directPeers: [
192+
{
193+
id: this.serverPeerId,
194+
addrs: [this.serverMultiaddr],
195+
},
196+
],
197+
}),
198+
},
199+
connectionManager: {
200+
maxPeerAddrsToDial: 10,
201+
minConnections: 0,
202+
maxConnections: 100,
203+
maxParallelDials: 20,
204+
},
205+
connectionGater: {
206+
//todo check all settings
207+
denyDialPeer: async () => Promise.resolve(false),
208+
denyDialMultiaddr: async () => Promise.resolve(false),
209+
denyInboundConnection: async () => Promise.resolve(false),
210+
denyOutboundConnection: async () => Promise.resolve(false),
211+
denyInboundEncryptedConnection: async () => Promise.resolve(false),
212+
denyOutboundEncryptedConnection: async () => Promise.resolve(false),
213+
denyInboundUpgradedConnection: async () => Promise.resolve(false),
214+
denyOutboundUpgradedConnection: async () => Promise.resolve(false),
215+
denyInboundRelayReservation: async () => Promise.resolve(false),
216+
denyOutboundRelayedConnection: async () => Promise.resolve(false),
217+
denyInboundRelayedConnection: async () => Promise.resolve(false),
218+
},
198219
...this.libp2pInit,
199220
};
200221
this.libp2p = await createLibp2p(config);
201222

202-
(this.libp2p.pubsub as CenterSub).addEventListener(
223+
(this.libp2p.services.pubsub as CenterSub).addEventListener(
203224
'gossipsub:heartbeat',
204225
() => {
205226
this.dispatchEvent(new CustomEvent<void>('heartbeat'));
@@ -208,7 +229,7 @@ export class Client<
208229

209230
this.libp2p.addEventListener('peer:connect', ({ detail }) => {
210231
try {
211-
if (detail.remotePeer.equals(this.serverPeerId)) {
232+
if (detail.equals(this.serverPeerId)) {
212233
this.dispatchEvent(new CustomEvent<void>('connected'));
213234
logger.trace(
214235
'🔗 Client connected to server at:',
@@ -222,7 +243,7 @@ export class Client<
222243

223244
this.libp2p.addEventListener('peer:disconnect', ({ detail }) => {
224245
try {
225-
if (detail.remotePeer.equals(this.serverPeerId)) {
246+
if (detail.equals(this.serverPeerId)) {
226247
this.dispatchEvent(new CustomEvent<void>('disconnected'));
227248
logger.trace(
228249
'🔌 Client disconnected from server at:',
@@ -234,34 +255,37 @@ export class Client<
234255
}
235256
});
236257

237-
this.libp2p.pubsub.addEventListener('message', ({ detail }) => {
238-
logger.trace(`Message on topic ${detail.topic}`);
258+
(this.libp2p.services.pubsub as CenterSub).addEventListener(
259+
'message',
260+
({ detail }) => {
261+
logger.trace(`Message on topic ${detail.topic}`);
239262

240-
try {
241-
/** Check is the message is an offer */
242-
const offer = JSON.parse(decodeText(detail.data)) as OfferData<
243-
CustomRequestQuery,
244-
CustomOfferOptions
245-
>;
263+
try {
264+
/** Check is the message is an offer */
265+
const offer = JSON.parse(decodeText(detail.data)) as OfferData<
266+
CustomRequestQuery,
267+
CustomOfferOptions
268+
>;
246269

247-
// @todo Validate offer
270+
// @todo Validate offer
248271

249-
logger.trace('Offer received:', offer);
272+
logger.trace('Offer received:', offer);
250273

251-
// @todo Implement offer verification
274+
// @todo Implement offer verification
252275

253-
this.dispatchEvent(
254-
new CustomEvent<OfferData<CustomRequestQuery, CustomOfferOptions>>(
255-
'offer',
256-
{
257-
detail: offer,
258-
},
259-
),
260-
);
261-
} catch (error) {
262-
logger.error(error);
263-
}
264-
});
276+
this.dispatchEvent(
277+
new CustomEvent<OfferData<CustomRequestQuery, CustomOfferOptions>>(
278+
'offer',
279+
{
280+
detail: offer,
281+
},
282+
),
283+
);
284+
} catch (error) {
285+
logger.error(error);
286+
}
287+
},
288+
);
265289

266290
await this.libp2p.start();
267291
this.dispatchEvent(new CustomEvent<void>('start'));
@@ -279,7 +303,7 @@ export class Client<
279303
throw new Error('libp2p not initialized yet');
280304
}
281305

282-
this.libp2p.pubsub
306+
(this.libp2p.services.pubsub as CenterSub)
283307
.publish(request.topic, encodeText(stringify(request)))
284308
.then(() => {
285309
this.dispatchEvent(
@@ -302,7 +326,7 @@ export class Client<
302326
throw new Error('libp2p not initialized yet');
303327
}
304328

305-
this.libp2p.pubsub.subscribe(topic);
329+
(this.libp2p.services.pubsub as CenterSub).subscribe(topic);
306330
}
307331

308332
/**
@@ -316,7 +340,7 @@ export class Client<
316340
throw new Error('libp2p not initialized yet');
317341
}
318342

319-
this.libp2p.pubsub.unsubscribe(topic);
343+
(this.libp2p.services.pubsub as CenterSub).unsubscribe(topic);
320344
}
321345

322346
/**

src/node/index.ts

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { CenterSub, centerSub } from '../shared/pubsub.js';
3232
import { RequestEvent } from './requestManager.js';
3333
import { decodeText, encodeText } from '../utils/text.js';
3434
import { createLogger } from '../utils/logger.js';
35+
import { yamux } from '@chainsafe/libp2p-yamux';
3536

3637
const logger = createLogger('Node');
3738

@@ -233,7 +234,7 @@ export class Node<
233234
get connected(): boolean {
234235
return (
235236
!!this.libp2p &&
236-
(this.libp2p.pubsub as CenterSub).started &&
237+
(this.libp2p.services.pubsub as CenterSub).started &&
237238
this.libp2p.getPeers().length > 0 &&
238239
this.libp2p.getConnections(this.serverPeerId)[0]?.stat.status === OPEN
239240
);
@@ -250,7 +251,7 @@ export class Node<
250251
}
251252

252253
for (const topic of this.topics) {
253-
this.libp2p.pubsub.subscribe(topic);
254+
(this.libp2p.services.pubsub as CenterSub).subscribe(topic);
254255
logger.trace(`Node subscribed to topic #${topic}`);
255256
}
256257

@@ -268,7 +269,7 @@ export class Node<
268269
}
269270

270271
for (const topic of this.topics) {
271-
this.libp2p.pubsub.unsubscribe(topic);
272+
(this.libp2p.services.pubsub as CenterSub).unsubscribe(topic);
272273
logger.trace(`Node unsubscribed from topic #${topic}`);
273274
}
274275

@@ -329,7 +330,7 @@ export class Node<
329330
});
330331
logger.trace(`Offer #${offer.id} is built`);
331332

332-
await this.libp2p.pubsub.publish(
333+
await (this.libp2p.services.pubsub as CenterSub).publish(
333334
offer.request.id,
334335
encodeText(stringify(offer)),
335336
);
@@ -347,22 +348,29 @@ export class Node<
347348
async start(): Promise<void> {
348349
const config: Libp2pOptions = {
349350
transports: [webSockets({ filter: all })],
350-
streamMuxers: [mplex()],
351+
streamMuxers: [yamux(), mplex()],
351352
connectionEncryption: [noise()],
352-
pubsub: centerSub({
353-
isClient: true,
354-
directPeers: [
355-
{
356-
id: this.serverPeerId,
357-
addrs: [this.serverMultiaddr],
358-
},
359-
],
360-
}),
353+
services: {
354+
pubsub: centerSub({
355+
isClient: true,
356+
directPeers: [
357+
{
358+
id: this.serverPeerId,
359+
addrs: [this.serverMultiaddr],
360+
},
361+
],
362+
}),
363+
},
364+
connectionManager: {
365+
maxPeerAddrsToDial: 10,
366+
minConnections: 0,
367+
maxConnections: 10000,
368+
maxParallelDials: 20,
369+
},
361370
...this.libp2pInit,
362371
};
363372
this.libp2p = await createLibp2p(config);
364-
365-
(this.libp2p.pubsub as CenterSub).addEventListener(
373+
(this.libp2p.services.pubsub as CenterSub).addEventListener(
366374
'gossipsub:heartbeat',
367375
() => {
368376
this.dispatchEvent(new CustomEvent<void>('heartbeat'));
@@ -371,7 +379,7 @@ export class Node<
371379

372380
this.libp2p.addEventListener('peer:connect', ({ detail }) => {
373381
try {
374-
if (detail.remotePeer.equals(this.serverPeerId)) {
382+
if (detail.equals(this.serverPeerId)) {
375383
this.dispatchEvent(new CustomEvent<void>('connected'));
376384
logger.trace(
377385
'🔗 Node connected to server at:',
@@ -385,7 +393,7 @@ export class Node<
385393

386394
this.libp2p.addEventListener('peer:disconnect', ({ detail }) => {
387395
try {
388-
if (detail.remotePeer.equals(this.serverPeerId)) {
396+
if (detail.equals(this.serverPeerId)) {
389397
this.dispatchEvent(new CustomEvent<void>('disconnected'));
390398
logger.trace(
391399
'🔌 Node disconnected from server at:',
@@ -397,23 +405,26 @@ export class Node<
397405
}
398406
});
399407

400-
this.libp2p.pubsub.addEventListener('message', ({ detail }) => {
401-
try {
402-
const topic = detail.topic;
403-
const data = decodeText(detail.data);
404-
logger.trace(`Message on topic ${detail.topic} with data: ${data}`);
405-
this.dispatchEvent(
406-
new CustomEvent<RawDecodedMessage>('message', {
407-
detail: {
408-
topic,
409-
data,
410-
},
411-
}),
412-
);
413-
} catch (error) {
414-
logger.error(error);
415-
}
416-
});
408+
(this.libp2p.services.pubsub as CenterSub).addEventListener(
409+
'message',
410+
({ detail }) => {
411+
try {
412+
const topic = detail.topic;
413+
const data = decodeText(detail.data);
414+
logger.trace(`Message on topic ${detail.topic} with data: ${data}`);
415+
this.dispatchEvent(
416+
new CustomEvent<RawDecodedMessage>('message', {
417+
detail: {
418+
topic,
419+
data,
420+
},
421+
}),
422+
);
423+
} catch (error) {
424+
logger.error(error);
425+
}
426+
},
427+
);
417428

418429
// Subscribe to topics
419430
this.enable();

0 commit comments

Comments
 (0)