@acala-network/chopsticks 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +6 -0
  3. package/dist/api.d.ts +3 -2
  4. package/dist/api.js +15 -20
  5. package/dist/blockchain/block-builder.d.ts +4 -0
  6. package/dist/blockchain/block-builder.js +71 -0
  7. package/dist/blockchain/block.d.ts +12 -8
  8. package/dist/blockchain/block.js +34 -75
  9. package/dist/blockchain/head-state.d.ts +4 -2
  10. package/dist/blockchain/head-state.js +13 -7
  11. package/dist/blockchain/index.d.ts +14 -13
  12. package/dist/blockchain/index.js +31 -27
  13. package/dist/blockchain/inherent/index.d.ts +17 -0
  14. package/dist/blockchain/inherent/index.js +32 -0
  15. package/dist/blockchain/inherent/para-enter.d.ts +7 -0
  16. package/dist/blockchain/inherent/para-enter.js +33 -0
  17. package/dist/blockchain/inherent/parachain/validation-data.d.ts +25 -0
  18. package/dist/blockchain/{inherents.js → inherent/parachain/validation-data.js} +87 -55
  19. package/dist/blockchain/txpool.d.ts +19 -3
  20. package/dist/blockchain/txpool.js +46 -81
  21. package/dist/decode-key.d.ts +2 -0
  22. package/dist/decode-key.js +24 -0
  23. package/dist/executor.d.ts +23 -4
  24. package/dist/executor.js +60 -12
  25. package/dist/executor.test.js +16 -21
  26. package/dist/genesis-provider.d.ts +5 -0
  27. package/dist/genesis-provider.js +21 -2
  28. package/dist/index.d.ts +1 -21
  29. package/dist/index.js +43 -159
  30. package/dist/logger.d.ts +3 -1
  31. package/dist/logger.js +5 -1
  32. package/dist/rpc/dev.js +11 -1
  33. package/dist/rpc/index.js +0 -2
  34. package/dist/rpc/shared.d.ts +0 -6
  35. package/dist/rpc/substrate/author.d.ts +1 -1
  36. package/dist/rpc/substrate/author.js +7 -2
  37. package/dist/rpc/substrate/chain.js +2 -2
  38. package/dist/rpc/substrate/state.js +1 -1
  39. package/dist/rpc/substrate/system.js +3 -3
  40. package/dist/run-block.d.ts +2 -0
  41. package/dist/run-block.js +36 -0
  42. package/dist/schema/index.d.ts +2 -5
  43. package/dist/schema/index.js +1 -2
  44. package/dist/server.d.ts +3 -3
  45. package/dist/server.js +26 -11
  46. package/dist/setup-with-server.d.ts +8 -0
  47. package/dist/setup-with-server.js +23 -0
  48. package/dist/setup.d.ts +10 -0
  49. package/dist/setup.js +62 -0
  50. package/dist/utils/index.d.ts +2 -0
  51. package/dist/utils/index.js +9 -1
  52. package/dist/utils/proof.d.ts +10 -2
  53. package/dist/utils/proof.js +12 -8
  54. package/dist/utils/set-storage.d.ts +2 -1
  55. package/dist/utils/set-storage.js +1 -1
  56. package/dist/utils/time-travel.d.ts +5 -0
  57. package/dist/utils/time-travel.js +64 -0
  58. package/dist/xcm/index.d.ts +3 -0
  59. package/dist/xcm/index.js +67 -0
  60. package/package.json +9 -10
  61. package/dist/blockchain/inherents.d.ts +0 -26
  62. package/dist/rpc/exec.d.ts +0 -3
  63. package/dist/rpc/exec.js +0 -44
  64. package/dist/task.d.ts +0 -38
  65. package/dist/task.js +0 -67
