From 482a8b4dab3ecc5d69acb6c78c4ce2922a211169 Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Sat, 28 Oct 2023 22:36:27 +0200 Subject: [PATCH 1/2] support http endpoint --- packages/chopsticks/src/server.ts | 2 +- packages/core/src/api.ts | 8 +++++--- packages/core/src/setup.ts | 4 +++- packages/e2e/src/batch-request.test.ts | 2 +- packages/e2e/src/build-block.test.ts | 2 +- packages/e2e/src/dev-dry-run-ump.test.ts | 2 +- packages/e2e/src/helper.ts | 13 +++++++++++-- packages/e2e/src/networks.ts | 2 +- 8 files changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/chopsticks/src/server.ts b/packages/chopsticks/src/server.ts index 1e0e7a66..901ce44e 100644 --- a/packages/chopsticks/src/server.ts +++ b/packages/chopsticks/src/server.ts @@ -1,6 +1,6 @@ +import { AddressInfo, WebSocket, WebSocketServer } from 'ws' import { ResponseError, SubscriptionManager } from '@acala-network/chopsticks-core' import { z } from 'zod' -import WebSocket, { AddressInfo, WebSocketServer } from 'ws' import { defaultLogger, truncate } from './logger' diff --git a/packages/core/src/api.ts b/packages/core/src/api.ts index 6b558930..17760b78 100644 --- a/packages/core/src/api.ts +++ b/packages/core/src/api.ts @@ -62,10 +62,12 @@ export class Api { this.#ready = this.#provider['isReady'] } else { this.#ready = new Promise((resolve): void => { - this.#provider.on('connected', (): void => { + if (this.#provider.hasSubscriptions) { + this.#provider.on('connected', resolve) + this.#provider.connect() + } else { resolve() - }) - this.#provider.connect() + } }) } } diff --git a/packages/core/src/setup.ts b/packages/core/src/setup.ts index 591874cf..dae2ed2d 100644 --- a/packages/core/src/setup.ts +++ b/packages/core/src/setup.ts @@ -1,8 +1,8 @@ import '@polkadot/types-codec' import { HexString } from '@polkadot/util/types' +import { HttpProvider, WsProvider } from '@polkadot/rpc-provider' import { ProviderInterface } from '@polkadot/rpc-provider/types' import { RegisteredTypes } from '@polkadot/types/types' -import { WsProvider } from '@polkadot/rpc-provider' import { Api } from './api' import { Blockchain } from './blockchain' @@ -37,6 +37,8 @@ export const setup = async (options: SetupOptions) => { let provider: ProviderInterface if (options.genesis) { provider = options.genesis + } else if (/^(https|http):\/\//.test(options.endpoint || '')) { + provider = new HttpProvider(options.endpoint) } else { provider = new WsProvider(options.endpoint) } diff --git a/packages/e2e/src/batch-request.test.ts b/packages/e2e/src/batch-request.test.ts index e5e3c54e..d968cbae 100644 --- a/packages/e2e/src/batch-request.test.ts +++ b/packages/e2e/src/batch-request.test.ts @@ -1,5 +1,5 @@ +import { WebSocket } from 'ws' import { afterAll, beforeAll, describe, expect, it } from 'vitest' -import WebSocket from 'ws' import networks from './networks' diff --git a/packages/e2e/src/build-block.test.ts b/packages/e2e/src/build-block.test.ts index 576e73af..c6bc5175 100644 --- a/packages/e2e/src/build-block.test.ts +++ b/packages/e2e/src/build-block.test.ts @@ -13,7 +13,7 @@ const KUSAMA_STORAGE = { } describe.runIf(process.env.CI).each([ - { chain: 'Polkadot', endpoint: 'wss://rpc.polkadot.io' }, + { chain: 'Polkadot', endpoint: 'https://rpc.polkadot.io' }, { chain: 'Statemint', endpoint: 'wss://statemint-rpc.polkadot.io' }, { chain: 'Polkadot Collectives', endpoint: 'wss://polkadot-collectives-rpc.polkadot.io' }, { chain: 'Acala', endpoint: 'wss://acala-rpc-1.aca-api.network' }, diff --git a/packages/e2e/src/dev-dry-run-ump.test.ts b/packages/e2e/src/dev-dry-run-ump.test.ts index 81c318af..53f78ec2 100644 --- a/packages/e2e/src/dev-dry-run-ump.test.ts +++ b/packages/e2e/src/dev-dry-run-ump.test.ts @@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest' import { setupApi, ws } from './helper' setupApi({ - endpoint: 'wss://rpc.polkadot.io', + endpoint: 'https://rpc.polkadot.io', blockHash: '0xb012d04c56b65cfa1f47cb1f884d920f95d0097b1ed42f5da18d5e2a436c2f3e', }) diff --git a/packages/e2e/src/helper.ts b/packages/e2e/src/helper.ts index fc077377..ad301612 100644 --- a/packages/e2e/src/helper.ts +++ b/packages/e2e/src/helper.ts @@ -1,6 +1,7 @@ -import { ApiPromise, WsProvider } from '@polkadot/api' +import { ApiPromise, HttpProvider, WsProvider } from '@polkadot/api' import { Codec, RegisteredTypes } from '@polkadot/types/types' import { HexString } from '@polkadot/util/types' +import { ProviderInterface } from '@polkadot/rpc-provider/types' import { beforeAll, beforeEach, expect, vi } from 'vitest' import { Api } from '@acala-network/chopsticks' @@ -58,7 +59,15 @@ export const setupAll = async ({ registeredTypes = {}, runtimeLogLevel, }: SetupOption) => { - const api = new Api(genesis ? await genesisFromUrl(genesis) : new WsProvider(endpoint), { + let provider: ProviderInterface + if (genesis) { + provider = await genesisFromUrl(genesis) + } else if (/^(https|http):\/\//.test(endpoint || '')) { + provider = new HttpProvider(endpoint) + } else { + provider = new WsProvider(endpoint) + } + const api = new Api(provider, { SetEvmOrigin: { payload: {}, extrinsic: {} }, }) diff --git a/packages/e2e/src/networks.ts b/packages/e2e/src/networks.ts index a4237336..0978ab97 100644 --- a/packages/e2e/src/networks.ts +++ b/packages/e2e/src/networks.ts @@ -5,7 +5,7 @@ import { SetupOption, setupContext } from '@acala-network/chopsticks-testing' dotenvConfig const endpoints = { - polkadot: 'wss://rpc.polkadot.io', + polkadot: 'https://rpc.polkadot.io', acala: 'wss://acala-rpc-1.aca-api.network', } From 3754ff0667f93485371a343d568c618aae106e9d Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Sun, 29 Oct 2023 00:47:47 +0200 Subject: [PATCH 2/2] only one http test to avoid rate limit --- packages/e2e/src/dev-dry-run-ump.test.ts | 2 +- packages/e2e/src/networks.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/e2e/src/dev-dry-run-ump.test.ts b/packages/e2e/src/dev-dry-run-ump.test.ts index 53f78ec2..81c318af 100644 --- a/packages/e2e/src/dev-dry-run-ump.test.ts +++ b/packages/e2e/src/dev-dry-run-ump.test.ts @@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest' import { setupApi, ws } from './helper' setupApi({ - endpoint: 'https://rpc.polkadot.io', + endpoint: 'wss://rpc.polkadot.io', blockHash: '0xb012d04c56b65cfa1f47cb1f884d920f95d0097b1ed42f5da18d5e2a436c2f3e', }) diff --git a/packages/e2e/src/networks.ts b/packages/e2e/src/networks.ts index 0978ab97..a4237336 100644 --- a/packages/e2e/src/networks.ts +++ b/packages/e2e/src/networks.ts @@ -5,7 +5,7 @@ import { SetupOption, setupContext } from '@acala-network/chopsticks-testing' dotenvConfig const endpoints = { - polkadot: 'https://rpc.polkadot.io', + polkadot: 'wss://rpc.polkadot.io', acala: 'wss://acala-rpc-1.aca-api.network', }