@acala-network/chopsticks-core 1.2.2 → 1.2.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.
package/dist/cjs/api.d.ts CHANGED
@@ -33,7 +33,7 @@ export declare class Api {
33
33
  getBlock(hash?: string): Promise<SignedBlock | null>;
34
34
  getStorage(key: string, hash?: string): Promise<`0x${string}` | null>;
35
35
  getKeysPaged(prefix: string, pageSize: number, startKey: string, hash?: string): Promise<`0x${string}`[]>;
36
- getStorageBatch(prefix: HexString, keys: HexString[], hash?: HexString): Promise<[`0x${string}`, `0x${string}` | null][]>;
36
+ getStorageBatch(prefix: HexString, keys: HexString[], hash?: HexString): any;
37
37
  subscribeRemoteNewHeads(cb: ProviderInterfaceCallback): Promise<string | number>;
38
38
  subscribeRemoteFinalizedHeads(cb: ProviderInterfaceCallback): Promise<string | number>;
39
39
  }
package/dist/cjs/api.js CHANGED
@@ -8,6 +8,7 @@ Object.defineProperty(exports, "Api", {
8
8
  return Api;
9
9
  }
10
10
  });
11
+ const _error = /*#__PURE__*/ _interop_require_default(require("@polkadot/rpc-provider/coder/error"));
11
12
  const _lodash = /*#__PURE__*/ _interop_require_default(require("lodash"));
12
13
  const _index = require("./utils/index.js");
