@dedot/api 0.9.4 → 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;
@@ -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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dedot/api",
3
- "version": "0.9.4",
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.4",
17
- "@dedot/providers": "0.9.4",
18
- "@dedot/runtime-specs": "0.9.4",
19
- "@dedot/shape": "0.9.4",
20
- "@dedot/storage": "0.9.4",
21
- "@dedot/types": "0.9.4",
22
- "@dedot/utils": "0.9.4"
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": "1deabbf35425a688e76f1612210bcda3f7bedba3",
51
+ "gitHead": "208913895242cc9c16cb8bb9bcb880393cb61e6b",
52
52
  "module": "./index.js",
53
53
  "types": "./index.d.ts"
54
54
  }