@atomiqlabs/chain-evm 1.0.0-dev.70 → 1.0.0-dev.71

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.
@@ -47,6 +47,8 @@ export declare class EVMBtcRelay<B extends BtcBlock> extends EVMContractBase<Btc
47
47
  */
48
48
  private _saveHeaders;
49
49
  private findStoredBlockheaderInTraces;
50
+ private commitHashCache;
51
+ private blockHashCache;
50
52
  private getBlock;
51
53
  private getBlockHeight;
52
54
  /**
@@ -54,6 +54,8 @@ class EVMBtcRelay extends EVMContractBase_1.EVMContractBase {
54
54
  this.maxHeadersPerTx = 100;
55
55
  this.maxForkHeadersPerTx = 50;
56
56
  this.maxShortForkHeadersPerTx = 100;
57
+ this.commitHashCache = new Map;
58
+ this.blockHashCache = new Map;
57
59
  this.bitcoinRpc = bitcoinRpc;
58
60
  }
59
61
  /**
@@ -151,14 +153,27 @@ class EVMBtcRelay extends EVMContractBase_1.EVMContractBase {
151
153
  return null;
152
154
  }
153
155
  getBlock(commitHash, blockHash) {
156
+ if (commitHash != null && this.commitHashCache.has(commitHash)) {
157
+ logger.debug("getBlock(): Returning block from commit hash cache: ", commitHash);
158
+ return Promise.resolve([this.commitHashCache.get(commitHash), commitHash]);
159
+ }
160
+ const blockHashString = blockHash == null ? null : "0x" + Buffer.from([...blockHash]).reverse().toString("hex");
161
+ if (blockHashString != null && this.blockHashCache.has(blockHashString)) {
162
+ logger.debug("getBlock(): Returning block from block hash cache: ", blockHashString);
163
+ const storedBlockheader = this.blockHashCache.get(commitHash);
164
+ return Promise.resolve([storedBlockheader, storedBlockheader.getCommitHash()]);
165
+ }
154
166
  return this.Events.findInContractEvents(["StoreHeader", "StoreForkHeader"], [
155
167
  commitHash,
156
- blockHash == null ? null : "0x" + Buffer.from([...blockHash]).reverse().toString("hex")
168
+ blockHashString
157
169
  ], async (event) => {
158
170
  const txTrace = await this.Chain.Transactions.traceTransaction(event.transactionHash);
159
171
  const storedBlockheader = await this.findStoredBlockheaderInTraces(txTrace, event.args.commitHash);
160
- if (storedBlockheader != null)
172
+ if (storedBlockheader != null) {
173
+ this.commitHashCache.set(event.args.commitHash, storedBlockheader);
174
+ this.blockHashCache.set(event.args.blockHash, storedBlockheader);
161
175
  return [storedBlockheader, event.args.commitHash];
176
+ }
162
177
  });
163
178
  }
164
179
  async getBlockHeight() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/chain-evm",
3
- "version": "1.0.0-dev.70",
3
+ "version": "1.0.0-dev.71",
4
4
  "description": "EVM specific base implementation",
5
5
  "main": "./dist/index.js",
6
6
  "types:": "./dist/index.d.ts",
@@ -23,7 +23,7 @@
23
23
  "author": "adambor",
24
24
  "license": "Apache-2.0",
25
25
  "dependencies": {
26
- "@atomiqlabs/base": "^10.0.0-dev.9",
26
+ "@atomiqlabs/base": "^10.0.0-dev.10",
27
27
  "@noble/hashes": "^1.8.0",
28
28
  "@scure/btc-signer": "1.6.0",
29
29
  "buffer": "6.0.3"
@@ -191,17 +191,35 @@ export class EVMBtcRelay<B extends BtcBlock>
191
191
  return null;
192
192
  }
193
193
 
194
+ private commitHashCache: Map<string, EVMBtcStoredHeader> = new Map<string, EVMBtcStoredHeader>;
195
+ private blockHashCache: Map<string, EVMBtcStoredHeader> = new Map<string, EVMBtcStoredHeader>;
196
+
194
197
  private getBlock(commitHash?: string, blockHash?: Buffer): Promise<[EVMBtcStoredHeader, string] | null> {
198
+ if(commitHash!=null && this.commitHashCache.has(commitHash)) {
199
+ logger.debug("getBlock(): Returning block from commit hash cache: ", commitHash);
200
+ return Promise.resolve([this.commitHashCache.get(commitHash), commitHash]);
201
+ }
202
+ const blockHashString = blockHash==null ? null : "0x"+Buffer.from([...blockHash]).reverse().toString("hex");
203
+ if(blockHashString!=null && this.blockHashCache.has(blockHashString)) {
204
+ logger.debug("getBlock(): Returning block from block hash cache: ", blockHashString);
205
+ const storedBlockheader = this.blockHashCache.get(commitHash);
206
+ return Promise.resolve([storedBlockheader, storedBlockheader.getCommitHash()]);
207
+ }
208
+
195
209
  return this.Events.findInContractEvents(
196
210
  ["StoreHeader", "StoreForkHeader"],
197
211
  [
198
212
  commitHash,
199
- blockHash==null ? null : "0x"+Buffer.from([...blockHash]).reverse().toString("hex")
213
+ blockHashString
200
214
  ],
201
215
  async (event) => {
202
216
  const txTrace = await this.Chain.Transactions.traceTransaction(event.transactionHash);
203
217
  const storedBlockheader = await this.findStoredBlockheaderInTraces(txTrace, event.args.commitHash);
204
- if(storedBlockheader!=null) return [storedBlockheader, event.args.commitHash];
218
+ if(storedBlockheader!=null) {
219
+ this.commitHashCache.set(event.args.commitHash, storedBlockheader);
220
+ this.blockHashCache.set(event.args.blockHash, storedBlockheader);
221
+ return [storedBlockheader, event.args.commitHash];
222
+ }
205
223
  }
206
224
  );
207
225
  }