@ckb-ccc/core 1.11.3 → 1.11.4
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/CHANGELOG.md +7 -0
- package/dist/client/cache/memory.advanced.d.ts +23 -26
- package/dist/client/cache/memory.advanced.d.ts.map +1 -1
- package/dist/client/cache/memory.advanced.js +42 -39
- package/dist/client/cache/memory.d.ts +4 -4
- package/dist/client/cache/memory.d.ts.map +1 -1
- package/dist/client/cache/memory.js +4 -4
- package/dist.commonjs/client/cache/memory.advanced.d.ts +23 -26
- package/dist.commonjs/client/cache/memory.advanced.d.ts.map +1 -1
- package/dist.commonjs/client/cache/memory.advanced.js +42 -39
- package/dist.commonjs/client/cache/memory.d.ts +4 -4
- package/dist.commonjs/client/cache/memory.d.ts.map +1 -1
- package/dist.commonjs/client/cache/memory.js +4 -4
- package/package.json +1 -1
- package/src/client/cache/memory.advanced.ts +50 -43
- package/src/client/cache/memory.ts +7 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# @ckb-ccc/core
|
|
2
2
|
|
|
3
|
+
## 1.11.4
|
|
4
|
+
### Patch Changes
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
- [#295](https://github.com/ckb-devrel/ccc/pull/295) [`1eb030f`](https://github.com/ckb-devrel/ccc/commit/1eb030fde95c545561a092a4025747e6d14fc8de) Thanks [@Hanssen0](https://github.com/Hanssen0)! - fix(core): `ClientCacheMemory.findCells` never stops
|
|
9
|
+
|
|
3
10
|
## 1.11.3
|
|
4
11
|
### Patch Changes
|
|
5
12
|
|
|
@@ -13,43 +13,40 @@ export declare function filterNumByRange(lengthLike: NumLike, range: [NumLike, N
|
|
|
13
13
|
export declare function filterScriptByLenRange(valueLike?: ScriptLike, scriptLenRange?: [NumLike, NumLike]): boolean;
|
|
14
14
|
export declare function filterCell(searchKeyLike: ClientCollectableSearchKeyLike, cellLike: CellLike): boolean;
|
|
15
15
|
/**
|
|
16
|
-
* A Least Recently Used (LRU) cache
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* When the cache is full, the least recently used entry is automatically evicted.
|
|
20
|
-
*
|
|
21
|
-
* @template K The type of the keys in the cache.
|
|
22
|
-
* @template V The type of the values in the cache.
|
|
16
|
+
* A Map-like class that implements a "Least Recently Used" (LRU) cache policy.
|
|
17
|
+
* When the cache reaches its capacity, the least recently used item is removed.
|
|
18
|
+
* @public
|
|
23
19
|
*/
|
|
24
20
|
export declare class MapLru<K, V> extends Map<K, V> {
|
|
25
21
|
private readonly capacity;
|
|
22
|
+
private readonly lru;
|
|
26
23
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* @param capacity The maximum number of entries the cache can hold. Must be a positive integer.
|
|
30
|
-
* @throws {Error} If the capacity is not a positive integer.
|
|
24
|
+
* @param capacity - The maximum number of items to store in the cache. Must be a positive integer.
|
|
31
25
|
*/
|
|
32
26
|
constructor(capacity: number);
|
|
33
27
|
/**
|
|
34
|
-
* Retrieves
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* @param key The key of the value to retrieve.
|
|
39
|
-
* @returns The value associated with the key, or undefined if the key is not present.
|
|
28
|
+
* Retrieves the value for a given key and marks it as recently used.
|
|
29
|
+
* @param key - The key of the element to retrieve.
|
|
30
|
+
* @returns The value associated with the key, or `undefined` if the key is not in the cache.
|
|
40
31
|
*/
|
|
41
32
|
get(key: K): V | undefined;
|
|
42
33
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* @param key The key of the value to insert or update.
|
|
50
|
-
* @param value The value to associate with the key.
|
|
51
|
-
* @returns This MapLru instance.
|
|
34
|
+
* Adds or updates a key-value pair in the cache and marks the key as recently used.
|
|
35
|
+
* If setting a new key causes the cache to exceed its capacity, the least recently used item is evicted.
|
|
36
|
+
* @param key - The key of the element to add or update.
|
|
37
|
+
* @param value - The value of the element to add or update.
|
|
38
|
+
* @returns The `MapLru` instance.
|
|
52
39
|
*/
|
|
53
40
|
set(key: K, value: V): this;
|
|
41
|
+
/**
|
|
42
|
+
* Removes the specified element from the cache.
|
|
43
|
+
* @param key - The key of the element to remove.
|
|
44
|
+
* @returns `true` if an element in the `MapLru` object existed and has been removed, or `false` if the element does not exist.
|
|
45
|
+
*/
|
|
46
|
+
delete(key: K): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Removes all key-value pairs from the cache.
|
|
49
|
+
*/
|
|
50
|
+
clear(): void;
|
|
54
51
|
}
|
|
55
52
|
//# sourceMappingURL=memory.advanced.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory.advanced.d.ts","sourceRoot":"","sources":["../../../src/client/cache/memory.advanced.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAU,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACxE,OAAO,EAAE,OAAO,EAAW,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,OAAO,EAAW,MAAM,oBAAoB,CAAC;AACtD,OAAO,EACL,8BAA8B,EAE/B,MAAM,4BAA4B,CAAC;AAGpC,eAAO,MAAM,4BAA4B,QAA0B,CAAC;AAGpE,MAAM,MAAM,UAAU,GAClB;IACE,KAAK;IACL,IAAI,CAAC,IAAI,EAAE,UAAU,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,YAAY,GAAG,YAAY,CAAC,CAAC;CAC1E,GACD,CAAC,IAAI,EAAE,IAAI,CAAC,GACZ,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;AAEtB,wBAAgB,UAAU,CACxB,QAAQ,EAAE,OAAO,EACjB,UAAU,EAAE,OAAO,GAAG,SAAS,EAC/B,UAAU,EAAE,OAAO,GAAG,QAAQ,GAAG,SAAS,GACzC,OAAO,CAgBT;AAED,wBAAgB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ACzC,OAAO,CAgBT;AAED,wBAAgB,YAAY,CAC1B,SAAS,EAAE,UAAU,GAAG,SAAS,EACjC,UAAU,EAAE,UAAU,GAAG,SAAS,EAClC,UAAU,EAAE,QAAQ,GAAG,OAAO,GAAG,SAAS,GACzC,OAAO,CAkBT;AAED,wBAAgB,gBAAgB,CAC9B,UAAU,EAAE,OAAO,EACnB,KAAK,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,SAAS,GACpC,OAAO,CAQT;AAED,wBAAgB,sBAAsB,CACpC,SAAS,CAAC,EAAE,UAAU,EACtB,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,GAClC,OAAO,CAYT;AAED,wBAAgB,UAAU,CACxB,aAAa,EAAE,8BAA8B,EAC7C,QAAQ,EAAE,QAAQ,GACjB,OAAO,CA4CT;AAED;;;;GAIG;AACH,qBAAa,MAAM,CAAC,CAAC,EAAE,CAAC,CAAE,SAAQ,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC;IAM7B,OAAO,CAAC,QAAQ,CAAC,QAAQ;IALrC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAwB;IAE5C;;OAEG;gBAC0B,QAAQ,EAAE,MAAM;IAQ7C;;;;OAIG;IACM,GAAG,CAAC,GAAG,EAAE,CAAC;IAWnB;;;;;;OAMG;IACM,GAAG,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC;IAc7B;;;;OAIG;IACM,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO;IAShC;;OAEG;IACM,KAAK;CAIf"}
|
|
@@ -80,67 +80,70 @@ export function filterCell(searchKeyLike, cellLike) {
|
|
|
80
80
|
return true;
|
|
81
81
|
}
|
|
82
82
|
/**
|
|
83
|
-
* A Least Recently Used (LRU) cache
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
* When the cache is full, the least recently used entry is automatically evicted.
|
|
87
|
-
*
|
|
88
|
-
* @template K The type of the keys in the cache.
|
|
89
|
-
* @template V The type of the values in the cache.
|
|
83
|
+
* A Map-like class that implements a "Least Recently Used" (LRU) cache policy.
|
|
84
|
+
* When the cache reaches its capacity, the least recently used item is removed.
|
|
85
|
+
* @public
|
|
90
86
|
*/
|
|
91
87
|
export class MapLru extends Map {
|
|
92
88
|
/**
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
* @param capacity The maximum number of entries the cache can hold. Must be a positive integer.
|
|
96
|
-
* @throws {Error} If the capacity is not a positive integer.
|
|
89
|
+
* @param capacity - The maximum number of items to store in the cache. Must be a positive integer.
|
|
97
90
|
*/
|
|
98
91
|
constructor(capacity) {
|
|
99
92
|
super();
|
|
100
93
|
this.capacity = capacity;
|
|
94
|
+
this.lru = new Set();
|
|
101
95
|
if (!Number.isInteger(capacity) || capacity < 1) {
|
|
102
96
|
throw new Error("Capacity must be a positive integer");
|
|
103
97
|
}
|
|
104
98
|
}
|
|
105
99
|
/**
|
|
106
|
-
* Retrieves
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
* @param key The key of the value to retrieve.
|
|
111
|
-
* @returns The value associated with the key, or undefined if the key is not present.
|
|
100
|
+
* Retrieves the value for a given key and marks it as recently used.
|
|
101
|
+
* @param key - The key of the element to retrieve.
|
|
102
|
+
* @returns The value associated with the key, or `undefined` if the key is not in the cache.
|
|
112
103
|
*/
|
|
113
104
|
get(key) {
|
|
114
|
-
// Check if the key exists. If not, return undefined.
|
|
115
105
|
if (!super.has(key)) {
|
|
116
|
-
return
|
|
106
|
+
return;
|
|
117
107
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
super.
|
|
121
|
-
super.set(key, value);
|
|
122
|
-
return value;
|
|
108
|
+
this.lru.delete(key);
|
|
109
|
+
this.lru.add(key);
|
|
110
|
+
return super.get(key);
|
|
123
111
|
}
|
|
124
112
|
/**
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
* @param key The key of the value to insert or update.
|
|
132
|
-
* @param value The value to associate with the key.
|
|
133
|
-
* @returns This MapLru instance.
|
|
113
|
+
* Adds or updates a key-value pair in the cache and marks the key as recently used.
|
|
114
|
+
* If setting a new key causes the cache to exceed its capacity, the least recently used item is evicted.
|
|
115
|
+
* @param key - The key of the element to add or update.
|
|
116
|
+
* @param value - The value of the element to add or update.
|
|
117
|
+
* @returns The `MapLru` instance.
|
|
134
118
|
*/
|
|
135
119
|
set(key, value) {
|
|
136
|
-
// Delete and re-insert to move key to the end (most-recently-used)
|
|
137
|
-
super.delete(key);
|
|
138
120
|
super.set(key, value);
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
121
|
+
this.lru.delete(key);
|
|
122
|
+
this.lru.add(key);
|
|
123
|
+
// Evict the oldest entry if capacity is exceeded.
|
|
124
|
+
if (this.lru.size > this.capacity) {
|
|
125
|
+
const oldestKey = this.lru.keys().next().value;
|
|
126
|
+
this.delete(oldestKey);
|
|
143
127
|
}
|
|
144
128
|
return this;
|
|
145
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Removes the specified element from the cache.
|
|
132
|
+
* @param key - The key of the element to remove.
|
|
133
|
+
* @returns `true` if an element in the `MapLru` object existed and has been removed, or `false` if the element does not exist.
|
|
134
|
+
*/
|
|
135
|
+
delete(key) {
|
|
136
|
+
if (!super.delete(key)) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
this.lru.delete(key);
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Removes all key-value pairs from the cache.
|
|
144
|
+
*/
|
|
145
|
+
clear() {
|
|
146
|
+
super.clear();
|
|
147
|
+
this.lru.clear();
|
|
148
|
+
}
|
|
146
149
|
}
|
|
@@ -8,7 +8,6 @@ export declare class ClientCacheMemory extends ClientCache {
|
|
|
8
8
|
private readonly maxCells;
|
|
9
9
|
private readonly maxTxs;
|
|
10
10
|
private readonly maxBlocks;
|
|
11
|
-
private readonly confirmedBlockTime;
|
|
12
11
|
/**
|
|
13
12
|
* OutPoint => [isLive, Cell | OutPoint]
|
|
14
13
|
*/
|
|
@@ -25,14 +24,15 @@ export declare class ClientCacheMemory extends ClientCache {
|
|
|
25
24
|
* Block Hash => Block Header / Full Block
|
|
26
25
|
*/
|
|
27
26
|
private readonly knownBlocks;
|
|
27
|
+
private readonly confirmedBlockTime;
|
|
28
28
|
/**
|
|
29
29
|
* @param maxCells - Maximum number of cells to store in the cache. Defaults to 512.
|
|
30
30
|
* @param maxTxs - Maximum number of transactions to store in the cache. Defaults to 256.
|
|
31
31
|
* @param maxBlocks - Maximum number of blocks to store in the cache. Defaults to 128.
|
|
32
|
-
* @param
|
|
33
|
-
*
|
|
32
|
+
* @param confirmedBlockTimeLike - Time in milliseconds after which a block is considered confirmed.
|
|
33
|
+
* Defaults to DEFAULT_CONFIRMED_BLOCK_TIME (50 blocks * 10s).
|
|
34
34
|
*/
|
|
35
|
-
constructor(maxCells?: number, maxTxs?: number, maxBlocks?: number,
|
|
35
|
+
constructor(maxCells?: number, maxTxs?: number, maxBlocks?: number, confirmedBlockTimeLike?: NumLike);
|
|
36
36
|
markUsableNoCache(...cellLikes: (CellLike | CellLike[])[]): Promise<void>;
|
|
37
37
|
markUnusable(...outPointLikes: (OutPointLike | OutPointLike[])[]): Promise<void>;
|
|
38
38
|
clear(): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../../src/client/cache/memory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAY,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAC5E,OAAO,EAAW,OAAO,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAgB,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,8BAA8B,EAAE,MAAM,4BAA4B,CAAC;A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C,WAAW,GAAG,SAAS,CAAC;IAWnE,gBAAgB,CACpB,UAAU,EAAE,OAAO,GAClB,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC;IAUnC,kBAAkB,CAAC,MAAM,EAAE,iBAAiB,GAAG,OAAO;CAGvD"}
|
|
@@ -9,19 +9,19 @@ export class ClientCacheMemory extends ClientCache {
|
|
|
9
9
|
* @param maxCells - Maximum number of cells to store in the cache. Defaults to 512.
|
|
10
10
|
* @param maxTxs - Maximum number of transactions to store in the cache. Defaults to 256.
|
|
11
11
|
* @param maxBlocks - Maximum number of blocks to store in the cache. Defaults to 128.
|
|
12
|
-
* @param
|
|
13
|
-
*
|
|
12
|
+
* @param confirmedBlockTimeLike - Time in milliseconds after which a block is considered confirmed.
|
|
13
|
+
* Defaults to DEFAULT_CONFIRMED_BLOCK_TIME (50 blocks * 10s).
|
|
14
14
|
*/
|
|
15
|
-
constructor(maxCells = 512, maxTxs = 256, maxBlocks = 128,
|
|
15
|
+
constructor(maxCells = 512, maxTxs = 256, maxBlocks = 128, confirmedBlockTimeLike = DEFAULT_CONFIRMED_BLOCK_TIME) {
|
|
16
16
|
super();
|
|
17
17
|
this.maxCells = maxCells;
|
|
18
18
|
this.maxTxs = maxTxs;
|
|
19
19
|
this.maxBlocks = maxBlocks;
|
|
20
|
-
this.confirmedBlockTime = confirmedBlockTime;
|
|
21
20
|
this.cells = new MapLru(this.maxCells);
|
|
22
21
|
this.knownTransactions = new MapLru(this.maxTxs);
|
|
23
22
|
this.knownBlockHashes = new MapLru(this.maxBlocks);
|
|
24
23
|
this.knownBlocks = new MapLru(this.maxBlocks);
|
|
24
|
+
this.confirmedBlockTime = numFrom(confirmedBlockTimeLike);
|
|
25
25
|
}
|
|
26
26
|
async markUsableNoCache(...cellLikes) {
|
|
27
27
|
cellLikes.flat().forEach((cellLike) => {
|
|
@@ -13,43 +13,40 @@ export declare function filterNumByRange(lengthLike: NumLike, range: [NumLike, N
|
|
|
13
13
|
export declare function filterScriptByLenRange(valueLike?: ScriptLike, scriptLenRange?: [NumLike, NumLike]): boolean;
|
|
14
14
|
export declare function filterCell(searchKeyLike: ClientCollectableSearchKeyLike, cellLike: CellLike): boolean;
|
|
15
15
|
/**
|
|
16
|
-
* A Least Recently Used (LRU) cache
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* When the cache is full, the least recently used entry is automatically evicted.
|
|
20
|
-
*
|
|
21
|
-
* @template K The type of the keys in the cache.
|
|
22
|
-
* @template V The type of the values in the cache.
|
|
16
|
+
* A Map-like class that implements a "Least Recently Used" (LRU) cache policy.
|
|
17
|
+
* When the cache reaches its capacity, the least recently used item is removed.
|
|
18
|
+
* @public
|
|
23
19
|
*/
|
|
24
20
|
export declare class MapLru<K, V> extends Map<K, V> {
|
|
25
21
|
private readonly capacity;
|
|
22
|
+
private readonly lru;
|
|
26
23
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* @param capacity The maximum number of entries the cache can hold. Must be a positive integer.
|
|
30
|
-
* @throws {Error} If the capacity is not a positive integer.
|
|
24
|
+
* @param capacity - The maximum number of items to store in the cache. Must be a positive integer.
|
|
31
25
|
*/
|
|
32
26
|
constructor(capacity: number);
|
|
33
27
|
/**
|
|
34
|
-
* Retrieves
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* @param key The key of the value to retrieve.
|
|
39
|
-
* @returns The value associated with the key, or undefined if the key is not present.
|
|
28
|
+
* Retrieves the value for a given key and marks it as recently used.
|
|
29
|
+
* @param key - The key of the element to retrieve.
|
|
30
|
+
* @returns The value associated with the key, or `undefined` if the key is not in the cache.
|
|
40
31
|
*/
|
|
41
32
|
get(key: K): V | undefined;
|
|
42
33
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* @param key The key of the value to insert or update.
|
|
50
|
-
* @param value The value to associate with the key.
|
|
51
|
-
* @returns This MapLru instance.
|
|
34
|
+
* Adds or updates a key-value pair in the cache and marks the key as recently used.
|
|
35
|
+
* If setting a new key causes the cache to exceed its capacity, the least recently used item is evicted.
|
|
36
|
+
* @param key - The key of the element to add or update.
|
|
37
|
+
* @param value - The value of the element to add or update.
|
|
38
|
+
* @returns The `MapLru` instance.
|
|
52
39
|
*/
|
|
53
40
|
set(key: K, value: V): this;
|
|
41
|
+
/**
|
|
42
|
+
* Removes the specified element from the cache.
|
|
43
|
+
* @param key - The key of the element to remove.
|
|
44
|
+
* @returns `true` if an element in the `MapLru` object existed and has been removed, or `false` if the element does not exist.
|
|
45
|
+
*/
|
|
46
|
+
delete(key: K): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Removes all key-value pairs from the cache.
|
|
49
|
+
*/
|
|
50
|
+
clear(): void;
|
|
54
51
|
}
|
|
55
52
|
//# sourceMappingURL=memory.advanced.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory.advanced.d.ts","sourceRoot":"","sources":["../../../src/client/cache/memory.advanced.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAU,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACxE,OAAO,EAAE,OAAO,EAAW,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,OAAO,EAAW,MAAM,oBAAoB,CAAC;AACtD,OAAO,EACL,8BAA8B,EAE/B,MAAM,4BAA4B,CAAC;AAGpC,eAAO,MAAM,4BAA4B,QAA0B,CAAC;AAGpE,MAAM,MAAM,UAAU,GAClB;IACE,KAAK;IACL,IAAI,CAAC,IAAI,EAAE,UAAU,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,YAAY,GAAG,YAAY,CAAC,CAAC;CAC1E,GACD,CAAC,IAAI,EAAE,IAAI,CAAC,GACZ,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;AAEtB,wBAAgB,UAAU,CACxB,QAAQ,EAAE,OA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
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* When the cache is full, the least recently used entry is automatically evicted.
|
|
95
|
-
*
|
|
96
|
-
* @template K The type of the keys in the cache.
|
|
97
|
-
* @template V The type of the values in the cache.
|
|
91
|
+
* A Map-like class that implements a "Least Recently Used" (LRU) cache policy.
|
|
92
|
+
* When the cache reaches its capacity, the least recently used item is removed.
|
|
93
|
+
* @public
|
|
98
94
|
*/
|
|
99
95
|
class MapLru extends Map {
|
|
100
96
|
/**
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
* @param capacity The maximum number of entries the cache can hold. Must be a positive integer.
|
|
104
|
-
* @throws {Error} If the capacity is not a positive integer.
|
|
97
|
+
* @param capacity - The maximum number of items to store in the cache. Must be a positive integer.
|
|
105
98
|
*/
|
|
106
99
|
constructor(capacity) {
|
|
107
100
|
super();
|
|
108
101
|
this.capacity = capacity;
|
|
102
|
+
this.lru = new Set();
|
|
109
103
|
if (!Number.isInteger(capacity) || capacity < 1) {
|
|
110
104
|
throw new Error("Capacity must be a positive integer");
|
|
111
105
|
}
|
|
112
106
|
}
|
|
113
107
|
/**
|
|
114
|
-
* Retrieves
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
* @param key The key of the value to retrieve.
|
|
119
|
-
* @returns The value associated with the key, or undefined if the key is not present.
|
|
108
|
+
* Retrieves the value for a given key and marks it as recently used.
|
|
109
|
+
* @param key - The key of the element to retrieve.
|
|
110
|
+
* @returns The value associated with the key, or `undefined` if the key is not in the cache.
|
|
120
111
|
*/
|
|
121
112
|
get(key) {
|
|
122
|
-
// Check if the key exists. If not, return undefined.
|
|
123
113
|
if (!super.has(key)) {
|
|
124
|
-
return
|
|
114
|
+
return;
|
|
125
115
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
super.
|
|
129
|
-
super.set(key, value);
|
|
130
|
-
return value;
|
|
116
|
+
this.lru.delete(key);
|
|
117
|
+
this.lru.add(key);
|
|
118
|
+
return super.get(key);
|
|
131
119
|
}
|
|
132
120
|
/**
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
* @param key The key of the value to insert or update.
|
|
140
|
-
* @param value The value to associate with the key.
|
|
141
|
-
* @returns This MapLru instance.
|
|
121
|
+
* Adds or updates a key-value pair in the cache and marks the key as recently used.
|
|
122
|
+
* If setting a new key causes the cache to exceed its capacity, the least recently used item is evicted.
|
|
123
|
+
* @param key - The key of the element to add or update.
|
|
124
|
+
* @param value - The value of the element to add or update.
|
|
125
|
+
* @returns The `MapLru` instance.
|
|
142
126
|
*/
|
|
143
127
|
set(key, value) {
|
|
144
|
-
// Delete and re-insert to move key to the end (most-recently-used)
|
|
145
|
-
super.delete(key);
|
|
146
128
|
super.set(key, value);
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
129
|
+
this.lru.delete(key);
|
|
130
|
+
this.lru.add(key);
|
|
131
|
+
// Evict the oldest entry if capacity is exceeded.
|
|
132
|
+
if (this.lru.size > this.capacity) {
|
|
133
|
+
const oldestKey = this.lru.keys().next().value;
|
|
134
|
+
this.delete(oldestKey);
|
|
151
135
|
}
|
|
152
136
|
return this;
|
|
153
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* Removes the specified element from the cache.
|
|
140
|
+
* @param key - The key of the element to remove.
|
|
141
|
+
* @returns `true` if an element in the `MapLru` object existed and has been removed, or `false` if the element does not exist.
|
|
142
|
+
*/
|
|
143
|
+
delete(key) {
|
|
144
|
+
if (!super.delete(key)) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
this.lru.delete(key);
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Removes all key-value pairs from the cache.
|
|
152
|
+
*/
|
|
153
|
+
clear() {
|
|
154
|
+
super.clear();
|
|
155
|
+
this.lru.clear();
|
|
156
|
+
}
|
|
154
157
|
}
|
|
155
158
|
exports.MapLru = MapLru;
|
|
@@ -8,7 +8,6 @@ export declare class ClientCacheMemory extends ClientCache {
|
|
|
8
8
|
private readonly maxCells;
|
|
9
9
|
private readonly maxTxs;
|
|
10
10
|
private readonly maxBlocks;
|
|
11
|
-
private readonly confirmedBlockTime;
|
|
12
11
|
/**
|
|
13
12
|
* OutPoint => [isLive, Cell | OutPoint]
|
|
14
13
|
*/
|
|
@@ -25,14 +24,15 @@ export declare class ClientCacheMemory extends ClientCache {
|
|
|
25
24
|
* Block Hash => Block Header / Full Block
|
|
26
25
|
*/
|
|
27
26
|
private readonly knownBlocks;
|
|
27
|
+
private readonly confirmedBlockTime;
|
|
28
28
|
/**
|
|
29
29
|
* @param maxCells - Maximum number of cells to store in the cache. Defaults to 512.
|
|
30
30
|
* @param maxTxs - Maximum number of transactions to store in the cache. Defaults to 256.
|
|
31
31
|
* @param maxBlocks - Maximum number of blocks to store in the cache. Defaults to 128.
|
|
32
|
-
* @param
|
|
33
|
-
*
|
|
32
|
+
* @param confirmedBlockTimeLike - Time in milliseconds after which a block is considered confirmed.
|
|
33
|
+
* Defaults to DEFAULT_CONFIRMED_BLOCK_TIME (50 blocks * 10s).
|
|
34
34
|
*/
|
|
35
|
-
constructor(maxCells?: number, maxTxs?: number, maxBlocks?: number,
|
|
35
|
+
constructor(maxCells?: number, maxTxs?: number, maxBlocks?: number, confirmedBlockTimeLike?: NumLike);
|
|
36
36
|
markUsableNoCache(...cellLikes: (CellLike | CellLike[])[]): Promise<void>;
|
|
37
37
|
markUnusable(...outPointLikes: (OutPointLike | OutPointLike[])[]): Promise<void>;
|
|
38
38
|
clear(): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../../src/client/cache/memory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAY,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAC5E,OAA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AChB,GAAG,MAAM,EAAE,CAAC,eAAe,GAAG,eAAe,EAAE,CAAC,EAAE,GACjD,OAAO,CAAC,IAAI,CAAC;IAQV,cAAc,CAAC,QAAQ,EAAE,OAAO,GAAG,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC;IAWnE,gBAAgB,CACpB,UAAU,EAAE,OAAO,GAClB,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC;IAUnC,kBAAkB,CAAC,MAAM,EAAE,iBAAiB,GAAG,OAAO;CAGvD"}
|
|
@@ -12,19 +12,19 @@ class ClientCacheMemory extends cache_js_1.ClientCache {
|
|
|
12
12
|
* @param maxCells - Maximum number of cells to store in the cache. Defaults to 512.
|
|
13
13
|
* @param maxTxs - Maximum number of transactions to store in the cache. Defaults to 256.
|
|
14
14
|
* @param maxBlocks - Maximum number of blocks to store in the cache. Defaults to 128.
|
|
15
|
-
* @param
|
|
16
|
-
*
|
|
15
|
+
* @param confirmedBlockTimeLike - Time in milliseconds after which a block is considered confirmed.
|
|
16
|
+
* Defaults to DEFAULT_CONFIRMED_BLOCK_TIME (50 blocks * 10s).
|
|
17
17
|
*/
|
|
18
|
-
constructor(maxCells = 512, maxTxs = 256, maxBlocks = 128,
|
|
18
|
+
constructor(maxCells = 512, maxTxs = 256, maxBlocks = 128, confirmedBlockTimeLike = memory_advanced_js_1.DEFAULT_CONFIRMED_BLOCK_TIME) {
|
|
19
19
|
super();
|
|
20
20
|
this.maxCells = maxCells;
|
|
21
21
|
this.maxTxs = maxTxs;
|
|
22
22
|
this.maxBlocks = maxBlocks;
|
|
23
|
-
this.confirmedBlockTime = confirmedBlockTime;
|
|
24
23
|
this.cells = new memory_advanced_js_1.MapLru(this.maxCells);
|
|
25
24
|
this.knownTransactions = new memory_advanced_js_1.MapLru(this.maxTxs);
|
|
26
25
|
this.knownBlockHashes = new memory_advanced_js_1.MapLru(this.maxBlocks);
|
|
27
26
|
this.knownBlocks = new memory_advanced_js_1.MapLru(this.maxBlocks);
|
|
27
|
+
this.confirmedBlockTime = (0, index_js_3.numFrom)(confirmedBlockTimeLike);
|
|
28
28
|
}
|
|
29
29
|
async markUsableNoCache(...cellLikes) {
|
|
30
30
|
cellLikes.flat().forEach((cellLike) => {
|
package/package.json
CHANGED
|
@@ -145,73 +145,80 @@ export function filterCell(
|
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
/**
|
|
148
|
-
* A Least Recently Used (LRU) cache
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
* When the cache is full, the least recently used entry is automatically evicted.
|
|
152
|
-
*
|
|
153
|
-
* @template K The type of the keys in the cache.
|
|
154
|
-
* @template V The type of the values in the cache.
|
|
148
|
+
* A Map-like class that implements a "Least Recently Used" (LRU) cache policy.
|
|
149
|
+
* When the cache reaches its capacity, the least recently used item is removed.
|
|
150
|
+
* @public
|
|
155
151
|
*/
|
|
156
152
|
export class MapLru<K, V> extends Map<K, V> {
|
|
153
|
+
private readonly lru: Set<K> = new Set<K>();
|
|
154
|
+
|
|
157
155
|
/**
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
* @param capacity The maximum number of entries the cache can hold. Must be a positive integer.
|
|
161
|
-
* @throws {Error} If the capacity is not a positive integer.
|
|
156
|
+
* @param capacity - The maximum number of items to store in the cache. Must be a positive integer.
|
|
162
157
|
*/
|
|
163
158
|
constructor(private readonly capacity: number) {
|
|
164
159
|
super();
|
|
160
|
+
|
|
165
161
|
if (!Number.isInteger(capacity) || capacity < 1) {
|
|
166
162
|
throw new Error("Capacity must be a positive integer");
|
|
167
163
|
}
|
|
168
164
|
}
|
|
169
165
|
|
|
170
166
|
/**
|
|
171
|
-
* Retrieves
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
* @param key The key of the value to retrieve.
|
|
176
|
-
* @returns The value associated with the key, or undefined if the key is not present.
|
|
167
|
+
* Retrieves the value for a given key and marks it as recently used.
|
|
168
|
+
* @param key - The key of the element to retrieve.
|
|
169
|
+
* @returns The value associated with the key, or `undefined` if the key is not in the cache.
|
|
177
170
|
*/
|
|
178
|
-
override get(key: K)
|
|
179
|
-
// Check if the key exists. If not, return undefined.
|
|
171
|
+
override get(key: K) {
|
|
180
172
|
if (!super.has(key)) {
|
|
181
|
-
return
|
|
173
|
+
return;
|
|
182
174
|
}
|
|
183
175
|
|
|
184
|
-
|
|
176
|
+
this.lru.delete(key);
|
|
177
|
+
this.lru.add(key);
|
|
185
178
|
|
|
186
|
-
|
|
187
|
-
super.delete(key);
|
|
188
|
-
super.set(key, value);
|
|
189
|
-
|
|
190
|
-
return value;
|
|
179
|
+
return super.get(key);
|
|
191
180
|
}
|
|
192
181
|
|
|
193
182
|
/**
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
* @param key The key of the value to insert or update.
|
|
201
|
-
* @param value The value to associate with the key.
|
|
202
|
-
* @returns This MapLru instance.
|
|
183
|
+
* Adds or updates a key-value pair in the cache and marks the key as recently used.
|
|
184
|
+
* If setting a new key causes the cache to exceed its capacity, the least recently used item is evicted.
|
|
185
|
+
* @param key - The key of the element to add or update.
|
|
186
|
+
* @param value - The value of the element to add or update.
|
|
187
|
+
* @returns The `MapLru` instance.
|
|
203
188
|
*/
|
|
204
|
-
override set(key: K, value: V)
|
|
205
|
-
// Delete and re-insert to move key to the end (most-recently-used)
|
|
206
|
-
super.delete(key);
|
|
189
|
+
override set(key: K, value: V) {
|
|
207
190
|
super.set(key, value);
|
|
208
191
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
const oldestKey = super.keys().next().value!;
|
|
212
|
-
super.delete(oldestKey);
|
|
213
|
-
}
|
|
192
|
+
this.lru.delete(key);
|
|
193
|
+
this.lru.add(key);
|
|
214
194
|
|
|
195
|
+
// Evict the oldest entry if capacity is exceeded.
|
|
196
|
+
if (this.lru.size > this.capacity) {
|
|
197
|
+
const oldestKey = this.lru.keys().next().value!;
|
|
198
|
+
this.delete(oldestKey);
|
|
199
|
+
}
|
|
215
200
|
return this;
|
|
216
201
|
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Removes the specified element from the cache.
|
|
205
|
+
* @param key - The key of the element to remove.
|
|
206
|
+
* @returns `true` if an element in the `MapLru` object existed and has been removed, or `false` if the element does not exist.
|
|
207
|
+
*/
|
|
208
|
+
override delete(key: K): boolean {
|
|
209
|
+
if (!super.delete(key)) {
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
this.lru.delete(key);
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Removes all key-value pairs from the cache.
|
|
219
|
+
*/
|
|
220
|
+
override clear() {
|
|
221
|
+
super.clear();
|
|
222
|
+
this.lru.clear();
|
|
223
|
+
}
|
|
217
224
|
}
|
|
@@ -42,18 +42,20 @@ export class ClientCacheMemory extends ClientCache {
|
|
|
42
42
|
Pick<ClientBlock, "header"> | ClientBlock
|
|
43
43
|
>;
|
|
44
44
|
|
|
45
|
+
private readonly confirmedBlockTime;
|
|
46
|
+
|
|
45
47
|
/**
|
|
46
48
|
* @param maxCells - Maximum number of cells to store in the cache. Defaults to 512.
|
|
47
49
|
* @param maxTxs - Maximum number of transactions to store in the cache. Defaults to 256.
|
|
48
50
|
* @param maxBlocks - Maximum number of blocks to store in the cache. Defaults to 128.
|
|
49
|
-
* @param
|
|
50
|
-
*
|
|
51
|
+
* @param confirmedBlockTimeLike - Time in milliseconds after which a block is considered confirmed.
|
|
52
|
+
* Defaults to DEFAULT_CONFIRMED_BLOCK_TIME (50 blocks * 10s).
|
|
51
53
|
*/
|
|
52
54
|
constructor(
|
|
53
55
|
private readonly maxCells = 512,
|
|
54
56
|
private readonly maxTxs = 256,
|
|
55
57
|
private readonly maxBlocks = 128,
|
|
56
|
-
|
|
58
|
+
confirmedBlockTimeLike: NumLike = DEFAULT_CONFIRMED_BLOCK_TIME,
|
|
57
59
|
) {
|
|
58
60
|
super();
|
|
59
61
|
|
|
@@ -66,6 +68,8 @@ export class ClientCacheMemory extends ClientCache {
|
|
|
66
68
|
string,
|
|
67
69
|
Pick<ClientBlock, "header"> | ClientBlock
|
|
68
70
|
>(this.maxBlocks);
|
|
71
|
+
|
|
72
|
+
this.confirmedBlockTime = numFrom(confirmedBlockTimeLike);
|
|
69
73
|
}
|
|
70
74
|
|
|
71
75
|
async markUsableNoCache(
|