@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.
Files changed (51) hide show
  1. package/dist/cjs/blockchain/block-builder.d.ts +6 -4
  2. package/dist/cjs/blockchain/block-builder.js +25 -20
  3. package/dist/cjs/blockchain/index.d.ts +4 -4
  4. package/dist/cjs/blockchain/index.js +19 -26
  5. package/dist/cjs/blockchain/inherent/index.d.ts +8 -15
  6. package/dist/cjs/blockchain/inherent/index.js +13 -100
  7. package/dist/cjs/blockchain/inherent/para-enter.d.ts +3 -3
  8. package/dist/cjs/blockchain/inherent/para-enter.js +3 -1
  9. package/dist/cjs/blockchain/inherent/parachain/babe-randomness.d.ts +3 -3
  10. package/dist/cjs/blockchain/inherent/parachain/babe-randomness.js +3 -1
  11. package/dist/cjs/blockchain/inherent/parachain/nimbus-author-inherent.d.ts +3 -3
  12. package/dist/cjs/blockchain/inherent/parachain/nimbus-author-inherent.js +20 -1
  13. package/dist/cjs/blockchain/inherent/parachain/validation-data.d.ts +3 -3
  14. package/dist/cjs/blockchain/inherent/parachain/validation-data.js +3 -1
  15. package/dist/cjs/blockchain/inherent/timestamp.d.ts +6 -0
  16. package/dist/cjs/blockchain/inherent/timestamp.js +24 -0
  17. package/dist/cjs/blockchain/txpool.d.ts +1 -1
  18. package/dist/cjs/blockchain/txpool.js +6 -8
  19. package/dist/cjs/logger.js +8 -2
  20. package/dist/cjs/setup.js +7 -13
  21. package/dist/cjs/utils/decoder.js +1 -1
  22. package/dist/cjs/utils/index.d.ts +3 -0
  23. package/dist/cjs/utils/index.js +35 -0
  24. package/dist/cjs/utils/time-travel.d.ts +0 -3
  25. package/dist/cjs/utils/time-travel.js +4 -37
  26. package/dist/esm/blockchain/block-builder.d.ts +6 -4
  27. package/dist/esm/blockchain/block-builder.js +24 -19
  28. package/dist/esm/blockchain/index.d.ts +4 -4
  29. package/dist/esm/blockchain/index.js +19 -26
  30. package/dist/esm/blockchain/inherent/index.d.ts +8 -15
  31. package/dist/esm/blockchain/inherent/index.js +12 -32
  32. package/dist/esm/blockchain/inherent/para-enter.d.ts +3 -3
  33. package/dist/esm/blockchain/inherent/para-enter.js +3 -1
  34. package/dist/esm/blockchain/inherent/parachain/babe-randomness.d.ts +3 -3
  35. package/dist/esm/blockchain/inherent/parachain/babe-randomness.js +3 -1
  36. package/dist/esm/blockchain/inherent/parachain/nimbus-author-inherent.d.ts +3 -3
  37. package/dist/esm/blockchain/inherent/parachain/nimbus-author-inherent.js +20 -1
  38. package/dist/esm/blockchain/inherent/parachain/validation-data.d.ts +3 -3
  39. package/dist/esm/blockchain/inherent/parachain/validation-data.js +3 -1
  40. package/dist/esm/blockchain/inherent/timestamp.d.ts +6 -0
  41. package/dist/esm/blockchain/inherent/timestamp.js +14 -0
  42. package/dist/esm/blockchain/txpool.d.ts +1 -1
  43. package/dist/esm/blockchain/txpool.js +5 -7
  44. package/dist/esm/logger.js +8 -2
  45. package/dist/esm/setup.js +2 -8
  46. package/dist/esm/utils/decoder.js +1 -1
  47. package/dist/esm/utils/index.d.ts +3 -0
  48. package/dist/esm/utils/index.js +27 -1
  49. package/dist/esm/utils/time-travel.d.ts +0 -3
  50. package/dist/esm/utils/time-travel.js +2 -21
  51. package/package.json +2 -2
@@ -1,6 +1,8 @@
1
1
  import { GenericExtrinsic } from '@polkadot/types';