package/dist/setup.js ADDED
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setup = void 0;
4
+ require("@polkadot/types-codec");
5
+ const api_1 = require("@polkadot/api");
6
+ const api_2 = require("./api");
7
+ const blockchain_1 = require("./blockchain");
8
+ const genesis_provider_1 = require("./genesis-provider");
9
+ const inherent_1 = require("./blockchain/inherent");
10
+ const logger_1 = require("./logger");
11
+ const import_storage_1 = require("./utils/import-storage");
12
+ const db_1 = require("./db");
13
+ const time_travel_1 = require("./utils/time-travel");
14
+ const setup = async (argv) => {
15
+ let provider;
16
+ if (argv.genesis) {
17
+ if (typeof argv.genesis === 'string') {
18
+ provider = await genesis_provider_1.GenesisProvider.fromUrl(argv.genesis);
19
+ }
20
+ else {
21
+ provider = new genesis_provider_1.GenesisProvider(argv.genesis);
22
+ }
23
+ }
24
+ else {
25
+ provider = new api_1.WsProvider(argv.endpoint);
26
+ }
27
+ const api = new api_2.Api(provider);
28
+ await api.isReady;
29
+ let blockHash;
30
+ if (argv.block == null) {
31
+ blockHash = await api.getBlockHash();
32
+ }
33
+ else if (Number.isInteger(argv.block)) {
34
+ blockHash = await api.getBlockHash(Number(argv.block));
35
+ }
36
+ else {
37
+ blockHash = argv.block;
38
+ }
39
+ logger_1.defaultLogger.debug({ ...argv, blockHash }, 'Args');
40
+ let db;
41
+ if (argv.db) {
42
+ db = await (0, db_1.openDb)(argv.db);
43
+ }
44
+ const header = await api.getHeader(blockHash);
45
+ const inherents = new inherent_1.InherentProviders(new inherent_1.SetTimestamp(), [new inherent_1.SetValidationData(), new inherent_1.ParaInherentEnter()]);
46
+ const chain = new blockchain_1.Blockchain({
47
+ api,
48
+ buildBlockMode: argv['build-block-mode'],
49
+ inherentProvider: inherents,
50
+ db,
51
+ header: {
52
+ hash: blockHash,
53
+ number: Number(header.number),
54
+ },
55
+ });
56
+ if (argv.timestamp)
57
+ await (0, time_travel_1.timeTravel)(chain, argv.timestamp);
58
+ await (0, import_storage_1.importStorage)(chain, argv['import-storage']);
59
+ await (0, import_storage_1.overrideWasm)(chain, argv['wasm-override']);
60
+ return { chain, api, ws: provider };
61
+ };
62
+ exports.setup = setup;
@@ -1,7 +1,9 @@
1
1
  import { HexString } from '@polkadot/util/types';
2
2
  import { StorageKey } from '@polkadot/types';
3
+ import { Blockchain } from '../blockchain';
3
4
  export type GetKeys = (startKey?: string) => Promise<StorageKey<any>[]>;
4
5
  export type ProcessKey = (key: StorageKey<any>) => any;
5
6
  export declare function fetchKeys(getKeys: GetKeys, processKey: ProcessKey): Promise<void>;
6
7
  export declare function fetchKeysToArray(getKeys: GetKeys): Promise<StorageKey<any>[]>;
7
8
  export declare const compactHex: (value: Uint8Array) => HexString;
9
+ export declare const getParaId: (chain: Blockchain) => Promise<import("@polkadot/types").u32>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.compactHex = exports.fetchKeysToArray = exports.fetchKeys = void 0;
3
+ exports.getParaId = exports.compactHex = exports.fetchKeysToArray = exports.fetchKeys = void 0;
4
4
  const util_1 = require("@polkadot/util");
