diff --git a/packages/chopsticks/src/context.ts b/packages/chopsticks/src/context.ts index 902e2b82..5b598b01 100644 --- a/packages/chopsticks/src/context.ts +++ b/packages/chopsticks/src/context.ts @@ -1,17 +1,43 @@ import './utils/tunnel' -import { BlockEntry, defaultLogger, setup, timeTravel } from '@acala-network/chopsticks-core' +import { BlockEntry, GenesisProvider, defaultLogger, isUrl, setup, timeTravel } from '@acala-network/chopsticks-core' import { Config } from './schema' import { HexString } from '@polkadot/util/types' import { SqliteDatabase } from '@acala-network/chopsticks-db' import { overrideStorage, overrideWasm } from './utils/override' +import axios from 'axios' const logger = defaultLogger.child({ name: 'setup-context' }) +export const genesisFromUrl = async (url: string) => { + const getFile = async (url: string) => { + if (isUrl(url)) { + return axios.get(url).then((x) => x.data) + } else if (typeof process === 'object') { + const { lstatSync, readFileSync } = await import('node:fs') + if (lstatSync(url).isFile()) { + return JSON.parse(String(readFileSync(url))) + } + } + throw Error(`invalid genesis path or url ${url}`) + } + + return new GenesisProvider(await getFile(url)) +} + export const setupContext = async (argv: Config, overrideParent = false) => { + let genesis: GenesisProvider | undefined + if (argv.genesis) { + if (typeof argv.genesis === 'string') { + genesis = await genesisFromUrl(argv.genesis) + } else { + genesis = new GenesisProvider(argv.genesis) + } + } + const chain = await setup({ endpoint: argv.endpoint, block: argv.block, - genesis: argv.genesis, + genesis, buildBlockMode: argv['build-block-mode'], db: argv.db ? new SqliteDatabase(argv.db) : undefined, mockSignatureHost: argv['mock-signature-host'], diff --git a/packages/core/package.json b/packages/core/package.json index ce64f124..81085971 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -13,7 +13,6 @@ "@acala-network/chopsticks-executor": "workspace:*", "@polkadot/api": "^10.9.1", "@polkadot/util-crypto": "^12.3.2", - "axios": "^1.5.1", "comlink": "^4.4.1", "eventemitter3": "^5.0.1", "lodash": "^4.17.21", diff --git a/packages/core/src/api.ts b/packages/core/src/api.ts index 89097769..6b558930 100644 --- a/packages/core/src/api.ts +++ b/packages/core/src/api.ts @@ -34,16 +34,7 @@ type SignedBlock = { * @example Instantiate an API * * ```ts - * let provider: ProviderInterface - * if (options.genesis) { - * if (typeof options.genesis === 'string') { - * provider = await GenesisProvider.fromUrl(options.genesis) - * } else { - * provider = new GenesisProvider(options.genesis) - * } - * } else { - * provider = new WsProvider(options.endpoint) - * } + * const provider = new WsProvider(options.endpoint) * const api = new Api(provider) * await api.isReady * ``` diff --git a/packages/core/src/genesis-provider.ts b/packages/core/src/genesis-provider.ts index a2828a26..e265855c 100644 --- a/packages/core/src/genesis-provider.ts +++ b/packages/core/src/genesis-provider.ts @@ -7,11 +7,9 @@ import { ProviderInterfaceEmitted, ProviderStats, } from '@polkadot/rpc-provider/types' -import axios from 'axios' import { Genesis, genesisSchema } from './schema' import { JsCallback, calculateStateRoot, emptyTaskHandler } from './wasm-executor' -import { isUrl } from './utils' export class GenesisProvider implements ProviderInterface { #isConnected = false @@ -25,7 +23,7 @@ export class GenesisProvider implements ProviderInterface { #stateRoot: Promise constructor(genesis: Genesis) { - this.#genesis = genesis + this.#genesis = genesisSchema.parse(genesis) this.#stateRoot = calculateStateRoot( Object.entries(this.#genesis.genesis.raw.top).reduce( (accu, item) => { @@ -44,25 +42,10 @@ export class GenesisProvider implements ProviderInterface { resolve() }) this.#eventemitter.once('error', reject) + this.connect() }) } - static fromUrl = async (url: string) => { - const getFile = async (url: string) => { - if (isUrl(url)) { - return axios.get(url).then((x) => x.data) - } else if (typeof process === 'object') { - const { lstatSync, readFileSync } = await import('node:fs') - if (lstatSync(url).isFile()) { - return JSON.parse(String(readFileSync(url))) - } - } - throw Error(`invalid genesis path or url ${url}`) - } - - return new GenesisProvider(genesisSchema.parse(await getFile(url))) - } - get isClonable(): boolean { return true } @@ -80,7 +63,6 @@ export class GenesisProvider implements ProviderInterface { } get isReady(): Promise { - this.connect() return this.#isReadyPromise } diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 56e6e152..8240aef6 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -24,4 +24,5 @@ export * from './blockchain/inherent' export * from './logger' export * from './offchain' export * from './chopsticks-provider' +export * from './genesis-provider' export * from './rpc' diff --git a/packages/core/src/setup.ts b/packages/core/src/setup.ts index dc39a9b0..fbcf5366 100644 --- a/packages/core/src/setup.ts +++ b/packages/core/src/setup.ts @@ -8,7 +8,6 @@ import { Api } from './api' import { Blockchain } from './blockchain' import { BuildBlockMode } from './blockchain/txpool' import { Database } from './database' -import { Genesis } from './schema' import { GenesisProvider } from './genesis-provider' import { InherentProviders, @@ -23,7 +22,7 @@ import { defaultLogger } from './logger' export type SetupOptions = { endpoint?: string block?: string | number | null - genesis?: string | Genesis + genesis?: GenesisProvider buildBlockMode?: BuildBlockMode db?: Database mockSignatureHost?: boolean @@ -37,11 +36,7 @@ export type SetupOptions = { export const setup = async (options: SetupOptions) => { let provider: ProviderInterface if (options.genesis) { - if (typeof options.genesis === 'string') { - provider = await GenesisProvider.fromUrl(options.genesis) - } else { - provider = new GenesisProvider(options.genesis) - } + provider = options.genesis } else { provider = new WsProvider(options.endpoint) } diff --git a/packages/e2e/src/helper.ts b/packages/e2e/src/helper.ts index 24c573dd..8e548206 100644 --- a/packages/e2e/src/helper.ts +++ b/packages/e2e/src/helper.ts @@ -6,7 +6,6 @@ import { beforeAll, beforeEach, expect, vi } from 'vitest' import { Api } from '@acala-network/chopsticks' import { Blockchain } from '@acala-network/chopsticks-core/blockchain' import { BuildBlockMode } from '@acala-network/chopsticks-core/blockchain/txpool' -import { GenesisProvider } from '@acala-network/chopsticks-core/genesis-provider' import { InherentProviders, ParaInherentEnter, @@ -18,6 +17,7 @@ import { import { StorageValues } from '@acala-network/chopsticks-core/utils/set-storage' import { createServer } from '@acala-network/chopsticks/server' import { defer } from '@acala-network/chopsticks-core/utils' +import { genesisFromUrl } from '@acala-network/chopsticks/context' import { handler } from '@acala-network/chopsticks/rpc' export { expectJson, expectHex, testingPairs } from '@acala-network/chopsticks-testing' @@ -53,7 +53,7 @@ export const setupAll = async ({ allowUnresolvedImports, genesis, }: SetupOption) => { - const api = new Api(genesis ? await GenesisProvider.fromUrl(genesis) : new WsProvider(endpoint), { + const api = new Api(genesis ? await genesisFromUrl(genesis) : new WsProvider(endpoint), { SetEvmOrigin: { payload: {}, extrinsic: {} }, }) diff --git a/yarn.lock b/yarn.lock index 80c4fcfc..40258cc3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -20,7 +20,6 @@ __metadata: "@polkadot/api": ^10.9.1 "@polkadot/util-crypto": ^12.3.2 "@types/lodash": ^4.14.199 - axios: ^1.5.1 comlink: ^4.4.1 eventemitter3: ^5.0.1 lodash: ^4.17.21