@acala-network/chopsticks-core 0.8.0-6 → 0.8.1

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.
package/lib/api.d.ts CHANGED
@@ -36,7 +36,7 @@ export declare class Api {
36
36
  getBlockHash(blockNumber?: number): Promise<`0x${string}` | null>;
37
37
  getHeader(hash?: string): Promise<Header | null>;
38
38
  getBlock(hash?: string): Promise<SignedBlock | null>;
39
- getStorage(key: string, hash?: string): Promise<string | null>;
39
+ getStorage(key: string, hash?: string): Promise<`0x${string}` | null>;
40
40
  getKeysPaged(prefix: string, pageSize: number, startKey: string, hash?: string): Promise<string[]>;
41
41
  }
42
42
  export {};
package/lib/api.js CHANGED
@@ -22,6 +22,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
22
22
  var _Api_provider, _Api_ready, _Api_chain, _Api_chainProperties;
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.Api = void 0;
25
+ const utils_1 = require("./utils");
25
26
  class Api {
26
27
  constructor(provider, signedExtensions) {
27
28
  _Api_provider.set(this, void 0);
@@ -96,18 +97,43 @@ class Api {
96
97
  }
97
98
  getStorage(key, hash) {
98
99
  return __awaiter(this, void 0, void 0, function* () {
99
- const params = [key];
100
- if (hash)
101
- params.push(hash);
102
- return __classPrivateFieldGet(this, _Api_provider, "f").send('state_getStorage', params);
100
+ const [child, storageKey] = (0, utils_1.splitChildKey)(key);
101
+ if (child) {
102
+ // child storage key, use childstate_getStorage
103
+ const params = [child, storageKey];
104
+ if (hash)
105
+ params.push(hash);
106
+ return __classPrivateFieldGet(this, _Api_provider, "f").send('childstate_getStorage', params);
107
+ }
108
+ else {
109
+ // main storage key, use state_getStorage
110
+ const params = [key];
111
+ if (hash)
112
+ params.push(hash);
113
+ return __classPrivateFieldGet(this, _Api_provider, "f").send('state_getStorage', params);
114
+ }
103
115
  });
104
116
  }
105
117
  getKeysPaged(prefix, pageSize, startKey, hash) {
106
118
  return __awaiter(this, void 0, void 0, function* () {
107
- const params = [prefix, pageSize, startKey];
108
- if (hash)
109
- params.push(hash);
110
- return __classPrivateFieldGet(this, _Api_provider, "f").send('state_getKeysPaged', params);
119
+ const [child, storageKey] = (0, utils_1.splitChildKey)(prefix);
120
+ if (child) {
121
+ // child storage key, use childstate_getKeysPaged
122
+ // strip child prefix from startKey
123
+ const params = [child, storageKey, pageSize, (0, utils_1.stripChildPrefix)(startKey)];
124
+ if (hash)
125
+ params.push(hash);
126
+ return __classPrivateFieldGet(this, _Api_provider, "f")
127
+ .send('childstate_getKeysPaged', params)
128
+ .then((keys) => keys.map((key) => (0, utils_1.prefixedChildKey)(child, key)));
129
+ }
130
+ else {
131
+ // main storage key, use state_getKeysPaged
132
+ const params = [prefix, pageSize, startKey];
133
+ if (hash)
134
+ params.push(hash);
135
+ return __classPrivateFieldGet(this, _Api_provider, "f").send('state_getKeysPaged', params);
136
+ }
111
137
  });
112
138
  }
113
139
  }
@@ -114,7 +114,7 @@ class Block {
114
114
  const layer = new storage_layer_1.StorageLayer(this.storage);
115
115
  yield layer.fold();
116
116
  const prefix = (_a = options.prefix) !== null && _a !== void 0 ? _a : '0x';
117
- const startKey = (_b = options.startKey) !== null && _b !== void 0 ? _b : prefix;
117
+ const startKey = (_b = options.startKey) !== null && _b !== void 0 ? _b : '0x';
118
118
  const pageSize = options.pageSize;
119
119
  return layer.getKeysPaged(prefix, pageSize, startKey);
120
120
  });
