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 esm #511

Merged
merged 3 commits into from
Nov 4, 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
2 changes: 1 addition & 1 deletion executor/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion executor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "chopsticks-executor"
version = "0.9.1-1"
version = "0.9.1-2"
description = "Chopsticks executor"
repository = "https://github.com/AcalaNetwork/chopsticks"
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion executor/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@acala-network/chopsticks-executor",
"description": "Chopsticks executor",
"version": "0.9.1-1",
"version": "0.9.1-2",
"license": "Apache-2.0",
"repository": {
"type": "git",
Expand Down
7 changes: 4 additions & 3 deletions packages/chopsticks/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@acala-network/chopsticks",
"version": "0.9.1-1",
"version": "0.9.1-2",
"author": "Acala Developers <[email protected]>",
"license": "Apache-2.0",
"bin": "./chopsticks.js",
"scripts": {
"clean": "rm -rf dist",
"build": "yarn clean && tsc -p ./tsconfig.json && tsc -p ./tsconfig.esm.json && yarn copyfiles",
"build": "yarn clean && tsc -p ./tsconfig.json && tsc -p ./tsconfig.esm.json && echo '{\"type\": \"module\"}' > ./dist/esm/package.json && yarn copyfiles",
"copyfiles": "cp -r src/utils/template dist/cjs/utils/ && cp -r src/utils/template dist/esm/utils/",
"script:start": "cd ../..; ts-node --transpile-only -r tsconfig-paths/register packages/chopsticks/src/cli.ts",
"script:run": "cd ../..; LOG_LEVEL=trace ts-node-dev --transpile-only -r tsconfig-paths/register --inspect --notify=false packages/chopsticks/src/cli.ts -- --config=configs/dev.yml",
Expand Down Expand Up @@ -70,6 +70,7 @@
"import": "./dist/esm/plugins/*.js",
"default": "./dist/esm/plugins/*.js"
},
"./package.json": "./package.json"
"./package.json": "./package.json",
"./package.esm.json": "./dist/esm/package.json"
}
}
3 changes: 2 additions & 1 deletion packages/chopsticks/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { config as dotenvConfig } from 'dotenv'
import { hideBin } from 'yargs/helpers'
import _ from 'lodash'
import yargs from 'yargs'
import type { MiddlewareFunction } from 'yargs'

import { Blockchain, BuildBlockMode, connectParachains, connectVertical } from '@acala-network/chopsticks-core'
import { Config, fetchConfig } from './schema'
Expand All @@ -11,7 +12,7 @@ import { setupWithServer } from '.'

dotenvConfig()

