Skip to content

Commit 587c8ca

Browse files
committed
feat(core): more rpc calls cache
1 parent 01d1eeb commit 587c8ca

File tree

9 files changed

+634
-108
lines changed

9 files changed

+634
-108
lines changed

.changeset/light-vans-arrive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ckb-ccc/core": minor
3+
---
4+
5+
feat(core): more rpc calls cache

packages/core/src/client/cache/cache.ts

Lines changed: 119 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,23 @@ import {
66
TransactionLike,
77
} from "../../ckb/index.js";
88
import { HexLike } from "../../hex/index.js";
9+
import { NumLike } from "../../num/index.js";
910
import { ClientCollectableSearchKeyLike } from "../clientTypes.advanced.js";
11+
import {
12+
ClientBlock,
13+
ClientBlockHeader,
14+
ClientBlockHeaderLike,
15+
ClientBlockLike,
16+
ClientTransactionResponse,
17+
ClientTransactionResponseLike,
18+
} from "../clientTypes.js";
1019

20+
/**
21+
* @public
22+
* The ClientCache class is mainly designed for chained transactions.
23+
* Consumed & Created cells are "marked" so they can be correctly handled when composing transactions.
24+
* It also act as cache for rpc requests to reduce cost, but this is optional.
25+
*/
1126
export abstract class ClientCache {
1227
abstract markUsable(...cellLikes: (CellLike | CellLike[])[]): Promise<void>;
1328
abstract markUnusable(
@@ -17,7 +32,12 @@ export abstract class ClientCache {
1732
...transactionLike: (TransactionLike | TransactionLike[])[]
1833
): Promise<void> {
1934
await Promise.all([
20-
this.recordTransactions(...transactionLike),
35+
this.recordTransactionResponses(
36+
transactionLike.flat().map((transaction) => ({
37+
transaction: transaction,
38+
status: "sent",
39+
})),
40+
),
2141
...transactionLike.flat().map((transactionLike) => {
2242
const tx = Transaction.from(transactionLike);
2343
const txHash = tx.hash();
@@ -42,34 +62,121 @@ export abstract class ClientCache {
4262
abstract findCells(
4363
filter: ClientCollectableSearchKeyLike,
4464
): AsyncGenerator<Cell>;
65+
abstract isUnusable(outPointLike: OutPointLike): Promise<boolean>;
66+
67+
// ======
68+
// Following methods are for requests caching and optional.
69+
// ======
70+
71+
/**
72+
* Record known cells
73+
* Implement this method to enable cells query caching
74+
* @param _cells
75+
*/
76+
async recordCells(..._cells: (CellLike | CellLike[])[]): Promise<void> {}
4577
/**
4678
* Get a known cell by out point
79+
* Implement this method to enable cells query caching
4780
* @param _outPoint
4881
*/
49-
abstract getCell(_outPoint: OutPointLike): Promise<Cell | undefined>;
50-
abstract isUnusable(outPointLike: OutPointLike): Promise<boolean>;
82+
async getCell(_outPoint: OutPointLike): Promise<Cell | undefined> {
83+
return;
84+
}
5185

5286
/**
53-
* Record known transactions
87+
* Record known transaction responses.
5488
* Implement this method to enable transactions query caching
5589
* @param _transactions
5690
*/
57-
async recordTransactions(
58-
..._transactions: (TransactionLike | TransactionLike[])[]
91+
async recordTransactionResponses(
92+
..._transactions: (
93+
| ClientTransactionResponseLike
94+
| ClientTransactionResponseLike[]
95+
)[]
5996
): Promise<void> {}
6097
/**
61-
* Get a known transaction by hash
98+
* Get a known transaction response by hash
6299
* Implement this method to enable transactions query caching
63100
* @param _txHash
64101
*/
65-
async getTransaction(_txHash: HexLike): Promise<Transaction | undefined> {
102+
async getTransactionResponse(
103+
_txHash: HexLike,
104+
): Promise<ClientTransactionResponse | undefined> {
105+
return;
106+
}
107+
/**
108+
* Record known transactions.
109+
* @param transactions
110+
*/
111+
async recordTransactions(
112+
...transactions: (TransactionLike | TransactionLike[])[]
113+
): Promise<void> {
114+
return this.recordTransactionResponses(
115+
transactions.flat().map((transaction) => ({
116+
transaction,
117+
status: "unknown",
118+
})),
119+
);
120+
}
121+
/**
122+
* Get a known transaction by hash
123+
* @param txHash
124+
*/
125+
async getTransaction(txHash: HexLike): Promise<Transaction | undefined> {
126+
return (await this.getTransactionResponse(txHash))?.transaction;
127+
}
128+
129+
/**
130+
* Record known block headers.
131+
* Implement this method to enable block headers query caching
132+
* @param _headers
133+
*/
134+
async recordHeaders(
135+
..._headers: (ClientBlockHeaderLike | ClientBlockHeaderLike[])[]
136+
): Promise<void> {}
137+
/**
138+
* Get a known block header by hash
139+
* Implement this method to enable block headers query caching
140+
* @param _hash
141+
*/
142+
async getHeaderByHash(
143+
_hash: HexLike,
144+
): Promise<ClientBlockHeader | undefined> {
145+
return;
146+
}
147+
/**
148+
* Get a known block header by number
149+
* Implement this method to enable block headers query caching
150+
* @param _number
151+
*/
152+
async getHeaderByNumber(
153+
_number: NumLike,
154+
): Promise<ClientBlockHeader | undefined> {
66155
return;
67156
}
68157

69158
/**
70-
* Record known cells
71-
* Implement this method to enable cells query caching
72-
* @param _cells
159+
* Record known blocks.
160+
* Implement this method to enable blocks query caching
161+
* @param _blocks
73162
*/
74-
async recordCells(..._cells: (CellLike | CellLike[])[]): Promise<void> {}
163+
async recordBlocks(
164+
..._blocks: (ClientBlockLike | ClientBlockLike[])[]
165+
): Promise<void> {}
166+
/**
167+
* Get a known block header by hash
168+
* Implement this method to enable block headers query caching
169+
* @param _hash
170+
*/
171+
async getBlockByHash(_hash: HexLike): Promise<ClientBlock | undefined> {
172+
return;
173+
}
174+
/**
175+
* Get a known block header by number
176+
* Implement this method to enable block headers query caching
177+
* @param _number
178+
*/
179+
async getBlockByNumber(_number: NumLike): Promise<ClientBlock | undefined> {
180+
return;
181+
}
75182
}

packages/core/src/client/cache/memory.advanced.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,12 @@ export class MapLru<K, V> extends Map<K, V> {
149149
super();
150150
}
151151

152-
access(key: K) {
152+
get(key: K) {
153+
const val = super.get(key);
154+
if (val === undefined) {
155+
return;
156+
}
157+
153158
const index = this.lru.indexOf(key);
154159
if (index !== -1) {
155160
this.lru.splice(index, 1);
@@ -159,15 +164,27 @@ export class MapLru<K, V> extends Map<K, V> {
159164
this.delete(this.lru[0]);
160165
this.lru.shift();
161166
}
162-
}
163167

164-
get(key: K) {
165-
this.access(key);
166-
return super.get(key);
168+
return val;
167169
}
168170

169171
set(key: K, value: V) {
170-
this.access(key);
171-
return super.set(key, value);
172+
this.get(key);
173+
174+
super.set(key, value);
175+
return this;
176+
}
177+
178+
delete(key: K): boolean {
179+
if (!super.delete(key)) {
180+
return false;
181+
}
182+
183+
const index = this.lru.indexOf(key);
184+
if (index !== -1) {
185+
this.lru.splice(index, 1);
186+
}
187+
188+
return true;
172189
}
173190
}

0 commit comments

Comments
 (0)