@acala-network/chopsticks-core 0.8.3 → 0.8.5-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/blockchain/block-builder.js +0 -3
- package/lib/blockchain/block.d.ts +1 -1
- package/lib/blockchain/block.js +4 -4
- package/lib/blockchain/head-state.d.ts +1 -1
- package/lib/blockchain/head-state.js +2 -2
- package/lib/blockchain/index.d.ts +2 -2
- package/lib/blockchain/index.js +5 -2
- package/lib/blockchain/inherent/parachain/validation-data.js +5 -5
- package/lib/chopsticks-provider.d.ts +31 -0
- package/lib/chopsticks-provider.js +169 -0
- package/lib/genesis-provider.d.ts +1 -1
- package/lib/genesis-provider.js +3 -3
- package/lib/index.d.ts +3 -1
- package/lib/index.js +3 -1
- package/lib/rpc/index.d.ts +5 -0
- package/lib/rpc/index.js +26 -0
- package/lib/rpc/shared.d.ts +27 -0
- package/lib/rpc/shared.js +19 -0
- package/lib/rpc/substrate/author.d.ts +28 -0
- package/lib/rpc/substrate/author.js +101 -0
- package/lib/rpc/substrate/chain.d.ts +45 -0
- package/lib/rpc/substrate/chain.js +103 -0
- package/lib/rpc/substrate/index.d.ts +66 -0
- package/lib/rpc/substrate/index.js +38 -0
- package/lib/rpc/substrate/payment.d.ts +16 -0
- package/lib/rpc/substrate/payment.js +54 -0
- package/lib/rpc/substrate/state.d.ts +97 -0
- package/lib/rpc/substrate/state.js +188 -0
- package/lib/rpc/substrate/system.d.ts +28 -0
- package/lib/rpc/substrate/system.js +71 -0
- package/lib/utils/time-travel.js +2 -2
- package/lib/wasm-executor/browser-wasm-executor.mjs +37 -0
- package/lib/wasm-executor/browser-worker.d.ts +5 -0
- package/lib/wasm-executor/browser-worker.js +28 -0
- package/lib/{executor.d.ts → wasm-executor/index.d.ts} +19 -2
- package/lib/{executor.js → wasm-executor/index.js} +61 -18
- package/lib/wasm-executor/node-wasm-executor.mjs +34 -0
- package/lib/wasm-executor/node-worker.d.ts +5 -0
- package/lib/wasm-executor/node-worker.js +31 -0
- package/package.json +10 -4
|
@@ -132,9 +132,6 @@ const buildBlock = (head, inherents, extrinsics, ump, callbacks, unsafeBlockHeig
|
|
|
132
132
|
const registry = yield head.registry;
|
|
133
133
|
const header = yield (0, exports.newHeader)(head, unsafeBlockHeight);
|
|
134
134
|
const newBlockNumber = header.number.toNumber();
|
|
135
|
-
if (newBlockNumber < head.number) {
|
|
136
|
-
throw new Error('unsafeBlockHeight is not allowed to be less than current block number');
|
|
137
|
-
}
|
|
138
135
|
logger.info({
|
|
139
136
|
number: newBlockNumber,
|
|
140
137
|
extrinsicsCount: extrinsics.length,
|
|
@@ -4,7 +4,7 @@ import { TypeRegistry } from '@polkadot/types';
|
|
|
4
4
|
import type { HexString } from '@polkadot/util/types';
|
|
5
5
|
import { Blockchain } from '.';
|
|
6
6
|
import { StorageLayer, StorageLayerProvider, StorageValue } from './storage-layer';
|
|
7
|
-
import type { RuntimeVersion } from '../executor';
|
|
7
|
+
import type { RuntimeVersion } from '../wasm-executor';
|
|
8
8
|
export type TaskCallResponse = {
|
|
9
9
|
result: HexString;
|
|
10
10
|
storageDiff: [HexString, HexString | null][];
|
package/lib/blockchain/block.js
CHANGED
|
@@ -29,7 +29,7 @@ const util_2 = require("@polkadot/util");
|
|
|
29
29
|
const storage_layer_1 = require("./storage-layer");
|
|
30
30
|
const utils_1 = require("../utils");
|
|
31
31
|
const logger_1 = require("../logger");
|
|
32
|
-
const
|
|
32
|
+
const wasm_executor_1 = require("../wasm-executor");
|
|
33
33
|
/**
|
|
34
34
|
* Block class.
|
|
35
35
|
*
|
|
@@ -223,7 +223,7 @@ class Block {
|
|
|
223
223
|
}
|
|
224
224
|
get runtimeVersion() {
|
|
225
225
|
if (!__classPrivateFieldGet(this, _Block_runtimeVersion, "f")) {
|
|
226
|
-
__classPrivateFieldSet(this, _Block_runtimeVersion, this.wasm.then(
|
|
226
|
+
__classPrivateFieldSet(this, _Block_runtimeVersion, this.wasm.then(wasm_executor_1.getRuntimeVersion), "f");
|
|
227
227
|
}
|
|
228
228
|
return __classPrivateFieldGet(this, _Block_runtimeVersion, "f");
|
|
229
229
|
}
|
|
@@ -248,13 +248,13 @@ class Block {
|
|
|
248
248
|
call(method, args) {
|
|
249
249
|
return __awaiter(this, void 0, void 0, function* () {
|
|
250
250
|
const wasm = yield this.wasm;
|
|
251
|
-
const response = yield (0,
|
|
251
|
+
const response = yield (0, wasm_executor_1.runTask)({
|
|
252
252
|
wasm,
|
|
253
253
|
calls: [[method, args]],
|
|
254
254
|
mockSignatureHost: __classPrivateFieldGet(this, _Block_chain, "f").mockSignatureHost,
|
|
255
255
|
allowUnresolvedImports: __classPrivateFieldGet(this, _Block_chain, "f").allowUnresolvedImports,
|
|
256
256
|
runtimeLogLevel: __classPrivateFieldGet(this, _Block_chain, "f").runtimeLogLevel,
|
|
257
|
-
}, (0,
|
|
257
|
+
}, (0, wasm_executor_1.taskHandler)(this));
|
|
258
258
|
if (response.Call) {
|
|
259
259
|
for (const log of response.Call.runtimeLogs) {
|
|
260
260
|
logger_1.defaultLogger.info(`RuntimeLogs:\n${log}`);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Block } from './block';
|
|
2
|
-
type Callback = (block: Block, pairs: [string, string][]) => void | Promise<void>;
|
|
2
|
+
type Callback = (block: Block, pairs: [string, string | null][]) => void | Promise<void>;
|
|
3
3
|
export declare const randomId: () => string;
|
|
4
4
|
export declare class HeadState {
|
|
5
5
|
#private;
|
|
@@ -48,7 +48,7 @@ class HeadState {
|
|
|
48
48
|
const id = (0, exports.randomId)();
|
|
49
49
|
__classPrivateFieldGet(this, _HeadState_storageListeners, "f")[id] = [keys, cb];
|
|
50
50
|
for (const key of keys) {
|
|
51
|
-
__classPrivateFieldGet(this, _HeadState_oldValues, "f")[key] = yield __classPrivateFieldGet(this, _HeadState_head, "f").get(key);
|
|
51
|
+
__classPrivateFieldGet(this, _HeadState_oldValues, "f")[key] = yield __classPrivateFieldGet(this, _HeadState_head, "f").get(key).then((val) => val || null);
|
|
52
52
|
}
|
|
53
53
|
return id;
|
|
54
54
|
});
|
|
@@ -61,7 +61,7 @@ class HeadState {
|
|
|
61
61
|
const id = (0, exports.randomId)();
|
|
62
62
|
const codeKey = (0, util_1.stringToHex)(':code');
|
|
63
63
|
__classPrivateFieldGet(this, _HeadState_storageListeners, "f")[id] = [[codeKey], cb];
|
|
64
|
-
__classPrivateFieldGet(this, _HeadState_oldValues, "f")[codeKey] = yield __classPrivateFieldGet(this, _HeadState_head, "f").get(codeKey);
|
|
64
|
+
__classPrivateFieldGet(this, _HeadState_oldValues, "f")[codeKey] = yield __classPrivateFieldGet(this, _HeadState_head, "f").get(codeKey).then((val) => val || null);
|
|
65
65
|
return id;
|
|
66
66
|
});
|
|
67
67
|
}
|
|
@@ -92,7 +92,7 @@ export declare class Blockchain {
|
|
|
92
92
|
/**
|
|
93
93
|
* Get block by number.
|
|
94
94
|
*/
|
|
95
|
-
getBlockAt(number?: number): Promise<Block | undefined>;
|
|
95
|
+
getBlockAt(number?: number | null): Promise<Block | undefined>;
|
|
96
96
|
/**
|
|
97
97
|
* Get block by hash.
|
|
98
98
|
*/
|
|
@@ -163,7 +163,7 @@ export declare class Blockchain {
|
|
|
163
163
|
*/
|
|
164
164
|
getInherents(): Promise<HexString[]>;
|
|
165
165
|
/**
|
|
166
|
-
* Close the db.
|
|
166
|
+
* Close the db and release worker.
|
|
167
167
|
*/
|
|
168
168
|
close(): Promise<void>;
|
|
169
169
|
}
|
package/lib/blockchain/index.js
CHANGED
|
@@ -32,6 +32,7 @@ const offchain_1 = require("../offchain");
|
|
|
32
32
|
const utils_1 = require("../utils");
|
|
33
33
|
const logger_1 = require("../logger");
|
|
34
34
|
const block_builder_1 = require("./block-builder");
|
|
35
|
+
const wasm_executor_1 = require("../wasm-executor");
|
|
35
36
|
const logger = logger_1.defaultLogger.child({ name: 'blockchain' });
|
|
36
37
|
/**
|
|
37
38
|
* Local blockchain which provides access to blocks, txpool and methods
|
|
@@ -160,7 +161,7 @@ class Blockchain {
|
|
|
160
161
|
*/
|
|
161
162
|
getBlockAt(number) {
|
|
162
163
|
return __awaiter(this, void 0, void 0, function* () {
|
|
163
|
-
if (number === undefined) {
|
|
164
|
+
if (number === null || number === undefined) {
|
|
164
165
|
return this.head;
|
|
165
166
|
}
|
|
166
167
|
if (number > __classPrivateFieldGet(this, _Blockchain_head, "f").number) {
|
|
@@ -445,11 +446,13 @@ class Blockchain {
|
|
|
445
446
|
});
|
|
446
447
|
}
|
|
447
448
|
/**
|
|
448
|
-
* Close the db.
|
|
449
|
+
* Close the db and release worker.
|
|
449
450
|
*/
|
|
450
451
|
close() {
|
|
451
452
|
var _a;
|
|
452
453
|
return __awaiter(this, void 0, void 0, function* () {
|
|
454
|
+
yield (0, wasm_executor_1.releaseWorker)();
|
|
455
|
+
yield this.api.disconnect();
|
|
453
456
|
yield ((_a = this.db) === null || _a === void 0 ? void 0 : _a.destroy());
|
|
454
457
|
});
|
|
455
458
|
}
|
|
@@ -19,7 +19,7 @@ const lodash_1 = __importDefault(require("lodash"));
|
|
|
19
19
|
const proof_1 = require("../../../utils/proof");
|
|
20
20
|
const util_crypto_1 = require("@polkadot/util-crypto");
|
|
21
21
|
const utils_1 = require("../../../utils");
|
|
22
|
-
const
|
|
22
|
+
const wasm_executor_1 = require("../../../wasm-executor");
|
|
23
23
|
const MOCK_VALIDATION_DATA = {
|
|
24
24
|
validationData: {
|
|
25
25
|
relayParentNumber: 1000,
|
|
@@ -78,7 +78,7 @@ class SetValidationData {
|
|
|
78
78
|
const hrmpIngressChannelIndexKey = (0, proof_1.hrmpIngressChannelIndex)(paraId);
|
|
79
79
|
const hrmpEgressChannelIndexKey = (0, proof_1.hrmpEgressChannelIndex)(paraId);
|
|
80
80
|
// TODO: refactor this to have a single decodeProof
|
|
81
|
-
const decoded = yield (0,
|
|
81
|
+
const decoded = yield (0, wasm_executor_1.decodeProof)(extrinsic.validationData.relayParentStorageRoot, [...Object.values(proof_1.WELL_KNOWN_KEYS), dmqMqcHeadKey, hrmpIngressChannelIndexKey, hrmpEgressChannelIndexKey], extrinsic.relayChainState.trieNodes);
|
|
82
82
|
for (const key of Object.values(proof_1.WELL_KNOWN_KEYS)) {
|
|
83
83
|
newEntries.push([key, decoded[key]]);
|
|
84
84
|
}
|
|
@@ -113,7 +113,7 @@ class SetValidationData {
|
|
|
113
113
|
receiver: paraId.toNumber(),
|
|
114
114
|
});
|
|
115
115
|
const hrmpChannelKey = (0, proof_1.hrmpChannels)(channelId);
|
|
116
|
-
const decoded = yield (0,
|
|
116
|
+
const decoded = yield (0, wasm_executor_1.decodeProof)(extrinsic.validationData.relayParentStorageRoot, [hrmpChannelKey], extrinsic.relayChainState.trieNodes);
|
|
117
117
|
const abridgedHrmpRaw = decoded[hrmpChannelKey];
|
|
118
118
|
if (!abridgedHrmpRaw)
|
|
119
119
|
throw new Error('Canoot find hrmp channels from validation data');
|
|
@@ -144,7 +144,7 @@ class SetValidationData {
|
|
|
144
144
|
receiver,
|
|
145
145
|
});
|
|
146
146
|
const hrmpChannelKey = (0, proof_1.hrmpChannels)(channelId);
|
|
147
|
-
const decoded = yield (0,
|
|
147
|
+
const decoded = yield (0, wasm_executor_1.decodeProof)(extrinsic.validationData.relayParentStorageRoot, [hrmpChannelKey], extrinsic.relayChainState.trieNodes);
|
|
148
148
|
newEntries.push([hrmpChannelKey, decoded[hrmpChannelKey]]);
|
|
149
149
|
}
|
|
150
150
|
const upgradeKey = (0, proof_1.upgradeGoAheadSignal)(paraId);
|
|
@@ -158,7 +158,7 @@ class SetValidationData {
|
|
|
158
158
|
// make sure previous goAhead is removed
|
|
159
159
|
newEntries.push([upgradeKey, null]);
|
|
160
160
|
}
|
|
161
|
-
const { trieRootHash, nodes } = yield (0,
|
|
161
|
+
const { trieRootHash, nodes } = yield (0, wasm_executor_1.createProof)(extrinsic.relayChainState.trieNodes, newEntries);
|
|
162
162
|
newData = Object.assign(Object.assign({}, extrinsic), { downwardMessages,
|
|
163
163
|
horizontalMessages, validationData: Object.assign(Object.assign({}, extrinsic.validationData), { relayParentStorageRoot: trieRootHash, relayParentNumber: extrinsic.validationData.relayParentNumber + 2 }), relayChainState: {
|
|
164
164
|
trieNodes: nodes,
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ProviderInterface, ProviderInterfaceCallback, ProviderInterfaceEmitCb, ProviderInterfaceEmitted } from '@polkadot/rpc-provider/types';
|
|
2
|
+
import { Blockchain } from './blockchain';
|
|
3
|
+
interface SubscriptionHandler {
|
|
4
|
+
callback: ProviderInterfaceCallback;
|
|
5
|
+
type: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Provider for local chopsticks chain
|
|
9
|
+
*/
|
|
10
|
+
export declare class ChopsticksProvider implements ProviderInterface {
|
|
11
|
+
#private;
|
|
12
|
+
readonly chain: Blockchain;
|
|
13
|
+
constructor(chain: Blockchain);
|
|
14
|
+
static fromEndpoint: (endpoint: string, block?: number | string | null, cache?: string) => Promise<ChopsticksProvider>;
|
|
15
|
+
get hasSubscriptions(): boolean;
|
|
16
|
+
get isClonable(): boolean;
|
|
17
|
+
get isConnected(): boolean;
|
|
18
|
+
get isReady(): Promise<void>;
|
|
19
|
+
clone: () => ChopsticksProvider;
|
|
20
|
+
connect: () => Promise<void>;
|
|
21
|
+
disconnect: () => Promise<void>;
|
|
22
|
+
on: (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb) => (() => void);
|
|
23
|
+
subscriptionManager: {
|
|
24
|
+
subscribe: (method: string, subid: string, onCancel?: () => void) => (data: any) => void;
|
|
25
|
+
unsubscribe: (subid: string) => void;
|
|
26
|
+
};
|
|
27
|
+
send: <T>(method: string, params: unknown[], _isCacheable?: boolean, subscription?: SubscriptionHandler) => Promise<T>;
|
|
28
|
+
subscribe(type: string, method: string, params: unknown[], callback: ProviderInterfaceCallback): Promise<number | string>;
|
|
29
|
+
unsubscribe(_type: string, method: string, id: number | string): Promise<boolean>;
|
|
30
|
+
}
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
12
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
13
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
14
|
+
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");
|
|
15
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
16
|
+
};
|
|
17
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
18
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
19
|
+
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");
|
|
20
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
21
|
+
};
|
|
22
|
+
var _a, _ChopsticksProvider_isConnected, _ChopsticksProvider_eventemitter, _ChopsticksProvider_isReadyPromise, _ChopsticksProvider_subscriptions;
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.ChopsticksProvider = void 0;
|
|
25
|
+
const eventemitter3_1 = require("eventemitter3");
|
|
26
|
+
const rpc_1 = require("./rpc");
|
|
27
|
+
const logger_1 = require("./logger");
|
|
28
|
+
const setup_1 = require("./setup");
|
|
29
|
+
const providerHandlers = Object.assign(Object.assign({}, rpc_1.allHandlers), { dev_newBlock: (context, _params, _subscriptionManager) => __awaiter(void 0, void 0, void 0, function* () {
|
|
30
|
+
const block = yield context.chain.newBlock();
|
|
31
|
+
return block.hash;
|
|
32
|
+
}) });
|
|
33
|
+
const logger = logger_1.defaultLogger.child({ name: '[Chopsticks provider]' });
|
|
34
|
+
/**
|
|
35
|
+
* Provider for local chopsticks chain
|
|
36
|
+
*/
|
|
37
|
+
class ChopsticksProvider {
|
|
38
|
+
constructor(chain) {
|
|
39
|
+
this.chain = chain;
|
|
40
|
+
_ChopsticksProvider_isConnected.set(this, false);
|
|
41
|
+
_ChopsticksProvider_eventemitter.set(this, void 0);
|
|
42
|
+
_ChopsticksProvider_isReadyPromise.set(this, void 0);
|
|
43
|
+
_ChopsticksProvider_subscriptions.set(this, {});
|
|
44
|
+
this.clone = () => {
|
|
45
|
+
return new ChopsticksProvider(this.chain);
|
|
46
|
+
};
|
|
47
|
+
this.connect = () => __awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
__classPrivateFieldSet(this, _ChopsticksProvider_isConnected, true, "f");
|
|
49
|
+
__classPrivateFieldGet(this, _ChopsticksProvider_eventemitter, "f").emit('connected');
|
|
50
|
+
});
|
|
51
|
+
this.disconnect = () => __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
__classPrivateFieldSet(this, _ChopsticksProvider_isConnected, false, "f");
|
|
53
|
+
__classPrivateFieldGet(this, _ChopsticksProvider_eventemitter, "f").emit('disconnected');
|
|
54
|
+
});
|
|
55
|
+
this.on = (type, sub) => {
|
|
56
|
+
__classPrivateFieldGet(this, _ChopsticksProvider_eventemitter, "f").on(type, sub);
|
|
57
|
+
return () => {
|
|
58
|
+
__classPrivateFieldGet(this, _ChopsticksProvider_eventemitter, "f").removeListener(type, sub);
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
this.subscriptionManager = {
|
|
62
|
+
subscribe: (method, subid, onCancel = () => { }) => {
|
|
63
|
+
const sub = __classPrivateFieldGet(this, _ChopsticksProvider_subscriptions, "f")[subid];
|
|
64
|
+
if (sub) {
|
|
65
|
+
sub.onCancel = onCancel;
|
|
66
|
+
}
|
|
67
|
+
return (data) => {
|
|
68
|
+
logger.debug('subscribe-callback', method, subid, data);
|
|
69
|
+
const sub = __classPrivateFieldGet(this, _ChopsticksProvider_subscriptions, "f")[subid];
|
|
70
|
+
if (sub) {
|
|
71
|
+
sub.callback(null, data);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
logger.trace(`Unable to find active subscription=${subid}`);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
unsubscribe: (subid) => {
|
|
79
|
+
var _b;
|
|
80
|
+
logger.debug('unsubscribe-callback', subid);
|
|
81
|
+
const sub = __classPrivateFieldGet(this, _ChopsticksProvider_subscriptions, "f")[subid];
|
|
82
|
+
if (sub) {
|
|
83
|
+
(_b = sub.onCancel) === null || _b === void 0 ? void 0 : _b.call(sub);
|
|
84
|
+
delete __classPrivateFieldGet(this, _ChopsticksProvider_subscriptions, "f")[subid];
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
logger.trace(`Unable to find active subscription=${subid}`);
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
this.send = (method, params, _isCacheable, subscription) => __awaiter(this, void 0, void 0, function* () {
|
|
92
|
+
try {
|
|
93
|
+
logger.debug('send', { method, params });
|
|
94
|
+
const rpcHandler = providerHandlers[method];
|
|
95
|
+
if (!rpcHandler) {
|
|
96
|
+
logger.error(`Unable to find rpc handler=${method}`);
|
|
97
|
+
throw new Error(`Unable to find rpc handler=${method}`);
|
|
98
|
+
}
|
|
99
|
+
if (subscription) {
|
|
100
|
+
logger.debug('subscribe', { method, params });
|
|
101
|
+
const subid = yield rpcHandler({ chain: this.chain }, params, this.subscriptionManager);
|
|
102
|
+
if (!subid) {
|
|
103
|
+
throw new Error(`Unable to subscribe=${method}`);
|
|
104
|
+
}
|
|
105
|
+
__classPrivateFieldGet(this, _ChopsticksProvider_subscriptions, "f")[subid] = {
|
|
106
|
+
callback: subscription.callback,
|
|
107
|
+
method,
|
|
108
|
+
params,
|
|
109
|
+
type: subscription.type,
|
|
110
|
+
};
|
|
111
|
+
return subid;
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
logger.debug('call', { method, params });
|
|
115
|
+
return rpcHandler({ chain: this.chain }, params, this.subscriptionManager);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
catch (e) {
|
|
119
|
+
logger.error('send error.', e);
|
|
120
|
+
throw e;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
__classPrivateFieldSet(this, _ChopsticksProvider_eventemitter, new eventemitter3_1.EventEmitter(), "f");
|
|
124
|
+
__classPrivateFieldSet(this, _ChopsticksProvider_isReadyPromise, new Promise((resolve, reject) => {
|
|
125
|
+
__classPrivateFieldGet(this, _ChopsticksProvider_eventemitter, "f").once('connected', resolve);
|
|
126
|
+
__classPrivateFieldGet(this, _ChopsticksProvider_eventemitter, "f").once('error', reject);
|
|
127
|
+
this.connect();
|
|
128
|
+
}), "f");
|
|
129
|
+
}
|
|
130
|
+
get hasSubscriptions() {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
get isClonable() {
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
get isConnected() {
|
|
137
|
+
return __classPrivateFieldGet(this, _ChopsticksProvider_isConnected, "f");
|
|
138
|
+
}
|
|
139
|
+
get isReady() {
|
|
140
|
+
return __classPrivateFieldGet(this, _ChopsticksProvider_isReadyPromise, "f");
|
|
141
|
+
}
|
|
142
|
+
subscribe(type, method, params, callback) {
|
|
143
|
+
return this.send(method, params, false, { callback, type });
|
|
144
|
+
}
|
|
145
|
+
unsubscribe(_type, method, id) {
|
|
146
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
147
|
+
if (!__classPrivateFieldGet(this, _ChopsticksProvider_subscriptions, "f")[id]) {
|
|
148
|
+
logger.trace(`Unable to find active subscription=${id}`);
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
return this.send(method, [id]);
|
|
153
|
+
}
|
|
154
|
+
catch (_b) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
exports.ChopsticksProvider = ChopsticksProvider;
|
|
161
|
+
_a = ChopsticksProvider, _ChopsticksProvider_isConnected = new WeakMap(), _ChopsticksProvider_eventemitter = new WeakMap(), _ChopsticksProvider_isReadyPromise = new WeakMap(), _ChopsticksProvider_subscriptions = new WeakMap();
|
|
162
|
+
ChopsticksProvider.fromEndpoint = (endpoint, block, cache) => __awaiter(void 0, void 0, void 0, function* () {
|
|
163
|
+
return new ChopsticksProvider(yield (0, setup_1.setup)({
|
|
164
|
+
endpoint,
|
|
165
|
+
mockSignatureHost: true,
|
|
166
|
+
block,
|
|
167
|
+
db: cache,
|
|
168
|
+
}));
|
|
169
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { HexString } from '@polkadot/util/types';
|
|
2
2
|
import { ProviderInterface, ProviderInterfaceCallback, ProviderInterfaceEmitCb, ProviderInterfaceEmitted, ProviderStats } from '@polkadot/rpc-provider/types';
|
|
3
3
|
import { Genesis } from './schema';
|
|
4
|
-
import { JsCallback } from './executor';
|
|
4
|
+
import { JsCallback } from './wasm-executor';
|
|
5
5
|
export declare class GenesisProvider implements ProviderInterface {
|
|
6
6
|
#private;
|
|
7
7
|
readonly stats?: ProviderStats;
|
package/lib/genesis-provider.js
CHANGED
|
@@ -51,7 +51,7 @@ exports.GenesisProvider = void 0;
|
|
|
51
51
|
const eventemitter3_1 = require("eventemitter3");
|
|
52
52
|
const axios_1 = __importDefault(require("axios"));
|
|
53
53
|
const schema_1 = require("./schema");
|
|
54
|
-
const
|
|
54
|
+
const wasm_executor_1 = require("./wasm-executor");
|
|
55
55
|
const utils_1 = require("./utils");
|
|
56
56
|
class GenesisProvider {
|
|
57
57
|
constructor(genesis) {
|
|
@@ -127,7 +127,7 @@ class GenesisProvider {
|
|
|
127
127
|
throw Error('unimplemented');
|
|
128
128
|
});
|
|
129
129
|
__classPrivateFieldSet(this, _GenesisProvider_genesis, genesis, "f");
|
|
130
|
-
__classPrivateFieldSet(this, _GenesisProvider_stateRoot, (0,
|
|
130
|
+
__classPrivateFieldSet(this, _GenesisProvider_stateRoot, (0, wasm_executor_1.calculateStateRoot)(Object.entries(__classPrivateFieldGet(this, _GenesisProvider_genesis, "f").genesis.raw.top).reduce((accu, item) => {
|
|
131
131
|
accu.push(item);
|
|
132
132
|
return accu;
|
|
133
133
|
}, []), 1), "f");
|
|
@@ -157,7 +157,7 @@ class GenesisProvider {
|
|
|
157
157
|
}
|
|
158
158
|
get _jsCallback() {
|
|
159
159
|
const storage = __classPrivateFieldGet(this, _GenesisProvider_genesis, "f").genesis.raw.top;
|
|
160
|
-
return Object.assign(Object.assign({},
|
|
160
|
+
return Object.assign(Object.assign({}, wasm_executor_1.emptyTaskHandler), { getStorage: function (key) {
|
|
161
161
|
return __awaiter(this, void 0, void 0, function* () {
|
|
162
162
|
return storage[key];
|
|
163
163
|
});
|
package/lib/index.d.ts
CHANGED
|
@@ -15,10 +15,12 @@ export * from './blockchain/txpool';
|
|
|
15
15
|
export * from './blockchain/storage-layer';
|
|
16
16
|
export * from './blockchain/head-state';
|
|
17
17
|
export * from './utils';
|
|
18
|
-
export * from './executor';
|
|
18
|
+
export * from './wasm-executor';
|
|
19
19
|
export * from './schema';
|
|
20
20
|
export * from './xcm';
|
|
21
21
|
export * from './setup';
|
|
22
22
|
export * from './blockchain/inherent';
|
|
23
23
|
export * from './logger';
|
|
24
24
|
export * from './offchain';
|
|
25
|
+
export * from './chopsticks-provider';
|
|
26
|
+
export * from './rpc';
|
package/lib/index.js
CHANGED
|
@@ -31,10 +31,12 @@ __exportStar(require("./blockchain/txpool"), exports);
|
|
|
31
31
|
__exportStar(require("./blockchain/storage-layer"), exports);
|
|
32
32
|
__exportStar(require("./blockchain/head-state"), exports);
|
|
33
33
|
__exportStar(require("./utils"), exports);
|
|
34
|
-
__exportStar(require("./executor"), exports);
|
|
34
|
+
__exportStar(require("./wasm-executor"), exports);
|
|
35
35
|
__exportStar(require("./schema"), exports);
|
|
36
36
|
__exportStar(require("./xcm"), exports);
|
|
37
37
|
__exportStar(require("./setup"), exports);
|
|
38
38
|
__exportStar(require("./blockchain/inherent"), exports);
|
|
39
39
|
__exportStar(require("./logger"), exports);
|
|
40
40
|
__exportStar(require("./offchain"), exports);
|
|
41
|
+
__exportStar(require("./chopsticks-provider"), exports);
|
|
42
|
+
__exportStar(require("./rpc"), exports);
|
package/lib/rpc/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.ResponseError = exports.substrate = exports.allHandlers = void 0;
|
|
16
|
+
const substrate_1 = __importDefault(require("./substrate"));
|
|
17
|
+
exports.allHandlers = Object.assign(Object.assign({}, substrate_1.default), { rpc_methods: () => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
|
+
return Promise.resolve({
|
|
19
|
+
version: 1,
|
|
20
|
+
methods: [...Object.keys(exports.allHandlers)],
|
|
21
|
+
});
|
|
22
|
+
}) });
|
|
23
|
+
var substrate_2 = require("./substrate");
|
|
24
|
+
Object.defineProperty(exports, "substrate", { enumerable: true, get: function () { return __importDefault(substrate_2).default; } });
|
|
25
|
+
var shared_1 = require("./shared");
|
|
26
|
+
Object.defineProperty(exports, "ResponseError", { enumerable: true, get: function () { return shared_1.ResponseError; } });
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Blockchain } from '@acala-network/chopsticks-core';
|
|
2
|
+
export declare const logger: import("pino").default.Logger<{
|
|
3
|
+
level: string;
|
|
4
|
+
transport: {
|
|
5
|
+
target: string;
|
|
6
|
+
};
|
|
7
|
+
}>;
|
|
8
|
+
export declare class ResponseError extends Error {
|
|
9
|
+
code: number;
|
|
10
|
+
constructor(code: number, message: string);
|
|
11
|
+
toJSON(): {
|
|
12
|
+
code: number;
|
|
13
|
+
message: string;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export interface Context {
|
|
17
|
+
/**
|
|
18
|
+
* The blockchain instance
|
|
19
|
+
*/
|
|
20
|
+
chain: Blockchain;
|
|
21
|
+
}
|
|
22
|
+
export interface SubscriptionManager {
|
|
23
|
+
subscribe: (method: string, subid: string, onCancel?: () => void) => (data: any) => void;
|
|
24
|
+
unsubscribe: (subid: string) => void;
|
|
25
|
+
}
|
|
26
|
+
export type Handler<TParams = any, TReturn = any> = (context: Context, params: TParams, subscriptionManager: SubscriptionManager) => Promise<TReturn>;
|
|
27
|
+
export type Handlers = Record<string, Handler>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ResponseError = exports.logger = void 0;
|
|
4
|
+
const logger_1 = require("../logger");
|
|
5
|
+
exports.logger = logger_1.defaultLogger.child({ name: 'rpc' });
|
|
6
|
+
class ResponseError extends Error {
|
|
7
|
+
constructor(code, message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = 'ResponseError';
|
|
10
|
+
this.code = code;
|
|
11
|
+
}
|
|
12
|
+
toJSON() {
|
|
13
|
+
return {
|
|
14
|
+
code: this.code,
|
|
15
|
+
message: this.message,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.ResponseError = ResponseError;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { HexString } from '@polkadot/util/types';
|
|
2
|
+
import { Handler } from '../shared';
|
|
3
|
+
/**
|
|
4
|
+
* @param context
|
|
5
|
+
* @param params - [`extrinsic`]
|
|
6
|
+
*
|
|
7
|
+
* @return Hash
|
|
8
|
+
*/
|
|
9
|
+
export declare const author_submitExtrinsic: Handler<[HexString], HexString>;
|
|
10
|
+
/**
|
|
11
|
+
* @param context
|
|
12
|
+
* @param params - [`extrinsic`]
|
|
13
|
+
* @param subscriptionManager
|
|
14
|
+
*
|
|
15
|
+
* @return subscription id
|
|
16
|
+
*/
|
|
17
|
+
export declare const author_submitAndWatchExtrinsic: Handler<[HexString], string>;
|
|
18
|
+
/**
|
|
19
|
+
* @param _context
|
|
20
|
+
* @param params - [`subid`]
|
|
21
|
+
*/
|
|
22
|
+
export declare const author_unwatchExtrinsic: Handler<[string], void>;
|
|
23
|
+
/**
|
|
24
|
+
* Get pending extrinsics
|
|
25
|
+
*
|
|
26
|
+
* @return Array of pending extrinsics
|
|
27
|
+
*/
|
|
28
|
+
export declare const author_pendingExtrinsics: Handler<void, HexString[]>;
|