Skip to content

Commit

Permalink
use ts path, refactor, cleanup (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci authored Dec 20, 2022
1 parent c52dc02 commit 9a7a976
Show file tree
Hide file tree
Showing 33 changed files with 456 additions and 270 deletions.
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ module.exports = {
},
],
},
settings: {
'import/resolver': {
typescript: {},
},
},
}
18 changes: 9 additions & 9 deletions e2e/helper.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { ApiPromise, WsProvider } from '@polkadot/api'
import { Codec } from '@polkadot/types/types'
import { Keyring } from '@polkadot/keyring'
import { ProviderInterface } from '@polkadot/rpc-provider/types'
import { beforeAll, beforeEach, expect, vi } from 'vitest'

import { Api } from '../src/api'
import { Blockchain } from '../src/blockchain'
import { BuildBlockMode } from '../src/blockchain/txpool'
import { GenesisProvider } from '../src/genesis-provider'
import { InherentProviders, SetTimestamp, SetValidationData } from '../src/blockchain/inherents'
import { ProviderInterface } from '@polkadot/rpc-provider/types'
import { StorageValues } from '../src/utils/set-storage'
import { createServer } from '../src/server'
import { handler } from '../src/rpc'
import { Api } from '@chopsticks/api'
import { Blockchain } from '@chopsticks/blockchain'
import { BuildBlockMode } from '@chopsticks/blockchain/txpool'
import { GenesisProvider } from '@chopsticks/genesis-provider'
import { InherentProviders, SetTimestamp, SetValidationData } from '@chopsticks/blockchain/inherent'
import { StorageValues } from '@chopsticks/utils/set-storage'
import { createServer } from '@chopsticks/server'
import { handler } from '@chopsticks/rpc'

