@dedot/api 0.9.3 → 0.9.5-next.20891389.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.
@@ -167,28 +167,18 @@ class ChainHead extends JsonRpcGroup_js_1.JsonRpcGroup {
167
167
  return;
168
168
  this.#finalizedQueue.push(hash);
169
169
  });
170
- this.emit('finalizedBlock', this.findBlock(this.#finalizedHash));
171
- // TODO should we find all descendants of the pruned blocks and unpin them as well?
172
- // that's probably a premature optimization
173
- const finalizedBlockHeights = finalizedBlockHashes.map((hash) => this.findBlock(hash).number);
174
- const pinnedHashes = Object.keys(this.#pinnedBlocks);
175
- const hashesToUnpin = new Set([
176
- ...prunedBlockHashes.filter((hash) => pinnedHashes.includes(hash)),
177
- // Since we have the current finalized blocks,
178
- // we can mark all the other blocks at the same height as pruned and unpin all together with the reported pruned blocks
179
- ...Object.values(this.#pinnedBlocks)
180
- .filter((b) => finalizedBlockHeights.includes(b.number))
181
- .filter((b) => !finalizedBlockHashes.includes(b.hash))
182
- .map((b) => b.hash),
183
- ]);
170
+ const currentFinalizedBlock = this.findBlock(this.#finalizedHash);
171
+ this.emit('finalizedBlock', currentFinalizedBlock);
184
172
  // TODO account for operations that haven't received its operationId yet
185
173
  Object.values(this.#handlers).forEach(({ defer, hash, operationId }) => {
186
- if (hashesToUnpin.has(hash)) {
174
+ if (prunedBlockHashes.includes(hash)) {
187
175
  defer.reject(new error_js_1.ChainHeadBlockPrunedError());
188
176
  this.stopOperation(operationId).catch(utils_1.noop);
189
177
  delete this.#handlers[operationId];
190
178
  }
191
179
  });
180
+ const pinnedHashes = Object.keys(this.#pinnedBlocks);
181
+ const hashesToUnpin = new Set(prunedBlockHashes.filter((hash) => pinnedHashes.includes(hash)));
192
182
  // Unpin the oldest finalized pinned blocks to maintain the queue size
193
183
  if (this.#finalizedQueue.length > exports.MIN_FINALIZED_QUEUE_SIZE) {
194
184
  const finalizedQueue = this.#finalizedQueue.slice();
@@ -206,6 +196,17 @@ class ChainHead extends JsonRpcGroup_js_1.JsonRpcGroup {
206
196
  });
207
197
  this.#finalizedQueue = finalizedQueue;
208
198
  }
199
+ // Unpin all obsolete blocks with blockNumber < the latest finalized block number
200
+ // & not a finalized block & is not in use
201
+ pinnedHashes.forEach((hash) => {
202
+ if (this.#blockUsage.usage(hash) > 0)
203
+ return;
204
+ if (this.#finalizedQueue.includes(hash))
205
+ return;
206
+ if (this.findBlock(hash).number > currentFinalizedBlock.number)
207
+ return;
208
+ hashesToUnpin.add(hash);
209
+ });
209
210
  hashesToUnpin.forEach((hash) => {
210
211
  if (!this.isPinned(hash))
211
212
  return;
@@ -555,16 +556,13 @@ class ChainHead extends JsonRpcGroup_js_1.JsonRpcGroup {
555
556
  const isSmoldot = typeof this.client.provider['chain'] === 'function';
556
557
  if (isSmoldot) {
557
558
  const fetchItem = async (item) => {
558
- const [newBatch, newDiscardedItems] = await this.#getStorage([item], childTrie ?? null, hash);
559
+ const [batch, newDiscardedItems] = await this.#getStorage([item], childTrie ?? null, hash);
559
560
  if (newDiscardedItems.length > 0) {
560
561
  return fetchItem(item);
561
562
  }
562
- if (newBatch.length === 0) {
563
- return { key: item.key, value: undefined };
564
- }
565
- return newBatch[0];
563
+ return batch;
566
564
  };
567
- results = await Promise.all(items.map((one) => fetchItem(one)));
565
+ results = (await Promise.all(items.map((one) => fetchItem(one)))).flat();
568
566
  }
569
567
  else {
570
568
  let queryItems = items;
@@ -164,28 +164,18 @@ export class ChainHead extends JsonRpcGroup {
164
164
  return;
165
165
  this.#finalizedQueue.push(hash);
166
166
  });
167
- this.emit('finalizedBlock', this.findBlock(this.#finalizedHash));
168
- // TODO should we find all descendants of the pruned blocks and unpin them as well?
169
- // that's probably a premature optimization
170
- const finalizedBlockHeights = finalizedBlockHashes.map((hash) => this.findBlock(hash).number);
171
- const pinnedHashes = Object.keys(this.#pinnedBlocks);
172
- const hashesToUnpin = new Set([
173
- ...prunedBlockHashes.filter((hash) => pinnedHashes.includes(hash)),
174
- // Since we have the current finalized blocks,
175
- // we can mark all the other blocks at the same height as pruned and unpin all together with the reported pruned blocks
176
- ...Object.values(this.#pinnedBlocks)
177
- .filter((b) => finalizedBlockHeights.includes(b.number))
178
- .filter((b) => !finalizedBlockHashes.includes(b.hash))
179
- .map((b) => b.hash),
180
- ]);
167
+ const currentFinalizedBlock = this.findBlock(this.#finalizedHash);
168
+ this.emit('finalizedBlock', currentFinalizedBlock);
181
169
  // TODO account for operations that haven't received its operationId yet
182
170
  Object.values(this.#handlers).forEach(({ defer, hash, operationId }) => {
183
- if (hashesToUnpin.has(hash)) {
171
+ if (prunedBlockHashes.includes(hash)) {
184
172
  defer.reject(new ChainHeadBlockPrunedError());
185
173
  this.stopOperation(operationId).catch(noop);
186
174
  delete this.#handlers[operationId];
187
175
  }
188
176
  });
177
+ const pinnedHashes = Object.keys(this.#pinnedBlocks);
178
+ const hashesToUnpin = new Set(prunedBlockHashes.filter((hash) => pinnedHashes.includes(hash)));
189
179
  // Unpin the oldest finalized pinned blocks to maintain the queue size
190
180
  if (this.#finalizedQueue.length > MIN_FINALIZED_QUEUE_SIZE) {
191
181
  const finalizedQueue = this.#finalizedQueue.slice();
@@ -203,6 +193,17 @@ export class ChainHead extends JsonRpcGroup {
203
193
  });
204
194
  this.#finalizedQueue = finalizedQueue;
205
195
  }
196
+ // Unpin all obsolete blocks with blockNumber < the latest finalized block number
197
+ // & not a finalized block & is not in use
198
+ pinnedHashes.forEach((hash) => {
199
+ if (this.#blockUsage.usage(hash) > 0)
200
+ return;
201
+ if (this.#finalizedQueue.includes(hash))
202
+ return;
203
+ if (this.findBlock(hash).number > currentFinalizedBlock.number)
204
+ return;
205
+ hashesToUnpin.add(hash);
206
+ });
206
207
  hashesToUnpin.forEach((hash) => {
207
208
  if (!this.isPinned(hash))
208
209
  return;
@@ -552,16 +553,13 @@ export class ChainHead extends JsonRpcGroup {
552
553
  const isSmoldot = typeof this.client.provider['chain'] === 'function';
553
554
  if (isSmoldot) {
554
555
  const fetchItem = async (item) => {
555
- const [newBatch, newDiscardedItems] = await this.#getStorage([item], childTrie ?? null, hash);
556
+ const [batch, newDiscardedItems] = await this.#getStorage([item], childTrie ?? null, hash);
556
557
  if (newDiscardedItems.length > 0) {
557
558
  return fetchItem(item);
558
559
  }
559
- if (newBatch.length === 0) {
560
- return { key: item.key, value: undefined };
561
- }
562
- return newBatch[0];
560
+ return batch;
563
561
  };
564
- results = await Promise.all(items.map((one) => fetchItem(one)));
562
+ results = (await Promise.all(items.map((one) => fetchItem(one)))).flat();
565
563
  }
566
564
  else {
567
565
  let queryItems = items;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dedot/api",
3
- "version": "0.9.3",
3
+ "version": "0.9.5-next.20891389.0+2089138",
4
4
  "description": "A delightful JavaScript/TypeScript client for Polkadot & Substrate",
5
5
  "author": "Thang X. Vu <thang@dedot.dev>",
6
6
  "homepage": "https://dedot.dev",
@@ -13,13 +13,13 @@
13
13
  "type": "module",
14
14
  "sideEffects": false,
15
15
  "dependencies": {
16
- "@dedot/codecs": "0.9.3",
17
- "@dedot/providers": "0.9.3",
18
- "@dedot/runtime-specs": "0.9.3",
19
- "@dedot/shape": "0.9.3",
20
- "@dedot/storage": "0.9.3",
21
- "@dedot/types": "0.9.3",
22
- "@dedot/utils": "0.9.3"
16
+ "@dedot/codecs": "0.9.5-next.20891389.0+2089138",
17
+ "@dedot/providers": "0.9.5-next.20891389.0+2089138",
18
+ "@dedot/runtime-specs": "0.9.5-next.20891389.0+2089138",
19
+ "@dedot/shape": "0.9.5-next.20891389.0+2089138",
20
+ "@dedot/storage": "0.9.5-next.20891389.0+2089138",
21
+ "@dedot/types": "0.9.5-next.20891389.0+2089138",
22
+ "@dedot/utils": "0.9.5-next.20891389.0+2089138"
23
23
  },
24
24
  "scripts": {
25
25
  "build": "tsc --project tsconfig.build.json && tsc --project tsconfig.build.cjs.json",
@@ -48,7 +48,7 @@
48
48
  "node": ">=18"
49
49
  },
50
50
  "license": "Apache-2.0",
51
- "gitHead": "206c4999865cddbd1078bf5b059c7b49822e20d0",
51
+ "gitHead": "208913895242cc9c16cb8bb9bcb880393cb61e6b",
52
52
  "module": "./index.js",
53
53
  "types": "./index.d.ts"
54
54
  }