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

fix macos resolving by default to ipv6 only #849

Merged
merged 6 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,6 @@ Documentation can be found [here](packages/chopsticks/src/plugins/try-runtime/RE

## FAQ

### 127.0.0.1:8000 Abnormal Closure

Chopsticks listen on `localhost:8000` by default, if you are on `macos`, it may not resolve to `127.0.0.1` to `localhost`. You can access chopsticks using `localhost:8000` instead, or you can start chopsticks with `--addr=127.0.0.1`.

### What is mocked? What are things that could work with chopsticks, but still fail in production?

Generally, anything that involves something more than onchain STF `new_state = f(old_state)` are not guaranteed to work in production.
Expand Down
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.addr, config.port)
logger.info(`${await context.chain.api.getSystemChain()} RPC listening on ${config.addr}:${listenPort}`)
const { close, addr } = await createServer(handler(context), config.port, config.host)
logger.info(`${await context.chain.api.getSystemChain()} RPC listening on http://${addr} and ws://${addr}`)

const chain = context.chain

Expand Down
4 changes: 2 additions & 2 deletions packages/chopsticks/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Handlers, environment } from '@acala-network/chopsticks-core'
import { lstatSync, readFileSync, readdirSync } from 'fs'
import { lstatSync, readFileSync, readdirSync } from 'node:fs'
import _ from 'lodash'
import type { Argv } from 'yargs'

import { defaultLogger } from '../logger.js'
import { resolve } from 'path'
import { resolve } from 'node:path'

const logger = defaultLogger.child({ name: 'plugin' })

Expand Down
2 changes: 1 addition & 1 deletion packages/chopsticks/src/plugins/trace-transaction/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Argv } from 'yargs'
import { pinoLogger } from '@acala-network/chopsticks-core'
import { writeFileSync } from 'fs'
import { writeFileSync } from 'node:fs'
import { z } from 'zod'
import _ from 'lodash'