export type SetupOption = {
endpoint?: string
Expand Down
2 changes: 1 addition & 1 deletion e2e/import-storage/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest'
import path from 'path'

import { api, chain, setupApi } from '../helper'
import { importStorage, overrideWasm } from '../../src/utils/import-storage'
import { importStorage, overrideWasm } from '@chopsticks/utils/import-storage'

setupApi({
endpoint: 'wss://acala-rpc-1.aca-api.network',
Expand Down
2 changes: 1 addition & 1 deletion e2e/metadata.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { chain, setupApi } from '../e2e/helper'
import { chain, setupApi } from './helper'
import { describe, expect, it } from 'vitest'

setupApi({
Expand Down
2 changes: 1 addition & 1 deletion executor/pkg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
],
"main": "chopsticks_executor.js",
"types": "chopsticks_executor.d.ts"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@typescript-eslint/parser": "^5.45.0",
"eslint": "^8.29.0",
"eslint-config-prettier": "^8.5.0",
"eslint-import-resolver-typescript": "^3.5.2",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-sort-imports-es6-autofix": "^0.6.0",
"husky": "^8.0.2",
Expand Down
6 changes: 3 additions & 3 deletions src/blockchain/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import type { HexString } from '@polkadot/util/types'

import { Blockchain } from '.'
import { RemoteStorageLayer, StorageLayer, StorageLayerProvider, StorageValueKind } from './storage-layer'
import { compactHex } from '../utils'
import { getRuntimeVersion, runTask, taskHandler } from '../executor'
import type { RuntimeVersion } from '../executor'
import { compactHex } from '@chopsticks/utils'
import { getRuntimeVersion, runTask, taskHandler } from '@chopsticks/executor'
import type { RuntimeVersion } from '@chopsticks/executor'

export type TaskCallResponse = {
result: HexString
Expand Down
8 changes: 4 additions & 4 deletions src/blockchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { blake2AsHex } from '@polkadot/util-crypto'
import { u8aConcat, u8aToHex } from '@polkadot/util'
import type { TransactionValidity } from '@polkadot/types/interfaces/txqueue'

import { Api } from '../api'
import { Api } from '@chopsticks/api'
import { Block } from './block'
import { BuildBlockMode, TxPool } from './txpool'
import { HeadState } from './head-state'
import { InherentProvider } from './inherents'
import { ResponseError } from '../rpc/shared'
import { defaultLogger } from '../logger'
import { InherentProvider } from './inherent'
import { ResponseError } from '@chopsticks/rpc/shared'
import { defaultLogger } from '@chopsticks/logger'

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

Expand Down
52 changes: 52 additions & 0 deletions src/blockchain/inherent/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Block } from '../block'
import { DecoratedMeta } from '@polkadot/types/metadata/decorate/types'
import { GenericExtrinsic } from '@polkadot/types'
import { HexString } from '@polkadot/util/types'

export { SetValidationData } from './parachain/validation-data'

export interface CreateInherents {
createInherents(meta: DecoratedMeta, timestamp: number, parent: Block): Promise<HexString[]>
}

export interface InherentProvider extends CreateInherents {
getTimestamp(blockNumber: number): number
}

export class SetTimestamp implements InherentProvider {
readonly #getTimestamp: (blockNumber: number) => number

constructor(getTimestamp: (blockNumber: number) => number = Date.now) {
this.#getTimestamp = getTimestamp
}

async createInherents(meta: DecoratedMeta, timestamp: number, _parent: Block): Promise<HexString[]> {
return [new GenericExtrinsic(meta.registry, meta.tx.timestamp.set(timestamp)).toHex()]
}

getTimestamp(blockNumber: number): number {
return this.#getTimestamp(blockNumber)
}
}

export class InherentProviders implements InherentProvider {
readonly #base: InherentProvider
readonly #providers: CreateInherents[]

constructor(base: InherentProvider, providers: CreateInherents[]) {
this.#base = base
this.#providers = providers
}

async createInherents(meta: DecoratedMeta, timestamp: number, parent: Block): Promise<HexString[]> {
const base = await this.#base.createInherents(meta, timestamp, parent)
const extra = await Promise.all(
this.#providers.map((provider) => provider.createInherents(meta, timestamp, parent))
)
return [...base, ...extra.flat()]
}

getTimestamp(blockNumber: number): number {
return this.#base.getTimestamp(blockNumber)
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,13 @@
import { Block } from './block'
import { DecoratedMeta } from '@polkadot/types/metadata/decorate/types'
import { GenericExtrinsic } from '@polkadot/types'
import { HexString } from '@polkadot/util/types'
import { compactHex } from '../utils'
import { createProof } from '../executor'
import { hexToU8a } from '@polkadot/util'
import { upgradeGoAheadSignal } from '../utils/proof'

export interface CreateInherents {
createInherents(meta: DecoratedMeta, timestamp: number, parent: Block): Promise<HexString[]>
}

export interface InherentProvider extends CreateInherents {
getTimestamp(blockNumber: number): number
}

export class SetTimestamp implements InherentProvider {
readonly #getTimestamp: (blockNumber: number) => number

constructor(getTimestamp: (blockNumber: number) => number = Date.now) {
this.#getTimestamp = getTimestamp
}

async createInherents(meta: DecoratedMeta, timestamp: number, _parent: Block): Promise<HexString[]> {
return [new GenericExtrinsic(meta.registry, meta.tx.timestamp.set(timestamp)).toHex()]
}

getTimestamp(blockNumber: number): number {
return this.#getTimestamp(blockNumber)
}
}
import { Block } from '@chopsticks/blockchain/block'
import { CreateInherents } from '..'
import { compactHex } from '@chopsticks/utils'
import { createProof } from '@chopsticks/executor'
import { upgradeGoAheadSignal } from '@chopsticks/utils/proof'

const MOCK_VALIDATION_DATA = {
validationData: {
Expand All @@ -56,28 +34,6 @@ const MOCK_VALIDATION_DATA = {
},
}

export class InherentProviders implements InherentProvider {
readonly #base: InherentProvider
readonly #providers: CreateInherents[]

constructor(base: InherentProvider, providers: CreateInherents[]) {
this.#base = base
this.#providers = providers
}

async createInherents(meta: DecoratedMeta, timestamp: number, parent: Block): Promise<HexString[]> {
const base = await this.#base.createInherents(meta, timestamp, parent)
const extra = await Promise.all(
this.#providers.map((provider) => provider.createInherents(meta, timestamp, parent))
)
return [...base, ...extra.flat()]
}

getTimestamp(blockNumber: number): number {
return this.#base.getTimestamp(blockNumber)
}
}

export class SetValidationData implements CreateInherents {
async createInherents(meta: DecoratedMeta, _timestamp: number, parent: Block): Promise<HexString[]> {
if (!meta.tx.parachainSystem?.setValidationData) {
Expand Down
4 changes: 2 additions & 2 deletions src/blockchain/storage-layer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DataSource } from 'typeorm'
import _ from 'lodash'

import { Api } from '../api'
import { defaultLogger } from '../logger'
import { Api } from '@chopsticks/api'
import { defaultLogger } from '@chopsticks/logger'

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

Expand Down
8 changes: 4 additions & 4 deletions src/blockchain/txpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import _ from 'lodash'

import { Block } from './block'
import { Blockchain } from '.'
import { InherentProvider } from './inherents'
import { ResponseError } from '../rpc/shared'
import { compactHex } from '../utils'
import { defaultLogger, truncate, truncateStorageDiff } from '../logger'
import { InherentProvider } from './inherent'
import { ResponseError } from '@chopsticks/rpc/shared'
import { compactHex } from '@chopsticks/utils'
import { defaultLogger, truncate, truncateStorageDiff } from '@chopsticks/logger'

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

Expand Down
24 changes: 24 additions & 0 deletions src/decode-key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import '@polkadot/types-codec'
import { u8aToHex } from '@polkadot/util'

import { setup } from './setup'

export const decodeKey = async (argv: any) => {
const context = await setup(argv)

const key = argv.key
const meta = await context.chain.head.meta
outer: for (const module of Object.values(meta.query)) {
for (const storage of Object.values(module)) {
const keyPrefix = u8aToHex(storage.keyPrefix())
if (key.startsWith(keyPrefix)) {
const decodedKey = meta.registry.createType('StorageKey', key)
decodedKey.setMeta(storage.meta)
console.log(`${storage.section}.${storage.method}`, decodedKey.args.map((x) => x.toHuman()).join(', '))
break outer
}
}
}

setTimeout(() => process.exit(0), 50)
}
18 changes: 9 additions & 9 deletions src/executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,34 @@ import {
hrmpEgressChannelIndex,
hrmpIngressChannelIndex,
upgradeGoAheadSignal,
} from './utils/proof'
import { calculateStateRoot, createProof, decodeProof, getRuntimeVersion } from './executor'
} from '@chopsticks/utils/proof'
import { calculateStateRoot, createProof, decodeProof, getRuntimeVersion } from '@chopsticks/executor'

describe('wasm', () => {
it('get runtime version from wasm runtime', async () => {
const code = String(readFileSync(path.join(__dirname, 'runtime.example'))).trim()
const code = String(readFileSync(path.join(__dirname, '../e2e/blobs/acala-runtime-2101.txt'))).trim()
expect(code.length).toBeGreaterThan(2)

const expectedRuntimeVersion = {
specName: 'acala',
implName: 'acala',
authoringVersion: 1,
specVersion: 2000,
specVersion: 2101,
implVersion: 0,
apis: [
['0xdf6acb689907609b', 3],
['0xdf6acb689907609b', 4],
['0x37e397fc7c91f5e4', 1],
['0x40fe3ad401f8959a', 5],
['0x40fe3ad401f8959a', 6],
['0xd2bc9897eed08f15', 3],
['0xf78b278be53f454c', 2],
['0xdd718d5cc53262d4', 1],
['0xab3c0572291feb8b', 1],
['0xbc9d89904f5b923f', 1],
['0x37c8bb1350a9a2a8', 1],
['0x6ef953004ba30e59', 1],
['0xf485c9145d3f0aad', 1],
['0xe3df3f2aa8a5cc57', 1],
['0xea93e3f16f3d6962', 1],
['0x955e168e0cfb3409', 1],
['0xe3df3f2aa8a5cc57', 2],
['0xea93e3f16f3d6962', 2],
],
transactionVersion: 1,
stateVersion: 0,
Expand Down
4 changes: 2 additions & 2 deletions src/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import {
} from '@polkadot/util'
global.WebSocket = WebSocket

import { Block } from './blockchain/block'
import { Block } from '@chopsticks/blockchain/block'
import {
calculate_state_root,
create_proof,
decode_proof,
get_runtime_version,
run_task,
} from '@acala-network/chopsticks-executor'
import { defaultLogger, truncate, truncateStorageDiff } from './logger'
import { defaultLogger, truncate, truncateStorageDiff } from '@chopsticks/logger'

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

Expand Down
4 changes: 2 additions & 2 deletions src/genesis-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { lstatSync, readFileSync } from 'node:fs'
import { stringToHex } from '@polkadot/util'
import axios from 'axios'

import { Genesis, genesisSchema } from './schema'
import { calculateStateRoot, runTask } from './executor'
import { Genesis, genesisSchema } from '@chopsticks/schema'
import { calculateStateRoot, runTask } from '@chopsticks/executor'

export class GenesisProvider implements ProviderInterface {
#isConnected = false
Expand Down
Loading

0 comments on commit 9a7a976

Please sign in to comment.