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

Add --addr cli options to specify the interface #826

Merged
merged 7 commits into from
Sep 27, 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
4 changes: 2 additions & 2 deletions packages/chopsticks/src/plugins/follow-chain/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export const cli = (y: Argv) => {
})

const context = await setupContext(config, true)
const { close, port: listenPort } = await createServer(handler(context), config.port)
logger.info(`${await context.chain.api.getSystemChain()} RPC listening on port ${listenPort}`)
const { close, port: listenPort } = await createServer(handler(context), config.addr, config.port)
logger.info(`${await context.chain.api.getSystemChain()} RPC listening on ${config.addr}:${listenPort}`)

const chain = context.chain

Expand Down
7 changes: 6 additions & 1 deletion packages/chopsticks/src/plugins/try-runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ export const cli = (y: Argv) => {
if (!config.db) {
console.log('⚠️ Make sure to provide db, it will speed up the process')
}
const context = await setupContext({ ...config, port: 8000, 'build-block-mode': BuildBlockMode.Manual })
const context = await setupContext({
...config,
addr: 'localhost',
port: 8000,
'build-block-mode': BuildBlockMode.Manual,
})
const block = context.chain.head
const registry = await block.registry
registry.register({
Expand Down
7 changes: 6 additions & 1 deletion packages/chopsticks/src/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ export const zHex = z.custom<HexString>((val: any) => /^0x\w+$/.test(val))
export const zHash = z.string().length(66).and(zHex)

export const configSchema = z.object({
port: z.number({ description: 'Port to listen on' }).default(8000),
addr: z
.union([z.literal('localhost'), z.string().ip()], {
description: 'Server listening interface',
})
.default('localhost'),
port: z.number({ description: 'Server listening port' }).default(8000),
endpoint: z.union([z.string(), z.array(z.string())], { description: 'Endpoint to connect to' }).optional(),
block: z
.union(
Expand Down
8 changes: 7 additions & 1 deletion packages/chopsticks/src/schema/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { expect, it } from 'vitest'
it('get yargs options from zod schema', () => {
expect(getYargsOptions(configSchema.shape)).toMatchInlineSnapshot(`
{
"addr": {
"choices": undefined,
"demandOption": false,
"description": "Server listening interface",
"type": "string",
},
"allow-unresolved-imports": {
"choices": undefined,
"demandOption": false,
Expand Down Expand Up @@ -77,7 +83,7 @@ it('get yargs options from zod schema', () => {
"port": {
"choices": undefined,
"demandOption": false,
"description": "Port to listen on",
"description": "Server listening port",
"type": "number",
},
"prefetch-storages": {
Expand Down
1 change: 1 addition & 0 deletions packages/chopsticks/src/schema/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('Existing configs', async () => {

describe('Parsed options', () => {
const defaults = {
addr: 'localhost',
port: 8000,
'build-block-mode': 'Batch',
}
Expand Down
4 changes: 2 additions & 2 deletions packages/chopsticks/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const portInUse = async (port: number) => {
return inUse
}

export const createServer = async (handler: Handler, port: number) => {
export const createServer = async (handler: Handler, addr: string, port: number) => {
let wss: WebSocketServer | undefined
let listenPort: number | undefined

Expand Down Expand Up @@ -162,7 +162,7 @@ export const createServer = async (handler: Handler, port: number) => {
reject(e)
}
server.once('error', onError)
server.listen(preferPort, () => {
server.listen(preferPort, addr, () => {
wss = new WebSocketServer({ server, maxPayload: 1024 * 1024 * 100 })
listenPort = (server.address() as AddressInfo).port
server.removeListener('error', onError)
Expand Down
6 changes: 4 additions & 2 deletions packages/chopsticks/src/setup-with-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import { setupContext } from './context.js'
export const setupWithServer = async (argv: Config) => {
const context = await setupContext(argv)

const { close, port: listenPort } = await createServer(handler(context), argv.port)
const addr = argv.addr ?? 'localhost'
const { close, port: listenPort } = await createServer(handler(context), addr, argv.port)

defaultLogger.info(`${await context.chain.api.getSystemChain()} RPC listening on port ${listenPort}`)
defaultLogger.info(`${await context.chain.api.getSystemChain()} RPC listening on ${addr}:${listenPort}`)

return {
...context,
addr: argv.addr,
listenPort,
async close() {
await context.chain.close()
Expand Down
1 change: 1 addition & 0 deletions packages/e2e/src/genesis-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe.each([
['Asset Hub Kusama', new URL('../blobs/asset-hub-kusama.json', import.meta.url).pathname],
])(`genesis provider works %s`, async (name, genesis) => {
const { chain, dev, api, teardown } = await setupContextWithConfig({
addr: 'localhost',
port: 1234,
genesis,
'build-block-mode': BuildBlockMode.Manual,
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const setupAll = async ({
await chain.newBlock()
}

const { port, close } = await createServer(handler({ chain }), 0)
const { port, close } = await createServer(handler({ chain }), 'localhost', 0)
const ws = new WsProvider(`ws://localhost:${port}`, 3_000, undefined, 300_000)

return {
Expand Down
8 changes: 6 additions & 2 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type SetupOption = {
wasmOverride?: string
db?: string
timeout?: number
addr?: string
port?: number
maxMemoryBlockCount?: number
resume?: boolean | HexString | number
Expand All @@ -45,17 +46,20 @@ export const createConfig = ({
wasmOverride,
db,
timeout,
addr,
port,
maxMemoryBlockCount,
resume,
runtimeLogLevel,
allowUnresolvedImports,
processQueuedMessages,
}: SetupOption): SetupConfig => {
addr = addr ?? 'localhost'
// random port if not specified
port = port ?? Math.floor(Math.random() * 10000) + 10000
const config = {
endpoint,
addr,
port,
block: blockNumber || blockHash,
'mock-signature-host': true,
Expand All @@ -77,9 +81,9 @@ export const setupContext = async (option: SetupOption) => {
}

export const setupContextWithConfig = async ({ timeout, ...config }: SetupConfig) => {
const { chain, listenPort, close } = await setupWithServer(config)
const { chain, addr, listenPort, close } = await setupWithServer(config)

const url = `ws://localhost:${listenPort}`
const url = `ws://${addr}:${listenPort}`
zjb0807 marked this conversation as resolved.
Show resolved Hide resolved
const ws = new WsProvider(url, 3_000, undefined, timeout)
const api = await ApiPromise.create({
provider: ws,
Expand Down
Loading