Skip to content

Commit

Permalink
Fix/history sync (#193)
Browse files Browse the repository at this point in the history
* Fixed a bug where a transaction would disappear from history immediately after sending

* Increasing version (6.0.1)
  • Loading branch information
EvgenKor authored Nov 7, 2024
1 parent 7a0e5b5 commit 2defb09
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,21 +471,16 @@ export class HistoryStorage {
sentHistoryRecords.push(aRec);
}

if (oldTxHash != txHash) {
// Here is a case when txHash has been changed for existing job:
// we should remove records from sentTxs with old txHash
this.removePendingTxByTxHash(oldTxHash);
}

// set history records in the sentTx mapping
// (the records with the old txHashes are still alive)
this.sentTxs.set(txHash, sentHistoryRecords);
}
}

// Mark job as completed: remove it from 'queuedTxs' and 'sentTxs' mappings
public async setQueuedTransactionsCompleted(job: SequencerJob, txHash: string) : Promise<boolean> {
return this.removePendingTxByJob(job) ||
this.removePendingTxByTxHash(txHash);
// Mark job as completed: do not remove associated records, just update txHash if needed
// An associated records from queuedTxs and sentTxs will be removed automatically on history sync
public async setQueuedTransactionsCompleted(job: SequencerJob, txHash: string) {
return this.setTxHashForQueuedTransactions(job, txHash);
}

// mark pending transaction as failed on the relayer level (we shouldn't have txHash here)
Expand Down Expand Up @@ -593,6 +588,32 @@ export class HistoryStorage {
return res;
}

private removePendingTxByCommit(commitment: bigint): boolean {
let res = false;

// remove records from the sentTxs by given commitment
this.sentTxs.forEach((records, txHash) => {
for (const aRec of records) {
if (aRec.commitment == commitment) {
this.sentTxs.delete(txHash);
res = true;
}
}
});

// remove queued txs with the same commitments
this.queuedTxs.forEach((records, jobHash) => {
for (const aRec of records) {
if (aRec.commitment == commitment) {
this.queuedTxs.delete(jobHash);
res = true;
}
}
});

return res;
}

// remove pending transactions with the txHash
private removeHistoryPendingRecordsByTxHash(txHash: string): boolean {
let deleted = false;
Expand Down Expand Up @@ -1052,9 +1073,9 @@ export class HistoryStorage {
throw new InternalError(`[HistoryStorage] Unknown transaction type ${details.txType}`)
}

if (!pending || (pending && details.isMined)) {
// if tx is in pending state - remove it only on success
this.removePendingTxByTxHash(details.txHash);
if (!pending) {
// remove non-pending tx from the aux queues
this.removePendingTxByCommit(BigInt(details.commitment));
}
} else if (txDetails.poolTxType == PoolTxType.DirectDepositBatch && txDetails.details instanceof DDBatchTxDetails) {
// transaction is DD batch on the pool
Expand Down

0 comments on commit 2defb09

Please sign in to comment.