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

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
@@ -33,11 +33,10 @@ export declare class Api {
33
33
  getSystemName(): Promise<string>;
34
34
  getSystemProperties(): Promise<ChainProperties>;
35
35
  getSystemChain(): Promise<string>;
36
- getMetadata(hash?: string): Promise<string>;
37
- getBlockHash(blockNumber?: number): Promise<`0x${string}`>;
38
- getHeader(hash?: string): Promise<Header>;
39
- getBlock(hash?: string): Promise<SignedBlock>;
40
- getStorage(key: string, hash?: string): Promise<string>;
36
+ getBlockHash(blockNumber?: number): Promise<`0x${string}` | null>;
37
+ getHeader(hash?: string): Promise<Header | null>;
38
+ getBlock(hash?: string): Promise<SignedBlock | null>;
39
+ getStorage(key: string, hash?: string): Promise<string | null>;
41
40
  getKeysPaged(prefix: string, pageSize: number, startKey: string, hash?: string): Promise<string[]>;
42
41
  }
43
42
  export {};
package/lib/api.js CHANGED
@@ -79,11 +79,6 @@ class Api {
79
79
  return __classPrivateFieldGet(this, _Api_provider, "f").send('system_chain', []);
80
80
  });
81
81
  }
82
- getMetadata(hash) {
83
- return __awaiter(this, void 0, void 0, function* () {
84
- return __classPrivateFieldGet(this, _Api_provider, "f").send('state_getMetadata', hash ? [hash] : []);
85
- });
86
- }
87
82
  getBlockHash(blockNumber) {
88
83
  return __awaiter(this, void 0, void 0, function* () {
89
84
  return __classPrivateFieldGet(this, _Api_provider, "f").send('chain_getBlockHash', Number.isInteger(blockNumber) ? [blockNumber] : []);
@@ -1,8 +1,8 @@
1
1
  import { Header, TransactionValidityError } from '@polkadot/types/interfaces';
2
2
  import { Block, TaskCallResponse } from './block';
3
3
  import { HexString } from '@polkadot/util/types';
4
- export declare const newHeader: (head: Block) => Promise<Header>;
5
- export declare const buildBlock: (head: Block, inherents: HexString[], extrinsics: HexString[], ump: Record<number, HexString[]>, onApplyExtrinsicError: (extrinsic: HexString, error: TransactionValidityError) => void) => Promise<[Block, HexString[]]>;
4
+ export declare const newHeader: (head: Block, unsafeBlockHeight?: number) => Promise<Header>;
5
+ export declare const buildBlock: (head: Block, inherents: HexString[], extrinsics: HexString[], ump: Record<number, HexString[]>, onApplyExtrinsicError: (extrinsic: HexString, error: TransactionValidityError) => void, unsafeBlockHeight?: number) => Promise<[Block, HexString[]]>;
6
6
  export declare const dryRunExtrinsic: (head: Block, inherents: HexString[], extrinsic: HexString | {
7
7
  call: HexString;
8
8
  address: string;
@@ -42,7 +42,7 @@ const getNewSlot = (digest, slotNumber) => {
42
42
  }
43
43
  return digest.toJSON();
44
44
  };
45
- const newHeader = (head) => __awaiter(void 0, void 0, void 0, function* () {
45
+ const newHeader = (head, unsafeBlockHeight) => __awaiter(void 0, void 0, void 0, function* () {
46
46
  var _a, _b;
47
47
  const meta = yield head.meta;
48
48
  const parentHeader = yield head.header;
@@ -80,7 +80,7 @@ const newHeader = (head) => __awaiter(void 0, void 0, void 0, function* () {
80
80
  }
81
81
  const header = meta.registry.createType('Header', {
82
82
  parentHash: head.hash,
83
- number: head.number + 1,
83
+ number: unsafeBlockHeight !== null && unsafeBlockHeight !== void 0 ? unsafeBlockHeight : head.number + 1,
84
84
  stateRoot: '0x0000000000000000000000000000000000000000000000000000000000000000',
85
85
  extrinsicsRoot: '0x0000000000000000000000000000000000000000000000000000000000000000',
86
86
  digest: {
@@ -126,14 +126,18 @@ const initNewBlock = (head, header, inherents, storageLayer) => __awaiter(void 0
126
126
  layers: layers,
127
127
  };
128
128
  });
129
- const buildBlock = (head, inherents, extrinsics, ump, onApplyExtrinsicError) => __awaiter(void 0, void 0, void 0, function* () {
129
+ const buildBlock = (head, inherents, extrinsics, ump, onApplyExtrinsicError, unsafeBlockHeight) => __awaiter(void 0, void 0, void 0, function* () {
130
130
  const registry = yield head.registry;
131
- const header = yield (0, exports.newHeader)(head);
131
+ const header = yield (0, exports.newHeader)(head, unsafeBlockHeight);
132
+ const newBlockNumber = header.number.toNumber();
133
+ if (newBlockNumber < head.number) {
134
+ throw new Error('unsafeBlockHeight is not allowed to be less than current block number');
135
+ }
132
136
  logger.info({
133
- number: head.number + 1,
137
+ number: newBlockNumber,
134
138
  extrinsicsCount: extrinsics.length,
135
139
  umpCount: Object.keys(ump).length,
136
- }, `Try building block #${(head.number + 1).toLocaleString()}`);
140
+ }, `Try building block #${newBlockNumber.toLocaleString()}`);
137
141
  let layer;
138
142
  // apply ump via storage override hack
139
143
  if (Object.keys(ump).length > 0) {
@@ -75,7 +75,12 @@ class Block {
75
75
  }
76
76
  get extrinsics() {
77
77
  if (!__classPrivateFieldGet(this, _Block_extrinsics, "f")) {
78
- __classPrivateFieldSet(this, _Block_extrinsics, __classPrivateFieldGet(this, _Block_chain, "f").api.getBlock(this.hash).then((b) => b.block.extrinsics), "f");
78
+ __classPrivateFieldSet(this, _Block_extrinsics, __classPrivateFieldGet(this, _Block_chain, "f").api.getBlock(this.hash).then((b) => {
79
+ if (!b) {
80
+ throw new Error(`Block ${this.hash} not found`);
81
+ }
82
+ return b.block.extrinsics;
83
+ }), "f");
79
84
  }
80
85
  return __classPrivateFieldGet(this, _Block_extrinsics, "f");
81
86
  }
@@ -91,14 +91,16 @@ class Blockchain {
91
91
  * If pass in number, get block by number, else get block by hash
92
92
  */
93
93
  loadBlockFromDB(key) {
94
+ var _a;
94
95
  return __awaiter(this, void 0, void 0, function* () {
95
96
  if (this.db) {
96
97
  const blockData = yield this.db
97
98
  .getRepository(entities_1.BlockEntity)
98
99
  .findOne({ where: { [typeof key === 'number' ? 'number' : 'hash']: key } });
99
100
  if (blockData) {
100
- const { hash, number, header, extrinsics, parentHash, storageDiff } = blockData;
101
+ const { hash, number, header, extrinsics, parentHash } = blockData;
101
102
  const parentBlock = parentHash ? __classPrivateFieldGet(this, _Blockchain_blocksByHash, "f")[parentHash] : undefined;
103
+ const storageDiff = (_a = blockData.storageDiff) !== null && _a !== void 0 ? _a : undefined;
102
104
  const block = new block_1.Block(this, number, hash, parentBlock, { header, extrinsics, storageDiff });
103
105
  __classPrivateFieldGet(this, _Blockchain_instances, "m", _Blockchain_registerBlock).call(this, block);
104
106
  return block;
@@ -121,6 +123,9 @@ class Blockchain {
121
123
  return blockFromDB;
122
124
  }
123
125
  const hash = yield this.api.getBlockHash(number);
126
+ if (!hash) {
127
+ return undefined;
128
+ }
124
129
  const block = new block_1.Block(this, number, hash);
125
130
  __classPrivateFieldGet(this, _Blockchain_instances, "m", _Blockchain_registerBlock).call(this, block);
126
131
  }
@@ -144,6 +149,9 @@ class Blockchain {
144
149
  const blockFromDB = yield this.loadBlockFromDB(hash);
145
150
  if (!blockFromDB) {
146
151
  const header = yield this.api.getHeader(hash);
152
+ if (!header) {
153
+ throw new Error(`Block ${hash} not found`);
154
+ }
147
155
  const block = new block_1.Block(this, Number(header.number), hash);
148
156
  __classPrivateFieldGet(this, _Blockchain_instances, "m", _Blockchain_registerBlock).call(this, block);
149
157
  }
@@ -47,19 +47,19 @@ class RemoteStorageLayer {
47
47
  __classPrivateFieldSet(this, _RemoteStorageLayer_db, db, "f");
48
48
  }
49
49
  get(key) {
50
- var _a;
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);
53
53
  if (__classPrivateFieldGet(this, _RemoteStorageLayer_db, "f")) {
54
54
  const res = yield (keyValuePair === null || keyValuePair === void 0 ? void 0 : keyValuePair.findOne({ where: { key, blockHash: __classPrivateFieldGet(this, _RemoteStorageLayer_at, "f") } }));
55
55
  if (res) {
56
- return res.value;
56
+ return (_b = res.value) !== null && _b !== void 0 ? _b : undefined;
57
57
  }
58
58
  }
59
59
  logger.trace({ at: __classPrivateFieldGet(this, _RemoteStorageLayer_at, "f"), key }, 'RemoteStorageLayer get');
60
60
  const data = yield __classPrivateFieldGet(this, _RemoteStorageLayer_api, "f").getStorage(key, __classPrivateFieldGet(this, _RemoteStorageLayer_at, "f"));
61
61
  keyValuePair === null || keyValuePair === void 0 ? void 0 : keyValuePair.upsert({ key, blockHash: __classPrivateFieldGet(this, _RemoteStorageLayer_at, "f"), value: data }, ['key', 'blockHash']);
62
- return data;
62
+ return data !== null && data !== void 0 ? data : undefined;
63
63
  });
64
64
  }
65
65
  foldInto(_into) {
@@ -21,6 +21,7 @@ export interface BuildBlockParams {
21
21
  upwardMessages: Record<number, HexString[]>;
22
22
  horizontalMessages: Record<number, HorizontalMessage[]>;
23
23
  transactions: HexString[];
24
+ unsafeBlockHeight?: number;
24
25
  }
25
26
  export declare class TxPool {
26
27
  #private;
@@ -131,6 +131,7 @@ class TxPool {
131
131
  const upwardMessages = (params === null || params === void 0 ? void 0 : params.upwardMessages) || Object.assign({}, __classPrivateFieldGet(this, _TxPool_ump, "f"));
132
132
  const downwardMessages = (params === null || params === void 0 ? void 0 : params.downwardMessages) || __classPrivateFieldGet(this, _TxPool_dmp, "f").splice(0);
133
133
  const horizontalMessages = (params === null || params === void 0 ? void 0 : params.horizontalMessages) || Object.assign({}, __classPrivateFieldGet(this, _TxPool_hrmp, "f"));
134
+ const unsafeBlockHeight = params === null || params === void 0 ? void 0 : params.unsafeBlockHeight;
134
135
  if (!(params === null || params === void 0 ? void 0 : params.upwardMessages)) {
135
136
  for (const id of Object.keys(__classPrivateFieldGet(this, _TxPool_ump, "f"))) {
136
137
  delete __classPrivateFieldGet(this, _TxPool_ump, "f")[id];
@@ -146,6 +147,7 @@ class TxPool {
146
147
  upwardMessages,
147
148
  downwardMessages,
148
149
  horizontalMessages,
150
+ unsafeBlockHeight,
149
151
  });
150
152
  });
151
153
  }
@@ -206,7 +208,7 @@ _TxPool_chain = new WeakMap(), _TxPool_pool = new WeakMap(), _TxPool_ump = new W
206
208
  const inherents = yield __classPrivateFieldGet(this, _TxPool_inherentProvider, "f").createInherents(head, params);
207
209
  const [newBlock, pendingExtrinsics] = yield (0, block_builder_1.buildBlock)(head, inherents, params.transactions, params.upwardMessages, (extrinsic, error) => {
208
210
  this.event.emit(exports.APPLY_EXTRINSIC_ERROR, [extrinsic, error]);
209
- });
211
+ }, params.unsafeBlockHeight);
210
212
  for (const extrinsic of pendingExtrinsics) {
211
213
  __classPrivateFieldGet(this, _TxPool_pool, "f").push({ extrinsic, signer: yield __classPrivateFieldGet(this, _TxPool_instances, "m", _TxPool_getSigner).call(this, extrinsic) });
212
214
  }
@@ -4,13 +4,13 @@ import { HexString } from '@polkadot/util/types';
4
4
  export declare const KeyValuePair: EntitySchema<{
5
5
  blockHash: string;
6
6
  key: string;
7
- value?: string | undefined;
7
+ value: string | null;
8
8
  }>;
9
9
  export declare const BlockEntity: EntitySchema<{
10
10
  hash: HexString;
11
11
  number: number;
12
12
  header: Header;
13
- parentHash?: `0x${string}` | undefined;
13
+ parentHash: HexString | null;
14
14
  extrinsics: HexString[];
15
- storageDiff?: Record<`0x${string}`, `0x${string}` | null> | undefined;
15
+ storageDiff: Record<HexString, HexString | null> | null;
16
16
  }>;
@@ -49,7 +49,6 @@ var _a, _GenesisProvider_isConnected, _GenesisProvider_eventemitter, _GenesisPro
49
49
  Object.defineProperty(exports, "__esModule", { value: true });
50
50
  exports.GenesisProvider = void 0;
51
51
  const eventemitter3_1 = require("eventemitter3");
52
- const util_1 = require("@polkadot/util");
53
52
  const axios_1 = __importDefault(require("axios"));
54
53
  const schema_1 = require("./schema");
55
54
  const executor_1 = require("./executor");
@@ -105,16 +104,6 @@ class GenesisProvider {
105
104
  return __classPrivateFieldGet(this, _GenesisProvider_genesis, "f").id;
106
105
  case 'system_name':
107
106
  return __classPrivateFieldGet(this, _GenesisProvider_genesis, "f").name;
108
- case 'state_getMetadata': {
109
- const code = __classPrivateFieldGet(this, _GenesisProvider_genesis, "f").genesis.raw.top[(0, util_1.stringToHex)(':code')];
110
- return (0, executor_1.runTask)({
111
- wasm: code,
112
- calls: [['Metadata_metadata', []]],
113
- mockSignatureHost: false,
114
- allowUnresolvedImports: true,
115
- runtimeLogLevel: 0,
116
- }, this._jsCallback);
117
- }
118
107
  case 'chain_getHeader':
119
108
  return this.getHeader();
120
109
  case 'chain_getBlock':
package/lib/setup.js CHANGED
@@ -35,13 +35,24 @@ const setup = (options) => __awaiter(void 0, void 0, void 0, function* () {
35
35
  yield api.isReady;
36
36
  let blockHash;
37
37
  if (options.block == null) {
38
- blockHash = yield api.getBlockHash();
38
+ blockHash = yield api.getBlockHash().then((hash) => {
39
+ if (!hash) {
40
+ // should not happen, but just in case
41
+ throw new Error('Cannot find block hash');
42
+ }
43
+ return hash;
44
+ });
39
45
  }
40
46
  else if (typeof options.block === 'string' && options.block.startsWith('0x')) {
41
47
  blockHash = options.block;
42
48
  }
43
49
  else if (Number.isInteger(+options.block)) {
44
- blockHash = yield api.getBlockHash(Number(options.block));
50
+ blockHash = yield api.getBlockHash(Number(options.block)).then((hash) => {
51
+ if (!hash) {
52
+ throw new Error(`Cannot find block hash for ${options.block}`);
53
+ }
54
+ return hash;
55
+ });
45
56
  }
46
57
  else {
47
58
  throw new Error(`Invalid block number or hash: ${options.block}`);
@@ -52,6 +63,9 @@ const setup = (options) => __awaiter(void 0, void 0, void 0, function* () {
52
63
  db = yield (0, db_1.openDb)(options.db);
53
64
  }
54
65
  const header = yield api.getHeader(blockHash);
66
+ if (!header) {
67
+ throw new Error(`Cannot find header for ${blockHash}`);
68
+ }
55
69
  const inherents = new inherent_1.InherentProviders(new inherent_1.SetTimestamp(), [
56
70
  new inherent_1.SetValidationData(),
57
71
  new inherent_1.ParaInherentEnter(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acala-network/chopsticks-core",
3
- "version": "0.8.0-4",
3
+ "version": "0.8.0-6",
4
4
  "author": "Bryan Chen <xlchen1291@gmail.com>",
5
5
  "license": "Apache-2.0",
6
6
  "scripts": {
@@ -9,9 +9,10 @@
9
9
  "build": "yarn pack-wasm; tsc -p ./tsconfig.json"
10
10
  },
11
11
  "dependencies": {
12
- "@acala-network/chopsticks-executor": "0.8.0-4",
12
+ "@acala-network/chopsticks-executor": "0.8.0-6",
13
13
  "@polkadot/api": "^10.9.1",
14
- "axios": "^1.4.0",
14
+ "@polkadot/util-crypto": "^12.3.2",
15
+ "axios": "^1.5.0",
15
16
  "eventemitter3": "^5.0.1",
16
17
  "localforage": "^1.10.0",
17
18
  "lodash": "^4.17.21",
@@ -20,7 +21,7 @@
20
21
  "sql.js": "^1.8.0",
21
22
  "sqlite3": "^5.1.6",
22
23
  "typeorm": "^0.3.17",
23
- "zod": "^3.21.4"
24
+ "zod": "^3.22.2"
24
25
  },
25
26
  "devDependencies": {
26
27
  "@types/lodash": "^4.14.197",