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

update genesis provider #462

Merged
merged 2 commits into from
Oct 23, 2023
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
30 changes: 28 additions & 2 deletions packages/chopsticks/src/context.ts
Original file line number Diff line number Diff line change
@@ -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'],
Expand Down
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 1 addition & 10 deletions packages/core/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
* ```
Expand Down
22 changes: 2 additions & 20 deletions packages/core/src/genesis-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,7 +23,7 @@ export class GenesisProvider implements ProviderInterface {
#stateRoot: Promise<HexString>

constructor(genesis: Genesis) {
this.#genesis = genesis
this.#genesis = genesisSchema.parse(genesis)
this.#stateRoot = calculateStateRoot(
Object.entries(this.#genesis.genesis.raw.top).reduce(
(accu, item) => {
Expand All @@ -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
}
Expand All @@ -80,7 +63,6 @@ export class GenesisProvider implements ProviderInterface {
}

get isReady(): Promise<void> {
this.connect()
return this.#isReadyPromise
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
9 changes: 2 additions & 7 deletions packages/core/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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)
}
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 @@ -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,
Expand All @@ -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'
Expand Down Expand Up @@ -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: {} },
})

Expand Down
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading