1
- import type { GossipSub } from "@chainsafe/libp2p-gossipsub" ;
2
- import { noise } from "@chainsafe/libp2p-noise" ;
3
- import { identify } from "@libp2p/identify" ;
4
- import type { PeerDiscovery } from "@libp2p/interface" ;
5
- import { mplex } from "@libp2p/mplex" ;
6
- import { ping } from "@libp2p/ping" ;
7
- import { webSockets } from "@libp2p/websockets" ;
8
- import { all as filterAll } from "@libp2p/websockets/filters" ;
9
- import { wakuFilter , wakuLightPush , wakuMetadata , wakuStore } from "@waku/core" ;
10
- import { enrTree , wakuDnsDiscovery } from "@waku/dns-discovery" ;
11
- import {
12
- type CreateLibp2pOptions ,
13
- DefaultPubsubTopic ,
14
- type FullNode ,
15
- type IMetadata ,
16
- type Libp2p ,
17
- type Libp2pComponents ,
18
- type LightNode ,
19
- type ProtocolCreateOptions ,
20
- PubsubTopic ,
21
- type ShardInfo
22
- } from "@waku/interfaces" ;
23
- import { wakuLocalPeerCacheDiscovery } from "@waku/local-peer-cache-discovery" ;
24
- import { wakuPeerExchangeDiscovery } from "@waku/peer-exchange" ;
25
- import { RelayCreateOptions , wakuGossipSub , wakuRelay } from "@waku/relay" ;
26
- import { ensureShardingConfigured } from "@waku/utils" ;
27
- import { createLibp2p } from "libp2p" ;
1
+ import { wakuFilter , wakuLightPush , wakuStore } from "@waku/core" ;
2
+ import { type Libp2pComponents , type LightNode } from "@waku/interfaces" ;
28
3
29
- import { DefaultUserAgent , WakuNode , WakuOptions } from "./waku.js" ;
30
-
31
- const DEFAULT_NODE_REQUIREMENTS = {
32
- lightPush : 1 ,
33
- filter : 1 ,
34
- store : 1
35
- } ;
4
+ import { createLibp2pAndUpdateOptions } from "./utils/libp2p.js" ;
5
+ import { CreateWakuNodeOptions , WakuNode , WakuOptions } from "./waku.js" ;
36
6
37
7
export { Libp2pComponents } ;
38
8
39
9
/**
40
10
* Create a Waku node configured to use autosharding or static sharding.
41
11
*/
42
12
export async function createNode (
43
- options ?: ProtocolCreateOptions &
44
- Partial < WakuOptions > &
45
- Partial < RelayCreateOptions >
13
+ options : CreateWakuNodeOptions = { pubsubTopics : [ ] }
46
14
) : Promise < LightNode > {
47
- options = options ?? { pubsubTopics : [ ] } ;
48
-
49
15
if ( ! options . shardInfo ) {
50
16
throw new Error ( "Shard info must be set" ) ;
51
17
}
52
18
53
- const shardInfo = ensureShardingConfigured ( options . shardInfo ) ;
54
- options . pubsubTopics = shardInfo . pubsubTopics ;
55
- options . shardInfo = shardInfo . shardInfo ;
56
-
57
- const libp2pOptions = options ?. libp2p ?? { } ;
58
- const peerDiscovery = libp2pOptions . peerDiscovery ?? [ ] ;
59
- if ( options ?. defaultBootstrap ) {
60
- peerDiscovery . push ( ...defaultPeerDiscoveries ( shardInfo . pubsubTopics ) ) ;
61
- Object . assign ( libp2pOptions , { peerDiscovery } ) ;
62
- }
63
-
64
- const libp2p = await defaultLibp2p (
65
- shardInfo . shardInfo ,
66
- wakuGossipSub ( options ) ,
67
- libp2pOptions ,
68
- options ?. userAgent
69
- ) ;
19
+ const libp2p = await createLibp2pAndUpdateOptions ( options ) ;
70
20
71
21
const store = wakuStore ( options ) ;
72
22
const lightPush = wakuLightPush ( options ) ;
@@ -87,30 +37,9 @@ export async function createNode(
87
37
* Uses Waku Filter V2 by default.
88
38
*/
89
39
export async function createLightNode (
90
- options ?: ProtocolCreateOptions & Partial < WakuOptions >
40
+ options : CreateWakuNodeOptions = { }
91
41
) : Promise < LightNode > {
92
- options = options ?? { } ;
93
-
94
- const shardInfo = options . shardInfo
95
- ? ensureShardingConfigured ( options . shardInfo )
96
- : undefined ;
97
-
98
- options . pubsubTopics = shardInfo ?. pubsubTopics ??
99
- options . pubsubTopics ?? [ DefaultPubsubTopic ] ;
100
-
101
- const libp2pOptions = options ?. libp2p ?? { } ;
102
- const peerDiscovery = libp2pOptions . peerDiscovery ?? [ ] ;
103
- if ( options ?. defaultBootstrap ) {
104
- peerDiscovery . push ( ...defaultPeerDiscoveries ( options . pubsubTopics ) ) ;
105
- Object . assign ( libp2pOptions , { peerDiscovery } ) ;
106
- }
107
-
108
- const libp2p = await defaultLibp2p (
109
- shardInfo ?. shardInfo ,
110
- wakuGossipSub ( options ) ,
111
- libp2pOptions ,
112
- options ?. userAgent
113
- ) ;
42
+ const libp2p = await createLibp2pAndUpdateOptions ( options ) ;
114
43
115
44
const store = wakuStore ( options ) ;
116
45
const lightPush = wakuLightPush ( options ) ;
@@ -124,127 +53,3 @@ export async function createLightNode(
124
53
filter
125
54
) as LightNode ;
126
55
}
127
-
128
- /**
129
- * Create a Waku node that uses all Waku protocols.
130
- *
131
- * This helper is not recommended except if:
132
- * - you are interfacing with nwaku v0.11 or below
133
- * - you are doing some form of testing
134
- *
135
- * If you are building a full node, it is recommended to use
136
- * [nwaku](github.com/status-im/nwaku) and its JSON RPC API or wip REST API.
137
- *
138
- * @see https://github.com/status-im/nwaku/issues/1085
139
- * @internal
140
- */
141
- export async function createFullNode (
142
- options ?: ProtocolCreateOptions &
143
- Partial < WakuOptions > &
144
- Partial < RelayCreateOptions >
145
- ) : Promise < FullNode > {
146
- options = options ?? { pubsubTopics : [ ] } ;
147
-
148
- const shardInfo = options . shardInfo
149
- ? ensureShardingConfigured ( options . shardInfo )
150
- : undefined ;
151
-
152
- const pubsubTopics = shardInfo ?. pubsubTopics ??
153
- options . pubsubTopics ?? [ DefaultPubsubTopic ] ;
154
- options . pubsubTopics = pubsubTopics ;
155
- options . shardInfo = shardInfo ?. shardInfo ;
156
-
157
- const libp2pOptions = options ?. libp2p ?? { } ;
158
- const peerDiscovery = libp2pOptions . peerDiscovery ?? [ ] ;
159
- if ( options ?. defaultBootstrap ) {
160
- peerDiscovery . push ( ...defaultPeerDiscoveries ( pubsubTopics ) ) ;
161
- Object . assign ( libp2pOptions , { peerDiscovery } ) ;
162
- }
163
-
164
- const libp2p = await defaultLibp2p (
165
- shardInfo ?. shardInfo ,
166
- wakuGossipSub ( options ) ,
167
- libp2pOptions ,
168
- options ?. userAgent
169
- ) ;
170
-
171
- const store = wakuStore ( options ) ;
172
- const lightPush = wakuLightPush ( options ) ;
173
- const filter = wakuFilter ( options ) ;
174
- const relay = wakuRelay ( pubsubTopics ) ;
175
-
176
- return new WakuNode (
177
- options as WakuOptions ,
178
- libp2p ,
179
- store ,
180
- lightPush ,
181
- filter ,
182
- relay
183
- ) as FullNode ;
184
- }
185
-
186
- export function defaultPeerDiscoveries (
187
- pubsubTopics : PubsubTopic [ ]
188
- ) : ( ( components : Libp2pComponents ) => PeerDiscovery ) [ ] {
189
- const discoveries = [
190
- wakuDnsDiscovery ( [ enrTree [ "PROD" ] ] , DEFAULT_NODE_REQUIREMENTS ) ,
191
- wakuLocalPeerCacheDiscovery ( ) ,
192
- wakuPeerExchangeDiscovery ( pubsubTopics )
193
- ] ;
194
- return discoveries ;
195
- }
196
-
197
- type PubsubService = {
198
- pubsub ?: ( components : Libp2pComponents ) => GossipSub ;
199
- } ;
200
-
201
- type MetadataService = {
202
- metadata ?: ( components : Libp2pComponents ) => IMetadata ;
203
- } ;
204
-
205
- export async function defaultLibp2p (
206
- shardInfo ?: ShardInfo ,
207
- wakuGossipSub ?: PubsubService [ "pubsub" ] ,
208
- options ?: Partial < CreateLibp2pOptions > ,
209
- userAgent ?: string
210
- ) : Promise < Libp2p > {
211
- if ( ! options ?. hideWebSocketInfo && process . env . NODE_ENV !== "test" ) {
212
- /* eslint-disable no-console */
213
- console . info (
214
- "%cIgnore WebSocket connection failures" ,
215
- "background: gray; color: white; font-size: x-large"
216
- ) ;
217
- console . info (
218
- "%cWaku tries to discover peers and some of them are expected to fail" ,
219
- "background: gray; color: white; font-size: x-large"
220
- ) ;
221
- /* eslint-enable no-console */
222
- }
223
-
224
- const pubsubService : PubsubService = wakuGossipSub
225
- ? { pubsub : wakuGossipSub }
226
- : { } ;
227
-
228
- const metadataService : MetadataService = shardInfo
229
- ? { metadata : wakuMetadata ( shardInfo ) }
230
- : { } ;
231
-
232
- return createLibp2p ( {
233
- connectionManager : {
234
- minConnections : 1
235
- } ,
236
- transports : [ webSockets ( { filter : filterAll } ) ] ,
237
- streamMuxers : [ mplex ( ) ] ,
238
- connectionEncryption : [ noise ( ) ] ,
239
- ...options ,
240
- services : {
241
- identify : identify ( {
242
- agentVersion : userAgent ?? DefaultUserAgent
243
- } ) ,
244
- ping : ping ( ) ,
245
- ...metadataService ,
246
- ...pubsubService ,
247
- ...options ?. services
248
- }
249
- } ) as any as Libp2p ; // TODO: make libp2p include it;
250
- }
0 commit comments