@acala-network/chopsticks-core 0.8.5-5 → 0.9.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.
- package/{lib → dist/cjs}/api.js +7 -7
- package/{lib → dist/cjs}/blockchain/block.js +1 -4
- package/{lib → dist/cjs}/genesis-provider.js +1 -1
- package/{lib → dist/cjs}/rpc/substrate/chain.js +1 -1
- package/{lib → dist/cjs}/utils/index.js +11 -1
- package/dist/esm/api.js +125 -0
- package/dist/esm/blockchain/block-builder.js +293 -0
- package/dist/esm/blockchain/block.js +277 -0
- package/dist/esm/blockchain/head-state.js +79 -0
- package/dist/esm/blockchain/index.js +413 -0
- package/dist/esm/blockchain/inherent/index.js +40 -0
- package/dist/esm/blockchain/inherent/para-enter.js +29 -0
- package/dist/esm/blockchain/inherent/parachain/babe-randomness.js +11 -0
- package/dist/esm/blockchain/inherent/parachain/nimbus-author-inherent.js +11 -0
- package/dist/esm/blockchain/inherent/parachain/validation-data.js +165 -0
- package/dist/esm/blockchain/storage-layer.js +215 -0
- package/dist/esm/blockchain/txpool.js +208 -0
- package/dist/esm/chopsticks-provider.js +156 -0
- package/dist/esm/database.js +1 -0
- package/dist/esm/genesis-provider.js +144 -0
- package/dist/esm/logger.js +34 -0
- package/dist/esm/offchain.js +36 -0
- package/dist/esm/rpc/index.js +10 -0
- package/dist/esm/rpc/shared.js +15 -0
- package/dist/esm/rpc/substrate/author.js +85 -0
- package/dist/esm/rpc/substrate/chain.js +84 -0
- package/dist/esm/rpc/substrate/index.js +18 -0
- package/dist/esm/rpc/substrate/payment.js +40 -0
- package/dist/esm/rpc/substrate/state.js +166 -0
- package/dist/esm/rpc/substrate/system.js +48 -0
- package/dist/esm/schema/index.js +11 -0
- package/dist/esm/setup.js +71 -0
- package/dist/esm/utils/decoder.js +95 -0
- package/dist/esm/utils/index.js +91 -0
- package/dist/esm/utils/key-cache.js +61 -0
- package/dist/esm/utils/proof.js +33 -0
- package/dist/esm/utils/set-storage.js +56 -0
- package/dist/esm/utils/time-travel.js +58 -0
- package/dist/esm/wasm-executor/browser-wasm-executor.mjs +32 -0
- package/dist/esm/wasm-executor/browser-worker.js +15 -0
- package/dist/esm/wasm-executor/index.js +146 -0
- package/dist/esm/wasm-executor/node-wasm-executor.mjs +29 -0
- package/dist/esm/wasm-executor/node-worker.js +15 -0
- package/dist/esm/xcm/downward.js +25 -0
- package/dist/esm/xcm/horizontal.js +25 -0
- package/dist/esm/xcm/index.js +20 -0
- package/dist/esm/xcm/upward.js +17 -0
- package/{lib → dist/types}/genesis-provider.d.ts +2 -2
- package/dist/types/index.d.ts +28 -0
- package/{lib → dist/types}/utils/index.d.ts +1 -0
- package/package.json +23 -14
- /package/{lib → dist/cjs}/blockchain/block-builder.js +0 -0
- /package/{lib → dist/cjs}/blockchain/head-state.js +0 -0
- /package/{lib → dist/cjs}/blockchain/index.js +0 -0
- /package/{lib → dist/cjs}/blockchain/inherent/index.js +0 -0
- /package/{lib → dist/cjs}/blockchain/inherent/para-enter.js +0 -0
- /package/{lib → dist/cjs}/blockchain/inherent/parachain/babe-randomness.js +0 -0
- /package/{lib → dist/cjs}/blockchain/inherent/parachain/nimbus-author-inherent.js +0 -0
- /package/{lib → dist/cjs}/blockchain/inherent/parachain/validation-data.js +0 -0
- /package/{lib → dist/cjs}/blockchain/storage-layer.js +0 -0
- /package/{lib → dist/cjs}/blockchain/txpool.js +0 -0
- /package/{lib → dist/cjs}/chopsticks-provider.js +0 -0
- /package/{lib → dist/cjs}/database.js +0 -0
- /package/{lib → dist/cjs}/index.js +0 -0
- /package/{lib → dist/cjs}/logger.js +0 -0
- /package/{lib → dist/cjs}/offchain.js +0 -0
- /package/{lib → dist/cjs}/rpc/index.js +0 -0
- /package/{lib → dist/cjs}/rpc/shared.js +0 -0
- /package/{lib → dist/cjs}/rpc/substrate/author.js +0 -0
- /package/{lib → dist/cjs}/rpc/substrate/index.js +0 -0
- /package/{lib → dist/cjs}/rpc/substrate/payment.js +0 -0
- /package/{lib → dist/cjs}/rpc/substrate/state.js +0 -0
- /package/{lib → dist/cjs}/rpc/substrate/system.js +0 -0
- /package/{lib → dist/cjs}/schema/index.js +0 -0
- /package/{lib → dist/cjs}/setup.js +0 -0
- /package/{lib → dist/cjs}/utils/decoder.js +0 -0
- /package/{lib → dist/cjs}/utils/key-cache.js +0 -0
- /package/{lib → dist/cjs}/utils/proof.js +0 -0
- /package/{lib → dist/cjs}/utils/set-storage.js +0 -0
- /package/{lib → dist/cjs}/utils/time-travel.js +0 -0
- /package/{lib → dist/cjs}/wasm-executor/browser-wasm-executor.mjs +0 -0
- /package/{lib → dist/cjs}/wasm-executor/browser-worker.js +0 -0
- /package/{lib → dist/cjs}/wasm-executor/index.js +0 -0
- /package/{lib → dist/cjs}/wasm-executor/node-wasm-executor.mjs +0 -0
- /package/{lib → dist/cjs}/wasm-executor/node-worker.js +0 -0
- /package/{lib → dist/cjs}/xcm/downward.js +0 -0
- /package/{lib → dist/cjs}/xcm/horizontal.js +0 -0
- /package/{lib → dist/cjs}/xcm/index.js +0 -0
- /package/{lib → dist/cjs}/xcm/upward.js +0 -0
- /package/{lib/index.d.ts → dist/esm/index.js} +0 -0
- /package/{lib → dist/types}/api.d.ts +0 -0
- /package/{lib → dist/types}/blockchain/block-builder.d.ts +0 -0
- /package/{lib → dist/types}/blockchain/block.d.ts +0 -0
- /package/{lib → dist/types}/blockchain/head-state.d.ts +0 -0
- /package/{lib → dist/types}/blockchain/index.d.ts +0 -0
- /package/{lib → dist/types}/blockchain/inherent/index.d.ts +0 -0
- /package/{lib → dist/types}/blockchain/inherent/para-enter.d.ts +0 -0
- /package/{lib → dist/types}/blockchain/inherent/parachain/babe-randomness.d.ts +0 -0
- /package/{lib → dist/types}/blockchain/inherent/parachain/nimbus-author-inherent.d.ts +0 -0
- /package/{lib → dist/types}/blockchain/inherent/parachain/validation-data.d.ts +0 -0
- /package/{lib → dist/types}/blockchain/storage-layer.d.ts +0 -0
- /package/{lib → dist/types}/blockchain/txpool.d.ts +0 -0
- /package/{lib → dist/types}/chopsticks-provider.d.ts +0 -0
- /package/{lib → dist/types}/database.d.ts +0 -0
- /package/{lib → dist/types}/logger.d.ts +0 -0
- /package/{lib → dist/types}/offchain.d.ts +0 -0
- /package/{lib → dist/types}/rpc/index.d.ts +0 -0
- /package/{lib → dist/types}/rpc/shared.d.ts +0 -0
- /package/{lib → dist/types}/rpc/substrate/author.d.ts +0 -0
- /package/{lib → dist/types}/rpc/substrate/chain.d.ts +0 -0
- /package/{lib → dist/types}/rpc/substrate/index.d.ts +0 -0
- /package/{lib → dist/types}/rpc/substrate/payment.d.ts +0 -0
- /package/{lib → dist/types}/rpc/substrate/state.d.ts +0 -0
- /package/{lib → dist/types}/rpc/substrate/system.d.ts +0 -0
- /package/{lib → dist/types}/schema/index.d.ts +0 -0
- /package/{lib → dist/types}/setup.d.ts +0 -0
- /package/{lib → dist/types}/utils/decoder.d.ts +0 -0
- /package/{lib → dist/types}/utils/key-cache.d.ts +0 -0
- /package/{lib → dist/types}/utils/proof.d.ts +0 -0
- /package/{lib → dist/types}/utils/set-storage.d.ts +0 -0
- /package/{lib → dist/types}/utils/time-travel.d.ts +0 -0
- /package/{lib → dist/types}/wasm-executor/browser-worker.d.ts +0 -0
- /package/{lib → dist/types}/wasm-executor/index.d.ts +0 -0
- /package/{lib → dist/types}/wasm-executor/node-worker.d.ts +0 -0
- /package/{lib → dist/types}/xcm/downward.d.ts +0 -0
- /package/{lib → dist/types}/xcm/horizontal.d.ts +0 -0
- /package/{lib → dist/types}/xcm/index.d.ts +0 -0
- /package/{lib → dist/types}/xcm/upward.d.ts +0 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
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");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _Block_chain, _Block_header, _Block_parentBlock, _Block_extrinsics, _Block_wasm, _Block_runtimeVersion, _Block_metadata, _Block_registry, _Block_meta, _Block_baseStorage, _Block_storages;
|
|
13
|
+
import { Metadata, TypeRegistry } from '@polkadot/types';
|
|
14
|
+
import { expandMetadata } from '@polkadot/types/metadata';
|
|
15
|
+
import { getSpecExtensions, getSpecHasher, getSpecTypes } from '@polkadot/types-known/util';
|
|
16
|
+
import { hexToU8a, objectSpread, stringToHex } from '@polkadot/util';
|
|
17
|
+
import { RemoteStorageLayer, StorageLayer, StorageValueKind } from './storage-layer';
|
|
18
|
+
import { compactHex, printRuntimeLogs } from '../utils';
|
|
19
|
+
import { getRuntimeVersion, runTask, taskHandler } from '../wasm-executor';
|
|
20
|
+
/**
|
|
21
|
+
* Block class.
|
|
22
|
+
*
|
|
23
|
+
* @example Instantiate a block
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* const block = new Block(chain, number, hash)
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @example Get storage
|
|
30
|
+
*
|
|
31
|
+
* ```ts
|
|
32
|
+
* const block = await chain.getBlock('0x...')
|
|
33
|
+
* block.storage()
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export class Block {
|
|
37
|
+
constructor(chain, number, hash, parentBlock, block) {
|
|
38
|
+
this.number = number;
|
|
39
|
+
this.hash = hash;
|
|
40
|
+
_Block_chain.set(this, void 0);
|
|
41
|
+
_Block_header.set(this, void 0);
|
|
42
|
+
_Block_parentBlock.set(this, void 0);
|
|
43
|
+
_Block_extrinsics.set(this, void 0);
|
|
44
|
+
_Block_wasm.set(this, void 0);
|
|
45
|
+
_Block_runtimeVersion.set(this, void 0);
|
|
46
|
+
_Block_metadata.set(this, void 0);
|
|
47
|
+
_Block_registry.set(this, void 0);
|
|
48
|
+
_Block_meta.set(this, void 0);
|
|
49
|
+
_Block_baseStorage.set(this, void 0);
|
|
50
|
+
_Block_storages.set(this, void 0);
|
|
51
|
+
__classPrivateFieldSet(this, _Block_chain, chain, "f");
|
|
52
|
+
__classPrivateFieldSet(this, _Block_parentBlock, parentBlock ? new WeakRef(parentBlock) : undefined, "f");
|
|
53
|
+
__classPrivateFieldSet(this, _Block_header, block?.header, "f");
|
|
54
|
+
__classPrivateFieldSet(this, _Block_extrinsics, block?.extrinsics, "f");
|
|
55
|
+
__classPrivateFieldSet(this, _Block_baseStorage, block?.storage ?? new RemoteStorageLayer(chain.api, hash, chain.db), "f");
|
|
56
|
+
__classPrivateFieldSet(this, _Block_storages, [], "f");
|
|
57
|
+
__classPrivateFieldSet(this, _Block_runtimeVersion, parentBlock?.runtimeVersion, "f");
|
|
58
|
+
__classPrivateFieldSet(this, _Block_metadata, parentBlock?.metadata, "f");
|
|
59
|
+
__classPrivateFieldSet(this, _Block_registry, parentBlock?.registry, "f");
|
|
60
|
+
__classPrivateFieldSet(this, _Block_meta, parentBlock?.meta, "f");
|
|
61
|
+
const storageDiff = block?.storageDiff;
|
|
62
|
+
if (storageDiff) {
|
|
63
|
+
// if code doesn't change then keep parent block's meta
|
|
64
|
+
// otherwise reset meta
|
|
65
|
+
if (storageDiff[stringToHex(':code')]) {
|
|
66
|
+
__classPrivateFieldSet(this, _Block_runtimeVersion, undefined, "f");
|
|
67
|
+
__classPrivateFieldSet(this, _Block_metadata, undefined, "f");
|
|
68
|
+
__classPrivateFieldSet(this, _Block_registry, undefined, "f");
|
|
69
|
+
__classPrivateFieldSet(this, _Block_meta, undefined, "f");
|
|
70
|
+
}
|
|
71
|
+
this.pushStorageLayer().setAll(storageDiff);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
get chain() {
|
|
75
|
+
return __classPrivateFieldGet(this, _Block_chain, "f");
|
|
76
|
+
}
|
|
77
|
+
get header() {
|
|
78
|
+
if (!__classPrivateFieldGet(this, _Block_header, "f")) {
|
|
79
|
+
__classPrivateFieldSet(this, _Block_header, Promise.all([this.registry, __classPrivateFieldGet(this, _Block_chain, "f").api.getHeader(this.hash)]).then(([registry, header]) => registry.createType('Header', header)), "f");
|
|
80
|
+
}
|
|
81
|
+
return __classPrivateFieldGet(this, _Block_header, "f");
|
|
82
|
+
}
|
|
83
|
+
get extrinsics() {
|
|
84
|
+
if (!__classPrivateFieldGet(this, _Block_extrinsics, "f")) {
|
|
85
|
+
__classPrivateFieldSet(this, _Block_extrinsics, __classPrivateFieldGet(this, _Block_chain, "f").api.getBlock(this.hash).then((b) => {
|
|
86
|
+
if (!b) {
|
|
87
|
+
throw new Error(`Block ${this.hash} not found`);
|
|
88
|
+
}
|
|
89
|
+
return b.block.extrinsics;
|
|
90
|
+
}), "f");
|
|
91
|
+
}
|
|
92
|
+
return __classPrivateFieldGet(this, _Block_extrinsics, "f");
|
|
93
|
+
}
|
|
94
|
+
get parentBlock() {
|
|
95
|
+
if (this.number === 0) {
|
|
96
|
+
return Promise.resolve(undefined);
|
|
97
|
+
}
|
|
98
|
+
const getBlock = async (header) => {
|
|
99
|
+
const _header = await header;
|
|
100
|
+
const block = await __classPrivateFieldGet(this, _Block_chain, "f").getBlock(_header.parentHash.toHex());
|
|
101
|
+
if (block)
|
|
102
|
+
__classPrivateFieldSet(this, _Block_parentBlock, new WeakRef(block), "f");
|
|
103
|
+
return block;
|
|
104
|
+
};
|
|
105
|
+
if (__classPrivateFieldGet(this, _Block_parentBlock, "f") instanceof WeakRef) {
|
|
106
|
+
const block = __classPrivateFieldGet(this, _Block_parentBlock, "f").deref();
|
|
107
|
+
if (block)
|
|
108
|
+
return Promise.resolve(block);
|
|
109
|
+
__classPrivateFieldSet(this, _Block_parentBlock, getBlock(this.header), "f");
|
|
110
|
+
}
|
|
111
|
+
else if (!__classPrivateFieldGet(this, _Block_parentBlock, "f")) {
|
|
112
|
+
__classPrivateFieldSet(this, _Block_parentBlock, getBlock(this.header), "f");
|
|
113
|
+
}
|
|
114
|
+
return __classPrivateFieldGet(this, _Block_parentBlock, "f");
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Get the block storage.
|
|
118
|
+
*/
|
|
119
|
+
get storage() {
|
|
120
|
+
return __classPrivateFieldGet(this, _Block_storages, "f")[__classPrivateFieldGet(this, _Block_storages, "f").length - 1] ?? __classPrivateFieldGet(this, _Block_baseStorage, "f");
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Get the block storage by key.
|
|
124
|
+
*/
|
|
125
|
+
async get(key) {
|
|
126
|
+
const val = await this.storage.get(key, true);
|
|
127
|
+
switch (val) {
|
|
128
|
+
case StorageValueKind.Deleted:
|
|
129
|
+
return undefined;
|
|
130
|
+
default:
|
|
131
|
+
return val;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
async read(type, query, ...args) {
|
|
135
|
+
const key = compactHex(query(...args));
|
|
136
|
+
const value = await this.get(key);
|
|
137
|
+
if (!value) {
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
const registry = await this.registry;
|
|
141
|
+
return registry.createType(type, hexToU8a(value));
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Get paged storage keys.
|
|
145
|
+
*/
|
|
146
|
+
async getKeysPaged(options) {
|
|
147
|
+
const layer = new StorageLayer(this.storage);
|
|
148
|
+
await layer.fold();
|
|
149
|
+
const prefix = options.prefix ?? '0x';
|
|
150
|
+
const startKey = options.startKey ?? '0x';
|
|
151
|
+
const pageSize = options.pageSize;
|
|
152
|
+
return layer.getKeysPaged(prefix, pageSize, startKey);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Push a layer to the storage stack.
|
|
156
|
+
*/
|
|
157
|
+
pushStorageLayer() {
|
|
158
|
+
const layer = new StorageLayer(this.storage);
|
|
159
|
+
__classPrivateFieldGet(this, _Block_storages, "f").push(layer);
|
|
160
|
+
return layer;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Pop a layer from the storage stack.
|
|
164
|
+
*/
|
|
165
|
+
popStorageLayer() {
|
|
166
|
+
__classPrivateFieldGet(this, _Block_storages, "f").pop();
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Get storage diff.
|
|
170
|
+
*/
|
|
171
|
+
async storageDiff() {
|
|
172
|
+
const storage = {};
|
|
173
|
+
for (const layer of __classPrivateFieldGet(this, _Block_storages, "f")) {
|
|
174
|
+
await layer.mergeInto(storage);
|
|
175
|
+
}
|
|
176
|
+
return storage;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Get the wasm string.
|
|
180
|
+
*/
|
|
181
|
+
get wasm() {
|
|
182
|
+
if (!__classPrivateFieldGet(this, _Block_wasm, "f")) {
|
|
183
|
+
__classPrivateFieldSet(this, _Block_wasm, (async () => {
|
|
184
|
+
const wasmKey = stringToHex(':code');
|
|
185
|
+
const wasm = await this.get(wasmKey);
|
|
186
|
+
if (!wasm) {
|
|
187
|
+
throw new Error('No wasm found');
|
|
188
|
+
}
|
|
189
|
+
return wasm;
|
|
190
|
+
})(), "f");
|
|
191
|
+
}
|
|
192
|
+
return __classPrivateFieldGet(this, _Block_wasm, "f");
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Set the runtime wasm.
|
|
196
|
+
*/
|
|
197
|
+
setWasm(wasm) {
|
|
198
|
+
const wasmKey = stringToHex(':code');
|
|
199
|
+
this.pushStorageLayer().set(wasmKey, wasm);
|
|
200
|
+
__classPrivateFieldSet(this, _Block_wasm, Promise.resolve(wasm), "f");
|
|
201
|
+
__classPrivateFieldSet(this, _Block_runtimeVersion, undefined, "f");
|
|
202
|
+
__classPrivateFieldSet(this, _Block_registry, undefined, "f");
|
|
203
|
+
__classPrivateFieldSet(this, _Block_meta, undefined, "f");
|
|
204
|
+
__classPrivateFieldSet(this, _Block_metadata, undefined, "f");
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Get the type registry.
|
|
208
|
+
* @see https://polkadot.js.org/docs/api/start/types.create#why-create-types
|
|
209
|
+
*/
|
|
210
|
+
get registry() {
|
|
211
|
+
if (!__classPrivateFieldGet(this, _Block_registry, "f")) {
|
|
212
|
+
__classPrivateFieldSet(this, _Block_registry, Promise.all([
|
|
213
|
+
this.metadata,
|
|
214
|
+
__classPrivateFieldGet(this, _Block_chain, "f").api.chainProperties,
|
|
215
|
+
__classPrivateFieldGet(this, _Block_chain, "f").api.chain,
|
|
216
|
+
this.runtimeVersion,
|
|
217
|
+
]).then(([data, properties, chain, version]) => {
|
|
218
|
+
const registry = new TypeRegistry(this.hash);
|
|
219
|
+
registry.setKnownTypes(this.chain.registeredTypes);
|
|
220
|
+
registry.setChainProperties(registry.createType('ChainProperties', properties));
|
|
221
|
+
registry.register(getSpecTypes(registry, chain, version.specName, version.specVersion));
|
|
222
|
+
registry.setHasher(getSpecHasher(registry, chain, version.specName));
|
|
223
|
+
registry.setMetadata(new Metadata(registry, data), undefined, objectSpread({}, getSpecExtensions(registry, chain, version.specName), __classPrivateFieldGet(this, _Block_chain, "f").api.signedExtensions));
|
|
224
|
+
return registry;
|
|
225
|
+
}), "f");
|
|
226
|
+
}
|
|
227
|
+
return __classPrivateFieldGet(this, _Block_registry, "f");
|
|
228
|
+
}
|
|
229
|
+
get runtimeVersion() {
|
|
230
|
+
if (!__classPrivateFieldGet(this, _Block_runtimeVersion, "f")) {
|
|
231
|
+
__classPrivateFieldSet(this, _Block_runtimeVersion, this.wasm.then(getRuntimeVersion), "f");
|
|
232
|
+
}
|
|
233
|
+
return __classPrivateFieldGet(this, _Block_runtimeVersion, "f");
|
|
234
|
+
}
|
|
235
|
+
get metadata() {
|
|
236
|
+
if (!__classPrivateFieldGet(this, _Block_metadata, "f")) {
|
|
237
|
+
__classPrivateFieldSet(this, _Block_metadata, this.call('Metadata_metadata', []).then((resp) => compactHex(hexToU8a(resp.result))), "f");
|
|
238
|
+
}
|
|
239
|
+
return __classPrivateFieldGet(this, _Block_metadata, "f");
|
|
240
|
+
}
|
|
241
|
+
get meta() {
|
|
242
|
+
if (!__classPrivateFieldGet(this, _Block_meta, "f")) {
|
|
243
|
+
__classPrivateFieldSet(this, _Block_meta, Promise.all([this.registry, this.metadata]).then(([registry, metadataStr]) => {
|
|
244
|
+
const metadata = new Metadata(registry, metadataStr);
|
|
245
|
+
return expandMetadata(registry, metadata);
|
|
246
|
+
}), "f");
|
|
247
|
+
}
|
|
248
|
+
return __classPrivateFieldGet(this, _Block_meta, "f");
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Call a runtime method.
|
|
252
|
+
*/
|
|
253
|
+
async call(method, args) {
|
|
254
|
+
const wasm = await this.wasm;
|
|
255
|
+
const response = await runTask({
|
|
256
|
+
wasm,
|
|
257
|
+
calls: [[method, args]],
|
|
258
|
+
mockSignatureHost: __classPrivateFieldGet(this, _Block_chain, "f").mockSignatureHost,
|
|
259
|
+
allowUnresolvedImports: __classPrivateFieldGet(this, _Block_chain, "f").allowUnresolvedImports,
|
|
260
|
+
runtimeLogLevel: __classPrivateFieldGet(this, _Block_chain, "f").runtimeLogLevel,
|
|
261
|
+
}, taskHandler(this));
|
|
262
|
+
if ('Call' in response) {
|
|
263
|
+
printRuntimeLogs(response.Call.runtimeLogs);
|
|
264
|
+
if (this.chain.offchainWorker) {
|
|
265
|
+
// apply offchain storage
|
|
266
|
+
for (const [key, value] of response.Call.offchainStorageDiff) {
|
|
267
|
+
this.chain.offchainWorker.set(key, value);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return response.Call;
|
|
271
|
+
}
|
|
272
|
+
if (response.Error)
|
|
273
|
+
throw Error(response.Error);
|
|
274
|
+
throw Error('Unexpected response');
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
_Block_chain = new WeakMap(), _Block_header = new WeakMap(), _Block_parentBlock = new WeakMap(), _Block_extrinsics = new WeakMap(), _Block_wasm = new WeakMap(), _Block_runtimeVersion = new WeakMap(), _Block_metadata = new WeakMap(), _Block_registry = new WeakMap(), _Block_meta = new WeakMap(), _Block_baseStorage = new WeakMap(), _Block_storages = new WeakMap();
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
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");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _HeadState_headListeners, _HeadState_storageListeners, _HeadState_oldValues, _HeadState_head;
|
|
13
|
+
import { stringToHex } from '@polkadot/util';
|
|
14
|
+
import { defaultLogger } from '../logger';
|
|
15
|
+
export const randomId = () => Math.random().toString(36).substring(2);
|
|
16
|
+
const logger = defaultLogger.child({ name: 'head-state' });
|
|
17
|
+
export class HeadState {
|
|
18
|
+
constructor(head) {
|
|
19
|
+
_HeadState_headListeners.set(this, {});
|
|
20
|
+
_HeadState_storageListeners.set(this, {});
|
|
21
|
+
_HeadState_oldValues.set(this, {});
|
|
22
|
+
_HeadState_head.set(this, void 0);
|
|
23
|
+
__classPrivateFieldSet(this, _HeadState_head, head, "f");
|
|
24
|
+
}
|
|
25
|
+
subscribeHead(cb) {
|
|
26
|
+
const id = randomId();
|
|
27
|
+
__classPrivateFieldGet(this, _HeadState_headListeners, "f")[id] = cb;
|
|
28
|
+
return id;
|
|
29
|
+
}
|
|
30
|
+
unsubscribeHead(id) {
|
|
31
|
+
delete __classPrivateFieldGet(this, _HeadState_headListeners, "f")[id];
|
|
32
|
+
}
|
|
33
|
+
async subscribeStorage(keys, cb) {
|
|
34
|
+
const id = randomId();
|
|
35
|
+
__classPrivateFieldGet(this, _HeadState_storageListeners, "f")[id] = [keys, cb];
|
|
36
|
+
for (const key of keys) {
|
|
37
|
+
__classPrivateFieldGet(this, _HeadState_oldValues, "f")[key] = await __classPrivateFieldGet(this, _HeadState_head, "f").get(key).then((val) => val || null);
|
|
38
|
+
}
|
|
39
|
+
return id;
|
|
40
|
+
}
|
|
41
|
+
unsubscribeStorage(id) {
|
|
42
|
+
delete __classPrivateFieldGet(this, _HeadState_storageListeners, "f")[id];
|
|
43
|
+
}
|
|
44
|
+
async subscrubeRuntimeVersion(cb) {
|
|
45
|
+
const id = randomId();
|
|
46
|
+
const codeKey = stringToHex(':code');
|
|
47
|
+
__classPrivateFieldGet(this, _HeadState_storageListeners, "f")[id] = [[codeKey], cb];
|
|
48
|
+
__classPrivateFieldGet(this, _HeadState_oldValues, "f")[codeKey] = await __classPrivateFieldGet(this, _HeadState_head, "f").get(codeKey).then((val) => val || null);
|
|
49
|
+
return id;
|
|
50
|
+
}
|
|
51
|
+
unsubscribeRuntimeVersion(id) {
|
|
52
|
+
delete __classPrivateFieldGet(this, _HeadState_storageListeners, "f")[id];
|
|
53
|
+
}
|
|
54
|
+
async setHead(head) {
|
|
55
|
+
__classPrivateFieldSet(this, _HeadState_head, head, "f");
|
|
56
|
+
for (const cb of Object.values(__classPrivateFieldGet(this, _HeadState_headListeners, "f"))) {
|
|
57
|
+
try {
|
|
58
|
+
await cb(head);
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
logger.error(error, 'setHead head callback error');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const diff = await __classPrivateFieldGet(this, _HeadState_head, "f").storageDiff();
|
|
65
|
+
for (const [keys, cb] of Object.values(__classPrivateFieldGet(this, _HeadState_storageListeners, "f"))) {
|
|
66
|
+
const changed = keys.filter((key) => diff[key]).map((key) => [key, diff[key]]);
|
|
67
|
+
if (changed.length > 0) {
|
|
68
|
+
try {
|
|
69
|
+
await cb(head, changed);
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
logger.error(error, 'setHead storage diff callback error');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
Object.assign(__classPrivateFieldGet(this, _HeadState_oldValues, "f"), diff);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
_HeadState_headListeners = new WeakMap(), _HeadState_storageListeners = new WeakMap(), _HeadState_oldValues = new WeakMap(), _HeadState_head = new WeakMap();
|