-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* improve load plugin * restore * add load rpc plugin * fix * add cli plugin lazy load * fix * fix DISABLE_PLUGINS * refactor * update * dev container * keep extend plugin * fix lint * update read rpc methods * revert executor build * fix lint
- Loading branch information
Showing
27 changed files
with
783 additions
and
760 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"postCreateCommand": "git submodule update --init" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Config } from '../../schema/index.js' | ||
import { HexString } from '@polkadot/util/types' | ||
import { decodeKey } from '@acala-network/chopsticks-core' | ||
import { defaultOptions } from '../../cli-options.js' | ||
import { setupContext } from '../../context.js' | ||
import type { Argv } from 'yargs' | ||
|
||
export const cli = (y: Argv) => { | ||
y.command( | ||
'decode-key <key>', | ||
'Deocde a key', | ||
(yargs) => | ||
yargs | ||
.positional('key', { | ||
desc: 'Key to decode', | ||
type: 'string', | ||
}) | ||
.options({ | ||
...defaultOptions, | ||
}), | ||
async (argv) => { | ||
const context = await setupContext(argv as Config) | ||
const { storage, decodedKey } = decodeKey( | ||
await context.chain.head.meta, | ||
context.chain.head, | ||
argv.key as HexString, | ||
) | ||
if (storage && decodedKey) { | ||
console.log( | ||
`${storage.section}.${storage.method}`, | ||
decodedKey.args.map((x) => JSON.stringify(x.toHuman())).join(', '), | ||
) | ||
} else { | ||
console.log('Unknown') | ||
} | ||
process.exit(0) | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1 @@ | ||
import { Config } from '../../schema/index.js' | ||
import { HexString } from '@polkadot/util/types' | ||
import { decodeKey } from '@acala-network/chopsticks-core' | ||
import { defaultOptions } from '../../cli-options.js' | ||
import { setupContext } from '../../context.js' | ||
import type { Argv } from 'yargs' | ||
|
||
export const cli = (y: Argv) => { | ||
y.command( | ||
'decode-key <key>', | ||
'Deocde a key', | ||
(yargs) => | ||
yargs | ||
.positional('key', { | ||
desc: 'Key to decode', | ||
type: 'string', | ||
}) | ||
.options({ | ||
...defaultOptions, | ||
}), | ||
async (argv) => { | ||
const context = await setupContext(argv as Config) | ||
const { storage, decodedKey } = decodeKey( | ||
await context.chain.head.meta, | ||
context.chain.head, | ||
argv.key as HexString, | ||
) | ||
if (storage && decodedKey) { | ||
console.log( | ||
`${storage.section}.${storage.method}`, | ||
decodedKey.args.map((x) => JSON.stringify(x.toHuman())).join(', '), | ||
) | ||
} else { | ||
console.log('Unknown') | ||
} | ||
process.exit(0) | ||
}, | ||
) | ||
} | ||
export * from './cli.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { Block, defaultLogger, runTask, taskHandler } from '@acala-network/chopsticks-core' | ||
import { Header } from '@polkadot/types/interfaces' | ||
import { HexString } from '@polkadot/util/types' | ||
import _ from 'lodash' | ||
import type { Argv } from 'yargs' | ||
|
||
import { createServer } from '../../server.js' | ||
import { defaultOptions } from '../../cli-options.js' | ||
import { handler } from '../../rpc/index.js' | ||
import { setupContext } from '../../context.js' | ||
import type { Config } from '../../schema/index.js' | ||
|
||
const logger = defaultLogger.child({ name: 'follow-chain' }) | ||
const options = _.pick(defaultOptions, ['endpoint', 'wasm-override', 'runtime-log-level', 'offchain-worker']) | ||
|
||
export const cli = (y: Argv) => { | ||
y.command( | ||
'follow-chain', | ||
'Always follow the latest block on upstream', | ||
(yargs) => | ||
yargs.options({ | ||
...options, | ||
port: { | ||
desc: 'Port to listen on', | ||
number: true, | ||
}, | ||
'head-mode': { | ||
desc: 'Head mode', | ||
choices: ['latest', 'finalized'], | ||
default: 'finalized', | ||
}, | ||
}), | ||
async (argv) => { | ||
const port = argv.port ?? 8000 | ||
const endpoint = argv.endpoint as string | ||
if (/^(https|http):\/\//.test(endpoint || '')) { | ||
throw Error('http provider is not supported') | ||
} | ||
|
||
const context = await setupContext(argv as Config, true) | ||
const { close, port: listenPort } = await createServer(handler(context), port) | ||
logger.info(`${await context.chain.api.getSystemChain()} RPC listening on port ${listenPort}`) | ||
|
||
const chain = context.chain | ||
|
||
chain.api[argv.headMode === 'latest' ? 'subscribeRemoteNewHeads' : 'subscribeRemoteFinalizedHeads']( | ||
async (error, data) => { | ||
try { | ||
if (error) throw error | ||
logger.info({ header: data }, `Follow ${argv.headMode} head from upstream`) | ||
const parent = await chain.getBlock(data.parentHash) | ||
if (!parent) throw Error(`Cannot find parent', ${data.parentHash}`) | ||
const registry = await parent.registry | ||
const header = registry.createType<Header>('Header', data) | ||
const wasm = await parent.wasm | ||
|
||
const block = new Block(chain, header.number.toNumber(), header.hash.toHex(), parent) | ||
await chain.setHead(block) | ||
|
||
const calls: [string, HexString[]][] = [['Core_initialize_block', [header.toHex()]]] | ||
|
||
for (const extrinsic of await block.extrinsics) { | ||
calls.push(['BlockBuilder_apply_extrinsic', [extrinsic]]) | ||
} | ||
|
||
calls.push(['BlockBuilder_finalize_block', []]) | ||
|
||
const result = await runTask( | ||
{ | ||
wasm, | ||
calls, | ||
mockSignatureHost: false, | ||
allowUnresolvedImports: false, | ||
runtimeLogLevel: (argv.runtimeLogLevel as number) || 0, | ||
}, | ||
taskHandler(parent), | ||
) | ||
|
||
if ('Error' in result) { | ||
throw new Error(result.Error) | ||
} | ||
} catch (e) { | ||
logger.error(e, 'Error when processing new head') | ||
await close() | ||
process.exit(1) | ||
} | ||
}, | ||
) | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1 @@ | ||
import { Block, defaultLogger, runTask, taskHandler } from '@acala-network/chopsticks-core' | ||
import { Header } from '@polkadot/types/interfaces' | ||
import { HexString } from '@polkadot/util/types' | ||
import _ from 'lodash' | ||
import type { Argv } from 'yargs' | ||
|
||
import { createServer } from '../../server.js' | ||
import { defaultOptions } from '../../cli-options.js' | ||
import { handler } from '../../rpc/index.js' | ||
import { setupContext } from '../../context.js' | ||
import type { Config } from '../../schema/index.js' | ||
|
||
const logger = defaultLogger.child({ name: 'follow-chain' }) | ||
const options = _.pick(defaultOptions, ['endpoint', 'wasm-override', 'runtime-log-level', 'offchain-worker']) | ||
|
||
export const cli = (y: Argv) => { | ||
y.command( | ||
'follow-chain', | ||
'Always follow the latest block on upstream', | ||
(yargs) => | ||
yargs.options({ | ||
...options, | ||
port: { | ||
desc: 'Port to listen on', | ||
number: true, | ||
}, | ||
'head-mode': { | ||
desc: 'Head mode', | ||
choices: ['latest', 'finalized'], | ||
default: 'finalized', | ||
}, | ||
}), | ||
async (argv) => { | ||
const port = argv.port ?? 8000 | ||
const endpoint = argv.endpoint as string | ||
if (/^(https|http):\/\//.test(endpoint || '')) { | ||
throw Error('http provider is not supported') | ||
} | ||
|
||
const context = await setupContext(argv as Config, true) | ||
const { close, port: listenPort } = await createServer(handler(context), port) | ||
logger.info(`${await context.chain.api.getSystemChain()} RPC listening on port ${listenPort}`) | ||
|
||
const chain = context.chain | ||
|
||
chain.api[argv.headMode === 'latest' ? 'subscribeRemoteNewHeads' : 'subscribeRemoteFinalizedHeads']( | ||
async (error, data) => { | ||
try { | ||
if (error) throw error | ||
logger.info({ header: data }, `Follow ${argv.headMode} head from upstream`) | ||
const parent = await chain.getBlock(data.parentHash) | ||
if (!parent) throw Error(`Cannot find parent', ${data.parentHash}`) | ||
const registry = await parent.registry | ||
const header = registry.createType<Header>('Header', data) | ||
const wasm = await parent.wasm | ||
|
||
const block = new Block(chain, header.number.toNumber(), header.hash.toHex(), parent) | ||
await chain.setHead(block) | ||
|
||
const calls: [string, HexString[]][] = [['Core_initialize_block', [header.toHex()]]] | ||
|
||
for (const extrinsic of await block.extrinsics) { | ||
calls.push(['BlockBuilder_apply_extrinsic', [extrinsic]]) | ||
} | ||
|
||
calls.push(['BlockBuilder_finalize_block', []]) | ||
|
||
const result = await runTask( | ||
{ | ||
wasm, | ||
calls, | ||
mockSignatureHost: false, | ||
allowUnresolvedImports: false, | ||
runtimeLogLevel: (argv.runtimeLogLevel as number) || 0, | ||
}, | ||
taskHandler(parent), | ||
) | ||
|
||
if ('Error' in result) { | ||
throw new Error(result.Error) | ||
} | ||
} catch (e) { | ||
logger.error(e, 'Error when processing new head') | ||
await close() | ||
process.exit(1) | ||
} | ||
}, | ||
) | ||
}, | ||
) | ||
} | ||
export * from './cli.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.