@@ -32,13 +32,14 @@ export declare class Blockchain {
32
32
  readonly db: DataSource | undefined;
33
33
  readonly mockSignatureHost: boolean;
34
34
  readonly allowUnresolvedImports: boolean;
35
- readonly runtimeLogLevel: number;
36
35
  readonly registeredTypes: RegisteredTypes;
37
36
  readonly headState: HeadState;
38
37
  readonly offchainWorker: OffchainWorker | undefined;
39
38
  constructor({ api, buildBlockMode, inherentProvider, db, header, mockSignatureHost, allowUnresolvedImports, runtimeLogLevel, registeredTypes, offchainWorker, maxMemoryBlockCount, }: Options);
40
39
  get head(): Block;
41
40
  get txPool(): TxPool;
41
+ get runtimeLogLevel(): number;
42
+ set runtimeLogLevel(level: number);
42
43
  saveBlockToDB(block: Block): Promise<void>;
43
44
  /**
44
45
  * Try to load block from db and register it
@@ -19,7 +19,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
19
19
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
20
20
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
21
21
  };
22
- var _Blockchain_instances, _Blockchain_txpool, _Blockchain_inherentProvider, _Blockchain_head, _Blockchain_blocksByNumber, _Blockchain_blocksByHash, _Blockchain_loadingBlocks, _Blockchain_maxMemoryBlockCount, _Blockchain_registerBlock;
22
+ var _Blockchain_instances, _Blockchain_runtimeLogLevel, _Blockchain_txpool, _Blockchain_inherentProvider, _Blockchain_head, _Blockchain_blocksByNumber, _Blockchain_blocksByHash, _Blockchain_loadingBlocks, _Blockchain_maxMemoryBlockCount, _Blockchain_registerBlock;
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.Blockchain = void 0;
25
25
  const util_crypto_1 = require("@polkadot/util-crypto");
@@ -37,6 +37,7 @@ class Blockchain {
37
37
  constructor({ api, buildBlockMode, inherentProvider, db, header, mockSignatureHost = false, allowUnresolvedImports = false, runtimeLogLevel = 0, registeredTypes = {}, offchainWorker = false, maxMemoryBlockCount = 2000, }) {
38
38
  _Blockchain_instances.add(this);
39
39
  this.uid = Math.random().toString(36).substring(2);
40
+ _Blockchain_runtimeLogLevel.set(this, void 0);
40
41
  _Blockchain_txpool.set(this, void 0);
41
42
  _Blockchain_inherentProvider.set(this, void 0);
42
43
  _Blockchain_head.set(this, void 0);
@@ -48,7 +49,7 @@ class Blockchain {
48
49
  this.db = db;
49
50
  this.mockSignatureHost = mockSignatureHost;
50
51
  this.allowUnresolvedImports = allowUnresolvedImports;
51
- this.runtimeLogLevel = runtimeLogLevel;
52
+ __classPrivateFieldSet(this, _Blockchain_runtimeLogLevel, runtimeLogLevel, "f");
52
53
  this.registeredTypes = registeredTypes;
53
54
  __classPrivateFieldSet(this, _Blockchain_head, new block_1.Block(this, header.number, header.hash), "f");
54
55
  __classPrivateFieldGet(this, _Blockchain_instances, "m", _Blockchain_registerBlock).call(this, __classPrivateFieldGet(this, _Blockchain_head, "f"));
@@ -66,6 +67,13 @@ class Blockchain {
66
67
  get txPool() {
67
68
  return __classPrivateFieldGet(this, _Blockchain_txpool, "f");
68
69
  }
70
+ get runtimeLogLevel() {
71
+ return __classPrivateFieldGet(this, _Blockchain_runtimeLogLevel, "f");
72
+ }
73
+ set runtimeLogLevel(level) {
74
+ __classPrivateFieldSet(this, _Blockchain_runtimeLogLevel, level, "f");
75
+ logger.debug(`Runtime log level set to ${logger.level}`);
76
+ }
69
77
  saveBlockToDB(block) {
70
78
  return __awaiter(this, void 0, void 0, function* () {
71
79
  if (this.db) {
@@ -98,10 +106,20 @@ class Blockchain {
98
106
  .getRepository(entities_1.BlockEntity)
99
107
  .findOne({ where: { [typeof key === 'number' ? 'number' : 'hash']: key } });
100
108
  if (blockData) {
101
- const { hash, number, header, extrinsics, parentHash } = blockData;
102
- const parentBlock = parentHash ? __classPrivateFieldGet(this, _Blockchain_blocksByHash, "f")[parentHash] : undefined;
109
+ const { hash, number, header, extrinsics } = blockData;
110
+ const parentHash = blockData.parentHash || undefined;
111
+ let parentBlock = parentHash ? __classPrivateFieldGet(this, _Blockchain_blocksByHash, "f")[parentHash] : undefined;
112
+ if (!parentBlock) {
113
+ parentBlock = yield this.getBlock(parentHash);
114
+ }
103
115
  const storageDiff = (_a = blockData.storageDiff) !== null && _a !== void 0 ? _a : undefined;
104
- const block = new block_1.Block(this, number, hash, parentBlock, { header, extrinsics, storageDiff });
116
+ const registry = yield this.head.registry;
117
+ const block = new block_1.Block(this, number, hash, parentBlock, {
118
+ header: registry.createType('Header', header),
119
+ extrinsics,
120
+ storage: parentBlock === null || parentBlock === void 0 ? void 0 : parentBlock.storage,
121
+ storageDiff,
122
+ });
105
123
  __classPrivateFieldGet(this, _Blockchain_instances, "m", _Blockchain_registerBlock).call(this, block);
106
124
  return block;
107
125
  }
@@ -358,7 +376,7 @@ class Blockchain {
358
376
  }
359
377
  }
360
378
  exports.Blockchain = Blockchain;
361
- _Blockchain_txpool = new WeakMap(), _Blockchain_inherentProvider = new WeakMap(), _Blockchain_head = new WeakMap(), _Blockchain_blocksByNumber = new WeakMap(), _Blockchain_blocksByHash = new WeakMap(), _Blockchain_loadingBlocks = new WeakMap(), _Blockchain_maxMemoryBlockCount = new WeakMap(), _Blockchain_instances = new WeakSet(), _Blockchain_registerBlock = function _Blockchain_registerBlock(block) {
379
+ _Blockchain_runtimeLogLevel = new WeakMap(), _Blockchain_txpool = new WeakMap(), _Blockchain_inherentProvider = new WeakMap(), _Blockchain_head = new WeakMap(), _Blockchain_blocksByNumber = new WeakMap(), _Blockchain_blocksByHash = new WeakMap(), _Blockchain_loadingBlocks = new WeakMap(), _Blockchain_maxMemoryBlockCount = new WeakMap(), _Blockchain_instances = new WeakSet(), _Blockchain_registerBlock = function _Blockchain_registerBlock(block) {
362
380
  // if exceed max memory block count, delete the oldest block
363
381
  if (__classPrivateFieldGet(this, _Blockchain_blocksByNumber, "f").size === __classPrivateFieldGet(this, _Blockchain_maxMemoryBlockCount, "f")) {
364
382
  const firstKey = __classPrivateFieldGet(this, _Blockchain_blocksByNumber, "f").keys().next().value;
@@ -14,7 +14,7 @@ export interface StorageLayerProvider {
14
14
  export declare class RemoteStorageLayer implements StorageLayerProvider {
15
15
  #private;
16
16
  constructor(api: Api, at: string, db: DataSource | undefined);
17
- get(key: string): Promise<StorageValue>;
17
+ get(key: string, _cache: boolean): Promise<StorageValue>;
18
18
  foldInto(_into: StorageLayer): Promise<StorageLayerProvider>;
19
19
  fold(): Promise<void>;
20
20
  getKeysPaged(prefix: string, pageSize: number, startKey: string): Promise<string[]>;
@@ -46,7 +46,7 @@ class RemoteStorageLayer {
46
46
  __classPrivateFieldSet(this, _RemoteStorageLayer_at, at, "f");
47
47
  __classPrivateFieldSet(this, _RemoteStorageLayer_db, db, "f");
48
48
  }
49
- get(key) {
49
+ get(key, _cache) {
50
50
  var _a, _b;
51
51
  return __awaiter(this, void 0, void 0, function* () {
52
52
  const keyValuePair = (_a = __classPrivateFieldGet(this, _RemoteStorageLayer_db, "f")) === null || _a === void 0 ? void 0 : _a.getRepository(entities_1.KeyValuePair);
@@ -124,7 +124,7 @@ class StorageLayer {
124
124
  if (key in __classPrivateFieldGet(this, _StorageLayer_store, "f")) {
125
125
  return __classPrivateFieldGet(this, _StorageLayer_store, "f")[key];
126
126
  }
127
- if (__classPrivateFieldGet(this, _StorageLayer_deletedPrefix, "f").some((prefix) => key.startsWith(prefix))) {
127
+ if (__classPrivateFieldGet(this, _StorageLayer_deletedPrefix, "f").some((dp) => key.startsWith(dp))) {
128
128
  return StorageValueKind.Deleted;
129
129
  }
130
130
  if (__classPrivateFieldGet(this, _StorageLayer_parent, "f")) {
@@ -194,10 +194,10 @@ class StorageLayer {
194
194
  getKeysPaged(prefix, pageSize, startKey) {
195
195
  var _a, _b;
196
196
  return __awaiter(this, void 0, void 0, function* () {
197
- if (!__classPrivateFieldGet(this, _StorageLayer_deletedPrefix, "f").some((prefix) => startKey.startsWith(prefix))) {
197
+ if (!__classPrivateFieldGet(this, _StorageLayer_deletedPrefix, "f").some((dp) => startKey.startsWith(dp))) {
198
198
  const remote = (_b = (yield ((_a = __classPrivateFieldGet(this, _StorageLayer_parent, "f")) === null || _a === void 0 ? void 0 : _a.getKeysPaged(prefix, pageSize, startKey)))) !== null && _b !== void 0 ? _b : [];
199
199
  for (const key of remote) {
200
- if (__classPrivateFieldGet(this, _StorageLayer_deletedPrefix, "f").some((prefix) => key.startsWith(prefix))) {
200
+ if (__classPrivateFieldGet(this, _StorageLayer_deletedPrefix, "f").some((dp) => key.startsWith(dp))) {
201
201
  continue;
202
202
  }
203
203
  __classPrivateFieldGet(this, _StorageLayer_instances, "m", _StorageLayer_addKey).call(this, key);
@@ -1,5 +1,4 @@
1
1
  import { EntitySchema } from 'typeorm';
2
- import { Header } from '@polkadot/types/interfaces';
3
2
  import { HexString } from '@polkadot/util/types';
4
3
  export declare const KeyValuePair: EntitySchema<{
5
4
  blockHash: string;
@@ -9,7 +8,7 @@ export declare const KeyValuePair: EntitySchema<{
9
8
  export declare const BlockEntity: EntitySchema<{
10
9
  hash: HexString;
11
10
  number: number;
12
- header: Header;
11
+ header: object;
13
12
  parentHash: HexString | null;
14
13
  extrinsics: HexString[];
15
14
  storageDiff: Record<HexString, HexString | null> | null;
@@ -34,12 +34,12 @@ export declare const genesisSchema: z.ZodObject<{
34
34
  }>;
35
35
  }, "strip", z.ZodTypeAny, {
36
36
  name: string;
37
- id: string;
38
37
  properties: {
39
38
  ss58Format?: number | undefined;
40
39
  tokenDecimals?: number | number[] | undefined;
41
40
  tokenSymbol?: string | string[] | undefined;
42
41
  };
42
+ id: string;
43
43
  genesis: {
44
44
  raw: {
45
45
  top: Record<string, string>;
@@ -47,12 +47,12 @@ export declare const genesisSchema: z.ZodObject<{
47
47
  };
48
48
  }, {
49
49
  name: string;
50
- id: string;
51
50
  properties: {
52
51
  ss58Format?: number | undefined;
53
52
  tokenDecimals?: number | number[] | undefined;
54
53
  tokenSymbol?: string | string[] | undefined;
55
54
  };
55
+ id: string;
56
56
  genesis: {
57
57
  raw: {
58
58
  top: Record<string, string>;
@@ -17,3 +17,7 @@ export type Deferred<T> = {
17
17
  promise: Promise<T>;
18
18
  };
19
19
  export declare function defer<T>(): Deferred<T>;
20
+ export declare const prefixedChildKey: (prefix: HexString, key: HexString) => string;
21
+ export declare const isPrefixedChildKey: (key: HexString) => boolean;
22
+ export declare const splitChildKey: (key: HexString) => never[] | [`0x${string}`, `0x${string}`];
23
+ export declare const stripChildPrefix: (key: HexString) => `0x${string}`;
@@ -23,8 +23,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
23
23
  });
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.defer = exports.isUrl = exports.getParaId = exports.compactHex = exports.fetchKeysToArray = exports.fetchKeys = void 0;
26
+ exports.stripChildPrefix = exports.splitChildKey = exports.isPrefixedChildKey = exports.prefixedChildKey = exports.defer = exports.isUrl = exports.getParaId = exports.compactHex = exports.fetchKeysToArray = exports.fetchKeys = void 0;
27
27
  const util_1 = require("@polkadot/util");
28
+ const hex_1 = require("@polkadot/util/hex");
28
29
  __exportStar(require("./set-storage"), exports);
29
30
  __exportStar(require("./time-travel"), exports);
30
31
  __exportStar(require("./decoder"), exports);
@@ -87,3 +88,34 @@ function defer() {
87
88
  return deferred;
88
89
  }
89
90
  exports.defer = defer;
91
+ // Chopsticks treats both main storage and child storage as a key-value store
92
+ // The difference is that child storage keys are prefixed with the child storage key
93
+ // :child_storage:default: as hex string
94
+ const DEFAULT_CHILD_STORAGE = '0x3a6368696c645f73746f726167653a64656661756c743a';
95
+ // length of the child storage key
96
+ const CHILD_LENGTH = DEFAULT_CHILD_STORAGE.length + 64;
97
+ // returns a key that is prefixed with the child storage key
98
+ const prefixedChildKey = (prefix, key) => prefix + (0, hex_1.hexStripPrefix)(key);
99
+ exports.prefixedChildKey = prefixedChildKey;
100
+ // returns true if the key is a child storage key
101
+ const isPrefixedChildKey = (key) => key.startsWith(DEFAULT_CHILD_STORAGE);
102
+ exports.isPrefixedChildKey = isPrefixedChildKey;
103
+ // returns a key that is split into the child storage key and the rest
104
+ const splitChildKey = (key) => {
105
+ if (!key.startsWith(DEFAULT_CHILD_STORAGE))
106
+ return [];
107
+ if (key.length < CHILD_LENGTH)
108
+ return [];
109
+ const child = key.slice(0, CHILD_LENGTH);
110
+ const rest = key.slice(CHILD_LENGTH);
111
+ return [child, (0, hex_1.hexAddPrefix)(rest)];
112
+ };
113
+ exports.splitChildKey = splitChildKey;
114
+ // returns a key that is stripped of the child storage key
115
+ const stripChildPrefix = (key) => {
116
+ const [child, storageKey] = (0, exports.splitChildKey)(key);
117
+ if (!child)
118
+ return key;
119
+ return storageKey;
120
+ };
121
+ exports.stripChildPrefix = stripChildPrefix;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acala-network/chopsticks-core",
3
- "version": "0.8.0-6",
3
+ "version": "0.8.1",
4
4
  "author": "Bryan Chen <xlchen1291@gmail.com>",
5
5
  "license": "Apache-2.0",
6
6
  "scripts": {
@@ -9,7 +9,7 @@
9
9
  "build": "yarn pack-wasm; tsc -p ./tsconfig.json"
10
10
  },
11
11
  "dependencies": {
12
- "@acala-network/chopsticks-executor": "0.8.0-6",
12
+ "@acala-network/chopsticks-executor": "0.8.1",
13
13
  "@polkadot/api": "^10.9.1",
14
14
  "@polkadot/util-crypto": "^12.3.2",
15
15
  "axios": "^1.5.0",
@@ -44,6 +44,5 @@
44
44
  "default": "./lib/*.js"
45
45
  },
46
46
  "./package.json": "./package.json"
47
- },
48
- "stableVersion": "0.7.3"
47
+ }
49
48
  }