5
5
  async function fetchKeys(getKeys, processKey) {
6
6
  const processKeys = async (keys) => {
@@ -30,3 +30,11 @@ const compactHex = (value) => {
30
30
  return (0, util_1.u8aToHex)((0, util_1.compactStripLength)(value)[1]);
31
31
  };
32
32
  exports.compactHex = compactHex;
33
+ const getParaId = async (chain) => {
34
+ const meta = await chain.head.meta;
35
+ const raw = await chain.head.get((0, exports.compactHex)(meta.query.parachainInfo.parachainId()));
36
+ if (!raw)
37
+ throw new Error('Cannot find parachain id');
38
+ return meta.registry.createType('u32', (0, util_1.hexToU8a)(raw));
39
+ };
40
+ exports.getParaId = getParaId;
@@ -1,7 +1,15 @@
1
- import { HexString } from '@polkadot/util/types';
1
+ import { HrmpChannelId } from '@polkadot/types/interfaces';
2
2
  import { u32 } from '@polkadot/types';
3
- export declare const WELL_KNOWN_KEYS: Record<string, HexString>;
3
+ export declare const WELL_KNOWN_KEYS: {
4
+ EPOCH_INDEX: `0x${string}`;
5
+ CURRENT_BLOCK_RANDOMNESS: `0x${string}`;
6
+ ONE_EPOCH_AGO_RANDOMNESS: `0x${string}`;
7
+ TWO_EPOCHS_AGO_RANDOMNESS: `0x${string}`;
8
+ CURRENT_SLOT: `0x${string}`;
9
+ ACTIVE_CONFIG: `0x${string}`;
10
+ };
4
11
  export declare const dmqMqcHead: (paraId: u32) => `0x${string}`;
5
12
  export declare const upgradeGoAheadSignal: (paraId: u32) => `0x${string}`;
6
13
  export declare const hrmpIngressChannelIndex: (paraId: u32) => `0x${string}`;
7
14
  export declare const hrmpEgressChannelIndex: (paraId: u32) => `0x${string}`;
15
+ export declare const hrmpChannels: (channelId: HrmpChannelId) => `0x${string}`;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hrmpEgressChannelIndex = exports.hrmpIngressChannelIndex = exports.upgradeGoAheadSignal = exports.dmqMqcHead = exports.WELL_KNOWN_KEYS = void 0;
3
+ exports.hrmpChannels = exports.hrmpEgressChannelIndex = exports.hrmpIngressChannelIndex = exports.upgradeGoAheadSignal = exports.dmqMqcHead = exports.WELL_KNOWN_KEYS = void 0;
4
4
  const util_1 = require("@polkadot/util");
5
5
  const util_crypto_1 = require("@polkadot/util-crypto");
6
6
  exports.WELL_KNOWN_KEYS = {
@@ -11,27 +11,31 @@ exports.WELL_KNOWN_KEYS = {
11
11
  CURRENT_SLOT: '0x1cb6f36e027abb2091cfb5110ab5087f06155b3cd9a8c9e5e9a23fd5dc13a5ed',
12
12
  ACTIVE_CONFIG: '0x06de3d8a54d27e44a9d5ce189618f22db4b49d95320d9021994c850f25b8e385',
13
13
  };
14
- const prefixWithParaId = (prefix, paraId) => {
15
- const id = paraId.toU8a();
16
- return (0, util_1.u8aToHex)((0, util_1.u8aConcat)((0, util_1.hexToU8a)(prefix), (0, util_crypto_1.xxhashAsU8a)(id, 64), id));
14
+ const hash = (prefix, suffix) => {
15
+ return (0, util_1.u8aToHex)((0, util_1.u8aConcat)((0, util_1.hexToU8a)(prefix), (0, util_crypto_1.xxhashAsU8a)(suffix, 64), suffix));
17
16
  };
18
17
  const dmqMqcHead = (paraId) => {
19
18
  const prefix = '0x63f78c98723ddc9073523ef3beefda0c4d7fefc408aac59dbfe80a72ac8e3ce5';
20
- return prefixWithParaId(prefix, paraId);
19
+ return hash(prefix, paraId.toU8a());
21
20
  };
22
21
  exports.dmqMqcHead = dmqMqcHead;
23
22
  const upgradeGoAheadSignal = (paraId) => {
24
23
  const prefix = '0xcd710b30bd2eab0352ddcc26417aa1949e94c040f5e73d9b7addd6cb603d15d3';
25
- return prefixWithParaId(prefix, paraId);
24
+ return hash(prefix, paraId.toU8a());
26
25
  };
27
26
  exports.upgradeGoAheadSignal = upgradeGoAheadSignal;
28
27
  const hrmpIngressChannelIndex = (paraId) => {
29
28
  const prefix = '0x6a0da05ca59913bc38a8630590f2627c1d3719f5b0b12c7105c073c507445948';
30
- return prefixWithParaId(prefix, paraId);
29
+ return hash(prefix, paraId.toU8a());
31
30
  };
32
31
  exports.hrmpIngressChannelIndex = hrmpIngressChannelIndex;
33
32
  const hrmpEgressChannelIndex = (paraId) => {
34
33
  const prefix = '0x6a0da05ca59913bc38a8630590f2627cf12b746dcf32e843354583c9702cc020';
35
- return prefixWithParaId(prefix, paraId);
34
+ return hash(prefix, paraId.toU8a());
36
35
  };
37
36
  exports.hrmpEgressChannelIndex = hrmpEgressChannelIndex;
37
+ const hrmpChannels = (channelId) => {
38
+ const prefix = '0x6a0da05ca59913bc38a8630590f2627cb6604cff828a6e3f579ca6c59ace013d';
39
+ return hash(prefix, channelId.toU8a());
40
+ };
41
+ exports.hrmpChannels = hrmpChannels;
@@ -1,6 +1,7 @@
1
+ import { HexString } from '@polkadot/util/types';
1
2
  import { Blockchain } from '../blockchain';
2
3
  type RawStorageValues = [string, string | null][];
3
4
  type StorageConfig = Record<string, Record<string, any>>;
4
5
  export type StorageValues = RawStorageValues | StorageConfig;
5
- export declare const setStorage: (chain: Blockchain, storage: StorageValues, blockHash?: string) => Promise<string>;
6
+ export declare const setStorage: (chain: Blockchain, storage: StorageValues, blockHash?: HexString) => Promise<HexString>;
6
7
  export {};
@@ -49,7 +49,7 @@ const setStorage = async (chain, storage, blockHash) => {
49
49
  storageItems = storage;
50
50
  }
51
51
  else {
52
- storageItems = objectToStorageItems(await block.withAvoidTasks(() => block.meta), storage);
52
+ storageItems = objectToStorageItems(await block.meta, storage);
53
53
  }
54
54
  block.pushStorageLayer().setAll(storageItems);
55
55
  return block.hash;
@@ -0,0 +1,5 @@
1
+ import { Blockchain } from '../blockchain';
2
+ export declare const getCurrentSlot: (chain: Blockchain) => Promise<number>;
3
+ export declare const getCurrentTimestamp: (chain: Blockchain) => Promise<number>;
4
+ export declare const getSlotDuration: (chain: Blockchain) => Promise<number>;
5
+ export declare const timeTravel: (chain: Blockchain, timestamp: number) => Promise<void>;
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.timeTravel = exports.getSlotDuration = exports.getCurrentTimestamp = exports.getCurrentSlot = void 0;
4
+ const util_1 = require("@polkadot/util");
5
+ const _1 = require(".");
6
+ const executor_1 = require("../executor");
7
+ const set_storage_1 = require("./set-storage");
8
+ const getCurrentSlot = async (chain) => {
9
+ const meta = await chain.head.meta;
10
+ const slotRaw = meta.consts.babe
11
+ ? await chain.head.get((0, _1.compactHex)(meta.query.babe.currentSlot()))
12
+ : await chain.head.get((0, _1.compactHex)(meta.query.aura.currentSlot()));
13
+ if (!slotRaw)
14
+ throw new Error('Cannot find current slot');
15
+ return meta.registry.createType('Slot', (0, util_1.hexToU8a)(slotRaw)).toNumber();
16
+ };
17
+ exports.getCurrentSlot = getCurrentSlot;
18
+ const getCurrentTimestamp = async (chain) => {
19
+ const meta = await chain.head.meta;
20
+ const currentTimestampRaw = (await chain.head.get((0, _1.compactHex)(meta.query.timestamp.now()))) || '0x';
21
+ return meta.registry.createType('u64', (0, util_1.hexToU8a)(currentTimestampRaw)).toNumber();
22
+ };
23
+ exports.getCurrentTimestamp = getCurrentTimestamp;
24
+ const getSlotDuration = async (chain) => {
25
+ const meta = await chain.head.meta;
26
+ return meta.consts.babe
27
+ ? meta.consts.babe.expectedBlockTime.toNumber()
28
+ : meta.query.aura
29
+ ? (0, executor_1.getAuraSlotDuration)(await chain.head.wasm, meta.registry)
30
+ : 12_000;
31
+ };
32
+ exports.getSlotDuration = getSlotDuration;
33
+ const timeTravel = async (chain, timestamp) => {
34
+ const meta = await chain.head.meta;
35
+ const slotDuration = await (0, exports.getSlotDuration)(chain);
36
+ const newSlot = Math.floor(timestamp / slotDuration);
37
+ // new timestamp
38
+ const storage = [
39
+ [(0, _1.compactHex)(meta.query.timestamp.now()), (0, util_1.u8aToHex)(meta.registry.createType('u64', timestamp).toU8a())],
40
+ ];
41
+ if (meta.consts.babe) {
42
+ // new slot
43
+ storage.push([
44
+ (0, _1.compactHex)(meta.query.babe.currentSlot()),
45
+ (0, util_1.u8aToHex)(meta.registry.createType('Slot', newSlot).toU8a()),
46
+ ]);
47
+ // new epoch
48
+ const epochDuration = meta.consts.babe.epochDuration.toNumber();
49
+ const newEpoch = Math.floor(timestamp / epochDuration);
50
+ storage.push([
51
+ (0, _1.compactHex)(meta.query.babe.epochIndex()),
52
+ (0, util_1.u8aToHex)(meta.registry.createType('u64', newEpoch).toU8a()),
53
+ ]);
54
+ }
55
+ else if (meta.query.aura) {
56
+ // new slot
57
+ storage.push([
58
+ (0, _1.compactHex)(meta.query.aura.currentSlot()),
59
+ (0, util_1.u8aToHex)(meta.registry.createType('Slot', newSlot).toU8a()),
60
+ ]);
61
+ }
62
+ await (0, set_storage_1.setStorage)(chain, storage);
63
+ };
64
+ exports.timeTravel = timeTravel;
@@ -0,0 +1,3 @@
1
+ import { Blockchain } from '../blockchain';
2
+ export declare const connectDownward: (relaychain: Blockchain, parachain: Blockchain) => Promise<void>;
3
+ export declare const connectParachains: (parachains: Blockchain[]) => Promise<void>;
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.connectParachains = exports.connectDownward = void 0;
4
+ const util_1 = require("@polkadot/util");
5
+ const utils_1 = require("../utils");
6
+ const logger_1 = require("../logger");
7
+ const set_storage_1 = require("../utils/set-storage");
8
+ const logger = logger_1.defaultLogger.child({ name: 'xcm' });
9
+ const connectDownward = async (relaychain, parachain) => {
10
+ const meta = await relaychain.head.meta;
11
+ const paraId = await (0, utils_1.getParaId)(parachain);
12
+ const downwardMessageQueuesKey = (0, utils_1.compactHex)(meta.query.dmp.downwardMessageQueues(paraId));
13
+ await relaychain.headState.subscribeStorage([downwardMessageQueuesKey], async (head, pairs) => {
14
+ const value = pairs[0][1];
15
+ if (!value)
16
+ return;
17
+ const meta = await relaychain.head.meta;
18
+ const downwardMessageQueuesKey = (0, utils_1.compactHex)(meta.query.dmp.downwardMessageQueues(paraId));
19
+ // clear relaychain message queue
20
+ await (0, set_storage_1.setStorage)(relaychain, [[downwardMessageQueuesKey, null]], head.hash);
21
+ const downwardMessages = meta.registry
22
+ .createType('Vec<PolkadotCorePrimitivesInboundDownwardMessage>', (0, util_1.hexToU8a)(value))
23
+ .toJSON();
24
+ logger.debug({ downwardMessages }, 'downward_message');
25
+ await parachain.newBlock({ inherent: { downwardMessages } });
26
+ });
27
+ logger.info(`Connected relaychain '${await relaychain.api.getSystemChain()}' with parachain '${await parachain.api.getSystemChain()}'`);
28
+ };
29
+ exports.connectDownward = connectDownward;
30
+ const connectParachains = async (parachains) => {
31
+ const list = {};
32
+ for (const chain of parachains) {
33
+ const paraId = await (0, utils_1.getParaId)(chain);
34
+ list[paraId.toNumber()] = chain;
35
+ }
36
+ await connectHorizontal(list);
37
+ logger.info('Parachains connected');
38
+ };
39
+ exports.connectParachains = connectParachains;
40
+ const connectHorizontal = async (parachains) => {
41
+ for (const [id, chain] of Object.entries(parachains)) {
42
+ const meta = await chain.head.meta;
43
+ const hrmpOutboundMessagesKey = (0, utils_1.compactHex)(meta.query.parachainSystem.hrmpOutboundMessages());
44
+ await chain.headState.subscribeStorage([hrmpOutboundMessagesKey], async (head, pairs) => {
45
+ const value = pairs[0][1];
46
+ if (!value)
47
+ return;
48
+ const meta = await chain.head.meta;
49
+ const hrmpOutboundMessagesKey = (0, utils_1.compactHex)(meta.query.parachainSystem.hrmpOutboundMessages());
50
+ // clear sender message queue
51
+ await (0, set_storage_1.setStorage)(chain, [[hrmpOutboundMessagesKey, null]], head.hash);
52
+ const outboundHrmpMessage = meta.registry
53
+ .createType('Vec<PolkadotCorePrimitivesOutboundHrmpMessage>', (0, util_1.hexToU8a)(value))
54
+ .toJSON();
55
+ logger.info({ outboundHrmpMessage }, 'outboundHrmpMessage');
56
+ for (const { recipient, data } of outboundHrmpMessage) {
57
+ const horizontalMessages = {
58
+ [Number(id)]: [{ sentAt: chain.head.number, data }],
59
+ };
60
+ const receiver = parachains[recipient];
61
+ if (receiver) {
62
+ await receiver.newBlock({ inherent: { horizontalMessages } });
63
+ }
64
+ }
65
+ });
66
+ }
67
+ };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@acala-network/chopsticks",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Bryan Chen <xlchen1291@gmail.com>",
7
- "license": "MIT",
7
+ "license": "Apache-2.0",
8
8
  "packageManager": "yarn@3.2.4",
9
9
  "workspaces": [
10
10
  ".",
@@ -17,18 +17,17 @@
17
17
  "prepare": "husky install",
18
18
  "start": "ts-node --transpile-only src/index.ts",
19
19
  "build": "rimraf dist && tsc -p tsconfig.prod.json",
20
- "build-wasm": "cd executor && wasm-pack --log-level=info build --target nodejs --no-default-features --scope acala-network",
20
+ "build-wasm": "wasm-pack build executor --target nodejs --scope acala-network",
21
+ "build-wasm-logging": "yarn build-wasm --features=logging",
21
22
  "check": "cd executor && cargo check --locked",
22
23
  "test": "vitest --silent",
23
- "test:native": "EXECUTOR_CMD='cargo run --manifest-path executor/Cargo.toml --' vitest",
24
24
  "test:dev": "LOG_LEVEL=trace vitest --inspect",
25
25
  "dev": "LOG_LEVEL=trace ts-node-dev --transpile-only --inspect --notify=false src/index.ts -- dev --config=configs/dev.yml",
26
- "dev:native": "yarn dev --executor-cmd='cargo run --manifest-path executor/Cargo.toml --'",
27
26
  "dev:karura": "ts-node-dev --transpile-only --inspect --notify=false src/index.ts -- dev --config=configs/karura.yml",
28
27
  "dev:acala": "ts-node-dev --transpile-only --inspect --notify=false src/index.ts -- dev --config=configs/acala.yml"
29
28
  },
30
29
  "dependencies": {
31
- "@acala-network/chopsticks-executor": "0.2.2",
30
+ "@acala-network/chopsticks-executor": "0.2.3",
32
31
  "@polkadot/api": "^9.10.1",
33
32
  "@polkadot/rpc-provider": "^9.10.1",
34
33
  "@polkadot/types": "^9.10.1",
@@ -36,7 +35,7 @@
36
35
  "@polkadot/types-known": "^9.10.1",
37
36
  "@polkadot/util": "^10.2.1",
38
37
  "@polkadot/util-crypto": "^10.2.1",
39
- "axios": "^1.2.1",
38
+ "axios": "^1.2.2",
40
39
  "js-yaml": "^4.1.0",
41
40
  "lodash": "^4.17.21",
42
41
  "pino": "^8.7.0",
@@ -52,13 +51,13 @@
52
51
  "devDependencies": {
53
52
  "@types/js-yaml": "^4.0.5",
54
53
  "@types/lodash": "^4.14.191",
55
- "@types/node": "^18.11.10",
54
+ "@types/node": "^18.11.18",
56
55
  "@types/rimraf": "^3",
57
56
  "@types/ws": "^8.5.3",
58
- "@types/yargs": "^17.0.15",
57
+ "@types/yargs": "^17.0.18",
59
58
  "@typescript-eslint/eslint-plugin": "^5.45.0",
60
59
  "@typescript-eslint/parser": "^5.45.0",
61
- "eslint": "^8.29.0",
60
+ "eslint": "^8.31.0",
62
61
  "eslint-config-prettier": "^8.5.0",
63
62
  "eslint-plugin-import": "^2.26.0",
64
63
  "eslint-plugin-sort-imports-es6-autofix": "^0.6.0",
@@ -1,26 +0,0 @@
1
- import { Block } from './block';
2
- import { DecoratedMeta } from '@polkadot/types/metadata/decorate/types';
3
- import { TaskManager } from '../task';
4
- export interface CreateInherents {
5
- createInherents(meta: DecoratedMeta, timestamp: number, parent: Block): Promise<string[]>;
6
- }
7
- export interface InherentProvider extends CreateInherents {
8
- getTimestamp(blockNumber: number): number;
9
- }
10
- export declare class SetTimestamp implements InherentProvider {
11
- #private;
12
- constructor(getTimestamp?: (blockNumber: number) => number);
13
- createInherents(meta: DecoratedMeta, timestamp: number, _parent: Block): Promise<string[]>;
14
- getTimestamp(blockNumber: number): number;
15
- }
16
- export declare class InherentProviders implements InherentProvider {
17
- #private;
18
- constructor(base: InherentProvider, providers: CreateInherents[]);
19
- createInherents(meta: DecoratedMeta, timestamp: number, parent: Block): Promise<string[]>;
20
- getTimestamp(blockNumber: number): number;
21
- }
22
- export declare class SetValidationData implements CreateInherents {
23
- #private;
24
- constructor(tasks: TaskManager, expectedIndex: number);
25
- createInherents(meta: DecoratedMeta, _timestamp: number, parent: Block): Promise<string[]>;
26
- }
@@ -1,3 +0,0 @@
1
- import { Handlers } from './shared';
2
- declare const handlers: Handlers;
3
- export default handlers;
package/dist/rpc/exec.js DELETED
@@ -1,44 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const shared_1 = require("./shared");
4
- const logger_1 = require("../logger");
5
- const logger = logger_1.defaultLogger.child({ name: 'rpc-exec' });
6
- const handlers = {
7
- exec_storageGet: async (context, [_taskId, blockHash, key]) => {
8
- const block = await context.chain.getBlock(blockHash);
9
- if (!block) {
10
- throw new shared_1.ResponseError(1, `Block not found ${blockHash}`);
11
- }
12
- const value = await block.get(key);
13
- logger.trace({ blockHash, key, value: (0, logger_1.truncate)(value) }, 'exec_storageGet');
14
- return value;
15
- },
16
- exec_prefixKeys: async (context, [_taskId, blockHash, key]) => {
17
- const block = await context.chain.getBlock(blockHash);
18
- if (!block) {
19
- throw new shared_1.ResponseError(1, `Block not found ${blockHash}`);
20
- }
21
- return block.getKeysPaged({ prefix: key, pageSize: 1000, startKey: key });
22
- },
23
- exec_nextKey: async (context, [_taskId, blockHash, key]) => {
24
- const block = await context.chain.getBlock(blockHash);
25
- if (!block) {
26
- throw new shared_1.ResponseError(1, `Block not found ${blockHash}`);
27
- }
28
- const res = await block.getKeysPaged({ prefix: key, pageSize: 1, startKey: key });
29
- return res[0] || null;
30
- },
31
- exec_getTask: async (context, [taskId]) => {
32
- logger.trace({ taskId }, 'exec_getTask');
33
- const task = context.tasks.getTask(Number(taskId));
34
- if (!task) {
35
- throw new shared_1.ResponseError(1, `Task not found ${taskId}`);
36
- }
37
- return task.task;
38
- },
39
- exec_taskResult: async (context, [taskId, resp]) => {
40
- logger.trace({ taskId }, 'exec_taskResult');
41
- context.tasks.getTask(Number(taskId))?.callback(resp);
42
- },
43
- };
44
- exports.default = handlers;
package/dist/task.d.ts DELETED
@@ -1,38 +0,0 @@
1
- export interface TaskResponseCall {
2
- Call: {
3
- result: `0x${string}}`;
4
- storageDiff: [`0x${string}}`, `0x${string}}` | null][];
5
- };
6
- }
7
- export interface TaskResponseError {
8
- Error: string;
9
- }
10
- export type TaskResponse = TaskResponseCall | TaskResponseError;
11
- interface TaskCall {
12
- wasm: string;
13
- blockHash: string;
14
- calls: [string, string][];
15
- mockSignatureHost?: boolean;
16
- allowUnresolvedImports?: boolean;
17
- }
18
- interface TaskCalculateStateRoot {
19
- entries: [string, string][];
20
- }
21
- type Task = {
22
- Call: TaskCall;
23
- } | {
24
- CalculateStateRoot: TaskCalculateStateRoot;
25
- };
26
- export declare class TaskManager {
27
- #private;
28
- constructor(listeningPort: number, mockSignatureHost?: boolean, executorCmd?: string, allowUnresolvedImports?: boolean);
29
- updateListeningPort(port: number): void;
30
- addTask(task: Task, callback?: (res: TaskResponse) => any): number;
31
- getTask(taskId: number): {
32
- task: Task;
33
- callback: (res: TaskResponse) => any;
34
- };
35
- runTask(taskId: number): Promise<void>;
36
- addAndRunTask(task: Task, callback?: (res: TaskResponse) => any): Promise<void>;
37
- }
38
- export {};
package/dist/task.js DELETED
@@ -1,67 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TaskManager = void 0;
4
- const node_child_process_1 = require("node:child_process");
5
- const logger_1 = require("./logger");
6
- const executor_1 = require("./executor");
7
- const logger = logger_1.defaultLogger.child({ name: 'task' });
8
- class TaskManager {
9
- #tasks = [];
10
- #listeningPort;
11
- #mockSignatureHost;
12
- #executorCmd;
13
- #allowUnresolvedImports;
14
- constructor(listeningPort, mockSignatureHost = false, executorCmd, allowUnresolvedImports = false) {
15
- this.#listeningPort = listeningPort;
16
- this.#mockSignatureHost = mockSignatureHost;
17
- this.#executorCmd = executorCmd;
18
- this.#allowUnresolvedImports = allowUnresolvedImports;
19
- if (this.#mockSignatureHost) {
20
- logger.info('Mock signature host enabled');
21
- }
22
- if (this.#allowUnresolvedImports) {
23
- logger.info('Allow unresolved imports enabled. Wasm may expect more host functions, but will not fail on load');
24
- }
25
- }
26
- updateListeningPort(port) {
27
- this.#listeningPort = port;
28
- }
29
- addTask(task, callback = () => { }) {
30
- logger.debug({
31
- kind: Object.keys(task)[0],
32
- port: this.#listeningPort,
33
- }, 'AddTask');
34
- if ('Call' in task && task.Call.mockSignatureHost === undefined) {
35
- task.Call.mockSignatureHost = this.#mockSignatureHost;
36
- }
37
- if ('Call' in task && task.Call.allowUnresolvedImports === undefined) {
38
- task.Call.allowUnresolvedImports = this.#allowUnresolvedImports;
39
- }
40
- this.#tasks.push({ task, callback });
41
- return this.#tasks.length - 1;
42
- }
43
- getTask(taskId) {
44
- return this.#tasks[taskId];
45
- }
46
- runTask(taskId) {
47
- if (this.#executorCmd) {
48
- const cmd = `${this.#executorCmd} --runner-url=ws://localhost:${this.#listeningPort} --task-id=${taskId}`;
49
- logger.info({ taskId, cmd }, 'RunTask');
50
- const p = (0, node_child_process_1.spawn)(cmd, { shell: true, stdio: 'inherit' });
51
- return new Promise((resolve) => {
52
- p.once('exit', (code) => {
53
- logger.debug({ taskId, code }, 'RunTask done');
54
- resolve();
55
- });
56
- });
57
- }
58
- else {
59
- return (0, executor_1.runTask)(taskId, `ws://localhost:${this.#listeningPort}`);
60
- }
61
- }
62
- async addAndRunTask(task, callback = () => { }) {
63
- const taskId = this.addTask(task, callback);
64
- await this.runTask(taskId);
65
- }
66
- }
67
- exports.TaskManager = TaskManager;