@ckb-ccc/core 1.5.3 → 1.7.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.
Files changed (161) hide show
  1. package/CHANGELOG.md +61 -7
  2. package/dist/ckb/script.d.ts +2 -1
  3. package/dist/ckb/script.d.ts.map +1 -1
  4. package/dist/ckb/transaction.d.ts +125 -7
  5. package/dist/ckb/transaction.d.ts.map +1 -1
  6. package/dist/ckb/transaction.js +263 -73
  7. package/dist/client/cache/cache.d.ts +67 -11
  8. package/dist/client/cache/cache.d.ts.map +1 -1
  9. package/dist/client/cache/cache.js +94 -9
  10. package/dist/client/cache/memory.advanced.d.ts +2 -2
  11. package/dist/client/cache/memory.advanced.d.ts.map +1 -1
  12. package/dist/client/cache/memory.advanced.js +19 -7
  13. package/dist/client/cache/memory.d.ts +24 -7
  14. package/dist/client/cache/memory.d.ts.map +1 -1
  15. package/dist/client/cache/memory.js +76 -18
  16. package/dist/client/client.d.ts +30 -7
  17. package/dist/client/client.d.ts.map +1 -1
  18. package/dist/client/client.js +114 -22
  19. package/dist/client/clientPublicMainnet.advanced.d.ts +2 -1
  20. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  21. package/dist/client/clientPublicMainnet.advanced.js +1 -1
  22. package/dist/client/clientPublicMainnet.d.ts +2 -1
  23. package/dist/client/clientPublicMainnet.d.ts.map +1 -1
  24. package/dist/client/clientPublicMainnet.js +1 -6
  25. package/dist/client/clientPublicTestnet.advanced.d.ts +2 -1
  26. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  27. package/dist/client/clientPublicTestnet.advanced.js +1 -1
  28. package/dist/client/clientPublicTestnet.d.ts +2 -1
  29. package/dist/client/clientPublicTestnet.d.ts.map +1 -1
  30. package/dist/client/clientTypes.advanced.d.ts +3 -0
  31. package/dist/client/clientTypes.advanced.d.ts.map +1 -1
  32. package/dist/client/clientTypes.advanced.js +3 -0
  33. package/dist/client/clientTypes.d.ts +110 -38
  34. package/dist/client/clientTypes.d.ts.map +1 -1
  35. package/dist/client/clientTypes.js +118 -31
  36. package/dist/client/index.d.ts +1 -0
  37. package/dist/client/index.d.ts.map +1 -1
  38. package/dist/client/index.js +1 -0
  39. package/dist/client/jsonRpc/client.d.ts +4 -4
  40. package/dist/client/jsonRpc/client.d.ts.map +1 -1
  41. package/dist/client/jsonRpc/client.js +4 -4
  42. package/dist/client/jsonRpc/transformers.js +3 -3
  43. package/dist/client/knownScript.d.ts +26 -0
  44. package/dist/client/knownScript.d.ts.map +1 -0
  45. package/dist/client/knownScript.js +27 -0
  46. package/dist/jsonRpc/transports/webSocket.d.ts.map +1 -1
  47. package/dist/jsonRpc/transports/webSocket.js +7 -3
  48. package/dist/molecule/predefined.d.ts +10 -10
  49. package/dist/molecule/predefined.d.ts.map +1 -1
  50. package/dist/num/index.d.ts.map +1 -1
  51. package/dist/num/index.js +17 -1
  52. package/dist/signer/ckb/signerCkbPublicKey.d.ts.map +1 -1
  53. package/dist/signer/ckb/signerCkbPublicKey.js +1 -5
  54. package/dist/signer/nostr/index.d.ts +1 -0
  55. package/dist/signer/nostr/index.d.ts.map +1 -1
  56. package/dist/signer/nostr/index.js +1 -0
  57. package/dist/signer/nostr/signerNostr.d.ts +1 -1
  58. package/dist/signer/nostr/signerNostr.d.ts.map +1 -1
  59. package/dist/signer/nostr/signerNostr.js +8 -0
  60. package/dist/signer/nostr/signerNostrPrivateKey.d.ts +5 -7
  61. package/dist/signer/nostr/signerNostrPrivateKey.d.ts.map +1 -1
  62. package/dist/signer/nostr/signerNostrPrivateKey.js +6 -12
  63. package/dist/signer/nostr/signerNostrPublicKeyReadonly.d.ts +11 -0
  64. package/dist/signer/nostr/signerNostrPublicKeyReadonly.d.ts.map +1 -0
  65. package/dist/signer/nostr/signerNostrPublicKeyReadonly.js +22 -0
  66. package/dist/utils/index.d.ts +4 -0
  67. package/dist/utils/index.d.ts.map +1 -1
  68. package/dist/utils/index.js +13 -1
  69. package/dist.commonjs/advanced.js +17 -7
  70. package/dist.commonjs/ckb/script.d.ts +2 -1
  71. package/dist.commonjs/ckb/script.d.ts.map +1 -1
  72. package/dist.commonjs/ckb/transaction.d.ts +125 -7
  73. package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
  74. package/dist.commonjs/ckb/transaction.js +352 -160
  75. package/dist.commonjs/client/cache/cache.d.ts +67 -11
  76. package/dist.commonjs/client/cache/cache.d.ts.map +1 -1
  77. package/dist.commonjs/client/cache/cache.js +94 -9
  78. package/dist.commonjs/client/cache/memory.advanced.d.ts +2 -2
  79. package/dist.commonjs/client/cache/memory.advanced.d.ts.map +1 -1
  80. package/dist.commonjs/client/cache/memory.advanced.js +19 -7
  81. package/dist.commonjs/client/cache/memory.d.ts +24 -7
  82. package/dist.commonjs/client/cache/memory.d.ts.map +1 -1
  83. package/dist.commonjs/client/cache/memory.js +75 -17
  84. package/dist.commonjs/client/client.d.ts +30 -7
  85. package/dist.commonjs/client/client.d.ts.map +1 -1
  86. package/dist.commonjs/client/client.js +112 -20
  87. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts +2 -1
  88. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  89. package/dist.commonjs/client/clientPublicMainnet.advanced.js +21 -21
  90. package/dist.commonjs/client/clientPublicMainnet.d.ts +2 -1
  91. package/dist.commonjs/client/clientPublicMainnet.d.ts.map +1 -1
  92. package/dist.commonjs/client/clientPublicMainnet.js +1 -6
  93. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts +2 -1
  94. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  95. package/dist.commonjs/client/clientPublicTestnet.advanced.js +21 -21
  96. package/dist.commonjs/client/clientPublicTestnet.d.ts +2 -1
  97. package/dist.commonjs/client/clientPublicTestnet.d.ts.map +1 -1
  98. package/dist.commonjs/client/clientTypes.advanced.d.ts +3 -0
  99. package/dist.commonjs/client/clientTypes.advanced.d.ts.map +1 -1
  100. package/dist.commonjs/client/clientTypes.advanced.js +4 -0
  101. package/dist.commonjs/client/clientTypes.d.ts +110 -38
  102. package/dist.commonjs/client/clientTypes.d.ts.map +1 -1
  103. package/dist.commonjs/client/clientTypes.js +123 -31
  104. package/dist.commonjs/client/index.d.ts +1 -0
  105. package/dist.commonjs/client/index.d.ts.map +1 -1
  106. package/dist.commonjs/client/index.js +1 -0
  107. package/dist.commonjs/client/jsonRpc/client.d.ts +4 -4
  108. package/dist.commonjs/client/jsonRpc/client.d.ts.map +1 -1
  109. package/dist.commonjs/client/jsonRpc/client.js +4 -4
  110. package/dist.commonjs/client/jsonRpc/transformers.js +2 -2
  111. package/dist.commonjs/client/knownScript.d.ts +26 -0
  112. package/dist.commonjs/client/knownScript.d.ts.map +1 -0
  113. package/dist.commonjs/client/knownScript.js +30 -0
  114. package/dist.commonjs/index.js +17 -7
  115. package/dist.commonjs/jsonRpc/transports/webSocket.d.ts.map +1 -1
  116. package/dist.commonjs/jsonRpc/transports/webSocket.js +7 -3
  117. package/dist.commonjs/molecule/index.js +17 -7
  118. package/dist.commonjs/num/index.d.ts.map +1 -1
  119. package/dist.commonjs/num/index.js +17 -1
  120. package/dist.commonjs/signer/ckb/signerCkbPublicKey.d.ts.map +1 -1
  121. package/dist.commonjs/signer/ckb/signerCkbPublicKey.js +1 -5
  122. package/dist.commonjs/signer/nostr/index.d.ts +1 -0
  123. package/dist.commonjs/signer/nostr/index.d.ts.map +1 -1
  124. package/dist.commonjs/signer/nostr/index.js +1 -0
  125. package/dist.commonjs/signer/nostr/signerNostr.d.ts +1 -1
  126. package/dist.commonjs/signer/nostr/signerNostr.d.ts.map +1 -1
  127. package/dist.commonjs/signer/nostr/signerNostr.js +8 -0
  128. package/dist.commonjs/signer/nostr/signerNostrPrivateKey.d.ts +5 -7
  129. package/dist.commonjs/signer/nostr/signerNostrPrivateKey.d.ts.map +1 -1
  130. package/dist.commonjs/signer/nostr/signerNostrPrivateKey.js +6 -12
  131. package/dist.commonjs/signer/nostr/signerNostrPublicKeyReadonly.d.ts +11 -0
  132. package/dist.commonjs/signer/nostr/signerNostrPublicKeyReadonly.d.ts.map +1 -0
  133. package/dist.commonjs/signer/nostr/signerNostrPublicKeyReadonly.js +26 -0
  134. package/dist.commonjs/utils/index.d.ts +4 -0
  135. package/dist.commonjs/utils/index.d.ts.map +1 -1
  136. package/dist.commonjs/utils/index.js +13 -0
  137. package/package.json +1 -1
  138. package/src/ckb/script.ts +2 -1
  139. package/src/ckb/transaction.ts +376 -97
  140. package/src/client/cache/cache.ts +126 -13
  141. package/src/client/cache/memory.advanced.ts +24 -7
  142. package/src/client/cache/memory.ts +130 -30
  143. package/src/client/client.ts +172 -28
  144. package/src/client/clientPublicMainnet.advanced.ts +2 -1
  145. package/src/client/clientPublicMainnet.ts +3 -7
  146. package/src/client/clientPublicTestnet.advanced.ts +2 -1
  147. package/src/client/clientPublicTestnet.ts +2 -1
  148. package/src/client/clientTypes.advanced.ts +4 -0
  149. package/src/client/clientTypes.ts +224 -61
  150. package/src/client/index.ts +1 -0
  151. package/src/client/jsonRpc/client.ts +11 -9
  152. package/src/client/jsonRpc/transformers.ts +2 -2
  153. package/src/client/knownScript.ts +27 -0
  154. package/src/jsonRpc/transports/webSocket.ts +17 -13
  155. package/src/num/index.ts +21 -1
  156. package/src/signer/ckb/signerCkbPublicKey.ts +3 -5
  157. package/src/signer/nostr/index.ts +1 -0
  158. package/src/signer/nostr/signerNostr.ts +3 -1
  159. package/src/signer/nostr/signerNostrPrivateKey.ts +7 -14
  160. package/src/signer/nostr/signerNostrPublicKeyReadonly.ts +29 -0
  161. package/src/utils/index.ts +14 -1
