@@ -27,22 +27,28 @@ import {
27
27
28
28
export class FSWorld extends World {
29
29
proxy : FSProxy ;
30
+ proxyUrl : string ;
31
+ nodeUrls : Record < string , string > ;
30
32
server ?: ChildProcess ;
31
33
sysAcc : FSContract ;
32
34
33
35
constructor ( {
34
36
proxy,
35
37
gasPrice,
38
+ nodeUrls = { } ,
36
39
explorerUrl,
37
40
server,
38
41
} : {
39
42
proxy : FSProxy ;
40
43
gasPrice : number ;
44
+ nodeUrls ?: Record < string , string > ;
41
45
explorerUrl ?: string ;
42
46
server ?: ChildProcess ;
43
47
} ) {
44
48
super ( { chainId : "chain" , proxy, gasPrice, explorerUrl } ) ;
45
49
this . proxy = proxy ;
50
+ this . proxyUrl = this . proxy . proxyUrl ;
51
+ this . nodeUrls = nodeUrls ;
46
52
this . server = server ;
47
53
this . sysAcc = this . newContract ( fullU8AAddress ) ;
48
54
}
@@ -51,10 +57,11 @@ export class FSWorld extends World {
51
57
if ( params . chainId !== undefined ) {
52
58
throw new Error ( "chainId is not undefined." ) ;
53
59
}
54
- const { proxyUrl, gasPrice, explorerUrl, server } = params ;
60
+ const { proxyUrl, gasPrice, nodeUrls , explorerUrl, server } = params ;
55
61
return new FSWorld ( {
56
62
proxy : new FSProxy ( { proxyUrl, explorerUrl } ) ,
57
63
gasPrice : gasPrice ?? 1_000_000_000 ,
64
+ nodeUrls,
58
65
explorerUrl,
59
66
server,
60
67
} ) ;
@@ -80,8 +87,8 @@ export class FSWorld extends World {
80
87
gasPrice ?: number ;
81
88
explorerUrl ?: string ;
82
89
} & ProxyParams = { } ) : Promise < FSWorld > {
83
- const { server , proxyUrl } = await startProxy ( proxyParams ) ;
84
- return this . new ( { proxyUrl, gasPrice, explorerUrl, server } ) ;
90
+ const { proxyUrl , nodeUrls , server } = await startProxy ( proxyParams ) ;
91
+ return this . new ( { proxyUrl, nodeUrls , gasPrice, explorerUrl, server } ) ;
85
92
}
86
93
87
94
async restartProxy ( proxyParams : ProxyParams = { } ) {
@@ -368,25 +375,41 @@ const startProxy = async ({
368
375
throw error ;
369
376
} ) ;
370
377
378
+ const nodeUrls : Record < string , string > = { } ;
379
+
371
380
const proxyUrl = await new Promise < string > ( ( resolve ) => {
372
- server . stdout . on ( "data" , ( data : Buffer ) => {
373
- const addressRegex =
381
+ const onData = ( data : Buffer ) => {
382
+ const dataStr = data . toString ( ) ;
383
+
384
+ const nodeUrlRegex =
385
+ / n o d e s t a t u s + .{ 4 , 7 } a d d r e s s .{ 4 , 7 } = ( h t t p : \/ \/ [ ^ \s ] + ) .{ 4 , 7 } s h a r d .{ 4 , 7 } = ( \d + ) / g;
386
+ for ( const match of [ ...dataStr . matchAll ( nodeUrlRegex ) ] ) {
387
+ const [ , url , shard ] = match ;
388
+ nodeUrls [ shard ] = url ;
389
+ }
390
+
391
+ const proxyHostRegex =
374
392
/ c h a i n s i m u l a t o r ' s i s a c c e s s i b l e t h r o u g h t h e U R L ( [ \w \d . : ] + ) / ;
375
- const match = data . toString ( ) . match ( addressRegex ) ;
393
+ const match = dataStr . match ( proxyHostRegex ) ;
376
394
if ( match ) {
377
- resolve ( `http://${ match [ 1 ] } ` ) ;
395
+ const [ , host ] = match ;
396
+ resolve ( `http://${ host } ` ) ;
397
+ server . stdout . off ( "data" , onData ) ;
378
398
}
379
- } ) ;
399
+ } ;
400
+
401
+ server . stdout . on ( "data" , onData ) ;
380
402
} ) ;
381
403
382
- return { proxyUrl, server } ;
404
+ return { proxyUrl, nodeUrls , server } ;
383
405
} ;
384
406
385
407
type FSWorldNewParams =
386
408
| {
387
409
chainId ?: undefined ;
388
410
proxyUrl : string ;
389
411
gasPrice ?: number ;
412
+ nodeUrls ?: Record < string , string > ;
390
413
explorerUrl ?: string ;
391
414
server ?: ChildProcess ;
392
415
}
0 commit comments