Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose node URLs #237

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion xsuite/src/world/fsworld.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,21 @@ test.concurrent("FSWorld.proxy.blockNonce", async () => {
});
});

test.concurrent("FSWorld.start - URLs", async () => {
const localhostRegex = /^http:\/\/localhost:\d+$/;
using world = await FSWorld.start();
expect(world.proxyUrl).toMatch(localhostRegex);
expect(world.nodeUrls).toEqual({
"0": expect.stringMatching(localhostRegex),
"1": expect.stringMatching(localhostRegex),
"2": expect.stringMatching(localhostRegex),
"4294967295": expect.stringMatching(localhostRegex),
});
});

test.concurrent("FSWorld.start - port 3000", async () => {
using world = await FSWorld.start({ binaryPort: 3000 });
expect(world.proxy.proxyUrl).toEqual("http://localhost:3000");
expect(world.proxyUrl).toEqual("http://localhost:3000");
});

test.concurrent("FSWorld.start - epoch, round, nonce", async () => {
Expand Down
41 changes: 32 additions & 9 deletions xsuite/src/world/fsworld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,28 @@ import {

export class FSWorld extends World {
proxy: FSProxy;
proxyUrl: string;
nodeUrls: Record<string, string>;
server?: ChildProcess;
sysAcc: FSContract;

constructor({
proxy,
gasPrice,
nodeUrls = {},
explorerUrl,
server,
}: {
proxy: FSProxy;
gasPrice: number;
nodeUrls?: Record<string, string>;
explorerUrl?: string;
server?: ChildProcess;
}) {
super({ chainId: "chain", proxy, gasPrice, explorerUrl });
this.proxy = proxy;
this.proxyUrl = this.proxy.proxyUrl;
this.nodeUrls = nodeUrls;
this.server = server;
this.sysAcc = this.newContract(fullU8AAddress);
}
Expand All @@ -51,10 +57,11 @@ export class FSWorld extends World {
if (params.chainId !== undefined) {
throw new Error("chainId is not undefined.");
}
const { proxyUrl, gasPrice, explorerUrl, server } = params;
const { proxyUrl, gasPrice, nodeUrls, explorerUrl, server } = params;
return new FSWorld({
proxy: new FSProxy({ proxyUrl, explorerUrl }),
gasPrice: gasPrice ?? 1_000_000_000,
nodeUrls,
explorerUrl,
server,
});
Expand All @@ -80,8 +87,8 @@ export class FSWorld extends World {
gasPrice?: number;
explorerUrl?: string;
} & ProxyParams = {}): Promise<FSWorld> {
const { server, proxyUrl } = await startProxy(proxyParams);
return this.new({ proxyUrl, gasPrice, explorerUrl, server });
const { proxyUrl, nodeUrls, server } = await startProxy(proxyParams);
return this.new({ proxyUrl, nodeUrls, gasPrice, explorerUrl, server });
}

async restartProxy(proxyParams: ProxyParams = {}) {
Expand Down Expand Up @@ -368,25 +375,41 @@ const startProxy = async ({
throw error;
});

const nodeUrls: Record<string, string> = {};

const proxyUrl = await new Promise<string>((resolve) => {
server.stdout.on("data", (data: Buffer) => {
const addressRegex =
const onData = (data: Buffer) => {
const dataStr = data.toString();

const nodeUrlRegex =
/node status +.{4,7}address.{4,7} = (http:\/\/[^\s]+) .{4,7}shard.{4,7} = (\d+)/g;
for (const match of [...dataStr.matchAll(nodeUrlRegex)]) {
const [, url, shard] = match;
nodeUrls[shard] = url;
}

const proxyHostRegex =
/chain simulator's is accessible through the URL ([\w\d.:]+)/;
const match = data.toString().match(addressRegex);
const match = dataStr.match(proxyHostRegex);
if (match) {
resolve(`http://${match[1]}`);
const [, host] = match;
resolve(`http://${host}`);
server.stdout.off("data", onData);
}
});
};

server.stdout.on("data", onData);
});

return { proxyUrl, server };
return { proxyUrl, nodeUrls, server };
};

type FSWorldNewParams =
| {
chainId?: undefined;
proxyUrl: string;
gasPrice?: number;
nodeUrls?: Record<string, string>;
explorerUrl?: string;
server?: ChildProcess;
}
Expand Down
7 changes: 5 additions & 2 deletions xsuite/src/world/lsworld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,16 @@ export class LSWorld extends World {
});

const proxyUrl = await new Promise<string>((resolve) => {
server.stdout.on("data", (data: Buffer) => {
const onData = (data: Buffer) => {
const addressRegex = /Server running on (http:\/\/[\w\d.:]+)/;
const match = data.toString().match(addressRegex);
if (match) {
resolve(match[1]);
server.stdout.off("data", onData);
}
});
};

server.stdout.on("data", onData);
});

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