@acala-network/chopsticks-core 0.8.0-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/api.d.ts +43 -0
- package/lib/api.js +116 -0
- package/lib/blockchain/block-builder.d.ts +10 -0
- package/lib/blockchain/block-builder.js +290 -0
- package/lib/blockchain/block.d.ts +45 -0
- package/lib/blockchain/block.js +221 -0
- package/lib/blockchain/head-state.d.ts +15 -0
- package/lib/blockchain/head-state.js +94 -0
- package/lib/blockchain/index.d.ts +73 -0
- package/lib/blockchain/index.js +351 -0
- package/lib/blockchain/inherent/index.d.ts +19 -0
- package/lib/blockchain/inherent/index.js +53 -0
- package/lib/blockchain/inherent/para-enter.d.ts +7 -0
- package/lib/blockchain/inherent/para-enter.js +37 -0
- package/lib/blockchain/inherent/parachain/babe-randomness.d.ts +7 -0
- package/lib/blockchain/inherent/parachain/babe-randomness.js +23 -0
- package/lib/blockchain/inherent/parachain/nimbus-author-inherent.d.ts +7 -0
- package/lib/blockchain/inherent/parachain/nimbus-author-inherent.js +23 -0
- package/lib/blockchain/inherent/parachain/validation-data.d.ts +19 -0
- package/lib/blockchain/inherent/parachain/validation-data.js +165 -0
- package/lib/blockchain/storage-layer.d.ts +32 -0
- package/lib/blockchain/storage-layer.js +242 -0
- package/lib/blockchain/txpool.d.ts +44 -0
- package/lib/blockchain/txpool.js +210 -0
- package/lib/db/browser.d.ts +2 -0
- package/lib/db/browser.js +33 -0
- package/lib/db/entities.d.ts +16 -0
- package/lib/db/entities.js +50 -0
- package/lib/db/index.d.ts +1 -0
- package/lib/db/index.js +17 -0
- package/lib/db/node.d.ts +2 -0
- package/lib/db/node.js +22 -0
- package/lib/executor.d.ts +41 -0
- package/lib/executor.js +160 -0
- package/lib/genesis-provider.d.ts +44 -0
- package/lib/genesis-provider.js +172 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.js +13 -0
- package/lib/logger.d.ts +7 -0
- package/lib/logger.js +34 -0
- package/lib/offchain.d.ts +10 -0
- package/lib/offchain.js +49 -0
- package/lib/schema/index.d.ts +62 -0
- package/lib/schema/index.js +11 -0
- package/lib/setup.d.ts +20 -0
- package/lib/setup.js +74 -0
- package/lib/utils/index.d.ts +18 -0
- package/lib/utils/index.js +65 -0
- package/lib/utils/key-cache.d.ts +10 -0
- package/lib/utils/key-cache.js +72 -0
- package/lib/utils/proof.d.ts +15 -0
- package/lib/utils/proof.js +33 -0
- package/lib/utils/set-storage.d.ts +7 -0
- package/lib/utils/set-storage.js +65 -0
- package/lib/utils/time-travel.d.ts +5 -0
- package/lib/utils/time-travel.js +67 -0
- package/lib/xcm/downward.d.ts +2 -0
- package/lib/xcm/downward.js +34 -0
- package/lib/xcm/horizontal.d.ts +2 -0
- package/lib/xcm/horizontal.js +34 -0
- package/lib/xcm/index.d.ts +9 -0
- package/lib/xcm/index.js +29 -0
- package/lib/xcm/upward.d.ts +2 -0
- package/lib/xcm/upward.js +26 -0
- package/package.json +46 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
11
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
12
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
13
|
+
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");
|
|
14
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
15
|
+
};
|
|
16
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
17
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
18
|
+
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");
|
|
19
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
20
|
+
};
|
|
21
|
+
var _Block_chain, _Block_header, _Block_parentBlock, _Block_extrinsics, _Block_wasm, _Block_runtimeVersion, _Block_metadata, _Block_registry, _Block_meta, _Block_baseStorage, _Block_storages;
|
|
22
|
+
import { Metadata, TypeRegistry } from '@polkadot/types';
|
|
23
|
+
import { expandMetadata } from '@polkadot/types/metadata';
|
|
24
|
+
import { getSpecExtensions, getSpecHasher, getSpecTypes } from '@polkadot/types-known/util';
|
|
25
|
+
import { hexToU8a, objectSpread, stringToHex } from '@polkadot/util';
|
|
26
|
+
import { RemoteStorageLayer, StorageLayer, StorageValueKind } from './storage-layer';
|
|
27
|
+
import { compactHex } from '../utils';
|
|
28
|
+
import { defaultLogger } from '../logger';
|
|
29
|
+
import { getRuntimeVersion, runTask, taskHandler } from '../executor';
|
|
30
|
+
export class Block {
|
|
31
|
+
constructor(chain, number, hash, parentBlock, block) {
|
|
32
|
+
var _a;
|
|
33
|
+
this.number = number;
|
|
34
|
+
this.hash = hash;
|
|
35
|
+
_Block_chain.set(this, void 0);
|
|
36
|
+
_Block_header.set(this, void 0);
|
|
37
|
+
_Block_parentBlock.set(this, void 0);
|
|
38
|
+
_Block_extrinsics.set(this, void 0);
|
|
39
|
+
_Block_wasm.set(this, void 0);
|
|
40
|
+
_Block_runtimeVersion.set(this, void 0);
|
|
41
|
+
_Block_metadata.set(this, void 0);
|
|
42
|
+
_Block_registry.set(this, void 0);
|
|
43
|
+
_Block_meta.set(this, void 0);
|
|
44
|
+
_Block_baseStorage.set(this, void 0);
|
|
45
|
+
_Block_storages.set(this, void 0);
|
|
46
|
+
__classPrivateFieldSet(this, _Block_chain, chain, "f");
|
|
47
|
+
__classPrivateFieldSet(this, _Block_parentBlock, parentBlock, "f");
|
|
48
|
+
__classPrivateFieldSet(this, _Block_header, block === null || block === void 0 ? void 0 : block.header, "f");
|
|
49
|
+
__classPrivateFieldSet(this, _Block_extrinsics, block === null || block === void 0 ? void 0 : block.extrinsics, "f");
|
|
50
|
+
__classPrivateFieldSet(this, _Block_baseStorage, (_a = block === null || block === void 0 ? void 0 : block.storage) !== null && _a !== void 0 ? _a : new RemoteStorageLayer(chain.api, hash, chain.db), "f");
|
|
51
|
+
__classPrivateFieldSet(this, _Block_storages, [], "f");
|
|
52
|
+
const storageDiff = block === null || block === void 0 ? void 0 : block.storageDiff;
|
|
53
|
+
if (storageDiff) {
|
|
54
|
+
// if code doesn't change then reuse parent block's meta
|
|
55
|
+
if (!(storageDiff === null || storageDiff === void 0 ? void 0 : storageDiff[stringToHex(':code')])) {
|
|
56
|
+
__classPrivateFieldSet(this, _Block_runtimeVersion, parentBlock === null || parentBlock === void 0 ? void 0 : parentBlock.runtimeVersion, "f");
|
|
57
|
+
__classPrivateFieldSet(this, _Block_metadata, parentBlock === null || parentBlock === void 0 ? void 0 : parentBlock.metadata, "f");
|
|
58
|
+
__classPrivateFieldSet(this, _Block_registry, parentBlock === null || parentBlock === void 0 ? void 0 : parentBlock.registry, "f");
|
|
59
|
+
__classPrivateFieldSet(this, _Block_meta, parentBlock === null || parentBlock === void 0 ? void 0 : parentBlock.meta, "f");
|
|
60
|
+
}
|
|
61
|
+
this.pushStorageLayer().setAll(storageDiff);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
get chain() {
|
|
65
|
+
return __classPrivateFieldGet(this, _Block_chain, "f");
|
|
66
|
+
}
|
|
67
|
+
get header() {
|
|
68
|
+
if (!__classPrivateFieldGet(this, _Block_header, "f")) {
|
|
69
|
+
__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");
|
|
70
|
+
}
|
|
71
|
+
return __classPrivateFieldGet(this, _Block_header, "f");
|
|
72
|
+
}
|
|
73
|
+
get extrinsics() {
|
|
74
|
+
if (!__classPrivateFieldGet(this, _Block_extrinsics, "f")) {
|
|
75
|
+
__classPrivateFieldSet(this, _Block_extrinsics, __classPrivateFieldGet(this, _Block_chain, "f").api.getBlock(this.hash).then((b) => b.block.extrinsics), "f");
|
|
76
|
+
}
|
|
77
|
+
return __classPrivateFieldGet(this, _Block_extrinsics, "f");
|
|
78
|
+
}
|
|
79
|
+
get parentBlock() {
|
|
80
|
+
if (this.number === 0) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
if (!__classPrivateFieldGet(this, _Block_parentBlock, "f")) {
|
|
84
|
+
__classPrivateFieldSet(this, _Block_parentBlock, Promise.resolve(this.header).then((h) => __classPrivateFieldGet(this, _Block_chain, "f").getBlock(h.parentHash.toHex())), "f");
|
|
85
|
+
}
|
|
86
|
+
return __classPrivateFieldGet(this, _Block_parentBlock, "f");
|
|
87
|
+
}
|
|
88
|
+
get storage() {
|
|
89
|
+
var _a;
|
|
90
|
+
return (_a = __classPrivateFieldGet(this, _Block_storages, "f")[__classPrivateFieldGet(this, _Block_storages, "f").length - 1]) !== null && _a !== void 0 ? _a : __classPrivateFieldGet(this, _Block_baseStorage, "f");
|
|
91
|
+
}
|
|
92
|
+
get(key) {
|
|
93
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
94
|
+
const val = yield this.storage.get(key, true);
|
|
95
|
+
switch (val) {
|
|
96
|
+
case StorageValueKind.Deleted:
|
|
97
|
+
return undefined;
|
|
98
|
+
default:
|
|
99
|
+
return val;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
getKeysPaged(options) {
|
|
104
|
+
var _a, _b;
|
|
105
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
106
|
+
const layer = new StorageLayer(this.storage);
|
|
107
|
+
yield layer.fold();
|
|
108
|
+
const prefix = (_a = options.prefix) !== null && _a !== void 0 ? _a : '0x';
|
|
109
|
+
const startKey = (_b = options.startKey) !== null && _b !== void 0 ? _b : prefix;
|
|
110
|
+
const pageSize = options.pageSize;
|
|
111
|
+
return layer.getKeysPaged(prefix, pageSize, startKey);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
pushStorageLayer() {
|
|
115
|
+
const layer = new StorageLayer(this.storage);
|
|
116
|
+
__classPrivateFieldGet(this, _Block_storages, "f").push(layer);
|
|
117
|
+
return layer;
|
|
118
|
+
}
|
|
119
|
+
popStorageLayer() {
|
|
120
|
+
__classPrivateFieldGet(this, _Block_storages, "f").pop();
|
|
121
|
+
}
|
|
122
|
+
storageDiff() {
|
|
123
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
124
|
+
const storage = {};
|
|
125
|
+
for (const layer of __classPrivateFieldGet(this, _Block_storages, "f")) {
|
|
126
|
+
yield layer.mergeInto(storage);
|
|
127
|
+
}
|
|
128
|
+
return storage;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
get wasm() {
|
|
132
|
+
if (!__classPrivateFieldGet(this, _Block_wasm, "f")) {
|
|
133
|
+
__classPrivateFieldSet(this, _Block_wasm, (() => __awaiter(this, void 0, void 0, function* () {
|
|
134
|
+
const wasmKey = stringToHex(':code');
|
|
135
|
+
const wasm = yield this.get(wasmKey);
|
|
136
|
+
if (!wasm) {
|
|
137
|
+
throw new Error('No wasm found');
|
|
138
|
+
}
|
|
139
|
+
return wasm;
|
|
140
|
+
}))(), "f");
|
|
141
|
+
}
|
|
142
|
+
return __classPrivateFieldGet(this, _Block_wasm, "f");
|
|
143
|
+
}
|
|
144
|
+
setWasm(wasm) {
|
|
145
|
+
const wasmKey = stringToHex(':code');
|
|
146
|
+
this.pushStorageLayer().set(wasmKey, wasm);
|
|
147
|
+
__classPrivateFieldSet(this, _Block_wasm, Promise.resolve(wasm), "f");
|
|
148
|
+
__classPrivateFieldSet(this, _Block_runtimeVersion, undefined, "f");
|
|
149
|
+
__classPrivateFieldSet(this, _Block_registry, undefined, "f");
|
|
150
|
+
__classPrivateFieldSet(this, _Block_meta, undefined, "f");
|
|
151
|
+
__classPrivateFieldSet(this, _Block_metadata, undefined, "f");
|
|
152
|
+
}
|
|
153
|
+
get registry() {
|
|
154
|
+
if (!__classPrivateFieldGet(this, _Block_registry, "f")) {
|
|
155
|
+
__classPrivateFieldSet(this, _Block_registry, Promise.all([
|
|
156
|
+
this.metadata,
|
|
157
|
+
__classPrivateFieldGet(this, _Block_chain, "f").api.chainProperties,
|
|
158
|
+
__classPrivateFieldGet(this, _Block_chain, "f").api.chain,
|
|
159
|
+
this.runtimeVersion,
|
|
160
|
+
]).then(([data, properties, chain, version]) => {
|
|
161
|
+
const registry = new TypeRegistry(this.hash);
|
|
162
|
+
registry.setKnownTypes(this.chain.registeredTypes);
|
|
163
|
+
registry.setChainProperties(registry.createType('ChainProperties', properties));
|
|
164
|
+
registry.register(getSpecTypes(registry, chain, version.specName, version.specVersion));
|
|
165
|
+
registry.setHasher(getSpecHasher(registry, chain, version.specName));
|
|
166
|
+
registry.setMetadata(new Metadata(registry, data), undefined, objectSpread({}, getSpecExtensions(registry, chain, version.specName), __classPrivateFieldGet(this, _Block_chain, "f").api.signedExtensions));
|
|
167
|
+
return registry;
|
|
168
|
+
}), "f");
|
|
169
|
+
}
|
|
170
|
+
return __classPrivateFieldGet(this, _Block_registry, "f");
|
|
171
|
+
}
|
|
172
|
+
get runtimeVersion() {
|
|
173
|
+
if (!__classPrivateFieldGet(this, _Block_runtimeVersion, "f")) {
|
|
174
|
+
__classPrivateFieldSet(this, _Block_runtimeVersion, this.wasm.then(getRuntimeVersion), "f");
|
|
175
|
+
}
|
|
176
|
+
return __classPrivateFieldGet(this, _Block_runtimeVersion, "f");
|
|
177
|
+
}
|
|
178
|
+
get metadata() {
|
|
179
|
+
if (!__classPrivateFieldGet(this, _Block_metadata, "f")) {
|
|
180
|
+
__classPrivateFieldSet(this, _Block_metadata, this.call('Metadata_metadata', []).then((resp) => compactHex(hexToU8a(resp.result))), "f");
|
|
181
|
+
}
|
|
182
|
+
return __classPrivateFieldGet(this, _Block_metadata, "f");
|
|
183
|
+
}
|
|
184
|
+
get meta() {
|
|
185
|
+
if (!__classPrivateFieldGet(this, _Block_meta, "f")) {
|
|
186
|
+
__classPrivateFieldSet(this, _Block_meta, Promise.all([this.registry, this.metadata]).then(([registry, metadataStr]) => {
|
|
187
|
+
const metadata = new Metadata(registry, metadataStr);
|
|
188
|
+
return expandMetadata(registry, metadata);
|
|
189
|
+
}), "f");
|
|
190
|
+
}
|
|
191
|
+
return __classPrivateFieldGet(this, _Block_meta, "f");
|
|
192
|
+
}
|
|
193
|
+
call(method, args) {
|
|
194
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
195
|
+
const wasm = yield this.wasm;
|
|
196
|
+
const response = yield runTask({
|
|
197
|
+
wasm,
|
|
198
|
+
calls: [[method, args]],
|
|
199
|
+
mockSignatureHost: __classPrivateFieldGet(this, _Block_chain, "f").mockSignatureHost,
|
|
200
|
+
allowUnresolvedImports: __classPrivateFieldGet(this, _Block_chain, "f").allowUnresolvedImports,
|
|
201
|
+
runtimeLogLevel: __classPrivateFieldGet(this, _Block_chain, "f").runtimeLogLevel,
|
|
202
|
+
}, taskHandler(this));
|
|
203
|
+
if (response.Call) {
|
|
204
|
+
for (const log of response.Call.runtimeLogs) {
|
|
205
|
+
defaultLogger.info(`RuntimeLogs:\n${log}`);
|
|
206
|
+
}
|
|
207
|
+
if (this.chain.offchainWorker) {
|
|
208
|
+
// apply offchain storage
|
|
209
|
+
for (const [key, value] of response.Call.offchainStorageDiff) {
|
|
210
|
+
this.chain.offchainWorker.set(key, value);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return response.Call;
|
|
214
|
+
}
|
|
215
|
+
if (response.Error)
|
|
216
|
+
throw Error(response.Error);
|
|
217
|
+
throw Error('Unexpected response');
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
_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,15 @@
|
|
|
1
|
+
import { Block } from './block';
|
|
2
|
+
type Callback = (block: Block, pairs: [string, string][]) => void | Promise<void>;
|
|
3
|
+
export declare const randomId: () => string;
|
|
4
|
+
export declare class HeadState {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(head: Block);
|
|
7
|
+
subscribeHead(cb: (block: Block) => void): string;
|
|
8
|
+
unsubscribeHead(id: string): void;
|
|
9
|
+
subscribeStorage(keys: string[], cb: Callback): Promise<string>;
|
|
10
|
+
unsubscribeStorage(id: string): void;
|
|
11
|
+
subscrubeRuntimeVersion(cb: (block: Block) => void): Promise<string>;
|
|
12
|
+
unsubscribeRuntimeVersion(id: string): void;
|
|
13
|
+
setHead(head: Block): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
11
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
12
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
13
|
+
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");
|
|
14
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
15
|
+
};
|
|
16
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
17
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
18
|
+
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");
|
|
19
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
20
|
+
};
|
|
21
|
+
var _HeadState_headListeners, _HeadState_storageListeners, _HeadState_oldValues, _HeadState_head;
|
|
22
|
+
import { stringToHex } from '@polkadot/util';
|
|
23
|
+
import { defaultLogger } from '../logger';
|
|
24
|
+
export const randomId = () => Math.random().toString(36).substring(2);
|
|
25
|
+
const logger = defaultLogger.child({ name: 'head-state' });
|
|
26
|
+
export class HeadState {
|
|
27
|
+
constructor(head) {
|
|
28
|
+
_HeadState_headListeners.set(this, {});
|
|
29
|
+
_HeadState_storageListeners.set(this, {});
|
|
30
|
+
_HeadState_oldValues.set(this, {});
|
|
31
|
+
_HeadState_head.set(this, void 0);
|
|
32
|
+
__classPrivateFieldSet(this, _HeadState_head, head, "f");
|
|
33
|
+
}
|
|
34
|
+
subscribeHead(cb) {
|
|
35
|
+
const id = randomId();
|
|
36
|
+
__classPrivateFieldGet(this, _HeadState_headListeners, "f")[id] = cb;
|
|
37
|
+
return id;
|
|
38
|
+
}
|
|
39
|
+
unsubscribeHead(id) {
|
|
40
|
+
delete __classPrivateFieldGet(this, _HeadState_headListeners, "f")[id];
|
|
41
|
+
}
|
|
42
|
+
subscribeStorage(keys, cb) {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
const id = randomId();
|
|
45
|
+
__classPrivateFieldGet(this, _HeadState_storageListeners, "f")[id] = [keys, cb];
|
|
46
|
+
for (const key of keys) {
|
|
47
|
+
__classPrivateFieldGet(this, _HeadState_oldValues, "f")[key] = yield __classPrivateFieldGet(this, _HeadState_head, "f").get(key);
|
|
48
|
+
}
|
|
49
|
+
return id;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
unsubscribeStorage(id) {
|
|
53
|
+
delete __classPrivateFieldGet(this, _HeadState_storageListeners, "f")[id];
|
|
54
|
+
}
|
|
55
|
+
subscrubeRuntimeVersion(cb) {
|
|
56
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
+
const id = randomId();
|
|
58
|
+
const codeKey = stringToHex(':code');
|
|
59
|
+
__classPrivateFieldGet(this, _HeadState_storageListeners, "f")[id] = [[codeKey], cb];
|
|
60
|
+
__classPrivateFieldGet(this, _HeadState_oldValues, "f")[codeKey] = yield __classPrivateFieldGet(this, _HeadState_head, "f").get(codeKey);
|
|
61
|
+
return id;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
unsubscribeRuntimeVersion(id) {
|
|
65
|
+
delete __classPrivateFieldGet(this, _HeadState_storageListeners, "f")[id];
|
|
66
|
+
}
|
|
67
|
+
setHead(head) {
|
|
68
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
__classPrivateFieldSet(this, _HeadState_head, head, "f");
|
|
70
|
+
for (const cb of Object.values(__classPrivateFieldGet(this, _HeadState_headListeners, "f"))) {
|
|
71
|
+
try {
|
|
72
|
+
yield cb(head);
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
logger.error(error, 'setHead head callback error');
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const diff = yield __classPrivateFieldGet(this, _HeadState_head, "f").storageDiff();
|
|
79
|
+
for (const [keys, cb] of Object.values(__classPrivateFieldGet(this, _HeadState_storageListeners, "f"))) {
|
|
80
|
+
const changed = keys.filter((key) => diff[key]).map((key) => [key, diff[key]]);
|
|
81
|
+
if (changed.length > 0) {
|
|
82
|
+
try {
|
|
83
|
+
yield cb(head, changed);
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
logger.error(error, 'setHead storage diff callback error');
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
Object.assign(__classPrivateFieldGet(this, _HeadState_oldValues, "f"), diff);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
_HeadState_headListeners = new WeakMap(), _HeadState_storageListeners = new WeakMap(), _HeadState_oldValues = new WeakMap(), _HeadState_head = new WeakMap();
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { ApplyExtrinsicResult } from '@polkadot/types/interfaces';
|
|
2
|
+
import { DataSource } from 'typeorm';
|
|
3
|
+
import { HexString } from '@polkadot/util/types';
|
|
4
|
+
import { RegisteredTypes } from '@polkadot/types/types';
|
|
5
|
+
import type { TransactionValidity } from '@polkadot/types/interfaces/txqueue';
|
|
6
|
+
import { Api } from '../api';
|
|
7
|
+
import { Block } from './block';
|
|
8
|
+
import { BuildBlockMode, BuildBlockParams, DownwardMessage, HorizontalMessage, TxPool } from './txpool';
|
|
9
|
+
import { HeadState } from './head-state';
|
|
10
|
+
import { InherentProvider } from './inherent';
|
|
11
|
+
import { OffchainWorker } from '../offchain';
|
|
12
|
+
export interface Options {
|
|
13
|
+
api: Api;
|
|
14
|
+
buildBlockMode?: BuildBlockMode;
|
|
15
|
+
inherentProvider: InherentProvider;
|
|
16
|
+
db?: DataSource;
|
|
17
|
+
header: {
|
|
18
|
+
number: number;
|
|
19
|
+
hash: HexString;
|
|
20
|
+
};
|
|
21
|
+
mockSignatureHost?: boolean;
|
|
22
|
+
allowUnresolvedImports?: boolean;
|
|
23
|
+
runtimeLogLevel?: number;
|
|
24
|
+
registeredTypes: RegisteredTypes;
|
|
25
|
+
offchainWorker?: boolean;
|
|
26
|
+
maxMemoryBlockCount?: number;
|
|
27
|
+
}
|
|
28
|
+
export declare class Blockchain {
|
|
29
|
+
#private;
|
|
30
|
+
readonly uid: string;
|
|
31
|
+
readonly api: Api;
|
|
32
|
+
readonly db: DataSource | undefined;
|
|
33
|
+
readonly mockSignatureHost: boolean;
|
|
34
|
+
readonly allowUnresolvedImports: boolean;
|
|
35
|
+
readonly runtimeLogLevel: number;
|
|
36
|
+
readonly registeredTypes: RegisteredTypes;
|
|
37
|
+
readonly headState: HeadState;
|
|
38
|
+
readonly offchainWorker: OffchainWorker | undefined;
|
|
39
|
+
constructor({ api, buildBlockMode, inherentProvider, db, header, mockSignatureHost, allowUnresolvedImports, runtimeLogLevel, registeredTypes, offchainWorker, maxMemoryBlockCount, }: Options);
|
|
40
|
+
get head(): Block;
|
|
41
|
+
get txPool(): TxPool;
|
|
42
|
+
saveBlockToDB(block: Block): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Try to load block from db and register it
|
|
45
|
+
* If pass in number, get block by number, else get block by hash
|
|
46
|
+
*/
|
|
47
|
+
loadBlockFromDB(key: number | HexString): Promise<Block | undefined>;
|
|
48
|
+
getBlockAt(number?: number): Promise<Block | undefined>;
|
|
49
|
+
getBlock(hash?: HexString): Promise<Block | undefined>;
|
|
50
|
+
blocksInMemory(): Block[];
|
|
51
|
+
unregisterBlock(block: Block): Promise<void>;
|
|
52
|
+
onNewBlock(block: Block): Promise<void>;
|
|
53
|
+
setHead(block: Block): Promise<void>;
|
|
54
|
+
submitExtrinsic(extrinsic: HexString): Promise<HexString>;
|
|
55
|
+
validateExtrinsic(extrinsic: HexString, source?: '0x00' | '0x01' | '0x02' /** External */): Promise<TransactionValidity>;
|
|
56
|
+
submitUpwardMessages(id: number, ump: HexString[]): void;
|
|
57
|
+
submitDownwardMessages(dmp: DownwardMessage[]): void;
|
|
58
|
+
submitHorizontalMessages(id: number, hrmp: HorizontalMessage[]): void;
|
|
59
|
+
newBlock(params?: Partial<BuildBlockParams>): Promise<Block>;
|
|
60
|
+
newBlockWithParams(params: BuildBlockParams): Promise<Block>;
|
|
61
|
+
upcomingBlocks(): Promise<number>;
|
|
62
|
+
dryRunExtrinsic(extrinsic: HexString | {
|
|
63
|
+
call: HexString;
|
|
64
|
+
address: string;
|
|
65
|
+
}, at?: HexString): Promise<{
|
|
66
|
+
outcome: ApplyExtrinsicResult;
|
|
67
|
+
storageDiff: [HexString, HexString | null][];
|
|
68
|
+
}>;
|
|
69
|
+
dryRunHrmp(hrmp: Record<number, HorizontalMessage[]>, at?: HexString): Promise<[HexString, HexString | null][]>;
|
|
70
|
+
dryRunDmp(dmp: DownwardMessage[], at?: HexString): Promise<[HexString, HexString | null][]>;
|
|
71
|
+
dryRunUmp(ump: Record<number, HexString[]>, at?: HexString): Promise<[HexString, HexString | null][]>;
|
|
72
|
+
getInherents(): Promise<HexString[]>;
|
|
73
|
+
}
|