2
2
  export class ParaInherentEnter {
3
- async createInherents(parent, _params) {
3
+ async createInherents(newBlock, _params) {
4
+ const parent = await newBlock.parentBlock;
5
+ if (!parent) throw new Error('parent block not found');
4
6
  const meta = await parent.meta;
5
7
  if (!meta.tx.paraInherent?.enter) {
6
8
  return [];
@@ -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 { CreateInherents } from '../index.js';
5
- export declare class SetBabeRandomness implements CreateInherents {
6
- createInherents(parent: Block, _params: BuildBlockParams): Promise<HexString[]>;
4
+ import { InherentProvider } from '../index.js';
5
+ export declare class SetBabeRandomness implements InherentProvider {
6
+ createInherents(newBlock: Block, _params: BuildBlockParams): Promise<HexString[]>;
7
7
  }
@@ -1,7 +1,9 @@
1
1
  import { GenericExtrinsic } from '@polkadot/types';
2
2
  // Support for Moonbeam pallet-randomness mandatory inherent
3
3
  export class SetBabeRandomness {
4
- async createInherents(parent, _params) {
4
+ async createInherents(newBlock, _params) {
5
+ const parent = await newBlock.parentBlock;
6
+ if (!parent) throw new Error('parent block not found');
5
7
  const meta = await parent.meta;
6
8
  if (!meta.tx.randomness?.setBabeRandomnessResults) {
7
9
  return [];
@@ -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 { CreateInherents } from '../index.js';
5
- export declare class SetNimbusAuthorInherent implements CreateInherents {
6
- createInherents(parent: Block, _params: BuildBlockParams): Promise<HexString[]>;
4
+ import { InherentProvider } from '../index.js';
5
+ export declare class SetNimbusAuthorInherent implements InherentProvider {
6
+ createInherents(newBlock: Block, _params: BuildBlockParams): Promise<HexString[]>;
7
7
  }
@@ -1,11 +1,30 @@
1
1
  import { GenericExtrinsic } from '@polkadot/types';
2
+ import { compactHex } from '../../../utils/index.js';
2
3
  // Support for Nimbus Author Inherent
3
4
  export class SetNimbusAuthorInherent {
4
- async createInherents(parent, _params) {
5
+ async createInherents(newBlock, _params) {
6
+ const parent = await newBlock.parentBlock;
7
+ if (!parent) throw new Error('parent block not found');
5
8
  const meta = await parent.meta;
6
9
  if (!meta.tx.authorInherent?.kickOffAuthorshipValidation) {
7
10
  return [];
8
11
  }
12
+ // mock author inherent data and authorities noting data
13
+ const layer = newBlock.pushStorageLayer();
14
+ const accountType = meta.registry.hasType('NimbusPrimitivesNimbusCryptoPublic') ? 'NimbusPrimitivesNimbusCryptoPublic' : 'AccountId';
15
+ const alice = meta.registry.hasType('NimbusPrimitivesNimbusCryptoPublic') ? '0x567b6ddb05396c0a83853b6f40d27450534c7963df8619b8c6064480c4db9703' : '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
16
+ layer.set(compactHex(meta.query.authorInherent.author()), meta.registry.createType(accountType, alice).toHex());
17
+ if (meta.query.authoritiesNoting) {
18
+ layer.set(compactHex(meta.query.authoritiesNoting.authorities()), meta.registry.createType(`Vec<${accountType}>`, [
19
+ alice
20
+ ]).toHex());
21
+ layer.set(compactHex(meta.query.authoritiesNoting.didSetOrchestratorAuthorityData()), meta.registry.createType('bool', true).toHex());
22
+ }
23
+ if (meta.query.parachainStaking) {
24
+ layer.set(compactHex(meta.query.parachainStaking.selectedCandidates()), meta.registry.createType(`Vec<${accountType}>`, [
25
+ alice
26
+ ]).toHex());
27
+ }
9
28
  return [
10
29
  new GenericExtrinsic(meta.registry, meta.tx.authorInherent.kickOffAuthorshipValidation()).toHex()
11
30
  ];
@@ -1,7 +1,7 @@
1
1
  import { HexString } from '@polkadot/util/types';
2
2
  import { Block } from '../../block.js';
3
3
  import { BuildBlockParams, DownwardMessage, HorizontalMessage } from '../../txpool.js';
4
- import { CreateInherents } from '../index.js';
4
+ import { InherentProvider } from '../index.js';
5
5
  export type ValidationData = {
6
6
  downwardMessages: DownwardMessage[];
7
7
  horizontalMessages: Record<number, HorizontalMessage[]>;
@@ -14,6 +14,6 @@ export type ValidationData = {
14
14
  trieNodes: HexString[];
15
15
  };
16
16
  };
17
- export declare class SetValidationData implements CreateInherents {
18
- createInherents(parent: Block, params: BuildBlockParams): Promise<HexString[]>;
17
+ export declare class SetValidationData implements InherentProvider {
18
+ createInherents(newBlock: Block, params: BuildBlockParams): Promise<HexString[]>;
19
19
  }
@@ -57,7 +57,9 @@ const getValidationData = async (parent)=>{
57
57
  return meta.registry.createType('GenericExtrinsic', validationDataExtrinsic).args[0].toJSON();
58
58
  };
59
59
  export class SetValidationData {
60
- async createInherents(parent, params) {
60
+ async createInherents(newBlock, params) {
61
+ const parent = await newBlock.parentBlock;
62
+ if (!parent) throw new Error('parent block not found');
61
63
  const meta = await parent.meta;
62
64
  if (!meta.tx.parachainSystem?.setValidationData) {
63
65
  return [];
@@ -0,0 +1,6 @@
1
+ import { Block } from '../block.js';
2
+ import { HexString } from '@polkadot/util/types';
3
+ import { InherentProvider } from './index.js';
4
+ export declare class SetTimestamp implements InherentProvider {
5
+ createInherents(newBlock: Block): Promise<HexString[]>;
6
+ }
@@ -0,0 +1,14 @@
1
+ import { GenericExtrinsic } from '@polkadot/types';
2
+ import { getCurrentTimestamp, getSlotDuration } from '../../utils/index.js';
3
+ export class SetTimestamp {
4
+ async createInherents(newBlock) {
5
+ const parent = await newBlock.parentBlock;
6
+ if (!parent) throw new Error('parent block not found');
7
+ const meta = await parent.meta;
8
+ const slotDuration = await getSlotDuration(parent.chain);
9
+ const currentTimestamp = await getCurrentTimestamp(parent.chain);
10
+ return [
11
+ new GenericExtrinsic(meta.registry, meta.tx.timestamp.set(currentTimestamp + BigInt(slotDuration))).toHex()
12
+ ];
13
+ }
14
+ }
@@ -29,7 +29,7 @@ export interface BuildBlockParams {
29
29
  export declare class TxPool {
30
30
  #private;
31
31
  readonly event: EventEmitter<string | symbol, any>;
32
- constructor(chain: Blockchain, inherentProvider: InherentProvider, mode?: BuildBlockMode);
32
+ constructor(chain: Blockchain, inherentProviders: InherentProvider[], mode?: BuildBlockMode);
33
33
  get pendingExtrinsics(): HexString[];
34
34
  get ump(): Record<number, HexString[]>;
35
35
  get dmp(): DownwardMessage[];
@@ -20,14 +20,14 @@ export class TxPool {
20
20
  #dmp = [];
21
21
  #hrmp = {};
22
22
  #mode;
23
- #inherentProvider;
23
+ #inherentProviders;
24
24
  #pendingBlocks = [];
25
25
  event = new EventEmitter();
26
26
  #isBuilding = false;
27
- constructor(chain, inherentProvider, mode = "Batch"){
27
+ constructor(chain, inherentProviders, mode = "Batch"){
28
28
  this.#chain = chain;
29
29
  this.#mode = mode;
30
- this.#inherentProvider = inherentProvider;
30
+ this.#inherentProviders = inherentProviders;
31
31
  }
32
32
  get pendingExtrinsics() {
33
33
  return this.#pool.map(({ extrinsic })=>extrinsic);
@@ -189,9 +189,7 @@ export class TxPool {
189
189
  logger.trace({
190
190
  params
191
191
  }, 'build block');
192
- const head = this.#chain.head;
193
- const inherents = await this.#inherentProvider.createInherents(head, params);
194
- const [newBlock, pendingExtrinsics] = await buildBlock(head, inherents, params.transactions, params.upwardMessages, {
192
+ const [newBlock, pendingExtrinsics] = await buildBlock(this.#chain.head, this.#inherentProviders, params, {
195
193
  onApplyExtrinsicError: (extrinsic, error)=>{
196
194
  this.event.emit(APPLY_EXTRINSIC_ERROR, [
197
195
  extrinsic,
@@ -210,7 +208,7 @@ export class TxPool {
210
208
  logger.trace(truncate(resp.storageDiff), `Apply extrinsic ${phase}`);
211
209
  }
212
210
  } : undefined
213
- }, params.unsafeBlockHeight);
211
+ });
214
212
  for (const extrinsic of pendingExtrinsics){
215
213
  this.#pool.push({
216
214
  extrinsic,
@@ -1,8 +1,14 @@
1
1
  import { pino } from 'pino';
2
+ const level = typeof process === 'object' && process.env.LOG_LEVEL || 'info';
3
+ const hideObject = typeof process === 'object' && !!process.env.LOG_COMPACT || false;
2
4
  export const pinoLogger = pino({
3
- level: typeof process === 'object' && process.env.LOG_LEVEL || 'info',
5
+ level,
4
6
  transport: {
5
- target: 'pino-pretty'
7
+ target: 'pino-pretty',
8
+ options: {
9
+ ignore: 'pid,hostname',
10
+ hideObject
11
+ }
6
12
  }
7
13
  });
8
14
  export const defaultLogger = pinoLogger.child({
package/dist/esm/setup.js CHANGED
@@ -3,9 +3,9 @@ import { HttpProvider, WsProvider } from '@polkadot/rpc-provider';
3
3
  import { compactAddLength } from '@polkadot/util';
4
4
  import { Api } from './api.js';
5
5
  import { Blockchain } from './blockchain/index.js';
6
- import { InherentProviders, ParaInherentEnter, SetBabeRandomness, SetNimbusAuthorInherent, SetTimestamp, SetValidationData } from './blockchain/inherent/index.js';
7
6
  import { defaultLogger } from './logger.js';
8
7
  import { getSlotDuration, setStorage } from './index.js';
8
+ import { inherentProviders } from './blockchain/inherent/index.js';
9
9
  export const genesisSetup = async (chain, genesis)=>{
10
10
  const meta = await chain.head.meta;
11
11
  const timestamp = Date.now();
@@ -86,16 +86,10 @@ export const setup = async (options)=>{
86
86
  if (!header) {
87
87
  throw new Error(`Cannot find header for ${blockHash}`);
88
88
  }
89
- const inherents = new InherentProviders(new SetTimestamp(), [
90
- new SetValidationData(),
91
- new ParaInherentEnter(),
92
- new SetNimbusAuthorInherent(),
93
- new SetBabeRandomness()
94
- ]);
95
89
  const chain = new Blockchain({
96
90
  api,
97
91
  buildBlockMode: options.buildBlockMode,
98
- inherentProvider: inherents,
92
+ inherentProviders,
99
93
  db: options.db,
100
94
  header: {
101
95
  hash: blockHash,
@@ -5,7 +5,7 @@ import _ from 'lodash';
5
5
  import { decodeWellKnownKey } from './well-known-keys.js';
6
6
  import { defaultLogger } from '../logger.js';
7
7
  const logger = defaultLogger.child({
8
- module: 'decoder'
8
+ name: 'decoder'
9
9
  });
10
10
  const _CACHE = new LRUCache({
11
11
  max: 20 /* max 20 registries */
@@ -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>;
@@ -1,4 +1,5 @@
1
- import { compactStripLength, u8aToHex } from '@polkadot/util';
1
+ import { compactStripLength, hexToU8a, u8aToHex } from '@polkadot/util';
2
+ import { getAuraSlotDuration } from '../wasm-executor/index.js';
2
3
  import { hexAddPrefix, hexStripPrefix } from '@polkadot/util/hex';
3
4
  export * from './set-storage.js';
4
5
  export * from './time-travel.js';
@@ -81,3 +82,28 @@ export const stripChildPrefix = (key)=>{
81
82
  if (!child) return key;
82
83
  return storageKey;
83
84
  };
85
+ // use raw key here because some chain did not expose those storage to metadata
86
+ const POTENTIAL_SLOT_KEYS = [
87
+ '0x1cb6f36e027abb2091cfb5110ab5087f06155b3cd9a8c9e5e9a23fd5dc13a5ed',
88
+ '0x57f8dc2f5ab09467896f47300f04243806155b3cd9a8c9e5e9a23fd5dc13a5ed',
89
+ '0xab2a8d5eca218f218c6fda6b1d22bb926bc171ab77f6a731a6e80c34ee1eda19'
90
+ ];
91
+ export const getCurrentSlot = async (chain)=>{
92
+ const meta = await chain.head.meta;
93
+ for (const key of POTENTIAL_SLOT_KEYS){
94
+ const slotRaw = await chain.head.get(key);
95
+ if (slotRaw) {
96
+ return meta.registry.createType('Slot', hexToU8a(slotRaw)).toNumber();
97
+ }
98
+ }
99
+ throw new Error('Cannot find current slot');
100
+ };
101
+ export const getCurrentTimestamp = async (chain)=>{
102
+ const meta = await chain.head.meta;
103
+ const timestamp = await chain.head.read('u64', meta.query.timestamp.now);
104
+ return timestamp?.toBigInt() ?? 0n;
105
+ };
106
+ export const getSlotDuration = async (chain)=>{
107
+ const meta = await chain.head.meta;
108
+ return meta.consts.babe ? meta.consts.babe.expectedBlockTime.toNumber() : meta.query.aura ? getAuraSlotDuration(await chain.head.wasm) : 12_000;
109
+ };
@@ -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>;
@@ -1,25 +1,6 @@
1
- import { hexToU8a, u8aToHex } from '@polkadot/util';
2
- import { compactHex } from './index.js';
3
- import { getAuraSlotDuration } from '../wasm-executor/index.js';
1
+ import { u8aToHex } from '@polkadot/util';
2
+ import { compactHex, getSlotDuration } from './index.js';
4
3
  import { setStorage } from './set-storage.js';
5
- export const getCurrentSlot = async (chain)=>{
6
- const meta = await chain.head.meta;
7
- // use raw key here because some chain did not expose those storage to metadata
8
- const slotRaw = meta.consts.babe ? await chain.head.get('0x1cb6f36e027abb2091cfb5110ab5087f06155b3cd9a8c9e5e9a23fd5dc13a5ed') // babe.currentSlot
9
- : await chain.head.get('0x57f8dc2f5ab09467896f47300f04243806155b3cd9a8c9e5e9a23fd5dc13a5ed') // aura.currentSlot
10
- ;
11
- if (!slotRaw) throw new Error('Cannot find current slot');
12
- return meta.registry.createType('Slot', hexToU8a(slotRaw)).toNumber();
13
- };
14
- export const getCurrentTimestamp = async (chain)=>{
15
- const meta = await chain.head.meta;
16
- const timestamp = await chain.head.read('u64', meta.query.timestamp.now);
17
- return timestamp?.toBigInt() ?? 0n;
18
- };
19
- export const getSlotDuration = async (chain)=>{
20
- const meta = await chain.head.meta;
21
- return meta.consts.babe ? meta.consts.babe.expectedBlockTime.toNumber() : meta.query.aura ? getAuraSlotDuration(await chain.head.wasm) : 12_000;
22
- };
23
4
  export const timeTravel = async (chain, timestamp)=>{
24
5
  const meta = await chain.head.meta;
25
6
  const slotDuration = await getSlotDuration(chain);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acala-network/chopsticks-core",
3
- "version": "0.9.8-1",
3
+ "version": "0.9.8-2",
4
4
  "author": "Acala Developers <hello@acala.network>",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -12,7 +12,7 @@
12
12
  "docs:prep": "typedoc"
13
13
  },
14
14
  "dependencies": {
15
- "@acala-network/chopsticks-executor": "0.9.8-1",
15
+ "@acala-network/chopsticks-executor": "0.9.8-2",
16
16
  "@polkadot/rpc-provider": "^10.11.2",
17
17
  "@polkadot/types": "^10.11.2",
18
18
  "@polkadot/types-codec": "^10.11.2",