Skip to content

Commit

Permalink
feat: simplify seenTxs key
Browse files Browse the repository at this point in the history
- no need to support legacy Ethereum txs that do not include chain id in the tx envelope
  • Loading branch information
turadg committed Dec 17, 2024
1 parent 71b1859 commit fd05a7e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 29 deletions.
32 changes: 7 additions & 25 deletions packages/fast-usdc/src/exos/status-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import { PendingTxStatus, TxStatus } from '../constants.js';
/**
* @typedef {`pendingTx:${bigint}:${NobleAddress}`} PendingTxKey
* The string template is for developer visibility but not meant to ever be parsed.
*
* @typedef {`seenTx:${string}:${EvmHash}`} SeenTxKey
* The string template is for developer visibility but not meant to ever be parsed.
*/

/**
Expand All @@ -48,20 +45,6 @@ const pendingTxKeyOf = evidence => {
return makePendingTxKey(forwardingAddress, amount);
};

/**
* Get the key for the seenTxs SetStore.
*
* The key is a composite but not meant to be parsable.
*
* @param {CctpTxEvidence} evidence
* @returns {SeenTxKey}
*/
const seenTxKeyOf = evidence => {
const { txHash, chainId } = evidence;
// chainId can't contain colon
return `seenTx:${chainId}:${txHash}`;
};

/**
* @typedef {{
* log?: LogFn;
Expand Down Expand Up @@ -95,7 +78,7 @@ export const prepareStatusManager = (
valueShape: M.arrayOf(PendingTxShape),
});

/** @type {SetStore<SeenTxKey>} */
/** @type {SetStore<EvmHash>} */
const seenTxs = zone.setStore('SeenTxs', {
keyShape: M.string(),
});
Expand All @@ -120,18 +103,18 @@ export const prepareStatusManager = (
* @param {PendingTxStatus} status
*/
const initPendingTx = (evidence, status) => {
const seenKey = seenTxKeyOf(evidence);
if (seenTxs.has(seenKey)) {
throw makeError(`Transaction already seen: ${q(seenKey)}`);
const { txHash } = evidence;
if (seenTxs.has(txHash)) {
throw makeError(`Transaction already seen: ${q(txHash)}`);
}
seenTxs.add(seenKey);
seenTxs.add(txHash);

appendToStoredArray(
pendingTxs,
pendingTxKeyOf(evidence),
harden({ ...evidence, status }),
);
publishStatus(evidence.txHash, status);
publishStatus(txHash, status);
};

/**
Expand Down Expand Up @@ -226,8 +209,7 @@ export const prepareStatusManager = (
* @param {CctpTxEvidence} evidence
*/
hasBeenObserved(evidence) {
const seenKey = seenTxKeyOf(evidence);
return seenTxs.has(seenKey);
return seenTxs.has(evidence.txHash);
},

/**
Expand Down
6 changes: 2 additions & 4 deletions packages/fast-usdc/test/exos/status-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,16 @@ test('cannot process same tx twice', t => {

t.throws(() => statusManager.advance(evidence), {
message:
'Transaction already seen: "seenTx:1:0xc81bc6105b60a234c7c50ac17816ebcd5561d366df8bf3be59ff387552761702"',
'Transaction already seen: "0xc81bc6105b60a234c7c50ac17816ebcd5561d366df8bf3be59ff387552761702"',
});

t.throws(() => statusManager.observe(evidence), {
message:
'Transaction already seen: "seenTx:1:0xc81bc6105b60a234c7c50ac17816ebcd5561d366df8bf3be59ff387552761702"',
'Transaction already seen: "0xc81bc6105b60a234c7c50ac17816ebcd5561d366df8bf3be59ff387552761702"',
});

// new txHash should not throw
t.notThrows(() => statusManager.advance({ ...evidence, txHash: '0xtest2' }));
// new chainId with existing txHash should not throw
t.notThrows(() => statusManager.advance({ ...evidence, chainId: 9999 }));
});

test('isSeen checks if a tx has been processed', t => {
Expand Down

0 comments on commit fd05a7e

Please sign in to comment.