@acala-network/chopsticks 0.5.3 → 0.5.5
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.js +1 -5
- package/lib/blockchain/block-builder.js +36 -43
- package/lib/blockchain/block.js +24 -28
- package/lib/blockchain/head-state.js +9 -14
- package/lib/blockchain/index.js +24 -28
- package/lib/blockchain/inherent/index.js +11 -20
- package/lib/blockchain/inherent/para-enter.js +3 -7
- package/lib/blockchain/inherent/parachain/babe-randomness.js +3 -7
- package/lib/blockchain/inherent/parachain/nimbus-author-inherent.js +3 -7
- package/lib/blockchain/inherent/parachain/validation-data.js +26 -33
- package/lib/blockchain/storage-layer.js +8 -16
- package/lib/blockchain/txpool.js +13 -20
- package/lib/cli.js +30 -35
- package/lib/db/entities.js +6 -9
- package/lib/db/index.js +5 -32
- package/lib/dry-run-preimage.js +23 -27
- package/lib/dry-run.js +12 -16
- package/lib/executor.js +24 -36
- package/lib/genesis-provider.js +17 -24
- package/lib/index.js +7 -31
- package/lib/logger.js +3 -10
- package/lib/rpc/dev/dry-run.js +21 -28
- package/lib/rpc/dev/index.js +14 -16
- package/lib/rpc/index.js +9 -16
- package/lib/rpc/shared.js +3 -7
- package/lib/rpc/substrate/author.js +8 -10
- package/lib/rpc/substrate/chain.js +5 -7
- package/lib/rpc/substrate/index.js +11 -16
- package/lib/rpc/substrate/payment.js +7 -9
- package/lib/rpc/substrate/state.js +5 -7
- package/lib/rpc/substrate/system.js +6 -11
- package/lib/run-block.js +12 -16
- package/lib/schema/index.js +22 -25
- package/lib/server.js +8 -35
- package/lib/setup-with-server.js +8 -12
- package/lib/setup.js +26 -30
- package/lib/utils/decoder.js +16 -25
- package/lib/utils/generate-html-diff.js +12 -20
- package/lib/utils/import-storage.js +11 -19
- package/lib/utils/index.js +10 -19
- package/lib/utils/open-html.js +3 -7
- package/lib/utils/proof.js +9 -17
- package/lib/utils/set-storage.js +10 -14
- package/lib/utils/time-travel.js +21 -28
- package/lib/xcm/downward.js +11 -15
- package/lib/xcm/horizontal.js +7 -11
- package/lib/xcm/index.js +14 -19
- package/lib/xcm/upward.js +6 -10
- package/package.json +2 -2
package/lib/executor.js
CHANGED
|
@@ -1,52 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const lodash_1 = __importDefault(require("lodash"));
|
|
11
|
-
const logger = logger_1.defaultLogger.child({ name: 'executor' });
|
|
12
|
-
const getRuntimeVersion = async (code) => {
|
|
13
|
-
return (0, chopsticks_executor_1.get_runtime_version)(code).then((version) => {
|
|
14
|
-
version.specName = (0, util_1.hexToString)(version.specName);
|
|
15
|
-
version.implName = (0, util_1.hexToString)(version.implName);
|
|
1
|
+
import { hexToString, hexToU8a } from '@polkadot/util';
|
|
2
|
+
import { calculate_state_root, create_proof, decode_proof, get_runtime_version, run_task, } from '@acala-network/chopsticks-executor';
|
|
3
|
+
import { defaultLogger, truncate } from './logger';
|
|
4
|
+
import _ from 'lodash';
|
|
5
|
+
const logger = defaultLogger.child({ name: 'executor' });
|
|
6
|
+
export const getRuntimeVersion = async (code) => {
|
|
7
|
+
return get_runtime_version(code).then((version) => {
|
|
8
|
+
version.specName = hexToString(version.specName);
|
|
9
|
+
version.implName = hexToString(version.implName);
|
|
16
10
|
return version;
|
|
17
11
|
});
|
|
18
12
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return (0, chopsticks_executor_1.calculate_state_root)(entries);
|
|
13
|
+
export const calculateStateRoot = async (entries) => {
|
|
14
|
+
return calculate_state_root(entries);
|
|
22
15
|
};
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
const decoded = await (0, chopsticks_executor_1.decode_proof)(trieRootHash, keys, nodes);
|
|
16
|
+
export const decodeProof = async (trieRootHash, keys, nodes) => {
|
|
17
|
+
const decoded = await decode_proof(trieRootHash, keys, nodes);
|
|
26
18
|
return decoded.reduce((accum, [key, value]) => {
|
|
27
19
|
accum[key] = value;
|
|
28
20
|
return accum;
|
|
29
21
|
}, {});
|
|
30
22
|
};
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
const result = await (0, chopsticks_executor_1.create_proof)(trieRootHash, nodes, entries);
|
|
23
|
+
export const createProof = async (trieRootHash, nodes, entries) => {
|
|
24
|
+
const result = await create_proof(trieRootHash, nodes, entries);
|
|
34
25
|
return { trieRootHash: result[0], nodes: result[1] };
|
|
35
26
|
};
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const response = await (0, chopsticks_executor_1.run_task)(task, callback);
|
|
27
|
+
export const runTask = async (task, callback = emptyTaskHandler) => {
|
|
28
|
+
logger.trace(truncate(task), 'taskRun');
|
|
29
|
+
const response = await run_task(task, callback);
|
|
40
30
|
if (response.Call) {
|
|
41
|
-
logger.trace(
|
|
31
|
+
logger.trace(truncate(response.Call), 'taskResponse');
|
|
42
32
|
}
|
|
43
33
|
else {
|
|
44
34
|
logger.trace({ response }, 'taskResponse');
|
|
45
35
|
}
|
|
46
36
|
return response;
|
|
47
37
|
};
|
|
48
|
-
|
|
49
|
-
const taskHandler = (block) => {
|
|
38
|
+
export const taskHandler = (block) => {
|
|
50
39
|
return {
|
|
51
40
|
getStorage: async function (key) {
|
|
52
41
|
return block.get(key);
|
|
@@ -60,8 +49,7 @@ const taskHandler = (block) => {
|
|
|
60
49
|
},
|
|
61
50
|
};
|
|
62
51
|
};
|
|
63
|
-
|
|
64
|
-
exports.emptyTaskHandler = {
|
|
52
|
+
export const emptyTaskHandler = {
|
|
65
53
|
getStorage: async function (_key) {
|
|
66
54
|
throw new Error('Method not implemented');
|
|
67
55
|
},
|
|
@@ -72,8 +60,8 @@ exports.emptyTaskHandler = {
|
|
|
72
60
|
throw new Error('Method not implemented');
|
|
73
61
|
},
|
|
74
62
|
};
|
|
75
|
-
|
|
76
|
-
const result = await
|
|
63
|
+
export const getAuraSlotDuration = _.memoize(async (wasm, registry) => {
|
|
64
|
+
const result = await runTask({
|
|
77
65
|
wasm,
|
|
78
66
|
calls: [['AuraApi_slot_duration', []]],
|
|
79
67
|
storage: [],
|
|
@@ -82,6 +70,6 @@ exports.getAuraSlotDuration = lodash_1.default.memoize(async (wasm, registry) =>
|
|
|
82
70
|
});
|
|
83
71
|
if (!result.Call)
|
|
84
72
|
throw new Error(result.Error);
|
|
85
|
-
const slotDuration = registry.createType('u64',
|
|
73
|
+
const slotDuration = registry.createType('u64', hexToU8a(result.Call.result)).toNumber();
|
|
86
74
|
return slotDuration;
|
|
87
75
|
});
|
package/lib/genesis-provider.js
CHANGED
|
@@ -1,17 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const util_1 = require("@polkadot/util");
|
|
10
|
-
const axios_1 = __importDefault(require("axios"));
|
|
11
|
-
const schema_1 = require("./schema");
|
|
12
|
-
const executor_1 = require("./executor");
|
|
13
|
-
const utils_1 = require("./utils");
|
|
14
|
-
class GenesisProvider {
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import { lstatSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { stringToHex } from '@polkadot/util';
|
|
4
|
+
import axios from 'axios';
|
|
5
|
+
import { genesisSchema } from './schema';
|
|
6
|
+
import { calculateStateRoot, runTask } from './executor';
|
|
7
|
+
import { isUrl } from './utils';
|
|
8
|
+
export class GenesisProvider {
|
|
15
9
|
#isConnected = false;
|
|
16
10
|
stats;
|
|
17
11
|
#eventemitter;
|
|
@@ -20,11 +14,11 @@ class GenesisProvider {
|
|
|
20
14
|
#stateRoot;
|
|
21
15
|
constructor(genesis) {
|
|
22
16
|
this.#genesis = genesis;
|
|
23
|
-
this.#stateRoot =
|
|
17
|
+
this.#stateRoot = calculateStateRoot(Object.entries(this.#genesis.genesis.raw.top).reduce((accu, item) => {
|
|
24
18
|
accu.push(item);
|
|
25
19
|
return accu;
|
|
26
20
|
}, []));
|
|
27
|
-
this.#eventemitter = new
|
|
21
|
+
this.#eventemitter = new EventEmitter();
|
|
28
22
|
this.#isReadyPromise = new Promise((resolve, reject) => {
|
|
29
23
|
this.#eventemitter.once('connected', () => {
|
|
30
24
|
resolve();
|
|
@@ -34,16 +28,16 @@ class GenesisProvider {
|
|
|
34
28
|
}
|
|
35
29
|
static fromUrl = async (url) => {
|
|
36
30
|
let file;
|
|
37
|
-
if (
|
|
38
|
-
file = await
|
|
31
|
+
if (isUrl(url)) {
|
|
32
|
+
file = await axios.get(url).then((x) => x.data);
|
|
39
33
|
}
|
|
40
|
-
else if (
|
|
41
|
-
file = JSON.parse(String(
|
|
34
|
+
else if (lstatSync(url).isFile()) {
|
|
35
|
+
file = JSON.parse(String(readFileSync(url)));
|
|
42
36
|
}
|
|
43
37
|
else {
|
|
44
38
|
throw Error(`invalid genesis path or url ${url}`);
|
|
45
39
|
}
|
|
46
|
-
return new GenesisProvider(
|
|
40
|
+
return new GenesisProvider(genesisSchema.parse(file));
|
|
47
41
|
};
|
|
48
42
|
get isClonable() {
|
|
49
43
|
return true;
|
|
@@ -120,8 +114,8 @@ class GenesisProvider {
|
|
|
120
114
|
case 'system_name':
|
|
121
115
|
return this.#genesis.name;
|
|
122
116
|
case 'state_getMetadata': {
|
|
123
|
-
const code = this.#genesis.genesis.raw.top[
|
|
124
|
-
return
|
|
117
|
+
const code = this.#genesis.genesis.raw.top[stringToHex(':code')];
|
|
118
|
+
return runTask({
|
|
125
119
|
wasm: code,
|
|
126
120
|
calls: [['Metadata_metadata', []]],
|
|
127
121
|
storage: [],
|
|
@@ -151,4 +145,3 @@ class GenesisProvider {
|
|
|
151
145
|
throw Error('unimplemented');
|
|
152
146
|
};
|
|
153
147
|
}
|
|
154
|
-
exports.GenesisProvider = GenesisProvider;
|
package/lib/index.js
CHANGED
|
@@ -1,31 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.setupWithServer = exports.setup = exports.connectVertical = exports.connectParachains = exports.BuildBlockMode = exports.Blockchain = exports.Api = void 0;
|
|
18
|
-
var api_1 = require("./api");
|
|
19
|
-
Object.defineProperty(exports, "Api", { enumerable: true, get: function () { return api_1.Api; } });
|
|
20
|
-
var blockchain_1 = require("./blockchain");
|
|
21
|
-
Object.defineProperty(exports, "Blockchain", { enumerable: true, get: function () { return blockchain_1.Blockchain; } });
|
|
22
|
-
var txpool_1 = require("./blockchain/txpool");
|
|
23
|
-
Object.defineProperty(exports, "BuildBlockMode", { enumerable: true, get: function () { return txpool_1.BuildBlockMode; } });
|
|
24
|
-
var xcm_1 = require("./xcm");
|
|
25
|
-
Object.defineProperty(exports, "connectParachains", { enumerable: true, get: function () { return xcm_1.connectParachains; } });
|
|
26
|
-
Object.defineProperty(exports, "connectVertical", { enumerable: true, get: function () { return xcm_1.connectVertical; } });
|
|
27
|
-
var setup_1 = require("./setup");
|
|
28
|
-
Object.defineProperty(exports, "setup", { enumerable: true, get: function () { return setup_1.setup; } });
|
|
29
|
-
var setup_with_server_1 = require("./setup-with-server");
|
|
30
|
-
Object.defineProperty(exports, "setupWithServer", { enumerable: true, get: function () { return setup_with_server_1.setupWithServer; } });
|
|
31
|
-
__exportStar(require("./blockchain/inherent"), exports);
|
|
1
|
+
export { Api } from './api';
|
|
2
|
+
export { Blockchain } from './blockchain';
|
|
3
|
+
export { BuildBlockMode } from './blockchain/txpool';
|
|
4
|
+
export { connectParachains, connectVertical } from './xcm';
|
|
5
|
+
export { setup } from './setup';
|
|
6
|
+
export { setupWithServer } from './setup-with-server';
|
|
7
|
+
export * from './blockchain/inherent';
|
package/lib/logger.js
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.truncate = exports.defaultLogger = void 0;
|
|
7
|
-
const pino_1 = __importDefault(require("pino"));
|
|
8
|
-
exports.defaultLogger = (0, pino_1.default)({
|
|
1
|
+
import createLogger from 'pino';
|
|
2
|
+
export const defaultLogger = createLogger({
|
|
9
3
|
level: process.env.LOG_LEVEL || 'info',
|
|
10
4
|
transport: {
|
|
11
5
|
target: 'pino-pretty',
|
|
@@ -35,5 +29,4 @@ const innerTruncate = (level = 0) => (val) => {
|
|
|
35
29
|
return val;
|
|
36
30
|
}
|
|
37
31
|
};
|
|
38
|
-
const truncate = (val) => innerTruncate(0)(val);
|
|
39
|
-
exports.truncate = truncate;
|
|
32
|
+
export const truncate = (val) => innerTruncate(0)(val);
|
package/lib/rpc/dev/dry-run.js
CHANGED
|
@@ -1,43 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const zHex = zod_1.default.custom((val) => /^0x\w+$/.test(val));
|
|
11
|
-
const zHash = zod_1.default.string().length(66).and(zHex);
|
|
12
|
-
const zParaId = zod_1.default.string().regex(/^\d+$/).transform(Number);
|
|
13
|
-
const dryRunSchema = zod_1.default.object({
|
|
14
|
-
raw: zod_1.default.boolean().optional(),
|
|
15
|
-
html: zod_1.default.boolean().optional(),
|
|
1
|
+
import z from 'zod';
|
|
2
|
+
import { decodeStorageDiff } from '../../utils/decoder';
|
|
3
|
+
import { generateHtmlDiff } from '../../utils/generate-html-diff';
|
|
4
|
+
const zHex = z.custom((val) => /^0x\w+$/.test(val));
|
|
5
|
+
const zHash = z.string().length(66).and(zHex);
|
|
6
|
+
const zParaId = z.string().regex(/^\d+$/).transform(Number);
|
|
7
|
+
const dryRunSchema = z.object({
|
|
8
|
+
raw: z.boolean().optional(),
|
|
9
|
+
html: z.boolean().optional(),
|
|
16
10
|
extrinsic: zHex
|
|
17
|
-
.or(
|
|
11
|
+
.or(z.object({
|
|
18
12
|
call: zHex,
|
|
19
13
|
address: zHex,
|
|
20
14
|
}))
|
|
21
15
|
.optional(),
|
|
22
|
-
hrmp:
|
|
23
|
-
.record(zParaId,
|
|
24
|
-
.array(
|
|
25
|
-
sentAt:
|
|
16
|
+
hrmp: z
|
|
17
|
+
.record(zParaId, z
|
|
18
|
+
.array(z.object({
|
|
19
|
+
sentAt: z.number(),
|
|
26
20
|
data: zHex,
|
|
27
21
|
}))
|
|
28
22
|
.min(1))
|
|
29
23
|
.optional(),
|
|
30
|
-
dmp:
|
|
31
|
-
.array(
|
|
32
|
-
sentAt:
|
|
24
|
+
dmp: z
|
|
25
|
+
.array(z.object({
|
|
26
|
+
sentAt: z.number(),
|
|
33
27
|
msg: zHex,
|
|
34
28
|
}))
|
|
35
29
|
.min(1)
|
|
36
30
|
.optional(),
|
|
37
|
-
ump:
|
|
31
|
+
ump: z.record(zParaId, z.array(zHex).min(1)).optional(),
|
|
38
32
|
at: zHash.optional(),
|
|
39
33
|
});
|
|
40
|
-
const dev_dryRun = async (context, [params]) => {
|
|
34
|
+
export const dev_dryRun = async (context, [params]) => {
|
|
41
35
|
const { html, extrinsic, hrmp, dmp, ump, raw, at } = dryRunSchema.parse(params);
|
|
42
36
|
const dryRun = async () => {
|
|
43
37
|
if (extrinsic) {
|
|
@@ -60,16 +54,15 @@ const dev_dryRun = async (context, [params]) => {
|
|
|
60
54
|
};
|
|
61
55
|
const storageDiff = await dryRun();
|
|
62
56
|
if (html) {
|
|
63
|
-
return
|
|
57
|
+
return generateHtmlDiff(context.chain.head, storageDiff);
|
|
64
58
|
}
|
|
65
59
|
if (raw) {
|
|
66
60
|
return storageDiff;
|
|
67
61
|
}
|
|
68
|
-
const [oldData, newData, delta] = await
|
|
62
|
+
const [oldData, newData, delta] = await decodeStorageDiff(context.chain.head, storageDiff);
|
|
69
63
|
return {
|
|
70
64
|
old: oldData,
|
|
71
65
|
new: newData,
|
|
72
66
|
delta,
|
|
73
67
|
};
|
|
74
68
|
};
|
|
75
|
-
exports.dev_dryRun = dev_dryRun;
|
package/lib/rpc/dev/index.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
const time_travel_1 = require("../../utils/time-travel");
|
|
8
|
-
const logger = logger_1.defaultLogger.child({ name: 'rpc-dev' });
|
|
1
|
+
import { ResponseError } from '../shared';
|
|
2
|
+
import { setStorage } from '../../utils/set-storage';
|
|
3
|
+
import { defaultLogger } from '../../logger';
|
|
4
|
+
import { dev_dryRun } from './dry-run';
|
|
5
|
+
import { timeTravel } from '../../utils/time-travel';
|
|
6
|
+
const logger = defaultLogger.child({ name: 'rpc-dev' });
|
|
9
7
|
const handlers = {
|
|
10
8
|
dev_newBlock: async (context, [param]) => {
|
|
11
9
|
const { count, to, hrmp, ump, dmp, transactions } = param || {};
|
|
@@ -22,7 +20,7 @@ const handlers = {
|
|
|
22
20
|
downwardMessages: dmp,
|
|
23
21
|
})
|
|
24
22
|
.catch((error) => {
|
|
25
|
-
throw new
|
|
23
|
+
throw new ResponseError(1, error.toString());
|
|
26
24
|
});
|
|
27
25
|
logger.debug({ hash: block.hash }, 'dev_newBlock');
|
|
28
26
|
finalHash = block.hash;
|
|
@@ -31,8 +29,8 @@ const handlers = {
|
|
|
31
29
|
},
|
|
32
30
|
dev_setStorage: async (context, params) => {
|
|
33
31
|
const [values, blockHash] = params;
|
|
34
|
-
const hash = await
|
|
35
|
-
throw new
|
|
32
|
+
const hash = await setStorage(context.chain, values, blockHash).catch((error) => {
|
|
33
|
+
throw new ResponseError(1, error.toString());
|
|
36
34
|
});
|
|
37
35
|
logger.debug({
|
|
38
36
|
hash,
|
|
@@ -43,8 +41,8 @@ const handlers = {
|
|
|
43
41
|
dev_timeTravel: async (context, [date]) => {
|
|
44
42
|
const timestamp = typeof date === 'string' ? Date.parse(date) : date;
|
|
45
43
|
if (Number.isNaN(timestamp))
|
|
46
|
-
throw new
|
|
47
|
-
await
|
|
44
|
+
throw new ResponseError(1, 'Invalid date');
|
|
45
|
+
await timeTravel(context.chain, timestamp);
|
|
48
46
|
return timestamp;
|
|
49
47
|
},
|
|
50
48
|
dev_setHead: async (context, [hashOrNumber]) => {
|
|
@@ -57,11 +55,11 @@ const handlers = {
|
|
|
57
55
|
block = await context.chain.getBlock(hashOrNumber);
|
|
58
56
|
}
|
|
59
57
|
if (!block) {
|
|
60
|
-
throw new
|
|
58
|
+
throw new ResponseError(1, `Block not found ${hashOrNumber}`);
|
|
61
59
|
}
|
|
62
60
|
await context.chain.setHead(block);
|
|
63
61
|
return block.hash;
|
|
64
62
|
},
|
|
65
|
-
dev_dryRun
|
|
63
|
+
dev_dryRun,
|
|
66
64
|
};
|
|
67
|
-
|
|
65
|
+
export default handlers;
|
package/lib/rpc/index.js
CHANGED
|
@@ -1,27 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.handler = void 0;
|
|
7
|
-
const shared_1 = require("./shared");
|
|
8
|
-
const dev_1 = __importDefault(require("./dev"));
|
|
9
|
-
const substrate_1 = __importDefault(require("./substrate"));
|
|
1
|
+
import { ResponseError, logger } from './shared';
|
|
2
|
+
import dev from './dev';
|
|
3
|
+
import substrate from './substrate';
|
|
10
4
|
const allHandlers = {
|
|
11
|
-
...
|
|
12
|
-
...
|
|
5
|
+
...substrate,
|
|
6
|
+
...dev,
|
|
13
7
|
rpc_methods: async () => Promise.resolve({
|
|
14
8
|
version: 1,
|
|
15
9
|
methods: Object.keys(allHandlers),
|
|
16
10
|
}),
|
|
17
11
|
};
|
|
18
|
-
const handler = (context) => ({ method, params }, subscriptionManager) => {
|
|
19
|
-
|
|
12
|
+
export const handler = (context) => ({ method, params }, subscriptionManager) => {
|
|
13
|
+
logger.trace('Handling %s', method);
|
|
20
14
|
const handler = allHandlers[method];
|
|
21
15
|
if (!handler) {
|
|
22
|
-
|
|
23
|
-
throw new
|
|
16
|
+
logger.warn('Method not found %s', method);
|
|
17
|
+
throw new ResponseError(-32601, `Method not found: ${method}`);
|
|
24
18
|
}
|
|
25
19
|
return handler(context, params, subscriptionManager);
|
|
26
20
|
};
|
|
27
|
-
exports.handler = handler;
|
package/lib/rpc/shared.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const logger_1 = require("../logger");
|
|
5
|
-
exports.logger = logger_1.defaultLogger.child({ name: 'rpc' });
|
|
6
|
-
class ResponseError extends Error {
|
|
1
|
+
import { defaultLogger } from '../logger';
|
|
2
|
+
export const logger = defaultLogger.child({ name: 'rpc' });
|
|
3
|
+
export class ResponseError extends Error {
|
|
7
4
|
code;
|
|
8
5
|
constructor(code, message) {
|
|
9
6
|
super(message);
|
|
@@ -17,4 +14,3 @@ class ResponseError extends Error {
|
|
|
17
14
|
};
|
|
18
15
|
}
|
|
19
16
|
}
|
|
20
|
-
exports.ResponseError = ResponseError;
|
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const logger_1 = require("../../logger");
|
|
6
|
-
const logger = logger_1.defaultLogger.child({ name: 'rpc-author' });
|
|
1
|
+
import { APPLY_EXTRINSIC_ERROR } from '../../blockchain/txpool';
|
|
2
|
+
import { ResponseError } from '../shared';
|
|
3
|
+
import { defaultLogger } from '../../logger';
|
|
4
|
+
const logger = defaultLogger.child({ name: 'rpc-author' });
|
|
7
5
|
const handlers = {
|
|
8
6
|
author_submitExtrinsic: async (context, [extrinsic]) => {
|
|
9
7
|
return context.chain.submitExtrinsic(extrinsic).catch((error) => {
|
|
10
8
|
const code = error.isInvalid ? 1010 : 1011;
|
|
11
|
-
throw new
|
|
9
|
+
throw new ResponseError(code, error.toString());
|
|
12
10
|
});
|
|
13
11
|
},
|
|
14
12
|
author_submitAndWatchExtrinsic: async (context, [extrinsic], { subscribe, unsubscribe }) => {
|
|
@@ -21,9 +19,9 @@ const handlers = {
|
|
|
21
19
|
done(id);
|
|
22
20
|
}
|
|
23
21
|
};
|
|
24
|
-
context.chain.txPool.event.on(
|
|
22
|
+
context.chain.txPool.event.on(APPLY_EXTRINSIC_ERROR, onExtrinsicFail);
|
|
25
23
|
const done = (id) => {
|
|
26
|
-
context.chain.txPool.event.removeListener(
|
|
24
|
+
context.chain.txPool.event.removeListener(APPLY_EXTRINSIC_ERROR, onExtrinsicFail);
|
|
27
25
|
unsubscribe(id);
|
|
28
26
|
};
|
|
29
27
|
update = async (block) => {
|
|
@@ -60,4 +58,4 @@ const handlers = {
|
|
|
60
58
|
return context.chain.txPool.pendingExtrinsics;
|
|
61
59
|
},
|
|
62
60
|
};
|
|
63
|
-
|
|
61
|
+
export default handlers;
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const shared_1 = require("../shared");
|
|
1
|
+
import { ResponseError } from '../shared';
|
|
4
2
|
const processHeader = (header) => {
|
|
5
3
|
const res = header.toJSON();
|
|
6
4
|
res.number = '0x' + res.number.toString(16); // number is hex format
|
|
@@ -10,21 +8,21 @@ const handlers = {
|
|
|
10
8
|
chain_getBlockHash: async (context, [blockNumber]) => {
|
|
11
9
|
const block = await context.chain.getBlockAt(blockNumber);
|
|
12
10
|
if (!block) {
|
|
13
|
-
throw new
|
|
11
|
+
throw new ResponseError(1, `Block #${blockNumber} not found`);
|
|
14
12
|
}
|
|
15
13
|
return block.hash;
|
|
16
14
|
},
|
|
17
15
|
chain_getHeader: async (context, [hash]) => {
|
|
18
16
|
const block = await context.chain.getBlock(hash);
|
|
19
17
|
if (!block) {
|
|
20
|
-
throw new
|
|
18
|
+
throw new ResponseError(1, `Block ${hash} not found`);
|
|
21
19
|
}
|
|
22
20
|
return processHeader(await block.header);
|
|
23
21
|
},
|
|
24
22
|
chain_getBlock: async (context, [hash]) => {
|
|
25
23
|
const block = await context.chain.getBlock(hash);
|
|
26
24
|
if (!block) {
|
|
27
|
-
throw new
|
|
25
|
+
throw new ResponseError(1, `Block ${hash} not found`);
|
|
28
26
|
}
|
|
29
27
|
return {
|
|
30
28
|
block: {
|
|
@@ -66,7 +64,7 @@ const alias = {
|
|
|
66
64
|
chain_unsubscribeNewHeads: handlers.chain_unsubscribeNewHead,
|
|
67
65
|
chain_unsubscribeFinalizedHeads: handlers.chain_unsubscribeNewHead,
|
|
68
66
|
};
|
|
69
|
-
|
|
67
|
+
export default {
|
|
70
68
|
...handlers,
|
|
71
69
|
...alias,
|
|
72
70
|
};
|
|
@@ -1,18 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const author_1 = __importDefault(require("./author"));
|
|
7
|
-
const chain_1 = __importDefault(require("./chain"));
|
|
8
|
-
const payment_1 = __importDefault(require("./payment"));
|
|
9
|
-
const state_1 = __importDefault(require("./state"));
|
|
10
|
-
const system_1 = __importDefault(require("./system"));
|
|
1
|
+
import author from './author';
|
|
2
|
+
import chain from './chain';
|
|
3
|
+
import payment from './payment';
|
|
4
|
+
import state from './state';
|
|
5
|
+
import system from './system';
|
|
11
6
|
const handlers = {
|
|
12
|
-
...
|
|
13
|
-
...
|
|
14
|
-
...
|
|
15
|
-
...
|
|
16
|
-
...
|
|
7
|
+
...author,
|
|
8
|
+
...chain,
|
|
9
|
+
...state,
|
|
10
|
+
...system,
|
|
11
|
+
...payment,
|
|
17
12
|
};
|
|
18
|
-
|
|
13
|
+
export default handlers;
|
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const shared_1 = require("../shared");
|
|
4
|
-
const util_1 = require("@polkadot/util");
|
|
1
|
+
import { ResponseError } from '../shared';
|
|
2
|
+
import { hexToU8a } from '@polkadot/util';
|
|
5
3
|
const handlers = {
|
|
6
4
|
payment_queryFeeDetails: async (context, [extrinsic, hash]) => {
|
|
7
5
|
const block = await context.chain.getBlock(hash);
|
|
8
6
|
if (!block) {
|
|
9
|
-
throw new
|
|
7
|
+
throw new ResponseError(1, `Block ${hash} not found`);
|
|
10
8
|
}
|
|
11
9
|
const registry = await block.registry;
|
|
12
|
-
const tx =
|
|
10
|
+
const tx = hexToU8a(extrinsic);
|
|
13
11
|
const resp = await block.call('TransactionPaymentApi_query_fee_details', [
|
|
14
12
|
registry.createType('Extrinsic', tx).toHex(),
|
|
15
13
|
registry.createType('u32', tx.byteLength).toHex(),
|
|
@@ -19,10 +17,10 @@ const handlers = {
|
|
|
19
17
|
payment_queryInfo: async (context, [extrinsic, hash]) => {
|
|
20
18
|
const block = await context.chain.getBlock(hash);
|
|
21
19
|
if (!block) {
|
|
22
|
-
throw new
|
|
20
|
+
throw new ResponseError(1, `Block ${hash} not found`);
|
|
23
21
|
}
|
|
24
22
|
const registry = await block.registry;
|
|
25
|
-
const tx =
|
|
23
|
+
const tx = hexToU8a(extrinsic);
|
|
26
24
|
const resp = await block.call('TransactionPaymentApi_query_info', [
|
|
27
25
|
registry.createType('Extrinsic', tx).toHex(),
|
|
28
26
|
registry.createType('u32', tx.byteLength).toHex(),
|
|
@@ -30,4 +28,4 @@ const handlers = {
|
|
|
30
28
|
return resp.result;
|
|
31
29
|
},
|
|
32
30
|
};
|
|
33
|
-
|
|
31
|
+
export default handlers;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
const logger_1 = require("../../logger");
|
|
5
|
-
const logger = logger_1.defaultLogger.child({ name: 'rpc-state' });
|
|
1
|
+
import { ResponseError } from '../shared';
|
|
2
|
+
import { defaultLogger } from '../../logger';
|
|
3
|
+
const logger = defaultLogger.child({ name: 'rpc-state' });
|
|
6
4
|
const handlers = {
|
|
7
5
|
state_getRuntimeVersion: async (context, [hash]) => {
|
|
8
6
|
const block = await context.chain.getBlock(hash);
|
|
@@ -36,7 +34,7 @@ const handlers = {
|
|
|
36
34
|
state_call: async (context, [method, data, hash]) => {
|
|
37
35
|
const block = await context.chain.getBlock(hash);
|
|
38
36
|
if (!block) {
|
|
39
|
-
throw new
|
|
37
|
+
throw new ResponseError(1, `Block ${hash} not found`);
|
|
40
38
|
}
|
|
41
39
|
const resp = await block.call(method, [data]);
|
|
42
40
|
return resp.result;
|
|
@@ -79,4 +77,4 @@ const handlers = {
|
|
|
79
77
|
unsubscribe(subid);
|
|
80
78
|
},
|
|
81
79
|
};
|
|
82
|
-
|
|
80
|
+
export default handlers;
|