Skip to content

Commit f76130d

Browse files
committed
Expose node URLs
1 parent e3345db commit f76130d

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

xsuite/src/world/fsworld.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,21 @@ test.concurrent("FSWorld.proxy.blockNonce", async () => {
4444
});
4545
});
4646

47+
test.concurrent("FSWorld.start - URLs", async () => {
48+
const localhostRegex = /^http:\/\/localhost:\d+$/;
49+
using world = await FSWorld.start();
50+
expect(world.proxyUrl).toMatch(localhostRegex);
51+
expect(world.nodeUrls).toEqual({
52+
"0": expect.stringMatching(localhostRegex),
53+
"1": expect.stringMatching(localhostRegex),
54+
"2": expect.stringMatching(localhostRegex),
55+
"4294967295": expect.stringMatching(localhostRegex),
56+
});
57+
});
58+
4759
test.concurrent("FSWorld.start - port 3000", async () => {
4860
using world = await FSWorld.start({ binaryPort: 3000 });
49-
expect(world.proxy.proxyUrl).toEqual("http://localhost:3000");
61+
expect(world.proxyUrl).toEqual("http://localhost:3000");
5062
});
5163

5264
test.concurrent("FSWorld.start - epoch, round, nonce", async () => {

xsuite/src/world/fsworld.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,28 @@ import {
2727

2828
export class FSWorld extends World {
2929
proxy: FSProxy;
30+
proxyUrl: string;
31+
nodeUrls: Record<string, string>;
3032
server?: ChildProcess;
3133
sysAcc: FSContract;
3234

3335
constructor({
3436
proxy,
3537
gasPrice,
38+
nodeUrls = {},
3639
explorerUrl,
3740
server,
3841
}: {
3942
proxy: FSProxy;
4043
gasPrice: number;
44+
nodeUrls?: Record<string, string>;
4145
explorerUrl?: string;
4246
server?: ChildProcess;
4347
}) {
4448
super({ chainId: "chain", proxy, gasPrice, explorerUrl });
4549
this.proxy = proxy;
50+
this.proxyUrl = this.proxy.proxyUrl;
51+
this.nodeUrls = nodeUrls;
4652
this.server = server;
4753
this.sysAcc = this.newContract(fullU8AAddress);
4854
}
@@ -51,10 +57,11 @@ export class FSWorld extends World {
5157
if (params.chainId !== undefined) {
5258
throw new Error("chainId is not undefined.");
5359
}
54-
const { proxyUrl, gasPrice, explorerUrl, server } = params;
60+
const { proxyUrl, gasPrice, nodeUrls, explorerUrl, server } = params;
5561
return new FSWorld({
5662
proxy: new FSProxy({ proxyUrl, explorerUrl }),
5763
gasPrice: gasPrice ?? 1_000_000_000,
64+
nodeUrls,
5865
explorerUrl,
5966
server,
6067
});
@@ -80,8 +87,8 @@ export class FSWorld extends World {
8087
gasPrice?: number;
8188
explorerUrl?: string;
8289
} & 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 });
8592
}
8693

8794
async restartProxy(proxyParams: ProxyParams = {}) {
@@ -368,25 +375,41 @@ const startProxy = async ({
368375
throw error;
369376
});
370377

378+
const nodeUrls: Record<string, string> = {};
379+
371380
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+
/node status +.{4,7}address.{4,7} = (http:\/\/[^\s]+) .{4,7}shard.{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 =
374392
/chain simulator's is accessible through the URL ([\w\d.:]+)/;
375-
const match = data.toString().match(addressRegex);
393+
const match = dataStr.match(proxyHostRegex);
376394
if (match) {
377-
resolve(`http://${match[1]}`);
395+
const [, host] = match;
396+
resolve(`http://${host}`);
397+
server.stdout.off("data", onData);
378398
}
379-
});
399+
};
400+
401+
server.stdout.on("data", onData);
380402
});
381403

382-
return { proxyUrl, server };
404+
return { proxyUrl, nodeUrls, server };
383405
};
384406

385407
type FSWorldNewParams =
386408
| {
387409
chainId?: undefined;
388410
proxyUrl: string;
389411
gasPrice?: number;
412+
nodeUrls?: Record<string, string>;
390413
explorerUrl?: string;
391414
server?: ChildProcess;
392415
}

xsuite/src/world/lsworld.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,16 @@ export class LSWorld extends World {
9898
});
9999

100100
const proxyUrl = await new Promise<string>((resolve) => {
101-
server.stdout.on("data", (data: Buffer) => {
101+
const onData = (data: Buffer) => {
102102
const addressRegex = /Server running on (http:\/\/[\w\d.:]+)/;
103103
const match = data.toString().match(addressRegex);
104104
if (match) {
105105
resolve(match[1]);
106+
server.stdout.off("data", onData);
106107
}
107-
});
108+
};
109+
110+
server.stdout.on("data", onData);
108111
});
109112

110113
return this.new({ proxyUrl, gasPrice, explorerUrl, server });

0 commit comments

Comments
 (0)