1
1
import { SignableENR , ENR } from '@chainsafe/enr'
2
2
import { keys } from '@libp2p/crypto'
3
3
import { multiaddr } from '@multiformats/multiaddr'
4
- import {
5
- PortalNetwork ,
6
- NetworkId ,
7
- TransportLayer ,
8
- BaseNetwork ,
9
- HistoryNetwork ,
10
- StateNetwork ,
11
- } from '../../portalnetwork/src'
4
+ import { PortalNetwork , NetworkId , TransportLayer , BaseNetwork } from '../../portalnetwork/src'
12
5
import repl from 'repl'
13
6
import { hexToBytes } from '@ethereumjs/util'
14
7
import debug , { Debugger } from 'debug'
8
+ import { DEFAULT_BOOTNODES } from '../src/util/bootnodes'
15
9
16
10
class PortalNetworkRepl {
17
11
private node ?: PortalNetwork
@@ -27,7 +21,7 @@ class PortalNetworkRepl {
27
21
28
22
this . logger = debug ( this . enr . nodeId . slice ( 0 , 5 ) ) . extend ( 'Portal' )
29
23
30
- const nodeAddr = multiaddr ( `/ip4/127 .0.0.1 /udp/${ port } ` )
24
+ const nodeAddr = multiaddr ( `/ip4/0 .0.0.0 /udp/${ port } ` )
31
25
this . enr . setLocationMultiaddr ( nodeAddr )
32
26
33
27
this . node = await PortalNetwork . create ( {
@@ -41,19 +35,11 @@ class PortalNetworkRepl {
41
35
bindAddrs : { ip4 : nodeAddr } ,
42
36
privateKey,
43
37
} ,
38
+ bootnodes : DEFAULT_BOOTNODES . mainnet ,
44
39
} )
45
40
46
- this . historyNetwork = new HistoryNetwork ( {
47
- client : this . node ,
48
- networkId : NetworkId . HistoryNetwork
49
- } )
50
- this . stateNetwork = new StateNetwork ( {
51
- client : this . node ,
52
- networkId : NetworkId . StateNetwork
53
- } )
54
-
55
- this . node . networks [ NetworkId . HistoryNetwork ] = this . historyNetwork
56
- this . node . networks [ NetworkId . StateNetwork ] = this . stateNetwork
41
+ this . historyNetwork = this . node . network ( ) [ '0x500b' ] !
42
+ this . stateNetwork = this . node . network ( ) [ '0x500c' ] !
57
43
58
44
await this . node . start ( )
59
45
@@ -74,26 +60,49 @@ class PortalNetworkRepl {
74
60
private setupEventListeners ( ) : void {
75
61
if ( ! this . node ) return
76
62
77
- this . node . on ( 'SendTalkReq' , ( nodeId , requestId , payload ) =>
78
- this . logger ( 'Sent talk request' , { nodeId, requestId, payload } ) )
79
-
80
- this . node . on ( 'SendTalkResp' , ( nodeId , requestId , payload ) =>
81
- this . logger ( 'Received talk response' , { nodeId, requestId, payload } ) )
63
+ this . node . on ( 'SendTalkReq' , ( nodeId , requestId , payload ) =>
64
+ this . logger ( 'Sent talk request' , { nodeId, requestId, payload } ) ,
65
+ )
66
+
67
+ this . node . on ( 'SendTalkResp' , ( nodeId , requestId , payload ) =>
68
+ this . logger ( 'Received talk response' , { nodeId, requestId, payload } ) ,
69
+ )
82
70
}
83
71
84
72
private startRepl ( ) : void {
85
73
const replServer = repl . start ( 'portal> ' )
86
74
75
+ replServer . defineCommand ( 'debug' , {
76
+ help : 'Set debug log topics (e.g. *Portal*,*uTP*)' ,
77
+ async action ( topics : string ) {
78
+ const context = this . context as any
79
+ const portalRepl : PortalNetworkRepl = context . portalRepl
80
+ portalRepl . node ?. enableLog ( topics )
81
+ this . displayPrompt ( )
82
+ } ,
83
+ } )
84
+
85
+ replServer . defineCommand ( 'bootstrap' , {
86
+ help : 'Bootstrap the network' ,
87
+ async action ( ) {
88
+ const context = this . context as any
89
+ const portalRepl : PortalNetworkRepl = context . portalRepl
90
+ await portalRepl . node ?. bootstrap ( )
91
+ this . displayPrompt ( )
92
+ } ,
93
+ } )
94
+
87
95
replServer . defineCommand ( 'ping' , {
88
96
help : 'Send ping to network (history/state)' ,
89
97
async action ( network : string ) {
90
98
const context = this . context as any
91
99
const portalRepl : PortalNetworkRepl = context . portalRepl
92
100
93
101
try {
94
- const networkObj = network . toLowerCase ( ) === 'history'
95
- ? portalRepl . historyNetwork
96
- : portalRepl . stateNetwork
102
+ const networkObj =
103
+ network . toLowerCase ( ) === 'history'
104
+ ? portalRepl . historyNetwork
105
+ : portalRepl . stateNetwork
97
106
98
107
if ( ! networkObj ) {
99
108
portalRepl . logger ( `${ network } Network not initialized` )
@@ -107,7 +116,7 @@ class PortalNetworkRepl {
107
116
portalRepl . logger ( `Ping to ${ network } network failed` , error )
108
117
}
109
118
this . displayPrompt ( )
110
- }
119
+ } ,
111
120
} )
112
121
113
122
replServer . defineCommand ( 'findnodes' , {
@@ -117,7 +126,7 @@ class PortalNetworkRepl {
117
126
const portalRepl : PortalNetworkRepl = context . portalRepl
118
127
119
128
const [ network , enr , ...distancesStr ] = args . split ( ' ' )
120
- const distances = distancesStr . map ( d => parseInt ( d , 10 ) )
129
+ const distances = distancesStr . map ( ( d ) => parseInt ( d , 10 ) )
121
130
122
131
try {
123
132
switch ( network . toLowerCase ( ) ) {
@@ -144,14 +153,13 @@ class PortalNetworkRepl {
144
153
console . log ( 'Find nodes failed:' , error )
145
154
}
146
155
this . displayPrompt ( )
147
- }
156
+ } ,
148
157
} )
149
158
150
159
replServer . defineCommand ( 'findcontent' , {
151
160
help : 'Find content in network (history/state) with ENR and content key. Usage: .findcontent <network> <enr> <contentKey>' ,
152
161
async action ( args : string ) {
153
162
try {
154
-
155
163
const context = this . context as any
156
164
const portalRepl : PortalNetworkRepl = context . portalRepl
157
165
@@ -163,9 +171,10 @@ class PortalNetworkRepl {
163
171
164
172
const [ network , enr , contentKey ] = parts
165
173
166
- const networkObj = network . toLowerCase ( ) === 'history'
167
- ? portalRepl . historyNetwork
168
- : portalRepl . stateNetwork
174
+ const networkObj =
175
+ network . toLowerCase ( ) === 'history'
176
+ ? portalRepl . historyNetwork
177
+ : portalRepl . stateNetwork
169
178
170
179
if ( ! networkObj ) {
171
180
portalRepl . logger ( `${ network } Network not initialized` )
@@ -199,30 +208,32 @@ class PortalNetworkRepl {
199
208
console . log ( 'Find content operation failed' , error )
200
209
}
201
210
this . displayPrompt ( )
202
- }
211
+ } ,
203
212
} )
204
213
205
214
replServer . defineCommand ( 'offer' , {
206
215
help : 'Offer content to a specific network. Usage: .offer <network> <enr> <contentKey> <contentValue>' ,
207
216
async action ( args : string ) {
208
217
try {
209
-
210
218
const context = this . context as any
211
219
const portalRepl : PortalNetworkRepl = context . portalRepl
212
220
213
221
const parts = args . trim ( ) . split ( / \s + / )
214
222
215
223
if ( parts . length < 4 ) {
216
- portalRepl . logger ( 'Invalid arguments. Usage: .offer <network> <enr> <contentKey> <contentValue>' )
224
+ portalRepl . logger (
225
+ 'Invalid arguments. Usage: .offer <network> <enr> <contentKey> <contentValue>' ,
226
+ )
217
227
return this . displayPrompt ( )
218
228
}
219
229
220
230
const [ network , enr , contentKey , ...contentValueParts ] = parts
221
231
const contentValue = contentValueParts . join ( ' ' )
222
232
223
- const networkObj = network . toLowerCase ( ) === 'history'
224
- ? portalRepl . historyNetwork
225
- : portalRepl . stateNetwork
233
+ const networkObj =
234
+ network . toLowerCase ( ) === 'history'
235
+ ? portalRepl . historyNetwork
236
+ : portalRepl . stateNetwork
226
237
227
238
if ( ! networkObj ) {
228
239
portalRepl . logger ( `${ network } Network not initialized` )
@@ -253,14 +264,10 @@ class PortalNetworkRepl {
253
264
}
254
265
255
266
portalRepl . logger ( 'Sending content offer: network=%s, key=%o' , network , contentKeyBytes )
256
-
267
+
257
268
let offerResult
258
269
try {
259
- offerResult = await networkObj . sendOffer (
260
- parsedEnr ,
261
- contentKeyBytes ,
262
- contentValueBytes ,
263
- )
270
+ offerResult = await networkObj . sendOffer ( parsedEnr , contentKeyBytes , contentValueBytes )
264
271
265
272
portalRepl . logger ( 'Content offer result: %O' , offerResult )
266
273
} catch ( offerError ) {
@@ -269,9 +276,9 @@ class PortalNetworkRepl {
269
276
} catch ( error ) {
270
277
console . log ( 'Offer operation failed: %O' , error )
271
278
}
272
-
279
+
273
280
this . displayPrompt ( )
274
- }
281
+ } ,
275
282
} )
276
283
277
284
replServer . defineCommand ( 'status' , {
@@ -285,7 +292,7 @@ class PortalNetworkRepl {
285
292
portalRepl . logger ( 'Node initialized:' , ! ! portalRepl . node )
286
293
287
294
this . displayPrompt ( )
288
- }
295
+ } ,
289
296
} )
290
297
291
298
replServer . context . portalRepl = this
@@ -308,7 +315,7 @@ async function main() {
308
315
}
309
316
}
310
317
311
- main ( ) . catch ( err => {
318
+ main ( ) . catch ( ( err ) => {
312
319
console . log ( 'Unhandled error in main' , err )
313
320
process . exit ( 1 )
314
321
} )
0 commit comments