@acala-network/chopsticks-core 0.13.2 → 0.14.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.
@@ -88,7 +88,7 @@ class SetValidationData {
88
88
  const hrmpIngressChannelIndexKey = (0, _proof.hrmpIngressChannelIndex)(paraId);
89
89
  const hrmpEgressChannelIndexKey = (0, _proof.hrmpEgressChannelIndex)(paraId);
90
90
  const decoded = await (0, _index1.decodeProof)(extrinsic.validationData.relayParentStorageRoot, extrinsic.relayChainState.trieNodes);
91
- const slotIncrease = Math.max(1, meta.consts.timestamp?.minimumPeriod // legacy
91
+ const relaySlotIncrease = Math.max(1, meta.consts.timestamp?.minimumPeriod // legacy
92
92
  ?.divn(3000) // relaychain min period
93
93
  ?.toNumber() || meta.consts.aura?.slotDuration // async backing
94
94
  ?.divn(6000) // relaychain block time
@@ -96,8 +96,8 @@ class SetValidationData {
96
96
  for (const key of Object.values(_proof.WELL_KNOWN_KEYS)){
97
97
  if (key === _proof.WELL_KNOWN_KEYS.CURRENT_SLOT) {
98
98
  // increment current slot
99
- const relayCurrentSlot = decoded[key] ? meta.registry.createType('Slot', (0, _util.hexToU8a)(decoded[key])).toNumber() : await (0, _index.getCurrentSlot)(parent) * slotIncrease;
100
- const newSlot = meta.registry.createType('Slot', relayCurrentSlot + slotIncrease);
99
+ const relayCurrentSlot = decoded[key] ? meta.registry.createType('Slot', (0, _util.hexToU8a)(decoded[key])).toNumber() : await (0, _index.getCurrentSlot)(parent) * relaySlotIncrease;
100
+ const newSlot = meta.registry.createType('Slot', relayCurrentSlot + relaySlotIncrease);
101
101
  newEntries.push([
102
102
  key,
103
103
  (0, _util.u8aToHex)(newSlot.toU8a())
@@ -237,7 +237,7 @@ class SetValidationData {
237
237
  validationData: {
238
238
  ...extrinsic.validationData,
239
239
  relayParentStorageRoot: trieRootHash,
240
- relayParentNumber: extrinsic.validationData.relayParentNumber + slotIncrease
240
+ relayParentNumber: extrinsic.validationData.relayParentNumber + relaySlotIncrease
241
241
  },
242
242
  relayChainState: {
243
243
  trieNodes: nodes
@@ -15,7 +15,7 @@ class SetTimestamp {
15
15
  const parent = await newBlock.parentBlock;
16
16
  if (!parent) throw new Error('parent block not found');
17
17
  const meta = await parent.meta;
18
- const slotDuration = await (0, _index.getSlotDuration)(parent);
18
+ const slotDuration = await (0, _index.getSlotDuration)(newBlock);
19
19
  const currentTimestamp = await (0, _index.getCurrentTimestamp)(parent);
20
20
  return [
21
21
  new _types.GenericExtrinsic(meta.registry, meta.tx.timestamp.set(currentTimestamp + BigInt(slotDuration))).toHex()
@@ -146,21 +146,7 @@ const stripChildPrefix = (key)=>{
146
146
  if (!child) return key;
147
147
  return storageKey;
148
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
- '0x8985dff79e6002d0deba9ddac46f32a5a70806914c906d747e668a21f9021729',
154
- '0xab2a8d5eca218f218c6fda6b1d22bb926bc171ab77f6a731a6e80c34ee1eda19'
155
- ];
156
149
  const getCurrentSlot = async (head)=>{
157
- const meta = await head.meta;
158
- for (const key of POTENTIAL_SLOT_KEYS){
159
- const slotRaw = await head.get(key);
160
- if (slotRaw) {
161
- return meta.registry.createType('Slot', (0, _util.hexToU8a)(slotRaw)).toNumber();
162
- }
163
- }
164
150
  const timestamp = await getCurrentTimestamp(head);
165
151
  const slotDuration = await getSlotDuration(head);
166
152
  return Math.floor(Number(timestamp / BigInt(slotDuration)));
@@ -172,5 +158,8 @@ const getCurrentTimestamp = async (head)=>{
172
158
  };
173
159
  const getSlotDuration = async (head)=>{
174
160
  const meta = await head.meta;
175
- return meta.consts.babe ? meta.consts.babe.expectedBlockTime.toNumber() : meta.query.aura ? (0, _index.getAuraSlotDuration)(await head.wasm) : meta.consts.asyncBacking ? meta.consts.asyncBacking.expectedBlockTime.toNumber() : 12_000;
161
+ let slotDuration;
162
+ slotDuration ??= meta.consts.babe?.expectedBlockTime?.toNumber();
163
+ slotDuration ??= meta.consts.asyncBacking?.expectedBlockTime?.toNumber();
164
+ return slotDuration || (0, _index.getAuraSlotDuration)(await head.wasm).catch(()=>12_000);
176
165
  };
@@ -73,7 +73,7 @@ export class SetValidationData {
73
73
  const hrmpIngressChannelIndexKey = hrmpIngressChannelIndex(paraId);
74
74
  const hrmpEgressChannelIndexKey = hrmpEgressChannelIndex(paraId);
75
75
  const decoded = await decodeProof(extrinsic.validationData.relayParentStorageRoot, extrinsic.relayChainState.trieNodes);
76
- const slotIncrease = Math.max(1, meta.consts.timestamp?.minimumPeriod // legacy
76
+ const relaySlotIncrease = Math.max(1, meta.consts.timestamp?.minimumPeriod // legacy
77
77
  ?.divn(3000) // relaychain min period
78
78
  ?.toNumber() || meta.consts.aura?.slotDuration // async backing
79
79
  ?.divn(6000) // relaychain block time
@@ -81,8 +81,8 @@ export class SetValidationData {
81
81
  for (const key of Object.values(WELL_KNOWN_KEYS)){
82
82
  if (key === WELL_KNOWN_KEYS.CURRENT_SLOT) {
83
83
  // increment current slot
84
- const relayCurrentSlot = decoded[key] ? meta.registry.createType('Slot', hexToU8a(decoded[key])).toNumber() : await getCurrentSlot(parent) * slotIncrease;
85
- const newSlot = meta.registry.createType('Slot', relayCurrentSlot + slotIncrease);
84
+ const relayCurrentSlot = decoded[key] ? meta.registry.createType('Slot', hexToU8a(decoded[key])).toNumber() : await getCurrentSlot(parent) * relaySlotIncrease;
85
+ const newSlot = meta.registry.createType('Slot', relayCurrentSlot + relaySlotIncrease);
86
86
  newEntries.push([
87
87
  key,
88
88
  u8aToHex(newSlot.toU8a())
@@ -222,7 +222,7 @@ export class SetValidationData {
222
222
  validationData: {
223
223
  ...extrinsic.validationData,
224
224
  relayParentStorageRoot: trieRootHash,
225
- relayParentNumber: extrinsic.validationData.relayParentNumber + slotIncrease
225
+ relayParentNumber: extrinsic.validationData.relayParentNumber + relaySlotIncrease
226
226
  },
227
227
  relayChainState: {
228
228
  trieNodes: nodes
@@ -5,7 +5,7 @@ export class SetTimestamp {
5
5
  const parent = await newBlock.parentBlock;
6
6
  if (!parent) throw new Error('parent block not found');
7
7
  const meta = await parent.meta;
8
- const slotDuration = await getSlotDuration(parent);
8
+ const slotDuration = await getSlotDuration(newBlock);
9
9
  const currentTimestamp = await getCurrentTimestamp(parent);
10
10
  return [
11
11
  new GenericExtrinsic(meta.registry, meta.tx.timestamp.set(currentTimestamp + BigInt(slotDuration))).toHex()
@@ -1,4 +1,4 @@
1
- import { compactStripLength, hexToU8a, u8aToHex } from '@polkadot/util';
1
+ import { compactStripLength, u8aToHex } from '@polkadot/util';
2
2
  import { getAuraSlotDuration } from '../wasm-executor/index.js';
3
3
  import { hexAddPrefix, hexStripPrefix } from '@polkadot/util/hex';
4
4
  export * from './set-storage.js';
@@ -82,21 +82,7 @@ export const stripChildPrefix = (key)=>{
82
82
  if (!child) return key;
83
83
  return storageKey;
84
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
- '0x8985dff79e6002d0deba9ddac46f32a5a70806914c906d747e668a21f9021729',
90
- '0xab2a8d5eca218f218c6fda6b1d22bb926bc171ab77f6a731a6e80c34ee1eda19'
91
- ];
92
85
  export const getCurrentSlot = async (head)=>{
93
- const meta = await head.meta;
94
- for (const key of POTENTIAL_SLOT_KEYS){
95
- const slotRaw = await head.get(key);
96
- if (slotRaw) {
97
- return meta.registry.createType('Slot', hexToU8a(slotRaw)).toNumber();
98
- }
99
- }
100
86
  const timestamp = await getCurrentTimestamp(head);
101
87
  const slotDuration = await getSlotDuration(head);
102
88
  return Math.floor(Number(timestamp / BigInt(slotDuration)));
@@ -108,5 +94,8 @@ export const getCurrentTimestamp = async (head)=>{
108
94
  };
109
95
  export const getSlotDuration = async (head)=>{
110
96
  const meta = await head.meta;
111
- return meta.consts.babe ? meta.consts.babe.expectedBlockTime.toNumber() : meta.query.aura ? getAuraSlotDuration(await head.wasm) : meta.consts.asyncBacking ? meta.consts.asyncBacking.expectedBlockTime.toNumber() : 12_000;
97
+ let slotDuration;
98
+ slotDuration ??= meta.consts.babe?.expectedBlockTime?.toNumber();
99
+ slotDuration ??= meta.consts.asyncBacking?.expectedBlockTime?.toNumber();
100
+ return slotDuration || getAuraSlotDuration(await head.wasm).catch(()=>12_000);
112
101
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acala-network/chopsticks-core",
3
- "version": "0.13.2",
3
+ "version": "0.14.0",
4
4
  "author": "Acala Developers <hello@acala.network>",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -11,7 +11,7 @@
11
11
  "depcheck": "npx depcheck"
12
12
  },
13
13
  "dependencies": {
14
- "@acala-network/chopsticks-executor": "0.13.2",
14
+ "@acala-network/chopsticks-executor": "0.14.0",
15
15
  "@polkadot/rpc-provider": "^12.3.1",
16
16
  "@polkadot/types": "^12.3.1",
17
17
  "@polkadot/types-codec": "^12.3.1",