13
14
  function _check_private_redeclaration(obj, privateCollection) {
@@ -179,6 +180,21 @@ class Api {
179
180
  return this.send('state_getKeysPaged', params, !!hash);
180
181
  }
181
182
  async getStorageBatch(prefix, keys, hash) {
183
+ // On response limit error, retry with a smaller batch size
184
+ const retryOnError = async (ex)=>{
185
+ if (ex instanceof _error.default && (ex.code === -32008 || ex.message === 'Response is too big')) {
186
+ // Can't split beyond key size = 2
187
+ if (keys.length < 2) throw ex;
188
+ const mid = Math.floor(keys.length / 2);
189
+ const batches = [
190
+ keys.slice(0, mid),
191
+ keys.slice(mid)
192
+ ];
193
+ const results = await Promise.all(batches.map((batch)=>this.getStorageBatch(prefix, batch, hash)));
194
+ return results.flat();
195
+ }
196
+ throw ex;
197
+ };
182
198
  const [child] = (0, _index.splitChildKey)(prefix);
183
199
  if (child) {
184
200
  // child storage key, use childstate_getStorageEntries
@@ -188,14 +204,14 @@ class Api {
188
204
  keys.map((key)=>(0, _index.stripChildPrefix)(key))
189
205
  ];
190
206
  if (hash) params.push(hash);
191
- return _class_private_field_get(this, _provider).send('childstate_getStorageEntries', params, !!hash).then((values)=>_lodash.default.zip(keys, values));
207
+ return _class_private_field_get(this, _provider).send('childstate_getStorageEntries', params, !!hash).then((values)=>_lodash.default.zip(keys, values)).catch(retryOnError);
192
208
  }
193
209
  // main storage key, use state_getStorageAt
194
210
  const params = [
195
211
  keys
196
212
  ];
197
213
  if (hash) params.push(hash);
198
- return _class_private_field_get(this, _provider).send('state_queryStorageAt', params, !!hash).then((result)=>result[0]?.['changes'] || []);
214
+ return _class_private_field_get(this, _provider).send('state_queryStorageAt', params, !!hash).then((result)=>result[0]?.['changes'] || []).catch(retryOnError);
199
215
  }
200
216
  async subscribeRemoteNewHeads(cb) {
201
217
  if (!_class_private_field_get(this, _provider).hasSubscriptions) {
@@ -24,6 +24,7 @@ function _interop_require_default(obj) {
24
24
  const logger = _logger.defaultLogger.child({
25
25
  name: 'parachain-validation-data'
26
26
  });
27
+ const RELAY_CHAIN_SLOT_DURATION_MILLIS = 6_000;
27
28
  const MOCK_VALIDATION_DATA = {
28
29
  validationData: {
29
30
  relayParentNumber: 1000,
@@ -116,17 +117,14 @@ class SetValidationData {
116
117
  const hrmpIngressChannelIndexKey = (0, _proof.hrmpIngressChannelIndex)(paraId);
117
118
  const hrmpEgressChannelIndexKey = (0, _proof.hrmpEgressChannelIndex)(paraId);
118
119
  const decoded = await (0, _index1.decodeProof)(extrinsic.validationData.relayParentStorageRoot, extrinsic.relayChainState.trieNodes);
119
- const relaySlotIncrease = Math.max(1, meta.consts.timestamp?.minimumPeriod // legacy
120
- ?.divn(3000) // relaychain min period
121
- ?.toNumber() || meta.consts.aura?.slotDuration // async backing
122
- ?.divn(6000) // relaychain block time
123
- ?.toNumber() || 1);
120
+ const slotDuration = await (0, _index.getSlotDuration)(newBlock);
121
+ const relaySlotIncrease = Math.trunc(slotDuration / RELAY_CHAIN_SLOT_DURATION_MILLIS) || 1 // at least increase by 1
122
+ ;
124
123
  for (const key of Object.values(_proof.WELL_KNOWN_KEYS)){
125
124
  if (key === _proof.WELL_KNOWN_KEYS.CURRENT_SLOT) {
126
125
  // increment current slot
127
126
  const relayCurrentSlot = decoded[key] ? meta.registry.createType('Slot', (0, _util.hexToU8a)(decoded[key])).toNumber() : await (0, _index.getCurrentSlot)(parent) * relaySlotIncrease;
128
- const newSlot = meta.registry.createType('Slot', relayCurrentSlot + relaySlotIncrease + 1) // +1 to be safe
129
- ;
127
+ const newSlot = meta.registry.createType('Slot', relayCurrentSlot + relaySlotIncrease);
130
128
  logger.debug({
131
129
  relayCurrentSlot,
132
130
  newSlot: newSlot.toNumber()
package/dist/esm/api.d.ts CHANGED
@@ -33,7 +33,7 @@ export declare class Api {
33
33
  getBlock(hash?: string): Promise<SignedBlock | null>;
34
34
  getStorage(key: string, hash?: string): Promise<`0x${string}` | null>;
35
35
  getKeysPaged(prefix: string, pageSize: number, startKey: string, hash?: string): Promise<`0x${string}`[]>;
36
- getStorageBatch(prefix: HexString, keys: HexString[], hash?: HexString): Promise<[`0x${string}`, `0x${string}` | null][]>;
36
+ getStorageBatch(prefix: HexString, keys: HexString[], hash?: HexString): any;
37
37
  subscribeRemoteNewHeads(cb: ProviderInterfaceCallback): Promise<string | number>;
38
38
  subscribeRemoteFinalizedHeads(cb: ProviderInterfaceCallback): Promise<string | number>;
39
39
  }
package/dist/esm/api.js CHANGED
@@ -1,3 +1,4 @@
1
+ import RpcError from '@polkadot/rpc-provider/coder/error';
1
2
  import _ from 'lodash';
2
3
  import { prefixedChildKey, splitChildKey, stripChildPrefix } from './utils/index.js';
3
4
  /**
@@ -131,6 +132,21 @@ import { prefixedChildKey, splitChildKey, stripChildPrefix } from './utils/index
131
132
  return this.send('state_getKeysPaged', params, !!hash);
132
133
  }
133
134
  async getStorageBatch(prefix, keys, hash) {
135
+ // On response limit error, retry with a smaller batch size
136
+ const retryOnError = async (ex)=>{
137
+ if (ex instanceof RpcError && (ex.code === -32008 || ex.message === 'Response is too big')) {
138
+ // Can't split beyond key size = 2
139
+ if (keys.length < 2) throw ex;
140
+ const mid = Math.floor(keys.length / 2);
141
+ const batches = [
142
+ keys.slice(0, mid),
143
+ keys.slice(mid)
144
+ ];
145
+ const results = await Promise.all(batches.map((batch)=>this.getStorageBatch(prefix, batch, hash)));
146
+ return results.flat();
147
+ }
148
+ throw ex;
149
+ };
134
150
  const [child] = splitChildKey(prefix);
135
151
  if (child) {
136
152
  // child storage key, use childstate_getStorageEntries
@@ -140,14 +156,14 @@ import { prefixedChildKey, splitChildKey, stripChildPrefix } from './utils/index
140
156
  keys.map((key)=>stripChildPrefix(key))
141
157
  ];
142
158
  if (hash) params.push(hash);
143
- return this.#provider.send('childstate_getStorageEntries', params, !!hash).then((values)=>_.zip(keys, values));
159
+ return this.#provider.send('childstate_getStorageEntries', params, !!hash).then((values)=>_.zip(keys, values)).catch(retryOnError);
144
160
  }
145
161
  // main storage key, use state_getStorageAt
146
162
  const params = [
147
163
  keys
148
164
  ];
149
165
  if (hash) params.push(hash);
150
- return this.#provider.send('state_queryStorageAt', params, !!hash).then((result)=>result[0]?.['changes'] || []);
166
+ return this.#provider.send('state_queryStorageAt', params, !!hash).then((result)=>result[0]?.['changes'] || []).catch(retryOnError);
151
167
  }
152
168
  async subscribeRemoteNewHeads(cb) {
153
169
  if (!this.#provider.hasSubscriptions) {
@@ -3,12 +3,13 @@ import { hexToU8a, u8aConcat, u8aToHex } from '@polkadot/util';
3
3
  import { blake2AsHex, blake2AsU8a } from '@polkadot/util-crypto';
4
4
  import _ from 'lodash';
5
5
  import { defaultLogger } from '../../../logger.js';
6
- import { compactHex, getCurrentSlot, getParaId } from '../../../utils/index.js';
6
+ import { compactHex, getCurrentSlot, getParaId, getSlotDuration } from '../../../utils/index.js';
7
7
  import { dmqMqcHead, hrmpChannels, hrmpEgressChannelIndex, hrmpIngressChannelIndex, paraHead, upgradeGoAheadSignal, WELL_KNOWN_KEYS } from '../../../utils/proof.js';
8
8
  import { createProof, decodeProof } from '../../../wasm-executor/index.js';
9
9
  const logger = defaultLogger.child({
10
10
  name: 'parachain-validation-data'
11
11
  });
12
+ const RELAY_CHAIN_SLOT_DURATION_MILLIS = 6_000;
12
13
  const MOCK_VALIDATION_DATA = {
13
14
  validationData: {
14
15
  relayParentNumber: 1000,
@@ -101,17 +102,14 @@ export class SetValidationData {
101
102
  const hrmpIngressChannelIndexKey = hrmpIngressChannelIndex(paraId);
102
103
  const hrmpEgressChannelIndexKey = hrmpEgressChannelIndex(paraId);
103
104
  const decoded = await decodeProof(extrinsic.validationData.relayParentStorageRoot, extrinsic.relayChainState.trieNodes);
104
- const relaySlotIncrease = Math.max(1, meta.consts.timestamp?.minimumPeriod // legacy
105
- ?.divn(3000) // relaychain min period
106
- ?.toNumber() || meta.consts.aura?.slotDuration // async backing
107
- ?.divn(6000) // relaychain block time
108
- ?.toNumber() || 1);
105
+ const slotDuration = await getSlotDuration(newBlock);
106
+ const relaySlotIncrease = Math.trunc(slotDuration / RELAY_CHAIN_SLOT_DURATION_MILLIS) || 1 // at least increase by 1
107
+ ;
109
108
  for (const key of Object.values(WELL_KNOWN_KEYS)){
110
109
  if (key === WELL_KNOWN_KEYS.CURRENT_SLOT) {
111
110
  // increment current slot
112
111
  const relayCurrentSlot = decoded[key] ? meta.registry.createType('Slot', hexToU8a(decoded[key])).toNumber() : await getCurrentSlot(parent) * relaySlotIncrease;
113
- const newSlot = meta.registry.createType('Slot', relayCurrentSlot + relaySlotIncrease + 1) // +1 to be safe
114
- ;
112
+ const newSlot = meta.registry.createType('Slot', relayCurrentSlot + relaySlotIncrease);
115
113
  logger.debug({
116
114
  relayCurrentSlot,
117
115
  newSlot: newSlot.toNumber()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acala-network/chopsticks-core",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "author": "Acala Developers <hello@acala.network>",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -14,7 +14,7 @@
14
14
  "depcheck": "npx depcheck"
15
15
  },
16
16
  "dependencies": {
17
- "@acala-network/chopsticks-executor": "1.2.2",
17
+ "@acala-network/chopsticks-executor": "1.2.3",
18
18
  "@polkadot/rpc-provider": "^16.4.1",
19
19
  "@polkadot/types": "^16.4.1",
20
20
  "@polkadot/types-codec": "^16.4.1",