const processArgv: yargs.MiddlewareFunction<{ config?: string; port?: number }> = async (argv) => {
const processArgv: MiddlewareFunction<{ config?: string; port?: number }> = async (argv) => {
if (argv.config) {
Object.assign(argv, _.defaults(argv, await fetchConfig(argv.config)))
}
Expand Down
4 changes: 2 additions & 2 deletions packages/chopsticks/src/plugins/decode-key/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { HexString } from '@polkadot/util/types'
import { decodeKey } from '@acala-network/chopsticks-core'
import { defaultOptions } from '../../cli-options'
import { setupContext } from '../../context'
import type yargs from 'yargs'
import type { Argv } from 'yargs'

export const cli = (y: yargs.Argv) => {
export const cli = (y: Argv) => {
y.command(
'decode-key <key>',
'Deocde a key',
Expand Down
4 changes: 2 additions & 2 deletions packages/chopsticks/src/plugins/dry-run/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Config } from '../../schema'
import { defaultOptions, mockOptions } from '../../cli-options'
import { dryRunExtrinsic } from './dry-run-extrinsic'
import { dryRunPreimage } from './dry-run-preimage'
import type yargs from 'yargs'
import type { Argv } from 'yargs'

export const cli = (y: yargs.Argv) => {
export const cli = (y: Argv) => {
y.command(
'dry-run',
'Dry run an extrinsic',
Expand Down
4 changes: 2 additions & 2 deletions packages/chopsticks/src/plugins/follow-chain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Block, defaultLogger, printRuntimeLogs, runTask, taskHandler } from '@a
import { Header } from '@polkadot/types/interfaces'
import { HexString } from '@polkadot/util/types'
import _ from 'lodash'
import type yargs from 'yargs'
import type { Argv } from 'yargs'

import { createServer } from '../../server'
import { defaultOptions } from '../../cli-options'
Expand All @@ -13,7 +13,7 @@ import type { Config } from '../../schema'
const logger = defaultLogger.child({ name: 'follow-chain' })
const options = _.pick(defaultOptions, ['endpoint', 'wasm-override', 'runtime-log-level', 'offchain-worker'])

export const cli = (y: yargs.Argv) => {
export const cli = (y: Argv) => {
y.command(
'follow-chain',
'Always follow the latest block on upstream',
Expand Down
23 changes: 17 additions & 6 deletions packages/chopsticks/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
import { Handlers } from '@acala-network/chopsticks-core'
import { camelCase } from 'lodash'
import { lstatSync, readdirSync } from 'fs'
import type yargs from 'yargs'
import _ from 'lodash'
import type { Argv } from 'yargs'

import { defaultLogger } from '../logger'

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

export const pluginHandlers: Handlers = {}

const plugins = readdirSync(__dirname).filter((file) => lstatSync(`${__dirname}/${file}`).isDirectory())
const plugins = [
'decode-key',
'dry-run',
'follow-chain',
'new-block',
'run-block',
'set-block-build-mode',
'set-head',
'set-runtime-log-level',
'set-storage',
'time-travel',
'try-runtime',
]

;(async () => {
for (const plugin of plugins) {
const { rpc, name } = await import(`./${plugin}`)
if (rpc) {
const methodName = name || camelCase(plugin)
const methodName = name || _.camelCase(plugin)
pluginHandlers[`dev_${methodName}`] = rpc
logger.debug(`Registered plugin ${plugin} RPC`)
}
}
})()

export const pluginExtendCli = async (y: yargs.Argv) => {
export const pluginExtendCli = async (y: Argv) => {
for (const plugin of plugins) {
const { cli } = await import(`./${plugin}`)
if (cli) {
Expand Down
4 changes: 2 additions & 2 deletions packages/chopsticks/src/plugins/run-block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { u8aToHex } from '@polkadot/util'
import { writeFileSync } from 'node:fs'
import { z } from 'zod'
import _ from 'lodash'
import type yargs from 'yargs'
import type { Argv } from 'yargs'

import {
Block,
Expand All @@ -24,7 +24,7 @@ import { generateHtmlDiffPreviewFile } from '../../utils/generate-html-diff'
import { openHtml } from '../../utils/open-html'
import { setupContext } from '../../context'

export const cli = (y: yargs.Argv) => {
export const cli = (y: Argv) => {
y.command(
'run-block',
'Replay a block',
Expand Down
4 changes: 2 additions & 2 deletions packages/chopsticks/src/plugins/try-runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { writeFileSync } from 'node:fs'
import type yargs from 'yargs'
import type { Argv } from 'yargs'

import { Config } from '../../schema'
import { defaultOptions } from '../../cli-options'
import { generateHtmlDiffPreviewFile } from '../../utils/generate-html-diff'
import { openHtml } from '../../utils/open-html'
import { setupContext } from '../../context'

export const cli = (y: yargs.Argv) => {
export const cli = (y: Argv) => {
y.command(
'try-runtime',
'Runs runtime upgrade',
Expand Down
4 changes: 2 additions & 2 deletions packages/chopsticks/src/utils/generate-html-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { Block } from '@acala-network/chopsticks-core'
import { HexString } from '@polkadot/util/types'
import { decodeStorageDiff } from './decoder'
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { template } from 'lodash'
import _ from 'lodash'
import url from 'node:url'

export const generateHtmlDiff = async (block: Block, diff: [HexString, HexString | null][]) => {
const { oldState, delta } = await decodeStorageDiff(block, diff)
const htmlTemplate = readFileSync(url.resolve(__filename, './template/diff.html'), 'utf-8')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this __filename also need to fix, not available in esm, I think we can swich to use a bundler, so we can use these stuff in code

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I know but it's used by cli so most likely it's fine. also I want to remove this completely in the future

return template(htmlTemplate)({ left: JSON.stringify(oldState), delta: JSON.stringify(delta) })
return _.template(htmlTemplate)({ left: JSON.stringify(oldState), delta: JSON.stringify(delta) })
}

export const generateHtmlDiffPreviewFile = async (
Expand Down
9 changes: 4 additions & 5 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@acala-network/chopsticks-core",
"version": "0.9.1-1",
"version": "0.9.1-2",
"author": "Acala Developers <[email protected]>",
"license": "Apache-2.0",
"scripts": {
"clean": "rm -rf dist",
"build": "yarn clean && tsc -p ./tsconfig.json && tsc -p ./tsconfig.esm.json && yarn copyfiles",
"build": "yarn clean && tsc -p ./tsconfig.json && tsc -p ./tsconfig.esm.json && echo '{\"type\": \"module\"}' > ./dist/esm/package.json && yarn copyfiles",
"copyfiles": "cp -r src/wasm-executor/*.mjs dist/cjs/wasm-executor/ && cp -r src/wasm-executor/*.mjs dist/esm/wasm-executor/",
"docs:prep": "typedoc"
},
Expand Down Expand Up @@ -45,12 +45,11 @@
"import": "./dist/esm/index.js",
"default": "./dist/esm/index.js"
},
"./package.json": "./package.json"
"./package.json": "./package.json",
"./package.esm.json": "./dist/esm/package.json"
},
"browser": {
"./dist/cjs/wasm-executor/node-wasm-executor.mjs": "./dist/cjs/wasm-executor/browser-wasm-executor.mjs",
"./dist/cjs/wasm-executor/node-worker.js": "./dist/cjs/wasm-executor/browser-worker.js",
"./dist/esm/wasm-executor/node-wasm-executor.mjs": "./dist/esm/wasm-executor/browser-wasm-executor.mjs",
"./dist/esm/wasm-executor/node-worker.js": "./dist/esm/wasm-executor/browser-worker.js"
}
}
2 changes: 1 addition & 1 deletion packages/core/src/blockchain/txpool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventEmitter } from 'eventemitter3'
import { default as EventEmitter } from 'eventemitter3'
import { GenericExtrinsic } from '@polkadot/types'
import { HexString } from '@polkadot/util/types'
import _ from 'lodash'
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/chopsticks-provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventEmitter } from 'eventemitter3'
import { default as EventEmitter } from 'eventemitter3'
import {
ProviderInterface,
ProviderInterfaceCallback,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/genesis-provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventEmitter } from 'eventemitter3'
import { default as EventEmitter } from 'eventemitter3'
import { HexString } from '@polkadot/util/types'
import {
ProviderInterface,
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/wasm-executor/browser-worker.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { wrap } from 'comlink'
import type { WasmExecutor } from '.'

export const startWorker = async () => {
export const startWorker = async <T>() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const worker = new Worker(new URL('browser-wasm-executor.mjs', import.meta.url), {
type: 'module',
name: 'chopsticks-wasm-executor',
})
return {
remote: wrap<WasmExecutor>(worker),
remote: wrap<T>(worker),
terminate: async () => {
worker.terminate()
},
Expand Down
29 changes: 0 additions & 29 deletions packages/core/src/wasm-executor/node-wasm-executor.mjs

This file was deleted.

40 changes: 35 additions & 5 deletions packages/core/src/wasm-executor/node-worker.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
import { wrap } from 'comlink'
import nodeEndpoint from 'comlink/dist/umd/node-adapter.js'
import threads from 'node:worker_threads'
import url from 'node:url'
import type { WasmExecutor } from '.'

export const startWorker = async () => {
const worker = new threads.Worker(url.resolve(__filename, 'node-wasm-executor.mjs'), {
export const startWorker = async <T>() => {
const workerCode = `
const Comlink = require('comlink')
const pkg = require('@acala-network/chopsticks-executor')
const { parentPort } = require('node:worker_threads')
const nodeEndpoint = require('comlink/dist/umd/node-adapter.js')

const getRuntimeVersion = async (code) => {
return pkg.get_runtime_version(code)
}

// trie_version: 0 for old trie, 1 for new trie
const calculateStateRoot = async (entries, trie_version) => {
return pkg.calculate_state_root(entries, trie_version)
}

const decodeProof = async (trieRootHash, keys, nodes) => {
return pkg.decode_proof(trieRootHash, keys, nodes)
}

const createProof = async (nodes, entries) => {
return pkg.create_proof(nodes, entries)
}

const runTask = async (task, callback) => {
return pkg.run_task(task, callback, process.env.RUST_LOG)
}

const wasmExecutor = { runTask, getRuntimeVersion, calculateStateRoot, createProof, decodeProof }

Comlink.expose(wasmExecutor, nodeEndpoint(parentPort))
`
const worker = new threads.Worker(workerCode, {
name: 'chopsticks-wasm-executor',
eval: true,
})
return {
remote: wrap<WasmExecutor>(nodeEndpoint(worker)),
remote: wrap<T>(nodeEndpoint(worker)),
terminate: async () => {
await worker.terminate()
},
Expand Down
7 changes: 4 additions & 3 deletions packages/db/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@acala-network/chopsticks-db",
"version": "0.9.1-1",
"version": "0.9.1-2",
"author": "Acala Developers <[email protected]>",
"license": "Apache-2.0",
"scripts": {
"clean": "rm -rf dist",
"build": "yarn clean && tsc -p ./tsconfig.json && tsc -p ./tsconfig.esm.json"
"build": "yarn clean && tsc -p ./tsconfig.json && tsc -p ./tsconfig.esm.json && echo '{\"type\": \"module\"}' > ./dist/esm/package.json"
},
"dependencies": {
"@acala-network/chopsticks-core": "workspace:*",
Expand Down Expand Up @@ -37,6 +37,7 @@
"import": "./dist/esm/index.js",
"default": "./dist/esm/index.js"
},
"./package.json": "./package.json"
"./package.json": "./package.json",
"./package.esm.json": "./dist/esm/package.json"
}
}
7 changes: 4 additions & 3 deletions packages/testing/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@acala-network/chopsticks-testing",
"version": "0.9.1-1",
"version": "0.9.1-2",
"author": "Acala Developers <[email protected]>",
"license": "Apache-2.0",
"scripts": {
"clean": "rm -rf dist",
"build": "yarn clean && tsc -p ./tsconfig.json && tsc -p ./tsconfig.esm.json"
"build": "yarn clean && tsc -p ./tsconfig.json && tsc -p ./tsconfig.esm.json && echo '{\"type\": \"module\"}' > ./dist/esm/package.json"
},
"dependencies": {
"@acala-network/chopsticks": "workspace:*"
Expand Down Expand Up @@ -38,6 +38,7 @@
"import": "./dist/esm/index.js",
"default": "./dist/esm/index.js"
},
"./package.json": "./package.json"
"./package.json": "./package.json",
"./package.esm.json": "./dist/esm/package.json"
}
}
Loading