@acala-network/chopsticks-core 0.8.2 → 0.8.3
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.
|
@@ -2,7 +2,11 @@ import { Header, TransactionValidityError } from '@polkadot/types/interfaces';
|
|
|
2
2
|
import { Block, TaskCallResponse } from './block';
|
|
3
3
|
import { HexString } from '@polkadot/util/types';
|
|
4
4
|
export declare const newHeader: (head: Block, unsafeBlockHeight?: number) => Promise<Header>;
|
|
5
|
-
export
|
|
5
|
+
export type BuildBlockCallbacks = {
|
|
6
|
+
onApplyExtrinsicError?: (extrinsic: HexString, error: TransactionValidityError) => void;
|
|
7
|
+
onPhaseApplied?: (phase: 'initialize' | 'finalize' | number, resp: TaskCallResponse) => void;
|
|
8
|
+
};
|
|
9
|
+
export declare const buildBlock: (head: Block, inherents: HexString[], extrinsics: HexString[], ump: Record<number, HexString[]>, callbacks?: BuildBlockCallbacks, unsafeBlockHeight?: number) => Promise<[Block, HexString[]]>;
|
|
6
10
|
export declare const dryRunExtrinsic: (head: Block, inherents: HexString[], extrinsic: HexString | {
|
|
7
11
|
call: HexString;
|
|
8
12
|
address: string;
|
|
@@ -90,7 +90,8 @@ const newHeader = (head, unsafeBlockHeight) => __awaiter(void 0, void 0, void 0,
|
|
|
90
90
|
return header;
|
|
91
91
|
});
|
|
92
92
|
exports.newHeader = newHeader;
|
|
93
|
-
const initNewBlock = (head, header, inherents, storageLayer) => __awaiter(void 0, void 0, void 0, function* () {
|
|
93
|
+
const initNewBlock = (head, header, inherents, storageLayer, callback) => __awaiter(void 0, void 0, void 0, function* () {
|
|
94
|
+
var _c, _d;
|
|
94
95
|
const blockNumber = header.number.toNumber();
|
|
95
96
|
const hash = `0x${Math.round(Math.random() * 100000000)
|
|
96
97
|
.toString(16)
|
|
@@ -102,19 +103,19 @@ const initNewBlock = (head, header, inherents, storageLayer) => __awaiter(void 0
|
|
|
102
103
|
});
|
|
103
104
|
{
|
|
104
105
|
// initialize block
|
|
105
|
-
const
|
|
106
|
-
newBlock.pushStorageLayer().setAll(storageDiff);
|
|
107
|
-
|
|
106
|
+
const resp = yield newBlock.call('Core_initialize_block', [header.toHex()]);
|
|
107
|
+
newBlock.pushStorageLayer().setAll(resp.storageDiff);
|
|
108
|
+
(_c = callback === null || callback === void 0 ? void 0 : callback.onPhaseApplied) === null || _c === void 0 ? void 0 : _c.call(callback, 'initialize', resp);
|
|
108
109
|
}
|
|
109
110
|
const layers = [];
|
|
110
111
|
// apply inherents
|
|
111
112
|
for (const extrinsic of inherents) {
|
|
112
113
|
try {
|
|
113
|
-
const
|
|
114
|
+
const resp = yield newBlock.call('BlockBuilder_apply_extrinsic', [extrinsic]);
|
|
114
115
|
const layer = newBlock.pushStorageLayer();
|
|
115
|
-
layer.setAll(storageDiff);
|
|
116
|
+
layer.setAll(resp.storageDiff);
|
|
116
117
|
layers.push(layer);
|
|
117
|
-
|
|
118
|
+
(_d = callback === null || callback === void 0 ? void 0 : callback.onPhaseApplied) === null || _d === void 0 ? void 0 : _d.call(callback, layers.length - 1, resp);
|
|
118
119
|
}
|
|
119
120
|
catch (e) {
|
|
120
121
|
logger.warn('Failed to apply inherents %o %s', e, e);
|
|
@@ -126,7 +127,8 @@ const initNewBlock = (head, header, inherents, storageLayer) => __awaiter(void 0
|
|
|
126
127
|
layers: layers,
|
|
127
128
|
};
|
|
128
129
|
});
|
|
129
|
-
const buildBlock = (head, inherents, extrinsics, ump,
|
|
130
|
+
const buildBlock = (head, inherents, extrinsics, ump, callbacks, unsafeBlockHeight) => __awaiter(void 0, void 0, void 0, function* () {
|
|
131
|
+
var _e, _f, _g;
|
|
130
132
|
const registry = yield head.registry;
|
|
131
133
|
const header = yield (0, exports.newHeader)(head, unsafeBlockHeight);
|
|
132
134
|
const newBlockNumber = header.number.toNumber();
|
|
@@ -216,15 +218,15 @@ const buildBlock = (head, inherents, extrinsics, ump, onApplyExtrinsicError, uns
|
|
|
216
218
|
// apply extrinsics
|
|
217
219
|
for (const extrinsic of extrinsics) {
|
|
218
220
|
try {
|
|
219
|
-
const
|
|
220
|
-
const outcome = registry.createType('ApplyExtrinsicResult', result);
|
|
221
|
+
const resp = yield newBlock.call('BlockBuilder_apply_extrinsic', [extrinsic]);
|
|
222
|
+
const outcome = registry.createType('ApplyExtrinsicResult', resp.result);
|
|
221
223
|
if (outcome.isErr) {
|
|
222
|
-
onApplyExtrinsicError(extrinsic, outcome.asErr);
|
|
224
|
+
(_e = callbacks === null || callbacks === void 0 ? void 0 : callbacks.onApplyExtrinsicError) === null || _e === void 0 ? void 0 : _e.call(callbacks, extrinsic, outcome.asErr);
|
|
223
225
|
continue;
|
|
224
226
|
}
|
|
225
|
-
newBlock.pushStorageLayer().setAll(storageDiff);
|
|
226
|
-
logger.trace((0, logger_1.truncate)(storageDiff), 'Applied extrinsic');
|
|
227
|
+
newBlock.pushStorageLayer().setAll(resp.storageDiff);
|
|
227
228
|
includedExtrinsic.push(extrinsic);
|
|
229
|
+
(_f = callbacks === null || callbacks === void 0 ? void 0 : callbacks.onPhaseApplied) === null || _f === void 0 ? void 0 : _f.call(callbacks, includedExtrinsic.length - 1, resp);
|
|
228
230
|
}
|
|
229
231
|
catch (e) {
|
|
230
232
|
logger.info('Failed to apply extrinsic %o %s', e, e);
|
|
@@ -233,16 +235,18 @@ const buildBlock = (head, inherents, extrinsics, ump, onApplyExtrinsicError, uns
|
|
|
233
235
|
}
|
|
234
236
|
{
|
|
235
237
|
// finalize block
|
|
236
|
-
const
|
|
237
|
-
newBlock.pushStorageLayer().setAll(storageDiff);
|
|
238
|
-
|
|
238
|
+
const resp = yield newBlock.call('BlockBuilder_finalize_block', []);
|
|
239
|
+
newBlock.pushStorageLayer().setAll(resp.storageDiff);
|
|
240
|
+
(_g = callbacks === null || callbacks === void 0 ? void 0 : callbacks.onPhaseApplied) === null || _g === void 0 ? void 0 : _g.call(callbacks, 'finalize', resp);
|
|
239
241
|
}
|
|
240
242
|
const blockData = registry.createType('Block', {
|
|
241
243
|
header,
|
|
242
244
|
extrinsics: includedExtrinsic,
|
|
243
245
|
});
|
|
244
246
|
const storageDiff = yield newBlock.storageDiff();
|
|
245
|
-
logger.
|
|
247
|
+
if (logger.level.toLowerCase() === 'trace') {
|
|
248
|
+
logger.trace(Object.entries(storageDiff).map(([key, value]) => [key, (0, logger_1.truncate)(value)]), 'Final block');
|
|
249
|
+
}
|
|
246
250
|
const finalBlock = new block_1.Block(head.chain, newBlock.number, blockData.hash.toHex(), head, {
|
|
247
251
|
header,
|
|
248
252
|
extrinsics: [...inherents, ...includedExtrinsic],
|
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
26
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
27
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -28,7 +51,7 @@ exports.StorageLayer = exports.RemoteStorageLayer = exports.StorageValueKind = v
|
|
|
28
51
|
const lodash_1 = __importDefault(require("lodash"));
|
|
29
52
|
const entities_1 = require("../db/entities");
|
|
30
53
|
const logger_1 = require("../logger");
|
|
31
|
-
const key_cache_1 =
|
|
54
|
+
const key_cache_1 = __importStar(require("../utils/key-cache"));
|
|
32
55
|
const logger = logger_1.defaultLogger.child({ name: 'layer' });
|
|
33
56
|
const BATCH_SIZE = 1000;
|
|
34
57
|
var StorageValueKind;
|
|
@@ -76,7 +99,7 @@ class RemoteStorageLayer {
|
|
|
76
99
|
throw new Error(`pageSize must be less or equal to ${BATCH_SIZE}`);
|
|
77
100
|
logger.trace({ at: __classPrivateFieldGet(this, _RemoteStorageLayer_at, "f"), prefix, pageSize, startKey }, 'RemoteStorageLayer getKeysPaged');
|
|
78
101
|
// can't handle keyCache without prefix
|
|
79
|
-
if (prefix.length <
|
|
102
|
+
if (prefix.length < key_cache_1.PREFIX_LENGTH || startKey.length < key_cache_1.PREFIX_LENGTH) {
|
|
80
103
|
return __classPrivateFieldGet(this, _RemoteStorageLayer_api, "f").getKeysPaged(prefix, pageSize, startKey, __classPrivateFieldGet(this, _RemoteStorageLayer_at, "f"));
|
|
81
104
|
}
|
|
82
105
|
let batchComplete = false;
|
package/lib/blockchain/txpool.js
CHANGED
|
@@ -209,8 +209,24 @@ _TxPool_chain = new WeakMap(), _TxPool_pool = new WeakMap(), _TxPool_ump = new W
|
|
|
209
209
|
logger.trace({ params }, 'build block');
|
|
210
210
|
const head = __classPrivateFieldGet(this, _TxPool_chain, "f").head;
|
|
211
211
|
const inherents = yield __classPrivateFieldGet(this, _TxPool_inherentProvider, "f").createInherents(head, params);
|
|
212
|
-
const [newBlock, pendingExtrinsics] = yield (0, block_builder_1.buildBlock)(head, inherents, params.transactions, params.upwardMessages,
|
|
213
|
-
|
|
212
|
+
const [newBlock, pendingExtrinsics] = yield (0, block_builder_1.buildBlock)(head, inherents, params.transactions, params.upwardMessages, {
|
|
213
|
+
onApplyExtrinsicError: (extrinsic, error) => {
|
|
214
|
+
this.event.emit(exports.APPLY_EXTRINSIC_ERROR, [extrinsic, error]);
|
|
215
|
+
},
|
|
216
|
+
onPhaseApplied: logger.level.toLowerCase() === 'trace'
|
|
217
|
+
? (phase, resp) => {
|
|
218
|
+
switch (phase) {
|
|
219
|
+
case 'initialize':
|
|
220
|
+
logger.trace((0, logger_1.truncate)(resp.storageDiff), 'Initialize block');
|
|
221
|
+
break;
|
|
222
|
+
case 'finalize':
|
|
223
|
+
logger.trace((0, logger_1.truncate)(resp.storageDiff), 'Finalize block');
|
|
224
|
+
break;
|
|
225
|
+
default:
|
|
226
|
+
logger.trace((0, logger_1.truncate)(resp.storageDiff), `Apply extrinsic ${phase}`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
: undefined,
|
|
214
230
|
}, params.unsafeBlockHeight);
|
|
215
231
|
for (const extrinsic of pendingExtrinsics) {
|
|
216
232
|
__classPrivateFieldGet(this, _TxPool_pool, "f").push({ extrinsic, signer: yield __classPrivateFieldGet(this, _TxPool_instances, "m", _TxPool_getSigner).call(this, extrinsic) });
|
package/lib/executor.js
CHANGED
|
@@ -18,6 +18,7 @@ const util_crypto_1 = require("@polkadot/util-crypto");
|
|
|
18
18
|
const key_cache_1 = require("./utils/key-cache");
|
|
19
19
|
const chopsticks_executor_1 = require("@acala-network/chopsticks-executor");
|
|
20
20
|
const logger_1 = require("./logger");
|
|
21
|
+
const utils_1 = require("./utils");
|
|
21
22
|
const lodash_1 = __importDefault(require("lodash"));
|
|
22
23
|
const logger = logger_1.defaultLogger.child({ name: 'executor' });
|
|
23
24
|
const getRuntimeVersion = (code) => __awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -78,7 +79,7 @@ const taskHandler = (block) => {
|
|
|
78
79
|
pageSize: 1,
|
|
79
80
|
startKey: key,
|
|
80
81
|
});
|
|
81
|
-
return nextKey;
|
|
82
|
+
return nextKey && (0, utils_1.stripChildPrefix)(nextKey);
|
|
82
83
|
});
|
|
83
84
|
},
|
|
84
85
|
offchainGetStorage: function (key) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acala-network/chopsticks-core",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.3",
|
|
4
4
|
"author": "Acala Developers <hello@acala.network>",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"scripts": {
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"docs:prep": "typedoc"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@acala-network/chopsticks-executor": "0.8.
|
|
13
|
+
"@acala-network/chopsticks-executor": "0.8.3",
|
|
14
14
|
"@polkadot/api": "^10.9.1",
|
|
15
15
|
"@polkadot/util-crypto": "^12.3.2",
|
|
16
16
|
"axios": "^1.5.1",
|