@@ -6,10 +6,31 @@ import {
6
6
  TransactionLike,
7
7
  } from "../../ckb/index.js";
8
8
  import { HexLike } from "../../hex/index.js";
9
+ import { NumLike } from "../../num/index.js";
9
10
  import { ClientCollectableSearchKeyLike } from "../clientTypes.advanced.js";
11
+ import {
12
+ ClientBlock,
13
+ ClientBlockHeader,
14
+ ClientBlockHeaderLike,
15
+ ClientBlockLike,
16
+ ClientTransactionResponse,
17
+ ClientTransactionResponseLike,
18
+ } from "../clientTypes.js";
10
19
 
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
+ */
11
26
  export abstract class ClientCache {
12
- abstract markUsable(...cellLikes: (CellLike | CellLike[])[]): Promise<void>;
27
+ abstract markUsableNoCache(
28
+ ...cellLikes: (CellLike | CellLike[])[]
29
+ ): Promise<void>;
30
+ async markUsable(...cellLikes: (CellLike | CellLike[])[]): Promise<void> {
31
+ await this.recordCells(...cellLikes);
32
+ return this.markUsableNoCache(...cellLikes);
33
+ }
13
34
  abstract markUnusable(
14
35
  ...outPointLike: (OutPointLike | OutPointLike[])[]
15
36
  ): Promise<void>;
@@ -17,7 +38,12 @@ export abstract class ClientCache {
17
38
  ...transactionLike: (TransactionLike | TransactionLike[])[]
18
39
  ): Promise<void> {
19
40
  await Promise.all([
20
- this.recordTransactions(...transactionLike),
41
+ this.recordTransactionResponses(
42
+ transactionLike.flat().map((transaction) => ({
43
+ transaction: transaction,
44
+ status: "sent",
45
+ })),
46
+ ),
21
47
  ...transactionLike.flat().map((transactionLike) => {
22
48
  const tx = Transaction.from(transactionLike);
23
49
  const txHash = tx.hash();
@@ -42,34 +68,121 @@ export abstract class ClientCache {
42
68
  abstract findCells(
43
69
  filter: ClientCollectableSearchKeyLike,
44
70
  ): AsyncGenerator<Cell>;
71
+ abstract isUnusable(outPointLike: OutPointLike): Promise<boolean>;
72
+
73
+ // ======
74
+ // Following methods are for requests caching and optional.
75
+ // ======
76
+
77
+ /**
78
+ * Record known cells
79
+ * Implement this method to enable cells query caching
80
+ * @param _cells
81
+ */
82
+ async recordCells(..._cells: (CellLike | CellLike[])[]): Promise<void> {}
45
83
  /**
46
84
  * Get a known cell by out point
85
+ * Implement this method to enable cells query caching
47
86
  * @param _outPoint
48
87
  */
49
- abstract getCell(_outPoint: OutPointLike): Promise<Cell | undefined>;
50
- abstract isUnusable(outPointLike: OutPointLike): Promise<boolean>;
88
+ async getCell(_outPoint: OutPointLike): Promise<Cell | undefined> {
89
+ return;
90
+ }
51
91
 
52
92
  /**
53
- * Record known transactions
93
+ * Record known transaction responses.
54
94
  * Implement this method to enable transactions query caching
55
95
  * @param _transactions
56
96
  */
57
- async recordTransactions(
58
- ..._transactions: (TransactionLike | TransactionLike[])[]
97
+ async recordTransactionResponses(
98
+ ..._transactions: (
99
+ | ClientTransactionResponseLike
100
+ | ClientTransactionResponseLike[]
101
+ )[]
59
102
  ): Promise<void> {}
60
103
  /**
61
- * Get a known transaction by hash
104
+ * Get a known transaction response by hash
62
105
  * Implement this method to enable transactions query caching
63
106
  * @param _txHash
64
107
  */
65
- async getTransaction(_txHash: HexLike): Promise<Transaction | undefined> {
108
+ async getTransactionResponse(
109
+ _txHash: HexLike,
110
+ ): Promise<ClientTransactionResponse | undefined> {
111
+ return;
112
+ }
113
+ /**
114
+ * Record known transactions.
115
+ * @param transactions
116
+ */
117
+ async recordTransactions(
118
+ ...transactions: (TransactionLike | TransactionLike[])[]
119
+ ): Promise<void> {
120
+ return this.recordTransactionResponses(
121
+ transactions.flat().map((transaction) => ({
122
+ transaction,
123
+ status: "unknown",
124
+ })),
125
+ );
126
+ }
127
+ /**
128
+ * Get a known transaction by hash
129
+ * @param txHash
130
+ */
131
+ async getTransaction(txHash: HexLike): Promise<Transaction | undefined> {
132
+ return (await this.getTransactionResponse(txHash))?.transaction;
133
+ }
134
+
135
+ /**
136
+ * Record known block headers.
137
+ * Implement this method to enable block headers query caching
138
+ * @param _headers
139
+ */
140
+ async recordHeaders(
141
+ ..._headers: (ClientBlockHeaderLike | ClientBlockHeaderLike[])[]
142
+ ): Promise<void> {}
143
+ /**
144
+ * Get a known block header by hash
145
+ * Implement this method to enable block headers query caching
146
+ * @param _hash
147
+ */
148
+ async getHeaderByHash(
149
+ _hash: HexLike,
150
+ ): Promise<ClientBlockHeader | undefined> {
151
+ return;
152
+ }
153
+ /**
154
+ * Get a known block header by number
155
+ * Implement this method to enable block headers query caching
156
+ * @param _number
157
+ */
158
+ async getHeaderByNumber(
159
+ _number: NumLike,
160
+ ): Promise<ClientBlockHeader | undefined> {
66
161
  return;
67
162
  }
68
163
 
69
164
  /**
70
- * Record known cells
71
- * Implement this method to enable cells query caching
72
- * @param _cells
165
+ * Record known blocks.
166
+ * Implement this method to enable blocks query caching
167
+ * @param _blocks
73
168
  */
74
- async recordCells(..._cells: (CellLike | CellLike[])[]): Promise<void> {}
169
+ async recordBlocks(
170
+ ..._blocks: (ClientBlockLike | ClientBlockLike[])[]
171
+ ): Promise<void> {}
172
+ /**
173
+ * Get a known block header by hash
174
+ * Implement this method to enable block headers query caching
175
+ * @param _hash
176
+ */
177
+ async getBlockByHash(_hash: HexLike): Promise<ClientBlock | undefined> {
178
+ return;
179
+ }
180
+ /**
181
+ * Get a known block header by number
182
+ * Implement this method to enable block headers query caching
183
+ * @param _number
184
+ */
185
+ async getBlockByNumber(_number: NumLike): Promise<ClientBlock | undefined> {
186
+ return;
187
+ }
75
188
  }
@@ -149,7 +149,12 @@ export class MapLru<K, V> extends Map<K, V> {
149
149
  super();
150
150
  }
151
151
 
152
- access(key: K) {
152
+ get(key: K) {
153
+ const val = super.get(key);
154
+ if (val === undefined) {
155
+ return;
156
+ }
157
+
153
158
  const index = this.lru.indexOf(key);
154
159
  if (index !== -1) {
155
160
  this.lru.splice(index, 1);
@@ -159,15 +164,27 @@ export class MapLru<K, V> extends Map<K, V> {
159
164
  this.delete(this.lru[0]);
160
165
  this.lru.shift();
161
166
  }
162
- }
163
167
 
164
- get(key: K) {
165
- this.access(key);
166
- return super.get(key);
168
+ return val;
167
169
  }
168
170
 
169
171
  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;
172
189
  }
173
190
  }
@@ -1,13 +1,15 @@
1
- import {
2
- Cell,
3
- CellLike,
4
- OutPoint,
5
- OutPointLike,
6
- Transaction,
7
- TransactionLike,
8
- } from "../../ckb/index.js";
1
+ import { Cell, CellLike, OutPoint, OutPointLike } from "../../ckb/index.js";
9
2
  import { hexFrom, HexLike } from "../../hex/index.js";
3
+ import { Num, numFrom, NumLike } from "../../num/index.js";
10
4
  import { ClientCollectableSearchKeyLike } from "../clientTypes.advanced.js";
5
+ import {
6
+ ClientBlock,
7
+ ClientBlockHeader,
8
+ ClientBlockHeaderLike,
9
+ ClientBlockLike,
10
+ ClientTransactionResponse,
11
+ ClientTransactionResponseLike,
12
+ } from "../clientTypes.js";
11
13
  import { ClientCache } from "./cache.js";
12
14
  import { CellRecord, filterCell, MapLru } from "./memory.advanced.js";
13
15
 
@@ -18,21 +20,44 @@ export class ClientCacheMemory extends ClientCache {
18
20
  private readonly cells: MapLru<string, CellRecord>;
19
21
 
20
22
  /**
21
- * TX Hash => Transaction
23
+ * TX Hash => Transaction Response
24
+ */
25
+ private readonly knownTransactions: MapLru<string, ClientTransactionResponse>;
26
+
27
+ /**
28
+ * Block Number => Block Hash
29
+ */
30
+ private readonly knownBlockHashes: MapLru<Num, string>;
31
+
32
+ /**
33
+ * Block Hash => Block Header / Full Block
22
34
  */
23
- private readonly knownTransactions: MapLru<string, Transaction>;
35
+ private readonly knownBlocks: MapLru<
36
+ string,
37
+ Pick<ClientBlock, "header"> | ClientBlock
38
+ >;
24
39
 
25
40
  constructor(
26
41
  private readonly maxCells = 512,
27
42
  private readonly maxTxs = 256,
43
+ private readonly maxBlocks = 128,
28
44
  ) {
29
45
  super();
30
46
 
31
47
  this.cells = new MapLru<string, CellRecord>(this.maxCells);
32
- this.knownTransactions = new MapLru<string, Transaction>(this.maxTxs);
48
+ this.knownTransactions = new MapLru<string, ClientTransactionResponse>(
49
+ this.maxTxs,
50
+ );
51
+ this.knownBlockHashes = new MapLru<Num, string>(this.maxBlocks);
52
+ this.knownBlocks = new MapLru<
53
+ string,
54
+ Pick<ClientBlock, "header"> | ClientBlock
55
+ >(this.maxBlocks);
33
56
  }
34
57
 
35
- async markUsable(...cellLikes: (CellLike | CellLike[])[]): Promise<void> {
58
+ async markUsableNoCache(
59
+ ...cellLikes: (CellLike | CellLike[])[]
60
+ ): Promise<void> {
36
61
  cellLikes.flat().forEach((cellLike) => {
37
62
  const cell = Cell.from(cellLike).clone();
38
63
  const outPointStr = hexFrom(cell.outPoint.toBytes());
@@ -73,10 +98,28 @@ export class ClientCacheMemory extends ClientCache {
73
98
  continue;
74
99
  }
75
100
 
76
- this.cells.access(key);
101
+ this.cells.get(key);
77
102
  yield cell.clone();
78
103
  }
79
104
  }
105
+
106
+ async isUnusable(outPointLike: OutPointLike): Promise<boolean> {
107
+ const outPoint = OutPoint.from(outPointLike);
108
+
109
+ return !(this.cells.get(hexFrom(outPoint.toBytes()))?.[0] ?? true);
110
+ }
111
+
112
+ async recordCells(...cells: (CellLike | CellLike[])[]): Promise<void> {
113
+ cells.flat().map((cellLike) => {
114
+ const cell = Cell.from(cellLike);
115
+ const outPointStr = hexFrom(cell.outPoint.toBytes());
116
+
117
+ if (this.cells.get(outPointStr)) {
118
+ return;
119
+ }
120
+ this.cells.set(outPointStr, [undefined, cell]);
121
+ });
122
+ }
80
123
  async getCell(outPointLike: OutPointLike): Promise<Cell | undefined> {
81
124
  const outPoint = OutPoint.from(outPointLike);
82
125
 
@@ -86,34 +129,91 @@ export class ClientCacheMemory extends ClientCache {
86
129
  }
87
130
  }
88
131
 
89
- async isUnusable(outPointLike: OutPointLike): Promise<boolean> {
90
- const outPoint = OutPoint.from(outPointLike);
91
-
92
- return !(this.cells.get(hexFrom(outPoint.toBytes()))?.[0] ?? true);
93
- }
94
-
95
- async recordTransactions(
96
- ...transactions: (TransactionLike | TransactionLike[])[]
132
+ async recordTransactionResponses(
133
+ ...transactions: (
134
+ | ClientTransactionResponseLike
135
+ | ClientTransactionResponseLike[]
136
+ )[]
97
137
  ): Promise<void> {
98
138
  transactions.flat().map((txLike) => {
99
- const tx = Transaction.from(txLike);
100
- this.knownTransactions.set(tx.hash(), tx);
139
+ const tx = ClientTransactionResponse.from(txLike);
140
+ this.knownTransactions.set(tx.transaction.hash(), tx);
101
141
  });
102
142
  }
103
- async getTransaction(txHashLike: HexLike): Promise<Transaction | undefined> {
143
+ async getTransactionResponse(
144
+ txHashLike: HexLike,
145
+ ): Promise<ClientTransactionResponse | undefined> {
104
146
  const txHash = hexFrom(txHashLike);
105
147
  return this.knownTransactions.get(txHash)?.clone();
106
148
  }
107
149
 
108
- async recordCells(...cells: (CellLike | CellLike[])[]): Promise<void> {
109
- cells.flat().map((cellLike) => {
110
- const cell = Cell.from(cellLike);
111
- const outPointStr = hexFrom(cell.outPoint.toBytes());
150
+ async recordHeaders(
151
+ ...headers: (ClientBlockHeaderLike | ClientBlockHeaderLike[])[]
152
+ ): Promise<void> {
153
+ headers.flat().map((headerLike) => {
154
+ const header = ClientBlockHeader.from(headerLike);
112
155
 
113
- if (this.cells.get(outPointStr)) {
156
+ this.knownBlockHashes.set(header.number, header.hash);
157
+
158
+ const existed = this.knownBlocks.get(header.hash);
159
+ if (existed) {
114
160
  return;
115
161
  }
116
- this.cells.set(outPointStr, [undefined, cell]);
162
+ this.knownBlocks.set(header.hash, { header });
117
163
  });
118
164
  }
165
+ async getHeaderByHash(
166
+ hashLike: HexLike,
167
+ ): Promise<ClientBlockHeader | undefined> {
168
+ const hash = hexFrom(hashLike);
169
+ const block = this.knownBlocks.get(hash);
170
+ if (block) {
171
+ this.knownBlockHashes.get(block.header.number); // For LRU
172
+ }
173
+ return block?.header;
174
+ }
175
+ async getHeaderByNumber(
176
+ numberLike: NumLike,
177
+ ): Promise<ClientBlockHeader | undefined> {
178
+ const number = numFrom(numberLike);
179
+
180
+ const hash = this.knownBlockHashes.get(number);
181
+ if (!hash) {
182
+ return;
183
+ }
184
+ return this.getHeaderByHash(hash);
185
+ }
186
+
187
+ async recordBlocks(
188
+ ...blocks: (ClientBlockLike | ClientBlockLike[])[]
189
+ ): Promise<void> {
190
+ blocks.flat().map((blockLike) => {
191
+ const block = ClientBlock.from(blockLike);
192
+
193
+ this.knownBlockHashes.set(block.header.number, block.header.hash);
194
+ this.knownBlocks.set(block.header.hash, block);
195
+ });
196
+ }
197
+ async getBlockByHash(hashLike: HexLike): Promise<ClientBlock | undefined> {
198
+ const hash = hexFrom(hashLike);
199
+ const block = this.knownBlocks.get(hash);
200
+ if (block) {
201
+ this.knownBlockHashes.get(block.header.number); // For LRU
202
+ if ("transactions" in block) {
203
+ return block;
204
+ }
205
+ }
206
+ return;
207
+ }
208
+ async getBlockByNumber(
209
+ numberLike: NumLike,
210
+ ): Promise<ClientBlock | undefined> {
211
+ const number = numFrom(numberLike);
212
+
213
+ const hash = this.knownBlockHashes.get(number);
214
+ if (!hash) {
215
+ return;
216
+ }
217
+ return this.getBlockByHash(hash);
218
+ }
119
219
  }