@acala-network/chopsticks-core 0.9.8-1 → 0.9.8-2
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/dist/cjs/blockchain/block-builder.d.ts +6 -4
- package/dist/cjs/blockchain/block-builder.js +25 -20
- package/dist/cjs/blockchain/index.d.ts +4 -4
- package/dist/cjs/blockchain/index.js +19 -26
- package/dist/cjs/blockchain/inherent/index.d.ts +8 -15
- package/dist/cjs/blockchain/inherent/index.js +13 -100
- package/dist/cjs/blockchain/inherent/para-enter.d.ts +3 -3
- package/dist/cjs/blockchain/inherent/para-enter.js +3 -1
- package/dist/cjs/blockchain/inherent/parachain/babe-randomness.d.ts +3 -3
- package/dist/cjs/blockchain/inherent/parachain/babe-randomness.js +3 -1
- package/dist/cjs/blockchain/inherent/parachain/nimbus-author-inherent.d.ts +3 -3
- package/dist/cjs/blockchain/inherent/parachain/nimbus-author-inherent.js +20 -1
- package/dist/cjs/blockchain/inherent/parachain/validation-data.d.ts +3 -3
- package/dist/cjs/blockchain/inherent/parachain/validation-data.js +3 -1
- package/dist/cjs/blockchain/inherent/timestamp.d.ts +6 -0
- package/dist/cjs/blockchain/inherent/timestamp.js +24 -0
- package/dist/cjs/blockchain/txpool.d.ts +1 -1
- package/dist/cjs/blockchain/txpool.js +6 -8
- package/dist/cjs/logger.js +8 -2
- package/dist/cjs/setup.js +7 -13
- package/dist/cjs/utils/decoder.js +1 -1
- package/dist/cjs/utils/index.d.ts +3 -0
- package/dist/cjs/utils/index.js +35 -0
- package/dist/cjs/utils/time-travel.d.ts +0 -3
- package/dist/cjs/utils/time-travel.js +4 -37
- package/dist/esm/blockchain/block-builder.d.ts +6 -4
- package/dist/esm/blockchain/block-builder.js +24 -19
- package/dist/esm/blockchain/index.d.ts +4 -4
- package/dist/esm/blockchain/index.js +19 -26
- package/dist/esm/blockchain/inherent/index.d.ts +8 -15
- package/dist/esm/blockchain/inherent/index.js +12 -32
- package/dist/esm/blockchain/inherent/para-enter.d.ts +3 -3
- package/dist/esm/blockchain/inherent/para-enter.js +3 -1
- package/dist/esm/blockchain/inherent/parachain/babe-randomness.d.ts +3 -3
- package/dist/esm/blockchain/inherent/parachain/babe-randomness.js +3 -1
- package/dist/esm/blockchain/inherent/parachain/nimbus-author-inherent.d.ts +3 -3
- package/dist/esm/blockchain/inherent/parachain/nimbus-author-inherent.js +20 -1
- package/dist/esm/blockchain/inherent/parachain/validation-data.d.ts +3 -3
- package/dist/esm/blockchain/inherent/parachain/validation-data.js +3 -1
- package/dist/esm/blockchain/inherent/timestamp.d.ts +6 -0
- package/dist/esm/blockchain/inherent/timestamp.js +14 -0
- package/dist/esm/blockchain/txpool.d.ts +1 -1
- package/dist/esm/blockchain/txpool.js +5 -7
- package/dist/esm/logger.js +8 -2
- package/dist/esm/setup.js +2 -8
- package/dist/esm/utils/decoder.js +1 -1
- package/dist/esm/utils/index.d.ts +3 -0
- package/dist/esm/utils/index.js +27 -1
- package/dist/esm/utils/time-travel.d.ts +0 -3
- package/dist/esm/utils/time-travel.js +2 -21
- package/package.json +2 -2
|
@@ -102,7 +102,7 @@ var BuildBlockMode;
|
|
|
102
102
|
/** One block per tx */ BuildBlockMode["Instant"] = "Instant";
|
|
103
103
|
/** Only build when triggered */ BuildBlockMode["Manual"] = "Manual";
|
|
104
104
|
})(BuildBlockMode || (BuildBlockMode = {}));
|
|
105
|
-
var _chain = /*#__PURE__*/ new WeakMap(), _pool = /*#__PURE__*/ new WeakMap(), _ump = /*#__PURE__*/ new WeakMap(), _dmp = /*#__PURE__*/ new WeakMap(), _hrmp = /*#__PURE__*/ new WeakMap(), _mode = /*#__PURE__*/ new WeakMap(),
|
|
105
|
+
var _chain = /*#__PURE__*/ new WeakMap(), _pool = /*#__PURE__*/ new WeakMap(), _ump = /*#__PURE__*/ new WeakMap(), _dmp = /*#__PURE__*/ new WeakMap(), _hrmp = /*#__PURE__*/ new WeakMap(), _mode = /*#__PURE__*/ new WeakMap(), _inherentProviders = /*#__PURE__*/ new WeakMap(), _pendingBlocks = /*#__PURE__*/ new WeakMap(), _isBuilding = /*#__PURE__*/ new WeakMap(), _getSigner = /*#__PURE__*/ new WeakSet(), _maybeBuildBlock = /*#__PURE__*/ new WeakSet(), _batchBuildBlock = /*#__PURE__*/ new WeakMap(), _buildBlockIfNeeded = /*#__PURE__*/ new WeakSet(), _buildBlock = /*#__PURE__*/ new WeakSet();
|
|
106
106
|
class TxPool {
|
|
107
107
|
get pendingExtrinsics() {
|
|
108
108
|
return _class_private_field_get(this, _pool).map(({ extrinsic })=>extrinsic);
|
|
@@ -223,7 +223,7 @@ class TxPool {
|
|
|
223
223
|
}
|
|
224
224
|
return count;
|
|
225
225
|
}
|
|
226
|
-
constructor(chain,
|
|
226
|
+
constructor(chain, inherentProviders, mode = "Batch"){
|
|
227
227
|
_class_private_method_init(this, _getSigner);
|
|
228
228
|
_class_private_method_init(this, _maybeBuildBlock);
|
|
229
229
|
_class_private_method_init(this, _buildBlockIfNeeded);
|
|
@@ -252,7 +252,7 @@ class TxPool {
|
|
|
252
252
|
writable: true,
|
|
253
253
|
value: void 0
|
|
254
254
|
});
|
|
255
|
-
_class_private_field_init(this,
|
|
255
|
+
_class_private_field_init(this, _inherentProviders, {
|
|
256
256
|
writable: true,
|
|
257
257
|
value: void 0
|
|
258
258
|
});
|
|
@@ -273,7 +273,7 @@ class TxPool {
|
|
|
273
273
|
});
|
|
274
274
|
_class_private_field_set(this, _chain, chain);
|
|
275
275
|
_class_private_field_set(this, _mode, mode);
|
|
276
|
-
_class_private_field_set(this,
|
|
276
|
+
_class_private_field_set(this, _inherentProviders, inherentProviders);
|
|
277
277
|
}
|
|
278
278
|
}
|
|
279
279
|
async function getSigner(extrinsic) {
|
|
@@ -314,9 +314,7 @@ async function buildBlock() {
|
|
|
314
314
|
logger.trace({
|
|
315
315
|
params
|
|
316
316
|
}, 'build block');
|
|
317
|
-
const
|
|
318
|
-
const inherents = await _class_private_field_get(this, _inherentProvider).createInherents(head, params);
|
|
319
|
-
const [newBlock, pendingExtrinsics] = await (0, _blockbuilder.buildBlock)(head, inherents, params.transactions, params.upwardMessages, {
|
|
317
|
+
const [newBlock, pendingExtrinsics] = await (0, _blockbuilder.buildBlock)(_class_private_field_get(this, _chain).head, _class_private_field_get(this, _inherentProviders), params, {
|
|
320
318
|
onApplyExtrinsicError: (extrinsic, error)=>{
|
|
321
319
|
this.event.emit(APPLY_EXTRINSIC_ERROR, [
|
|
322
320
|
extrinsic,
|
|
@@ -335,7 +333,7 @@ async function buildBlock() {
|
|
|
335
333
|
logger.trace((0, _logger.truncate)(resp.storageDiff), `Apply extrinsic ${phase}`);
|
|
336
334
|
}
|
|
337
335
|
} : undefined
|
|
338
|
-
}
|
|
336
|
+
});
|
|
339
337
|
for (const extrinsic of pendingExtrinsics){
|
|
340
338
|
_class_private_field_get(this, _pool).push({
|
|
341
339
|
extrinsic,
|
package/dist/cjs/logger.js
CHANGED
|
@@ -20,10 +20,16 @@ _export(exports, {
|
|
|
20
20
|
}
|
|
21
21
|
});
|
|
22
22
|
const _pino = require("pino");
|
|
23
|
+
const level = typeof process === 'object' && process.env.LOG_LEVEL || 'info';
|
|
24
|
+
const hideObject = typeof process === 'object' && !!process.env.LOG_COMPACT || false;
|
|
23
25
|
const pinoLogger = (0, _pino.pino)({
|
|
24
|
-
level
|
|
26
|
+
level,
|
|
25
27
|
transport: {
|
|
26
|
-
target: 'pino-pretty'
|
|
28
|
+
target: 'pino-pretty',
|
|
29
|
+
options: {
|
|
30
|
+
ignore: 'pid,hostname',
|
|
31
|
+
hideObject
|
|
32
|
+
}
|
|
27
33
|
}
|
|
28
34
|
});
|
|
29
35
|
const defaultLogger = pinoLogger.child({
|
package/dist/cjs/setup.js
CHANGED
|
@@ -21,21 +21,21 @@ const _rpcprovider = require("@polkadot/rpc-provider");
|
|
|
21
21
|
const _util = require("@polkadot/util");
|
|
22
22
|
const _api = require("./api.js");
|
|
23
23
|
const _index = require("./blockchain/index.js");
|
|
24
|
-
const _index1 = require("./blockchain/inherent/index.js");
|
|
25
24
|
const _logger = require("./logger.js");
|
|
26
|
-
const
|
|
25
|
+
const _index1 = require("./index.js");
|
|
26
|
+
const _index2 = require("./blockchain/inherent/index.js");
|
|
27
27
|
const genesisSetup = async (chain, genesis)=>{
|
|
28
28
|
const meta = await chain.head.meta;
|
|
29
29
|
const timestamp = Date.now();
|
|
30
|
-
await (0,
|
|
30
|
+
await (0, _index1.setStorage)(chain, {
|
|
31
31
|
Timestamp: {
|
|
32
32
|
Now: timestamp
|
|
33
33
|
}
|
|
34
34
|
});
|
|
35
|
-
const slotDuration = await (0,
|
|
35
|
+
const slotDuration = await (0, _index1.getSlotDuration)(chain);
|
|
36
36
|
const currentSlot = Math.floor(timestamp / slotDuration);
|
|
37
37
|
if (meta.consts.babe) {
|
|
38
|
-
await (0,
|
|
38
|
+
await (0, _index1.setStorage)(chain, {
|
|
39
39
|
Babe: {
|
|
40
40
|
CurrentSlot: currentSlot
|
|
41
41
|
}
|
|
@@ -44,7 +44,7 @@ const genesisSetup = async (chain, genesis)=>{
|
|
|
44
44
|
'0x0642414245b50103020200001c5fef100000000044cadd14aaefbda13ac8d85e1a6d58be082e7e2f56a4f95a3c612c784aaa4063f5517bf67d93ce633cde2fde7fbcf8ddca80017aaf8cd48436514687c662f60eda0ffa2c4781906416f4e71a196c9783c60c1b83d54c3a29365d03706714570b'
|
|
45
45
|
];
|
|
46
46
|
} else {
|
|
47
|
-
await (0,
|
|
47
|
+
await (0, _index1.setStorage)(chain, {
|
|
48
48
|
Aura: {
|
|
49
49
|
CurrentSlot: currentSlot
|
|
50
50
|
}
|
|
@@ -104,16 +104,10 @@ const setup = async (options)=>{
|
|
|
104
104
|
if (!header) {
|
|
105
105
|
throw new Error(`Cannot find header for ${blockHash}`);
|
|
106
106
|
}
|
|
107
|
-
const inherents = new _index1.InherentProviders(new _index1.SetTimestamp(), [
|
|
108
|
-
new _index1.SetValidationData(),
|
|
109
|
-
new _index1.ParaInherentEnter(),
|
|
110
|
-
new _index1.SetNimbusAuthorInherent(),
|
|
111
|
-
new _index1.SetBabeRandomness()
|
|
112
|
-
]);
|
|
113
107
|
const chain = new _index.Blockchain({
|
|
114
108
|
api,
|
|
115
109
|
buildBlockMode: options.buildBlockMode,
|
|
116
|
-
|
|
110
|
+
inherentProviders: _index2.inherentProviders,
|
|
117
111
|
db: options.db,
|
|
118
112
|
header: {
|
|
119
113
|
hash: blockHash,
|
|
@@ -23,3 +23,6 @@ export declare const prefixedChildKey: (prefix: HexString, key: HexString) => st
|
|
|
23
23
|
export declare const isPrefixedChildKey: (key: HexString) => boolean;
|
|
24
24
|
export declare const splitChildKey: (key: HexString) => never[] | [`0x${string}`, `0x${string}`];
|
|
25
25
|
export declare const stripChildPrefix: (key: HexString) => `0x${string}`;
|
|
26
|
+
export declare const getCurrentSlot: (chain: Blockchain) => Promise<number>;
|
|
27
|
+
export declare const getCurrentTimestamp: (chain: Blockchain) => Promise<bigint>;
|
|
28
|
+
export declare const getSlotDuration: (chain: Blockchain) => Promise<number>;
|
package/dist/cjs/utils/index.js
CHANGED
|
@@ -27,9 +27,18 @@ _export(exports, {
|
|
|
27
27
|
fetchKeysToArray: function() {
|
|
28
28
|
return fetchKeysToArray;
|
|
29
29
|
},
|
|
30
|
+
getCurrentSlot: function() {
|
|
31
|
+
return getCurrentSlot;
|
|
32
|
+
},
|
|
33
|
+
getCurrentTimestamp: function() {
|
|
34
|
+
return getCurrentTimestamp;
|
|
35
|
+
},
|
|
30
36
|
getParaId: function() {
|
|
31
37
|
return getParaId;
|
|
32
38
|
},
|
|
39
|
+
getSlotDuration: function() {
|
|
40
|
+
return getSlotDuration;
|
|
41
|
+
},
|
|
33
42
|
isPrefixedChildKey: function() {
|
|
34
43
|
return isPrefixedChildKey;
|
|
35
44
|
},
|
|
@@ -47,6 +56,7 @@ _export(exports, {
|
|
|
47
56
|
}
|
|
48
57
|
});
|
|
49
58
|
const _util = require("@polkadot/util");
|
|
59
|
+
const _index = require("../wasm-executor/index.js");
|
|
50
60
|
const _hex = require("@polkadot/util/hex");
|
|
51
61
|
_export_star(require("./set-storage.js"), exports);
|
|
52
62
|
_export_star(require("./time-travel.js"), exports);
|
|
@@ -136,3 +146,28 @@ const stripChildPrefix = (key)=>{
|
|
|
136
146
|
if (!child) return key;
|
|
137
147
|
return storageKey;
|
|
138
148
|
};
|
|
149
|
+
// use raw key here because some chain did not expose those storage to metadata
|
|
150
|
+
const POTENTIAL_SLOT_KEYS = [
|
|
151
|
+
'0x1cb6f36e027abb2091cfb5110ab5087f06155b3cd9a8c9e5e9a23fd5dc13a5ed',
|
|
152
|
+
'0x57f8dc2f5ab09467896f47300f04243806155b3cd9a8c9e5e9a23fd5dc13a5ed',
|
|
153
|
+
'0xab2a8d5eca218f218c6fda6b1d22bb926bc171ab77f6a731a6e80c34ee1eda19'
|
|
154
|
+
];
|
|
155
|
+
const getCurrentSlot = async (chain)=>{
|
|
156
|
+
const meta = await chain.head.meta;
|
|
157
|
+
for (const key of POTENTIAL_SLOT_KEYS){
|
|
158
|
+
const slotRaw = await chain.head.get(key);
|
|
159
|
+
if (slotRaw) {
|
|
160
|
+
return meta.registry.createType('Slot', (0, _util.hexToU8a)(slotRaw)).toNumber();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
throw new Error('Cannot find current slot');
|
|
164
|
+
};
|
|
165
|
+
const getCurrentTimestamp = async (chain)=>{
|
|
166
|
+
const meta = await chain.head.meta;
|
|
167
|
+
const timestamp = await chain.head.read('u64', meta.query.timestamp.now);
|
|
168
|
+
return timestamp?.toBigInt() ?? 0n;
|
|
169
|
+
};
|
|
170
|
+
const getSlotDuration = async (chain)=>{
|
|
171
|
+
const meta = await chain.head.meta;
|
|
172
|
+
return meta.consts.babe ? meta.consts.babe.expectedBlockTime.toNumber() : meta.query.aura ? (0, _index.getAuraSlotDuration)(await chain.head.wasm) : 12_000;
|
|
173
|
+
};
|
|
@@ -1,5 +1,2 @@
|
|
|
1
1
|
import { Blockchain } from '../blockchain/index.js';
|
|
2
|
-
export declare const getCurrentSlot: (chain: Blockchain) => Promise<number>;
|
|
3
|
-
export declare const getCurrentTimestamp: (chain: Blockchain) => Promise<bigint>;
|
|
4
|
-
export declare const getSlotDuration: (chain: Blockchain) => Promise<number>;
|
|
5
2
|
export declare const timeTravel: (chain: Blockchain, timestamp: number) => Promise<void>;
|
|
@@ -2,51 +2,18 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", {
|
|
3
3
|
value: true
|
|
4
4
|
});
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
get: all[name]
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
getCurrentSlot: function() {
|
|
13
|
-
return getCurrentSlot;
|
|
14
|
-
},
|
|
15
|
-
getCurrentTimestamp: function() {
|
|
16
|
-
return getCurrentTimestamp;
|
|
17
|
-
},
|
|
18
|
-
getSlotDuration: function() {
|
|
19
|
-
return getSlotDuration;
|
|
20
|
-
},
|
|
21
|
-
timeTravel: function() {
|
|
5
|
+
Object.defineProperty(exports, "timeTravel", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
22
8
|
return timeTravel;
|
|
23
9
|
}
|
|
24
10
|
});
|
|
25
11
|
const _util = require("@polkadot/util");
|
|
26
12
|
const _index = require("./index.js");
|
|
27
|
-
const _index1 = require("../wasm-executor/index.js");
|
|
28
13
|
const _setstorage = require("./set-storage.js");
|
|
29
|
-
const getCurrentSlot = async (chain)=>{
|
|
30
|
-
const meta = await chain.head.meta;
|
|
31
|
-
// use raw key here because some chain did not expose those storage to metadata
|
|
32
|
-
const slotRaw = meta.consts.babe ? await chain.head.get('0x1cb6f36e027abb2091cfb5110ab5087f06155b3cd9a8c9e5e9a23fd5dc13a5ed') // babe.currentSlot
|
|
33
|
-
: await chain.head.get('0x57f8dc2f5ab09467896f47300f04243806155b3cd9a8c9e5e9a23fd5dc13a5ed') // aura.currentSlot
|
|
34
|
-
;
|
|
35
|
-
if (!slotRaw) throw new Error('Cannot find current slot');
|
|
36
|
-
return meta.registry.createType('Slot', (0, _util.hexToU8a)(slotRaw)).toNumber();
|
|
37
|
-
};
|
|
38
|
-
const getCurrentTimestamp = async (chain)=>{
|
|
39
|
-
const meta = await chain.head.meta;
|
|
40
|
-
const timestamp = await chain.head.read('u64', meta.query.timestamp.now);
|
|
41
|
-
return timestamp?.toBigInt() ?? 0n;
|
|
42
|
-
};
|
|
43
|
-
const getSlotDuration = async (chain)=>{
|
|
44
|
-
const meta = await chain.head.meta;
|
|
45
|
-
return meta.consts.babe ? meta.consts.babe.expectedBlockTime.toNumber() : meta.query.aura ? (0, _index1.getAuraSlotDuration)(await chain.head.wasm) : 12_000;
|
|
46
|
-
};
|
|
47
14
|
const timeTravel = async (chain, timestamp)=>{
|
|
48
15
|
const meta = await chain.head.meta;
|
|
49
|
-
const slotDuration = await getSlotDuration(chain);
|
|
16
|
+
const slotDuration = await (0, _index.getSlotDuration)(chain);
|
|
50
17
|
const newSlot = Math.floor(timestamp / slotDuration);
|
|
51
18
|
// new timestamp
|
|
52
19
|
const storage = [
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { Header, TransactionValidityError } from '@polkadot/types/interfaces';
|
|
2
2
|
import { Block } from './block.js';
|
|
3
|
+
import { BuildBlockParams } from './txpool.js';
|
|
3
4
|
import { HexString } from '@polkadot/util/types';
|
|
5
|
+
import { InherentProvider } from './inherent/index.js';
|
|
4
6
|
import { TaskCallResponse } from '../wasm-executor/index.js';
|
|
5
7
|
export declare const newHeader: (head: Block, unsafeBlockHeight?: number) => Promise<Header>;
|
|
6
8
|
export type BuildBlockCallbacks = {
|
|
7
9
|
onApplyExtrinsicError?: (extrinsic: HexString, error: TransactionValidityError) => void;
|
|
8
10
|
onPhaseApplied?: (phase: 'initialize' | 'finalize' | number, resp: TaskCallResponse) => void;
|
|
9
11
|
};
|
|
10
|
-
export declare const buildBlock: (head: Block,
|
|
11
|
-
export declare const dryRunExtrinsic: (head: Block,
|
|
12
|
+
export declare const buildBlock: (head: Block, inherentProviders: InherentProvider[], params: BuildBlockParams, callbacks?: BuildBlockCallbacks) => Promise<[Block, HexString[]]>;
|
|
13
|
+
export declare const dryRunExtrinsic: (head: Block, inherentProviders: InherentProvider[], extrinsic: HexString | {
|
|
12
14
|
call: HexString;
|
|
13
15
|
address: string;
|
|
14
|
-
}) => Promise<TaskCallResponse>;
|
|
15
|
-
export declare const dryRunInherents: (head: Block,
|
|
16
|
+
}, params: BuildBlockParams) => Promise<TaskCallResponse>;
|
|
17
|
+
export declare const dryRunInherents: (head: Block, inherentProviders: InherentProvider[], params: BuildBlockParams) => Promise<[HexString, HexString | null][]>;
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { Block } from './block.js';
|
|
2
2
|
import { StorageLayer, StorageValueKind } from './storage-layer.js';
|
|
3
3
|
import { compactAddLength, hexToU8a, stringToHex, u8aConcat } from '@polkadot/util';
|
|
4
|
-
import { compactHex } from '../utils/index.js';
|
|
4
|
+
import { compactHex, getCurrentSlot } from '../utils/index.js';
|
|
5
5
|
import { defaultLogger, truncate } from '../logger.js';
|
|
6
|
-
import { getCurrentSlot } from '../utils/time-travel.js';
|
|
7
6
|
const logger = defaultLogger.child({
|
|
8
7
|
name: 'block-builder'
|
|
9
8
|
});
|
|
@@ -103,7 +102,7 @@ export const newHeader = async (head, unsafeBlockHeight)=>{
|
|
|
103
102
|
});
|
|
104
103
|
return header;
|
|
105
104
|
};
|
|
106
|
-
const initNewBlock = async (head, header,
|
|
105
|
+
const initNewBlock = async (head, header, inherentProviders, params, storageLayer, callback)=>{
|
|
107
106
|
const blockNumber = header.number.toNumber();
|
|
108
107
|
const hash = `0x${Math.round(Math.random() * 100000000).toString(16).padEnd(64, '0')}`;
|
|
109
108
|
const newBlock = new Block(head.chain, blockNumber, hash, head, {
|
|
@@ -131,16 +130,20 @@ const initNewBlock = async (head, header, inherents, storageLayer, callback)=>{
|
|
|
131
130
|
}
|
|
132
131
|
callback?.onPhaseApplied?.('initialize', resp);
|
|
133
132
|
}
|
|
133
|
+
const inherents = [];
|
|
134
134
|
const layers = [];
|
|
135
135
|
// apply inherents
|
|
136
|
-
for (const
|
|
136
|
+
for (const inherentProvider of inherentProviders){
|
|
137
137
|
try {
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
138
|
+
const extrinsics = await inherentProvider.createInherents(newBlock, params);
|
|
139
|
+
if (extrinsics.length === 0) {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const resp = await newBlock.call('BlockBuilder_apply_extrinsic', extrinsics);
|
|
141
143
|
const layer = newBlock.pushStorageLayer();
|
|
142
144
|
layer.setAll(resp.storageDiff);
|
|
143
145
|
layers.push(layer);
|
|
146
|
+
inherents.push(...extrinsics);
|
|
144
147
|
callback?.onPhaseApplied?.(layers.length - 1, resp);
|
|
145
148
|
} catch (e) {
|
|
146
149
|
logger.warn('Failed to apply inherents %o %s', e, e);
|
|
@@ -149,18 +152,20 @@ const initNewBlock = async (head, header, inherents, storageLayer, callback)=>{
|
|
|
149
152
|
}
|
|
150
153
|
return {
|
|
151
154
|
block: newBlock,
|
|
152
|
-
layers
|
|
155
|
+
layers,
|
|
156
|
+
inherents
|
|
153
157
|
};
|
|
154
158
|
};
|
|
155
|
-
export const buildBlock = async (head,
|
|
159
|
+
export const buildBlock = async (head, inherentProviders, params, callbacks)=>{
|
|
160
|
+
const { transactions: extrinsics, upwardMessages: ump, unsafeBlockHeight } = params;
|
|
156
161
|
const registry = await head.registry;
|
|
157
162
|
const header = await newHeader(head, unsafeBlockHeight);
|
|
158
163
|
const newBlockNumber = header.number.toNumber();
|
|
159
164
|
logger.info({
|
|
160
165
|
number: newBlockNumber,
|
|
161
|
-
|
|
166
|
+
extrinsics: extrinsics.map(truncate),
|
|
162
167
|
umpCount: Object.keys(ump).length
|
|
163
|
-
},
|
|
168
|
+
}, `${await head.chain.api.getSystemChain()} building #${newBlockNumber.toLocaleString()}`);
|
|
164
169
|
let layer;
|
|
165
170
|
// apply ump via storage override hack
|
|
166
171
|
if (Object.keys(ump).length > 0) {
|
|
@@ -249,7 +254,7 @@ export const buildBlock = async (head, inherents, extrinsics, ump, callbacks, un
|
|
|
249
254
|
layer.set(compactHex(meta.query.ump.needsDispatch()), needsDispatch.toHex());
|
|
250
255
|
}
|
|
251
256
|
}
|
|
252
|
-
const { block: newBlock } = await initNewBlock(head, header,
|
|
257
|
+
const { block: newBlock, inherents } = await initNewBlock(head, header, inherentProviders, params, layer);
|
|
253
258
|
const pendingExtrinsics = [];
|
|
254
259
|
const includedExtrinsic = [];
|
|
255
260
|
// apply extrinsics
|
|
@@ -298,21 +303,21 @@ export const buildBlock = async (head, inherents, extrinsics, ump, callbacks, un
|
|
|
298
303
|
storageDiff
|
|
299
304
|
});
|
|
300
305
|
logger.info({
|
|
301
|
-
number:
|
|
306
|
+
number: finalBlock.number,
|
|
302
307
|
hash: finalBlock.hash,
|
|
303
308
|
extrinsics: truncate(includedExtrinsic),
|
|
304
|
-
|
|
309
|
+
pendingExtrinsics: pendingExtrinsics.map(truncate),
|
|
305
310
|
ump: truncate(ump)
|
|
306
|
-
},
|
|
311
|
+
}, `${await head.chain.api.getSystemChain()} new head #${finalBlock.number.toLocaleString()}`);
|
|
307
312
|
return [
|
|
308
313
|
finalBlock,
|
|
309
314
|
pendingExtrinsics
|
|
310
315
|
];
|
|
311
316
|
};
|
|
312
|
-
export const dryRunExtrinsic = async (head,
|
|
317
|
+
export const dryRunExtrinsic = async (head, inherentProviders, extrinsic, params)=>{
|
|
313
318
|
const registry = await head.registry;
|
|
314
319
|
const header = await newHeader(head);
|
|
315
|
-
const { block: newBlock } = await initNewBlock(head, header,
|
|
320
|
+
const { block: newBlock } = await initNewBlock(head, header, inherentProviders, params);
|
|
316
321
|
if (typeof extrinsic !== 'string') {
|
|
317
322
|
if (!head.chain.mockSignatureHost) {
|
|
318
323
|
throw new Error('Cannot fake signature because mock signature host is not enabled. Start chain with `mockSignatureHost: true`');
|
|
@@ -351,9 +356,9 @@ export const dryRunExtrinsic = async (head, inherents, extrinsic)=>{
|
|
|
351
356
|
extrinsic
|
|
352
357
|
]);
|
|
353
358
|
};
|
|
354
|
-
export const dryRunInherents = async (head,
|
|
359
|
+
export const dryRunInherents = async (head, inherentProviders, params)=>{
|
|
355
360
|
const header = await newHeader(head);
|
|
356
|
-
const { layers } = await initNewBlock(head, header,
|
|
361
|
+
const { layers } = await initNewBlock(head, header, inherentProviders, params);
|
|
357
362
|
const storage = {};
|
|
358
363
|
for (const layer of layers){
|
|
359
364
|
await layer.mergeInto(storage);
|
|
@@ -17,7 +17,7 @@ export interface Options {
|
|
|
17
17
|
/** Build block mode. Default to Batch. */
|
|
18
18
|
buildBlockMode?: BuildBlockMode;
|
|
19
19
|
/** Inherent provider, for creating inherents. */
|
|
20
|
-
|
|
20
|
+
inherentProviders: InherentProvider[];
|
|
21
21
|
/** Datasource for caching storage and blocks data. */
|
|
22
22
|
db?: Database;
|
|
23
23
|
/** Used to create the initial head. */
|
|
@@ -48,7 +48,7 @@ export interface Options {
|
|
|
48
48
|
* const chain = new Blockchain({
|
|
49
49
|
* api,
|
|
50
50
|
* buildBlockMode: BuildBlockMode.Manual,
|
|
51
|
-
*
|
|
51
|
+
* inherentProviders,
|
|
52
52
|
* header: {
|
|
53
53
|
* hash: blockHash,
|
|
54
54
|
* number: Number(header.number),
|
|
@@ -79,7 +79,7 @@ export declare class Blockchain {
|
|
|
79
79
|
/**
|
|
80
80
|
* @param options - Options for instantiating the blockchain
|
|
81
81
|
*/
|
|
82
|
-
constructor({ api, buildBlockMode,
|
|
82
|
+
constructor({ api, buildBlockMode, inherentProviders, db, header, mockSignatureHost, allowUnresolvedImports, runtimeLogLevel, registeredTypes, offchainWorker, maxMemoryBlockCount, }: Options);
|
|
83
83
|
get head(): Block;
|
|
84
84
|
get txPool(): TxPool;
|
|
85
85
|
get runtimeLogLevel(): number;
|
|
@@ -163,7 +163,7 @@ export declare class Blockchain {
|
|
|
163
163
|
/**
|
|
164
164
|
* Get inherents of head.
|
|
165
165
|
*/
|
|
166
|
-
getInherents():
|
|
166
|
+
getInherents(): InherentProvider[];
|
|
167
167
|
/**
|
|
168
168
|
* Close the db and disconnect api.
|
|
169
169
|
*/
|
|
@@ -23,7 +23,7 @@ const logger = defaultLogger.child({
|
|
|
23
23
|
* const chain = new Blockchain({
|
|
24
24
|
* api,
|
|
25
25
|
* buildBlockMode: BuildBlockMode.Manual,
|
|
26
|
-
*
|
|
26
|
+
* inherentProviders,
|
|
27
27
|
* header: {
|
|
28
28
|
* hash: blockHash,
|
|
29
29
|
* number: Number(header.number),
|
|
@@ -43,7 +43,7 @@ const logger = defaultLogger.child({
|
|
|
43
43
|
#runtimeLogLevel;
|
|
44
44
|
/** Polkadot.js custom types registration. */ registeredTypes;
|
|
45
45
|
#txpool;
|
|
46
|
-
#
|
|
46
|
+
#inherentProviders;
|
|
47
47
|
#head;
|
|
48
48
|
#blocksByNumber = new Map();
|
|
49
49
|
#blocksByHash = new Map();
|
|
@@ -65,7 +65,7 @@ const logger = defaultLogger.child({
|
|
|
65
65
|
});
|
|
66
66
|
/**
|
|
67
67
|
* @param options - Options for instantiating the blockchain
|
|
68
|
-
*/ constructor({ api, buildBlockMode,
|
|
68
|
+
*/ constructor({ api, buildBlockMode, inherentProviders, db, header, mockSignatureHost = false, allowUnresolvedImports = false, runtimeLogLevel = 0, registeredTypes = {}, offchainWorker = false, maxMemoryBlockCount = 500 }){
|
|
69
69
|
this.api = api;
|
|
70
70
|
this.db = db;
|
|
71
71
|
this.mockSignatureHost = mockSignatureHost;
|
|
@@ -74,8 +74,8 @@ const logger = defaultLogger.child({
|
|
|
74
74
|
this.registeredTypes = registeredTypes;
|
|
75
75
|
this.#head = new Block(this, header.number, header.hash);
|
|
76
76
|
this.#registerBlock(this.#head);
|
|
77
|
-
this.#txpool = new TxPool(this,
|
|
78
|
-
this.#
|
|
77
|
+
this.#txpool = new TxPool(this, inherentProviders, buildBlockMode);
|
|
78
|
+
this.#inherentProviders = inherentProviders;
|
|
79
79
|
this.headState = new HeadState(this.#head);
|
|
80
80
|
if (offchainWorker) {
|
|
81
81
|
this.offchainWorker = new OffchainWorker();
|
|
@@ -311,13 +311,13 @@ const logger = defaultLogger.child({
|
|
|
311
311
|
throw new Error(`Cannot find block ${at}`);
|
|
312
312
|
}
|
|
313
313
|
const registry = await head.registry;
|
|
314
|
-
const
|
|
314
|
+
const params = {
|
|
315
315
|
transactions: [],
|
|
316
316
|
downwardMessages: [],
|
|
317
317
|
upwardMessages: [],
|
|
318
318
|
horizontalMessages: {}
|
|
319
|
-
}
|
|
320
|
-
const { result, storageDiff } = await dryRunExtrinsic(head,
|
|
319
|
+
};
|
|
320
|
+
const { result, storageDiff } = await dryRunExtrinsic(head, this.#inherentProviders, extrinsic, params);
|
|
321
321
|
const outcome = registry.createType('ApplyExtrinsicResult', result);
|
|
322
322
|
return {
|
|
323
323
|
outcome,
|
|
@@ -333,13 +333,13 @@ const logger = defaultLogger.child({
|
|
|
333
333
|
if (!head) {
|
|
334
334
|
throw new Error(`Cannot find block ${at}`);
|
|
335
335
|
}
|
|
336
|
-
const
|
|
336
|
+
const params = {
|
|
337
337
|
transactions: [],
|
|
338
338
|
downwardMessages: [],
|
|
339
339
|
upwardMessages: [],
|
|
340
340
|
horizontalMessages: hrmp
|
|
341
|
-
}
|
|
342
|
-
return dryRunInherents(head,
|
|
341
|
+
};
|
|
342
|
+
return dryRunInherents(head, this.#inherentProviders, params);
|
|
343
343
|
}
|
|
344
344
|
/**
|
|
345
345
|
* Dry run dmp messages in block `at`.
|
|
@@ -350,13 +350,13 @@ const logger = defaultLogger.child({
|
|
|
350
350
|
if (!head) {
|
|
351
351
|
throw new Error(`Cannot find block ${at}`);
|
|
352
352
|
}
|
|
353
|
-
const
|
|
353
|
+
const params = {
|
|
354
354
|
transactions: [],
|
|
355
355
|
downwardMessages: dmp,
|
|
356
356
|
upwardMessages: [],
|
|
357
357
|
horizontalMessages: {}
|
|
358
|
-
}
|
|
359
|
-
return dryRunInherents(head,
|
|
358
|
+
};
|
|
359
|
+
return dryRunInherents(head, this.#inherentProviders, params);
|
|
360
360
|
}
|
|
361
361
|
/**
|
|
362
362
|
* Dry run ump messages in block `at`.
|
|
@@ -392,25 +392,18 @@ const logger = defaultLogger.child({
|
|
|
392
392
|
]);
|
|
393
393
|
}
|
|
394
394
|
head.pushStorageLayer().setAll(storageValues);
|
|
395
|
-
const
|
|
395
|
+
const params = {
|
|
396
396
|
transactions: [],
|
|
397
397
|
downwardMessages: [],
|
|
398
398
|
upwardMessages: [],
|
|
399
399
|
horizontalMessages: {}
|
|
400
|
-
}
|
|
401
|
-
return dryRunInherents(head,
|
|
400
|
+
};
|
|
401
|
+
return dryRunInherents(head, this.#inherentProviders, params);
|
|
402
402
|
}
|
|
403
403
|
/**
|
|
404
404
|
* Get inherents of head.
|
|
405
|
-
*/
|
|
406
|
-
|
|
407
|
-
const inherents = await this.#inherentProvider.createInherents(this.head, {
|
|
408
|
-
transactions: [],
|
|
409
|
-
downwardMessages: [],
|
|
410
|
-
upwardMessages: [],
|
|
411
|
-
horizontalMessages: {}
|
|
412
|
-
});
|
|
413
|
-
return inherents;
|
|
405
|
+
*/ getInherents() {
|
|
406
|
+
return this.#inherentProviders;
|
|
414
407
|
}
|
|
415
408
|
/**
|
|
416
409
|
* Close the db and disconnect api.
|
|
@@ -1,19 +1,12 @@
|
|
|
1
1
|
import { Block } from '../block.js';
|
|
2
2
|
import { BuildBlockParams } from '../txpool.js';
|
|
3
3
|
import { HexString } from '@polkadot/util/types';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export type InherentProvider = CreateInherents;
|
|
12
|
-
export declare class SetTimestamp implements InherentProvider {
|
|
13
|
-
createInherents(parent: Block): Promise<HexString[]>;
|
|
14
|
-
}
|
|
15
|
-
export declare class InherentProviders implements InherentProvider {
|
|
16
|
-
#private;
|
|
17
|
-
constructor(base: InherentProvider, providers: CreateInherents[]);
|
|
18
|
-
createInherents(parent: Block, params: BuildBlockParams): Promise<HexString[]>;
|
|
4
|
+
import { ParaInherentEnter } from './para-enter.js';
|
|
5
|
+
import { SetBabeRandomness } from './parachain/babe-randomness.js';
|
|
6
|
+
import { SetNimbusAuthorInherent } from './parachain/nimbus-author-inherent.js';
|
|
7
|
+
import { SetTimestamp } from './timestamp.js';
|
|
8
|
+
import { SetValidationData } from './parachain/validation-data.js';
|
|
9
|
+
export interface InherentProvider {
|
|
10
|
+
createInherents(newBlock: Block, params: BuildBlockParams): Promise<HexString[]>;
|
|
19
11
|
}
|
|
12
|
+
export declare const inherentProviders: (ParaInherentEnter | SetBabeRandomness | SetNimbusAuthorInherent | SetTimestamp | SetValidationData)[];
|
|
@@ -1,32 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
new GenericExtrinsic(meta.registry, meta.tx.timestamp.set(currentTimestamp + BigInt(slotDuration))).toHex()
|
|
14
|
-
];
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
export class InherentProviders {
|
|
18
|
-
#base;
|
|
19
|
-
#providers;
|
|
20
|
-
constructor(base, providers){
|
|
21
|
-
this.#base = base;
|
|
22
|
-
this.#providers = providers;
|
|
23
|
-
}
|
|
24
|
-
async createInherents(parent, params) {
|
|
25
|
-
const base = await this.#base.createInherents(parent, params);
|
|
26
|
-
const extra = await Promise.all(this.#providers.map((provider)=>provider.createInherents(parent, params)));
|
|
27
|
-
return [
|
|
28
|
-
...base,
|
|
29
|
-
...extra.flat()
|
|
30
|
-
];
|
|
31
|
-
}
|
|
32
|
-
}
|
|
1
|
+
import { ParaInherentEnter } from './para-enter.js';
|
|
2
|
+
import { SetBabeRandomness } from './parachain/babe-randomness.js';
|
|
3
|
+
import { SetNimbusAuthorInherent } from './parachain/nimbus-author-inherent.js';
|
|
4
|
+
import { SetTimestamp } from './timestamp.js';
|
|
5
|
+
import { SetValidationData } from './parachain/validation-data.js';
|
|
6
|
+
export const inherentProviders = [
|
|
7
|
+
new SetTimestamp(),
|
|
8
|
+
new SetValidationData(),
|
|
9
|
+
new ParaInherentEnter(),
|
|
10
|
+
new SetNimbusAuthorInherent(),
|
|
11
|
+
new SetBabeRandomness()
|
|
12
|
+
];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { HexString } from '@polkadot/util/types';
|
|
2
2
|
import { Block } from '../block.js';
|
|
3
3
|
import { BuildBlockParams } from '../txpool.js';
|
|
4
|
-
import {
|
|
5
|
-
export declare class ParaInherentEnter implements
|
|
6
|
-
createInherents(
|
|
4
|
+
import { InherentProvider } from './index.js';
|
|
5
|
+
export declare class ParaInherentEnter implements InherentProvider {
|
|
6
|
+
createInherents(newBlock: Block, _params: BuildBlockParams): Promise<HexString[]>;
|
|
7
7
|
}
|