Expand Down
2 changes: 1 addition & 1 deletion packages/chopsticks/src/plugins/try-runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const cli = (y: Argv) => {
}
const context = await setupContext({
...config,
addr: 'localhost',
host: 'localhost',
port: 8000,
'build-block-mode': BuildBlockMode.Manual,
})
Expand Down
4 changes: 2 additions & 2 deletions packages/chopsticks/src/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ 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({
addr: z
host: z
.union([z.literal('localhost'), z.string().ip()], {
description: 'Server listening interface',
})
.default('localhost'),
.optional(),
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
Expand Down
12 changes: 6 additions & 6 deletions packages/chopsticks/src/schema/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ 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 @@ -56,6 +50,12 @@ it('get yargs options from zod schema', () => {
"description": "Alias to \`chain-spec\`. URL to chain spec file. NOTE: Only parachains with AURA consensus are supported!",
"type": "string",
},
"host": {
"choices": undefined,
"demandOption": false,
"description": "Server listening interface",
"type": "string",
},
"import-storage": {
"choices": undefined,
"demandOption": false,
Expand Down
5 changes: 2 additions & 3 deletions packages/chopsticks/src/schema/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest'
import { readdirSync } from 'fs'
import { readdirSync } from 'node:fs'
import _ from 'lodash'
import path from 'path'
import path from 'node:path'

import { configSchema, fetchConfig } from './index.js'

Expand All @@ -28,7 +28,6 @@ describe('Existing configs', async () => {

describe('Parsed options', () => {
const defaults = {
addr: 'localhost',
port: 8000,
'build-block-mode': 'Batch',
}
Expand Down
20 changes: 11 additions & 9 deletions packages/chopsticks/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const respond = (res: http.ServerResponse, data?: any) => {
res.end()
}

const portInUse = async (port: number, addr: string) => {
const portInUse = async (port: number, host?: string) => {
const server = http.createServer()
const inUse = await new Promise<boolean>((resolve) => {
server.once('error', (e: any) => {
Expand All @@ -71,16 +71,17 @@ const portInUse = async (port: number, addr: string) => {
server.close()
resolve(false)
})
server.listen(port, addr)
server.listen(port, host)
})
server.removeAllListeners()
server.unref()
await new Promise((r) => setTimeout(r, 50))
return inUse
}

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

const emptySubscriptionManager = {
subscribe: () => {
Expand Down Expand Up @@ -151,7 +152,7 @@ export const createServer = async (handler: Handler, addr: string, port: number)
})

for (let i = 0; i < 10; i++) {
if (port && (await portInUse(port + i, addr))) {
if (port && (await portInUse(port + i, host))) {
continue
}
const preferPort = port ? port + i : undefined
Expand All @@ -162,17 +163,17 @@ export const createServer = async (handler: Handler, addr: string, port: number)
reject(e)
}
server.once('error', onError)
server.listen(preferPort, addr, () => {
server.listen(preferPort, host, () => {
wss = new WebSocketServer({ server, maxPayload: 1024 * 1024 * 100 })
listenPort = (server.address() as AddressInfo).port
addressInfo = server.address() as AddressInfo
server.removeListener('error', onError)
resolve()
})
})
break
}

if (!wss || !listenPort) {
if (!wss || !addressInfo) {
throw new Error(`Failed to create WebsocketServer at port ${port}`)
}

Expand Down Expand Up @@ -289,7 +290,8 @@ export const createServer = async (handler: Handler, addr: string, port: number)
})

return {
port: listenPort,
addr: `${addressInfo.family === 'IPv6' ? `[${addressInfo.address}]` : addressInfo.address}:${addressInfo.port}`,
port: addressInfo.port,
close: async () => {
server.close()
server.closeAllConnections()
Expand Down
9 changes: 3 additions & 6 deletions packages/chopsticks/src/setup-with-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ import { setupContext } from './context.js'
export const setupWithServer = async (argv: Config) => {
const context = await setupContext(argv)

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 ${addr}:${listenPort}`)
const { close, addr } = await createServer(handler(context), argv.port, argv.host)
defaultLogger.info(`${await context.chain.api.getSystemChain()} RPC listening on http://${addr} and ws://${addr}`)

return {
...context,
addr: argv.addr,
listenPort,
addr,
async close() {
await context.chain.close()
await context.fetchStorageWorker?.terminate()
Expand Down
4 changes: 2 additions & 2 deletions packages/chopsticks/src/utils/tunnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import npmConf from '@pnpm/npm-conf'

const npmConfig = npmConf().config

global.GLOBAL_AGENT.HTTP_PROXY =
globalThis.GLOBAL_AGENT.HTTP_PROXY =
environment.HTTP_PROXY ||
environment.http_proxy ||
environment.HTTPS_PROXY ||
environment.https_proxy ||
npmConfig.get('proxy') ||
npmConfig.get('https-proxy') ||
global.GLOBAL_AGENT.HTTP_PROXY
globalThis.GLOBAL_AGENT.HTTP_PROXY
2 changes: 1 addition & 1 deletion packages/e2e/src/genesis-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +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',
host: 'localhost',
port: 1234,
genesis,
'build-block-mode': BuildBlockMode.Manual,
Expand Down
4 changes: 2 additions & 2 deletions packages/e2e/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ export const setupAll = async ({
await chain.newBlock()
}

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

return {
chain,
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e/src/import-storage/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import path from 'path'
import path from 'node:path'

import { api, chain, setupApi } from '../helper.js'
import { compactHex } from '@acala-network/chopsticks'
Expand Down
11 changes: 5 additions & 6 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type SetupOption = {
wasmOverride?: string
db?: string
timeout?: number
addr?: string
host?: string
port?: number
maxMemoryBlockCount?: number
resume?: boolean | HexString | number
Expand All @@ -47,20 +47,19 @@ export const createConfig = ({
wasmOverride,
db,
timeout,
addr,
host,
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,
host,
port,
block: blockNumber || blockHash,
'mock-signature-host': true,
Expand All @@ -82,9 +81,9 @@ export const setupContext = async (option: SetupOption) => {
}

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

const url = `ws://${addr}:${listenPort}`
const url = `ws://${addr}`
const ws = new WsProvider(url, 3_000, undefined, timeout)
const api = await ApiPromise.create({
provider: ws,
Expand Down
35 changes: 5 additions & 30 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1259,14 +1259,14 @@ __metadata:
languageName: node
linkType: hard

"@noble/hashes@npm:1.4.0, @noble/hashes@npm:^1.3.1, @noble/hashes@npm:^1.3.3":
"@noble/hashes@npm:1.4.0":
version: 1.4.0
resolution: "@noble/hashes@npm:1.4.0"
checksum: 10c0/8c3f005ee72e7b8f9cff756dfae1241485187254e3f743873e22073d63906863df5d4f13d441b7530ea614b7a093f0d889309f28b59850f33b66cb26a779a4a5
languageName: node
linkType: hard

"@noble/hashes@npm:^1.4.0":
"@noble/hashes@npm:^1.3.1, @noble/hashes@npm:^1.3.3, @noble/hashes@npm:^1.4.0":
version: 1.5.0
resolution: "@noble/hashes@npm:1.5.0"
checksum: 10c0/1b46539695fbfe4477c0822d90c881a04d4fa2921c08c552375b444a48cac9930cb1ee68de0a3c7859e676554d0f3771999716606dc4d8f826e414c11692cdd9
Expand Down Expand Up @@ -7755,14 +7755,7 @@ __metadata:
languageName: node
linkType: hard

"picocolors@npm:^1.0.0, picocolors@npm:^1.0.1":
version: 1.0.1
resolution: "picocolors@npm:1.0.1"
checksum: 10c0/c63cdad2bf812ef0d66c8db29583802355d4ca67b9285d846f390cc15c2f6ccb94e8cb7eb6a6e97fc5990a6d3ad4ae42d86c84d3146e667c739a4234ed50d400
languageName: node
linkType: hard

"picocolors@npm:^1.1.0":
"picocolors@npm:^1.0.0, picocolors@npm:^1.0.1, picocolors@npm:^1.1.0":
version: 1.1.0
resolution: "picocolors@npm:1.1.0"
checksum: 10c0/86946f6032148801ef09c051c6fb13b5cf942eaf147e30ea79edb91dd32d700934edebe782a1078ff859fb2b816792e97ef4dab03d7f0b804f6b01a0df35e023
Expand Down Expand Up @@ -7899,18 +7892,7 @@ __metadata:
languageName: node
linkType: hard

"postcss@npm:^8.4.40":
version: 8.4.41
resolution: "postcss@npm:8.4.41"
dependencies:
nanoid: "npm:^3.3.7"
picocolors: "npm:^1.0.1"
source-map-js: "npm:^1.2.0"
checksum: 10c0/c1828fc59e7ec1a3bf52b3a42f615dba53c67960ed82a81df6441b485fe43c20aba7f4e7c55425762fd99c594ecabbaaba8cf5b30fd79dfec5b52a9f63a2d690
languageName: node
linkType: hard

"postcss@npm:^8.4.43":
"postcss@npm:^8.4.40, postcss@npm:^8.4.43":
version: 8.4.47
resolution: "postcss@npm:8.4.47"
dependencies:
Expand Down Expand Up @@ -8869,14 +8851,7 @@ __metadata:
languageName: node
linkType: hard

"source-map-js@npm:^1.2.0":
version: 1.2.0
resolution: "source-map-js@npm:1.2.0"
checksum: 10c0/7e5f896ac10a3a50fe2898e5009c58ff0dc102dcb056ed27a354623a0ece8954d4b2649e1a1b2b52ef2e161d26f8859c7710350930751640e71e374fe2d321a4
languageName: node
linkType: hard

"source-map-js@npm:^1.2.1":
"source-map-js@npm:^1.2.0, source-map-js@npm:^1.2.1":
version: 1.2.1
resolution: "source-map-js@npm:1.2.1"
checksum: 10c0/7bda1fc4c197e3c6ff17de1b8b2c20e60af81b63a52cb32ec5a5d67a20a7d42651e2cb34ebe93833c5a2a084377e17455854fee3e21e7925c64a51b6a52b0faf
Expand Down
Loading