@atomiqlabs/chain-evm 1.0.0-dev.69 → 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() {
@@ -31,12 +31,15 @@ class EVMTransactions extends EVMModule_1.EVMModule {
31
31
  };
32
32
  this.onBeforeTxReplace(txReplaceListener);
33
33
  let state = "pending";
34
+ let confirmedTxId = null;
34
35
  while (state === "pending" || state === "not_found") {
35
36
  await (0, Utils_1.timeoutPromise)(3000, abortSignal);
36
37
  for (let txId of checkTxns) {
37
38
  state = await this.getTxIdStatus(txId);
38
- if (state === "reverted" || state === "success")
39
+ if (state === "reverted" || state === "success") {
40
+ confirmedTxId = txId;
39
41
  break;
42
+ }
40
43
  }
41
44
  }
42
45
  this.offBeforeTxReplace(txReplaceListener);
@@ -47,6 +50,7 @@ class EVMTransactions extends EVMModule_1.EVMModule {
47
50
  }
48
51
  if (state === "reverted")
49
52
  throw new Error("Transaction reverted!");
53
+ return confirmedTxId;
50
54
  }
51
55
  /**
52
56
  * Prepares starknet transactions, checks if the account is deployed, assigns nonces if needed & calls beforeTxSigned callback
@@ -138,7 +142,7 @@ class EVMTransactions extends EVMModule_1.EVMModule {
138
142
  }
139
143
  this.logger.debug("sendAndConfirm(): sending transactions, count: " + txs.length +
140
144
  " waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
141
- const txIds = [];
145
+ let txIds = [];
142
146
  if (parallel) {
143
147
  let promises = [];
144
148
  for (let i = 0; i < txs.length; i++) {
@@ -166,7 +170,7 @@ class EVMTransactions extends EVMModule_1.EVMModule {
166
170
  }
167
171
  }
168
172
  if (promises.length > 0)
169
- await Promise.all(promises);
173
+ txIds = await Promise.all(promises);
170
174
  }
171
175
  else {
172
176
  for (let i = 0; i < txs.length; i++) {
@@ -187,9 +191,10 @@ class EVMTransactions extends EVMModule_1.EVMModule {
187
191
  const confirmPromise = this.confirmTransaction(tx, abortSignal);
188
192
  this.logger.debug("sendAndConfirm(): transaction sent (" + (i + 1) + "/" + txs.length + "): " + tx.hash);
189
193
  //Don't await the last promise when !waitForConfirmation
194
+ let txHash = tx.hash;
190
195
  if (i < txs.length - 1 || waitForConfirmation)
191
- await confirmPromise;
192
- txIds.push(tx.hash);
196
+ txHash = await confirmPromise;
197
+ txIds.push(txHash);
193
198
  }
194
199
  }
195
200
  this.logger.info("sendAndConfirm(): sent transactions, count: " + txs.length +
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/chain-evm",
3
- "version": "1.0.0-dev.69",
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
  }
@@ -39,7 +39,7 @@ export class EVMTransactions extends EVMModule<any> {
39
39
  * @param abortSignal signal to abort waiting for tx confirmation
40
40
  * @private
41
41
  */
42
- private async confirmTransaction(tx: TransactionResponse | Transaction, abortSignal?: AbortSignal) {
42
+ private async confirmTransaction(tx: TransactionResponse | Transaction, abortSignal?: AbortSignal): Promise<string> {
43
43
  const checkTxns: Set<string> = new Set([tx.hash]);
44
44
 
45
45
  const txReplaceListener = (oldTx: string, oldTxId: string, newTx: string, newTxId: string) => {
@@ -49,11 +49,15 @@ export class EVMTransactions extends EVMModule<any> {
49
49
  this.onBeforeTxReplace(txReplaceListener);
50
50
 
51
51
  let state = "pending";
52
+ let confirmedTxId: string = null;
52
53
  while(state==="pending" || state==="not_found") {
53
54
  await timeoutPromise(3000, abortSignal);
54
55
  for(let txId of checkTxns) {
55
56
  state = await this.getTxIdStatus(txId);
56
- if(state==="reverted" || state==="success") break;
57
+ if(state==="reverted" || state==="success") {
58
+ confirmedTxId = txId;
59
+ break;
60
+ }
57
61
  }
58
62
  }
59
63
 
@@ -65,6 +69,8 @@ export class EVMTransactions extends EVMModule<any> {
65
69
  this.latestConfirmedNonces[tx.from] = nextAccountNonce;
66
70
  }
67
71
  if(state==="reverted") throw new Error("Transaction reverted!");
72
+
73
+ return confirmedTxId;
68
74
  }
69
75
 
70
76
  /**
@@ -168,9 +174,9 @@ export class EVMTransactions extends EVMModule<any> {
168
174
  this.logger.debug("sendAndConfirm(): sending transactions, count: "+txs.length+
169
175
  " waitForConfirmation: "+waitForConfirmation+" parallel: "+parallel);
170
176
 
171
- const txIds: string[] = [];
177
+ let txIds: string[] = [];
172
178
  if(parallel) {
173
- let promises: Promise<void>[] = [];
179
+ let promises: Promise<string>[] = [];
174
180
  for(let i=0;i<txs.length;i++) {
175
181
  let tx: TransactionResponse | Transaction;
176
182
  if(signer.signTransaction==null) {
@@ -195,7 +201,7 @@ export class EVMTransactions extends EVMModule<any> {
195
201
  promises = [];
196
202
  }
197
203
  }
198
- if(promises.length>0) await Promise.all(promises);
204
+ if(promises.length>0) txIds = await Promise.all(promises);
199
205
  } else {
200
206
  for(let i=0;i<txs.length;i++) {
201
207
  let tx: TransactionResponse | Transaction;
@@ -216,8 +222,9 @@ export class EVMTransactions extends EVMModule<any> {
216
222
  const confirmPromise = this.confirmTransaction(tx, abortSignal);
217
223
  this.logger.debug("sendAndConfirm(): transaction sent ("+(i+1)+"/"+txs.length+"): "+tx.hash);
218
224
  //Don't await the last promise when !waitForConfirmation
219
- if(i<txs.length-1 || waitForConfirmation) await confirmPromise;
220
- txIds.push(tx.hash);
225
+ let txHash = tx.hash;
226
+ if(i<txs.length-1 || waitForConfirmation) txHash = await confirmPromise;
227
+ txIds.push(txHash);
221
228
  }
222
229
  }
223
230