@acala-network/chopsticks-core 0.8.0 → 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
  });
@@ -106,13 +106,18 @@ class Blockchain {
106
106
  .getRepository(entities_1.BlockEntity)
107
107
  .findOne({ where: { [typeof key === 'number' ? 'number' : 'hash']: key } });
108
108
  if (blockData) {
109
- const { hash, number, header, extrinsics, parentHash } = blockData;
110
- 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
+ }
111
115
  const storageDiff = (_a = blockData.storageDiff) !== null && _a !== void 0 ? _a : undefined;
112
116
  const registry = yield this.head.registry;
113
117
  const block = new block_1.Block(this, number, hash, parentBlock, {
114
118
  header: registry.createType('Header', header),
115
119
  extrinsics,
120
+ storage: parentBlock === null || parentBlock === void 0 ? void 0 : parentBlock.storage,
116
121
  storageDiff,
117
122
  });
118
123
  __classPrivateFieldGet(this, _Blockchain_instances, "m", _Blockchain_registerBlock).call(this, block);
@@ -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);
@@ -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",
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",
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",