@acala-network/chopsticks-core 0.9.2-4 → 0.9.2-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/dist/cjs/blockchain/block.js +7 -18
- package/dist/cjs/blockchain/index.js +36 -9
- package/dist/cjs/utils/decoder.js +10 -3
- package/dist/cjs/utils/time-travel.js +1 -1
- package/dist/cjs/wasm-executor/index.js +2 -3
- package/dist/cjs/wasm-executor/node-wasm-executor.js +5 -0
- package/dist/esm/blockchain/block.js +3 -14
- package/dist/esm/blockchain/index.js +22 -3
- package/dist/esm/utils/decoder.js +10 -3
- package/dist/esm/utils/time-travel.js +1 -1
- package/dist/esm/wasm-executor/index.js +3 -4
- package/dist/esm/wasm-executor/node-wasm-executor.js +5 -0
- package/dist/types/blockchain/block.d.ts +1 -1
- package/dist/types/blockchain/index.d.ts +3 -0
- package/dist/types/schema/index.d.ts +2 -2
- package/dist/types/wasm-executor/index.d.ts +1 -2
- package/package.json +3 -2
|
@@ -10,8 +10,7 @@ Object.defineProperty(exports, "Block", {
|
|
|
10
10
|
});
|
|
11
11
|
const _types = require("@polkadot/types");
|
|
12
12
|
const _metadata = require("@polkadot/types/metadata");
|
|
13
|
-
const _util = require("@polkadot/
|
|
14
|
-
const _util1 = require("@polkadot/util");
|
|
13
|
+
const _util = require("@polkadot/util");
|
|
15
14
|
const _storagelayer = require("./storage-layer.js");
|
|
16
15
|
const _index = require("../utils/index.js");
|
|
17
16
|
const _index1 = require("../wasm-executor/index.js");
|
|
@@ -135,7 +134,7 @@ class Block {
|
|
|
135
134
|
return undefined;
|
|
136
135
|
}
|
|
137
136
|
const registry = await this.registry;
|
|
138
|
-
return registry.createType(type, (0,
|
|
137
|
+
return registry.createType(type, (0, _util.hexToU8a)(value));
|
|
139
138
|
}
|
|
140
139
|
/**
|
|
141
140
|
* Get paged storage keys.
|
|
@@ -173,7 +172,7 @@ class Block {
|
|
|
173
172
|
*/ get wasm() {
|
|
174
173
|
if (!_class_private_field_get(this, _wasm)) {
|
|
175
174
|
_class_private_field_set(this, _wasm, (async ()=>{
|
|
176
|
-
const wasmKey = (0,
|
|
175
|
+
const wasmKey = (0, _util.stringToHex)(':code');
|
|
177
176
|
const wasm = await this.get(wasmKey);
|
|
178
177
|
if (!wasm) {
|
|
179
178
|
throw new Error('No wasm found');
|
|
@@ -186,7 +185,7 @@ class Block {
|
|
|
186
185
|
/**
|
|
187
186
|
* Set the runtime wasm.
|
|
188
187
|
*/ setWasm(wasm) {
|
|
189
|
-
const wasmKey = (0,
|
|
188
|
+
const wasmKey = (0, _util.stringToHex)(':code');
|
|
190
189
|
this.pushStorageLayer().set(wasmKey, wasm);
|
|
191
190
|
_class_private_field_set(this, _wasm, Promise.resolve(wasm));
|
|
192
191
|
_class_private_field_set(this, _runtimeVersion, undefined);
|
|
@@ -201,18 +200,8 @@ class Block {
|
|
|
201
200
|
if (!_class_private_field_get(this, _registry)) {
|
|
202
201
|
_class_private_field_set(this, _registry, Promise.all([
|
|
203
202
|
this.metadata,
|
|
204
|
-
_class_private_field_get(this, _chain).api.chainProperties,
|
|
205
|
-
_class_private_field_get(this, _chain).api.chain,
|
|
206
203
|
this.runtimeVersion
|
|
207
|
-
]).then(([data,
|
|
208
|
-
const registry = new _types.TypeRegistry(this.hash);
|
|
209
|
-
registry.setKnownTypes(this.chain.registeredTypes);
|
|
210
|
-
registry.setChainProperties(registry.createType('ChainProperties', properties));
|
|
211
|
-
registry.register((0, _util.getSpecTypes)(registry, chain, version.specName, version.specVersion));
|
|
212
|
-
registry.setHasher((0, _util.getSpecHasher)(registry, chain, version.specName));
|
|
213
|
-
registry.setMetadata(new _types.Metadata(registry, data), undefined, (0, _util1.objectSpread)({}, (0, _util.getSpecExtensions)(registry, chain, version.specName), _class_private_field_get(this, _chain).api.signedExtensions), true);
|
|
214
|
-
return registry;
|
|
215
|
-
}));
|
|
204
|
+
]).then(([data, version])=>_class_private_field_get(this, _chain).buildRegistry(data, version)));
|
|
216
205
|
}
|
|
217
206
|
return _class_private_field_get(this, _registry);
|
|
218
207
|
}
|
|
@@ -224,7 +213,7 @@ class Block {
|
|
|
224
213
|
}
|
|
225
214
|
get metadata() {
|
|
226
215
|
if (!_class_private_field_get(this, _metadata1)) {
|
|
227
|
-
_class_private_field_set(this, _metadata1, this.call('Metadata_metadata', []).then((resp)=>(0, _index.compactHex)((0,
|
|
216
|
+
_class_private_field_set(this, _metadata1, this.call('Metadata_metadata', []).then((resp)=>(0, _index.compactHex)((0, _util.hexToU8a)(resp.result))));
|
|
228
217
|
}
|
|
229
218
|
return _class_private_field_get(this, _metadata1);
|
|
230
219
|
}
|
|
@@ -331,7 +320,7 @@ class Block {
|
|
|
331
320
|
if (storageDiff) {
|
|
332
321
|
// if code doesn't change then keep parent block's meta
|
|
333
322
|
// otherwise reset meta
|
|
334
|
-
if (storageDiff[(0,
|
|
323
|
+
if (storageDiff[(0, _util.stringToHex)(':code')]) {
|
|
335
324
|
_class_private_field_set(this, _runtimeVersion, undefined);
|
|
336
325
|
_class_private_field_set(this, _metadata1, undefined);
|
|
337
326
|
_class_private_field_set(this, _registry, undefined);
|
|
@@ -8,16 +8,19 @@ Object.defineProperty(exports, "Blockchain", {
|
|
|
8
8
|
return Blockchain;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
+
const _types = require("@polkadot/types");
|
|
11
12
|
const _utilcrypto = require("@polkadot/util-crypto");
|
|
12
|
-
const _util = require("@polkadot/util");
|
|
13
|
+
const _util = require("@polkadot/types-known/util");
|
|
14
|
+
const _util1 = require("@polkadot/util");
|
|
15
|
+
const _lodash = /*#__PURE__*/ _interop_require_default(require("lodash"));
|
|
13
16
|
const _block = require("./block.js");
|
|
14
17
|
const _txpool = require("./txpool.js");
|
|
15
18
|
const _headstate = require("./head-state.js");
|
|
16
19
|
const _offchain = require("../offchain.js");
|
|
17
|
-
const _index = require("../
|
|
20
|
+
const _index = require("../wasm-executor/index.js");
|
|
21
|
+
const _index1 = require("../utils/index.js");
|
|
18
22
|
const _logger = require("../logger.js");
|
|
19
23
|
const _blockbuilder = require("./block-builder.js");
|
|
20
|
-
const _index1 = require("../wasm-executor/index.js");
|
|
21
24
|
function _check_private_redeclaration(obj, privateCollection) {
|
|
22
25
|
if (privateCollection.has(obj)) {
|
|
23
26
|
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
@@ -81,10 +84,15 @@ function _define_property(obj, key, value) {
|
|
|
81
84
|
}
|
|
82
85
|
return obj;
|
|
83
86
|
}
|
|
87
|
+
function _interop_require_default(obj) {
|
|
88
|
+
return obj && obj.__esModule ? obj : {
|
|
89
|
+
default: obj
|
|
90
|
+
};
|
|
91
|
+
}
|
|
84
92
|
const logger = _logger.defaultLogger.child({
|
|
85
93
|
name: 'blockchain'
|
|
86
94
|
});
|
|
87
|
-
var _runtimeLogLevel = /*#__PURE__*/ new WeakMap(), _txpool1 = /*#__PURE__*/ new WeakMap(), _inherentProvider = /*#__PURE__*/ new WeakMap(), _head = /*#__PURE__*/ new WeakMap(), _blocksByNumber = /*#__PURE__*/ new WeakMap(), _blocksByHash = /*#__PURE__*/ new WeakMap(), _loadingBlocks = /*#__PURE__*/ new WeakMap(), _maxMemoryBlockCount = /*#__PURE__*/ new WeakMap(), _registerBlock = /*#__PURE__*/ new WeakSet();
|
|
95
|
+
var _runtimeLogLevel = /*#__PURE__*/ new WeakMap(), _txpool1 = /*#__PURE__*/ new WeakMap(), _inherentProvider = /*#__PURE__*/ new WeakMap(), _head = /*#__PURE__*/ new WeakMap(), _blocksByNumber = /*#__PURE__*/ new WeakMap(), _blocksByHash = /*#__PURE__*/ new WeakMap(), _loadingBlocks = /*#__PURE__*/ new WeakMap(), _maxMemoryBlockCount = /*#__PURE__*/ new WeakMap(), _registryBuilder = /*#__PURE__*/ new WeakMap(), _registerBlock = /*#__PURE__*/ new WeakSet();
|
|
88
96
|
class Blockchain {
|
|
89
97
|
get head() {
|
|
90
98
|
return _class_private_field_get(this, _head);
|
|
@@ -99,6 +107,10 @@ class Blockchain {
|
|
|
99
107
|
_class_private_field_set(this, _runtimeLogLevel, level);
|
|
100
108
|
logger.debug(`Runtime log level set to ${logger.level}`);
|
|
101
109
|
}
|
|
110
|
+
async buildRegistry(metadata, version) {
|
|
111
|
+
const cacheKey = `${(0, _utilcrypto.xxhashAsHex)(metadata, 256)}-${version.specVersion}`;
|
|
112
|
+
return _class_private_field_get(this, _registryBuilder).call(this, cacheKey, metadata, version);
|
|
113
|
+
}
|
|
102
114
|
async saveBlockToDB(block) {
|
|
103
115
|
if (this.db) {
|
|
104
116
|
const { hash, number, header, extrinsics } = block;
|
|
@@ -248,7 +260,7 @@ class Blockchain {
|
|
|
248
260
|
/**
|
|
249
261
|
* Validate extrinsic by calling `TaggedTransactionQueue_validate_transaction`.
|
|
250
262
|
*/ async validateExtrinsic(extrinsic, source = '0x02' /** External */ ) {
|
|
251
|
-
const args = (0,
|
|
263
|
+
const args = (0, _util1.u8aToHex)((0, _util1.u8aConcat)(source, extrinsic, this.head.hash));
|
|
252
264
|
const res = await this.head.call('TaggedTransactionQueue_validate_transaction', [
|
|
253
265
|
args
|
|
254
266
|
]);
|
|
@@ -361,7 +373,7 @@ class Blockchain {
|
|
|
361
373
|
const needsDispatch = meta.registry.createType('Vec<u32>', Object.keys(ump));
|
|
362
374
|
const storageValues = [
|
|
363
375
|
[
|
|
364
|
-
(0,
|
|
376
|
+
(0, _index1.compactHex)(meta.query.ump.needsDispatch()),
|
|
365
377
|
needsDispatch.toHex()
|
|
366
378
|
]
|
|
367
379
|
];
|
|
@@ -373,11 +385,11 @@ class Blockchain {
|
|
|
373
385
|
upwardMessages.map((x)=>x.byteLength).reduce((s, i)=>s + i, 0)
|
|
374
386
|
]);
|
|
375
387
|
storageValues.push([
|
|
376
|
-
(0,
|
|
388
|
+
(0, _index1.compactHex)(meta.query.ump.relayDispatchQueues(paraId)),
|
|
377
389
|
upwardMessages.toHex()
|
|
378
390
|
]);
|
|
379
391
|
storageValues.push([
|
|
380
|
-
(0,
|
|
392
|
+
(0, _index1.compactHex)(meta.query.ump.relayDispatchQueueSize(paraId)),
|
|
381
393
|
queueSize.toHex()
|
|
382
394
|
]);
|
|
383
395
|
}
|
|
@@ -405,7 +417,7 @@ class Blockchain {
|
|
|
405
417
|
/**
|
|
406
418
|
* Close the db and release worker.
|
|
407
419
|
*/ async close() {
|
|
408
|
-
await (0,
|
|
420
|
+
await (0, _index.releaseWorker)();
|
|
409
421
|
await this.api.disconnect();
|
|
410
422
|
await this.db?.close();
|
|
411
423
|
}
|
|
@@ -453,6 +465,21 @@ class Blockchain {
|
|
|
453
465
|
writable: true,
|
|
454
466
|
value: void 0
|
|
455
467
|
});
|
|
468
|
+
// first arg is used as cache key
|
|
469
|
+
_class_private_field_init(this, _registryBuilder, {
|
|
470
|
+
writable: true,
|
|
471
|
+
value: _lodash.default.memoize(async (_cacheKey, metadata, version)=>{
|
|
472
|
+
const chain = await this.api.chain;
|
|
473
|
+
const properties = await this.api.chainProperties;
|
|
474
|
+
const registry = new _types.TypeRegistry();
|
|
475
|
+
registry.setKnownTypes(this.registeredTypes);
|
|
476
|
+
registry.setChainProperties(registry.createType('ChainProperties', properties));
|
|
477
|
+
registry.register((0, _util.getSpecTypes)(registry, chain, version.specName, version.specVersion));
|
|
478
|
+
registry.setHasher((0, _util.getSpecHasher)(registry, chain, version.specName));
|
|
479
|
+
registry.setMetadata(new _types.Metadata(registry, metadata), undefined, (0, _util1.objectSpread)({}, (0, _util.getSpecExtensions)(registry, chain, version.specName), this.api.signedExtensions), true);
|
|
480
|
+
return registry;
|
|
481
|
+
})
|
|
482
|
+
});
|
|
456
483
|
this.api = api;
|
|
457
484
|
this.db = db;
|
|
458
485
|
this.mockSignatureHost = mockSignatureHost;
|
|
@@ -23,6 +23,7 @@ _export(exports, {
|
|
|
23
23
|
}
|
|
24
24
|
});
|
|
25
25
|
require("@polkadot/types-codec");
|
|
26
|
+
const _lrucache = require("lru-cache");
|
|
26
27
|
const _util = require("@polkadot/util");
|
|
27
28
|
const _lodash = /*#__PURE__*/ _interop_require_default(require("lodash"));
|
|
28
29
|
const _wellknownkeys = require("./well-known-keys.js");
|
|
@@ -32,16 +33,22 @@ function _interop_require_default(obj) {
|
|
|
32
33
|
};
|
|
33
34
|
}
|
|
34
35
|
const _CACHE = {};
|
|
36
|
+
function createCache() {
|
|
37
|
+
return new _lrucache.LRUCache({
|
|
38
|
+
max: 50
|
|
39
|
+
});
|
|
40
|
+
}
|
|
35
41
|
const getCache = (uid)=>{
|
|
36
42
|
if (!_CACHE[uid]) {
|
|
37
|
-
_CACHE[uid] =
|
|
43
|
+
_CACHE[uid] = createCache();
|
|
38
44
|
}
|
|
39
45
|
return _CACHE[uid];
|
|
40
46
|
};
|
|
41
47
|
const getStorageEntry = (meta, block, key)=>{
|
|
42
48
|
const cache = getCache(block.chain.uid);
|
|
43
|
-
for (const
|
|
44
|
-
if (key.startsWith(prefix))
|
|
49
|
+
for (const prefix of cache.keys()){
|
|
50
|
+
if (key.startsWith(prefix)) // update the recency of the cache entry
|
|
51
|
+
return cache.get(prefix);
|
|
45
52
|
}
|
|
46
53
|
for (const module of Object.values(meta.query)){
|
|
47
54
|
for (const storage of Object.values(module)){
|
|
@@ -42,7 +42,7 @@ const getCurrentTimestamp = async (chain)=>{
|
|
|
42
42
|
};
|
|
43
43
|
const getSlotDuration = async (chain)=>{
|
|
44
44
|
const meta = await chain.head.meta;
|
|
45
|
-
return meta.consts.babe ? meta.consts.babe.expectedBlockTime.toNumber() : meta.query.aura ? (0, _index1.getAuraSlotDuration)(await chain.head.wasm
|
|
45
|
+
return meta.consts.babe ? meta.consts.babe.expectedBlockTime.toNumber() : meta.query.aura ? (0, _index1.getAuraSlotDuration)(await chain.head.wasm) : 12_000;
|
|
46
46
|
};
|
|
47
47
|
const timeTravel = async (chain, timestamp)=>{
|
|
48
48
|
const meta = await chain.head.meta;
|
|
@@ -213,7 +213,7 @@ const emptyTaskHandler = {
|
|
|
213
213
|
throw new Error('Method not implemented');
|
|
214
214
|
}
|
|
215
215
|
};
|
|
216
|
-
const getAuraSlotDuration = _lodash.default.memoize(async (wasm
|
|
216
|
+
const getAuraSlotDuration = _lodash.default.memoize(async (wasm)=>{
|
|
217
217
|
const result = await runTask({
|
|
218
218
|
wasm,
|
|
219
219
|
calls: [
|
|
@@ -227,8 +227,7 @@ const getAuraSlotDuration = _lodash.default.memoize(async (wasm, registry)=>{
|
|
|
227
227
|
runtimeLogLevel: 0
|
|
228
228
|
});
|
|
229
229
|
if ('Error' in result) throw new Error(result.Error);
|
|
230
|
-
|
|
231
|
-
return slotDuration;
|
|
230
|
+
return (0, _util.u8aToBn)((0, _util.hexToU8a)(result.Call.result).subarray(0, 8 /* u64: 8 bytes */ )).toNumber();
|
|
232
231
|
});
|
|
233
232
|
const releaseWorker = async ()=>{
|
|
234
233
|
if (!__executor_worker) return;
|
|
@@ -53,19 +53,24 @@ function _interop_require_wildcard(obj, nodeInterop) {
|
|
|
53
53
|
return newObj;
|
|
54
54
|
}
|
|
55
55
|
const getRuntimeVersion = async (code)=>{
|
|
56
|
+
await _chopsticksexecutor.wasmReady;
|
|
56
57
|
return _chopsticksexecutor.get_runtime_version(code);
|
|
57
58
|
};
|
|
58
59
|
// trie_version: 0 for old trie, 1 for new trie
|
|
59
60
|
const calculateStateRoot = async (entries, trie_version)=>{
|
|
61
|
+
await _chopsticksexecutor.wasmReady;
|
|
60
62
|
return _chopsticksexecutor.calculate_state_root(entries, trie_version);
|
|
61
63
|
};
|
|
62
64
|
const decodeProof = async (trieRootHash, keys, nodes)=>{
|
|
65
|
+
await _chopsticksexecutor.wasmReady;
|
|
63
66
|
return _chopsticksexecutor.decode_proof(trieRootHash, keys, nodes);
|
|
64
67
|
};
|
|
65
68
|
const createProof = async (nodes, entries)=>{
|
|
69
|
+
await _chopsticksexecutor.wasmReady;
|
|
66
70
|
return _chopsticksexecutor.create_proof(nodes, entries);
|
|
67
71
|
};
|
|
68
72
|
const runTask = async (task, callback)=>{
|
|
73
|
+
await _chopsticksexecutor.wasmReady;
|
|
69
74
|
return _chopsticksexecutor.run_task(task, callback, process.env.RUST_LOG);
|
|
70
75
|
};
|
|
71
76
|
const wasmExecutor = {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { Metadata
|
|
1
|
+
import { Metadata } from '@polkadot/types';
|
|
2
2
|
import { expandMetadata } from '@polkadot/types/metadata';
|
|
3
|
-
import {
|
|
4
|
-
import { hexToU8a, objectSpread, stringToHex } from '@polkadot/util';
|
|
3
|
+
import { hexToU8a, stringToHex } from '@polkadot/util';
|
|
5
4
|
import { RemoteStorageLayer, StorageLayer, StorageValueKind } from './storage-layer.js';
|
|
6
5
|
import { compactHex } from '../utils/index.js';
|
|
7
6
|
import { getRuntimeVersion, runTask, taskHandler } from '../wasm-executor/index.js';
|
|
@@ -191,18 +190,8 @@ import { getRuntimeVersion, runTask, taskHandler } from '../wasm-executor/index.
|
|
|
191
190
|
if (!this.#registry) {
|
|
192
191
|
this.#registry = Promise.all([
|
|
193
192
|
this.metadata,
|
|
194
|
-
this.#chain.api.chainProperties,
|
|
195
|
-
this.#chain.api.chain,
|
|
196
193
|
this.runtimeVersion
|
|
197
|
-
]).then(([data,
|
|
198
|
-
const registry = new TypeRegistry(this.hash);
|
|
199
|
-
registry.setKnownTypes(this.chain.registeredTypes);
|
|
200
|
-
registry.setChainProperties(registry.createType('ChainProperties', properties));
|
|
201
|
-
registry.register(getSpecTypes(registry, chain, version.specName, version.specVersion));
|
|
202
|
-
registry.setHasher(getSpecHasher(registry, chain, version.specName));
|
|
203
|
-
registry.setMetadata(new Metadata(registry, data), undefined, objectSpread({}, getSpecExtensions(registry, chain, version.specName), this.#chain.api.signedExtensions), true);
|
|
204
|
-
return registry;
|
|
205
|
-
});
|
|
194
|
+
]).then(([data, version])=>this.#chain.buildRegistry(data, version));
|
|
206
195
|
}
|
|
207
196
|
return this.#registry;
|
|
208
197
|
}
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { Metadata, TypeRegistry } from '@polkadot/types';
|
|
2
|
+
import { blake2AsHex, xxhashAsHex } from '@polkadot/util-crypto';
|
|
3
|
+
import { getSpecExtensions, getSpecHasher, getSpecTypes } from '@polkadot/types-known/util';
|
|
4
|
+
import { objectSpread, u8aConcat, u8aToHex } from '@polkadot/util';
|
|
5
|
+
import _ from 'lodash';
|
|
3
6
|
import { Block } from './block.js';
|
|
4
7
|
import { TxPool } from './txpool.js';
|
|
5
8
|
import { HeadState } from './head-state.js';
|
|
6
9
|
import { OffchainWorker } from '../offchain.js';
|
|
10
|
+
import { releaseWorker } from '../wasm-executor/index.js';
|
|
7
11
|
import { compactHex } from '../utils/index.js';
|
|
8
12
|
import { defaultLogger } from '../logger.js';
|
|
9
13
|
import { dryRunExtrinsic, dryRunInherents } from './block-builder.js';
|
|
10
|
-
import { releaseWorker } from '../wasm-executor/index.js';
|
|
11
14
|
const logger = defaultLogger.child({
|
|
12
15
|
name: 'blockchain'
|
|
13
16
|
});
|
|
@@ -50,6 +53,18 @@ const logger = defaultLogger.child({
|
|
|
50
53
|
/** For subscribing and managing the head state. */ headState;
|
|
51
54
|
offchainWorker;
|
|
52
55
|
#maxMemoryBlockCount;
|
|
56
|
+
// first arg is used as cache key
|
|
57
|
+
#registryBuilder = _.memoize(async (_cacheKey, metadata, version)=>{
|
|
58
|
+
const chain = await this.api.chain;
|
|
59
|
+
const properties = await this.api.chainProperties;
|
|
60
|
+
const registry = new TypeRegistry();
|
|
61
|
+
registry.setKnownTypes(this.registeredTypes);
|
|
62
|
+
registry.setChainProperties(registry.createType('ChainProperties', properties));
|
|
63
|
+
registry.register(getSpecTypes(registry, chain, version.specName, version.specVersion));
|
|
64
|
+
registry.setHasher(getSpecHasher(registry, chain, version.specName));
|
|
65
|
+
registry.setMetadata(new Metadata(registry, metadata), undefined, objectSpread({}, getSpecExtensions(registry, chain, version.specName), this.api.signedExtensions), true);
|
|
66
|
+
return registry;
|
|
67
|
+
});
|
|
53
68
|
/**
|
|
54
69
|
* @param options - Options for instantiating the blockchain
|
|
55
70
|
*/ constructor({ api, buildBlockMode, inherentProvider, db, header, mockSignatureHost = false, allowUnresolvedImports = false, runtimeLogLevel = 0, registeredTypes = {}, offchainWorker = false, maxMemoryBlockCount = 500 }){
|
|
@@ -92,6 +107,10 @@ const logger = defaultLogger.child({
|
|
|
92
107
|
this.#runtimeLogLevel = level;
|
|
93
108
|
logger.debug(`Runtime log level set to ${logger.level}`);
|
|
94
109
|
}
|
|
110
|
+
async buildRegistry(metadata, version) {
|
|
111
|
+
const cacheKey = `${xxhashAsHex(metadata, 256)}-${version.specVersion}`;
|
|
112
|
+
return this.#registryBuilder(cacheKey, metadata, version);
|
|
113
|
+
}
|
|
95
114
|
async saveBlockToDB(block) {
|
|
96
115
|
if (this.db) {
|
|
97
116
|
const { hash, number, header, extrinsics } = block;
|
|
@@ -1,18 +1,25 @@
|
|
|
1
1
|
import '@polkadot/types-codec';
|
|
2
|
+
import { LRUCache } from 'lru-cache';
|
|
2
3
|
import { hexToU8a, u8aToHex } from '@polkadot/util';
|
|
3
4
|
import _ from 'lodash';
|
|
4
5
|
import { decodeWellKnownKey } from './well-known-keys.js';
|
|
5
6
|
const _CACHE = {};
|
|
7
|
+
function createCache() {
|
|
8
|
+
return new LRUCache({
|
|
9
|
+
max: 50
|
|
10
|
+
});
|
|
11
|
+
}
|
|
6
12
|
const getCache = (uid)=>{
|
|
7
13
|
if (!_CACHE[uid]) {
|
|
8
|
-
_CACHE[uid] =
|
|
14
|
+
_CACHE[uid] = createCache();
|
|
9
15
|
}
|
|
10
16
|
return _CACHE[uid];
|
|
11
17
|
};
|
|
12
18
|
const getStorageEntry = (meta, block, key)=>{
|
|
13
19
|
const cache = getCache(block.chain.uid);
|
|
14
|
-
for (const
|
|
15
|
-
if (key.startsWith(prefix))
|
|
20
|
+
for (const prefix of cache.keys()){
|
|
21
|
+
if (key.startsWith(prefix)) // update the recency of the cache entry
|
|
22
|
+
return cache.get(prefix);
|
|
16
23
|
}
|
|
17
24
|
for (const module of Object.values(meta.query)){
|
|
18
25
|
for (const storage of Object.values(module)){
|
|
@@ -18,7 +18,7 @@ export const getCurrentTimestamp = async (chain)=>{
|
|
|
18
18
|
};
|
|
19
19
|
export const getSlotDuration = async (chain)=>{
|
|
20
20
|
const meta = await chain.head.meta;
|
|
21
|
-
return meta.consts.babe ? meta.consts.babe.expectedBlockTime.toNumber() : meta.query.aura ? getAuraSlotDuration(await chain.head.wasm
|
|
21
|
+
return meta.consts.babe ? meta.consts.babe.expectedBlockTime.toNumber() : meta.query.aura ? getAuraSlotDuration(await chain.head.wasm) : 12_000;
|
|
22
22
|
};
|
|
23
23
|
export const timeTravel = async (chain, timestamp)=>{
|
|
24
24
|
const meta = await chain.head.meta;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as Comlink from 'comlink';
|
|
2
|
-
import { hexToString, hexToU8a } from '@polkadot/util';
|
|
2
|
+
import { hexToString, hexToU8a, u8aToBn } from '@polkadot/util';
|
|
3
3
|
import { randomAsHex } from '@polkadot/util-crypto';
|
|
4
4
|
import _ from 'lodash';
|
|
5
5
|
import { PREFIX_LENGTH } from '../utils/key-cache.js';
|
|
@@ -129,7 +129,7 @@ export const emptyTaskHandler = {
|
|
|
129
129
|
throw new Error('Method not implemented');
|
|
130
130
|
}
|
|
131
131
|
};
|
|
132
|
-
export const getAuraSlotDuration = _.memoize(async (wasm
|
|
132
|
+
export const getAuraSlotDuration = _.memoize(async (wasm)=>{
|
|
133
133
|
const result = await runTask({
|
|
134
134
|
wasm,
|
|
135
135
|
calls: [
|
|
@@ -143,8 +143,7 @@ export const getAuraSlotDuration = _.memoize(async (wasm, registry)=>{
|
|
|
143
143
|
runtimeLogLevel: 0
|
|
144
144
|
});
|
|
145
145
|
if ('Error' in result) throw new Error(result.Error);
|
|
146
|
-
|
|
147
|
-
return slotDuration;
|
|
146
|
+
return u8aToBn(hexToU8a(result.Call.result).subarray(0, 8 /* u64: 8 bytes */ )).toNumber();
|
|
148
147
|
});
|
|
149
148
|
export const releaseWorker = async ()=>{
|
|
150
149
|
if (!__executor_worker) return;
|
|
@@ -3,19 +3,24 @@ import * as pkg from '@acala-network/chopsticks-executor';
|
|
|
3
3
|
import { parentPort } from 'node:worker_threads';
|
|
4
4
|
import nodeEndpoint from 'comlink/dist/umd/node-adapter.js';
|
|
5
5
|
const getRuntimeVersion = async (code)=>{
|
|
6
|
+
await pkg.wasmReady;
|
|
6
7
|
return pkg.get_runtime_version(code);
|
|
7
8
|
};
|
|
8
9
|
// trie_version: 0 for old trie, 1 for new trie
|
|
9
10
|
const calculateStateRoot = async (entries, trie_version)=>{
|
|
11
|
+
await pkg.wasmReady;
|
|
10
12
|
return pkg.calculate_state_root(entries, trie_version);
|
|
11
13
|
};
|
|
12
14
|
const decodeProof = async (trieRootHash, keys, nodes)=>{
|
|
15
|
+
await pkg.wasmReady;
|
|
13
16
|
return pkg.decode_proof(trieRootHash, keys, nodes);
|
|
14
17
|
};
|
|
15
18
|
const createProof = async (nodes, entries)=>{
|
|
19
|
+
await pkg.wasmReady;
|
|
16
20
|
return pkg.create_proof(nodes, entries);
|
|
17
21
|
};
|
|
18
22
|
const runTask = async (task, callback)=>{
|
|
23
|
+
await pkg.wasmReady;
|
|
19
24
|
return pkg.run_task(task, callback, process.env.RUST_LOG);
|
|
20
25
|
};
|
|
21
26
|
const wasmExecutor = {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Header } from '@polkadot/types/interfaces';
|
|
2
1
|
import { DecoratedMeta } from '@polkadot/types/metadata/decorate/types';
|
|
2
|
+
import { Header } from '@polkadot/types/interfaces';
|
|
3
3
|
import { TypeRegistry } from '@polkadot/types';
|
|
4
4
|
import { StorageEntry } from '@polkadot/types/primitive/types';
|
|
5
5
|
import type { HexString } from '@polkadot/util/types';
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ApplyExtrinsicResult } from '@polkadot/types/interfaces';
|
|
2
2
|
import { HexString } from '@polkadot/util/types';
|
|
3
|
+
import { TypeRegistry } from '@polkadot/types';
|
|
3
4
|
import { RegisteredTypes } from '@polkadot/types/types';
|
|
4
5
|
import type { TransactionValidity } from '@polkadot/types/interfaces/txqueue';
|
|
5
6
|
import { Api } from '../api.js';
|
|
@@ -9,6 +10,7 @@ import { Database } from '../database.js';
|
|
|
9
10
|
import { HeadState } from './head-state.js';
|
|
10
11
|
import { InherentProvider } from './inherent/index.js';
|
|
11
12
|
import { OffchainWorker } from '../offchain.js';
|
|
13
|
+
import { RuntimeVersion } from '../wasm-executor/index.js';
|
|
12
14
|
export interface Options {
|
|
13
15
|
/** API instance, for getting on-chain data. */
|
|
14
16
|
api: Api;
|
|
@@ -83,6 +85,7 @@ export declare class Blockchain {
|
|
|
83
85
|
get txPool(): TxPool;
|
|
84
86
|
get runtimeLogLevel(): number;
|
|
85
87
|
set runtimeLogLevel(level: number);
|
|
88
|
+
buildRegistry(metadata: HexString, version: RuntimeVersion): Promise<TypeRegistry>;
|
|
86
89
|
saveBlockToDB(block: Block): Promise<void>;
|
|
87
90
|
/**
|
|
88
91
|
* Try to load block from db and register it.
|
|
@@ -34,12 +34,12 @@ export declare const genesisSchema: z.ZodObject<{
|
|
|
34
34
|
}>;
|
|
35
35
|
}, "strip", z.ZodTypeAny, {
|
|
36
36
|
name: string;
|
|
37
|
+
id: string;
|
|
37
38
|
properties: {
|
|
38
39
|
ss58Format?: number | undefined;
|
|
39
40
|
tokenDecimals?: number | number[] | undefined;
|
|
40
41
|
tokenSymbol?: string | string[] | undefined;
|
|
41
42
|
};
|
|
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;
|
|
50
51
|
properties: {
|
|
51
52
|
ss58Format?: number | undefined;
|
|
52
53
|
tokenDecimals?: number | number[] | undefined;
|
|
53
54
|
tokenSymbol?: string | string[] | undefined;
|
|
54
55
|
};
|
|
55
|
-
id: string;
|
|
56
56
|
genesis: {
|
|
57
57
|
raw: {
|
|
58
58
|
top: Record<string, string>;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { HexString } from '@polkadot/util/types';
|
|
2
|
-
import { Registry } from '@polkadot/types-codec/types';
|
|
3
2
|
import _ from 'lodash';
|
|
4
3
|
import { Block } from '../blockchain/block.js';
|
|
5
4
|
import type { JsCallback } from '@acala-network/chopsticks-executor';
|
|
@@ -71,5 +70,5 @@ export declare const emptyTaskHandler: {
|
|
|
71
70
|
offchainRandomSeed: () => Promise<never>;
|
|
72
71
|
offchainSubmitTransaction: (_tx: HexString) => Promise<never>;
|
|
73
72
|
};
|
|
74
|
-
export declare const getAuraSlotDuration: ((wasm: HexString
|
|
73
|
+
export declare const getAuraSlotDuration: ((wasm: HexString) => Promise<number>) & _.MemoizedFunction;
|
|
75
74
|
export declare const releaseWorker: () => Promise<void>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acala-network/chopsticks-core",
|
|
3
|
-
"version": "0.9.2-
|
|
3
|
+
"version": "0.9.2-6",
|
|
4
4
|
"author": "Acala Developers <hello@acala.network>",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -13,12 +13,13 @@
|
|
|
13
13
|
"docs:prep": "typedoc"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@acala-network/chopsticks-executor": "0.9.2-
|
|
16
|
+
"@acala-network/chopsticks-executor": "0.9.2-6",
|
|
17
17
|
"@polkadot/api": "^10.10.1",
|
|
18
18
|
"@polkadot/util-crypto": "^12.5.1",
|
|
19
19
|
"comlink": "^4.4.1",
|
|
20
20
|
"eventemitter3": "^5.0.1",
|
|
21
21
|
"lodash": "^4.17.21",
|
|
22
|
+
"lru-cache": "^10.0.1",
|
|
22
23
|
"pino": "^8.16.1",
|
|
23
24
|
"pino-pretty": "^10.2.3",
|
|
24
25
|
"zod": "^3.22.4"
|