Skip to content

Commit

Permalink
feat: StatusManager tracks seenTxs
Browse files Browse the repository at this point in the history
- use a composite key of `txHash+chainId` to track unique `EventFeed` submissions
  • Loading branch information
0xpatrickdev committed Nov 7, 2024
1 parent 024fd1d commit 444aedd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
35 changes: 33 additions & 2 deletions packages/fast-usdc/src/exos/status-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { CctpTxEvidenceShape, PendingTxShape } from '../typeGuards.js';
import { TxStatus } from '../constants.js';

/**
* @import {MapStore} from '@agoric/store';
* @import {MapStore, SetStore} from '@agoric/store';
* @import {Zone} from '@agoric/zone';
* @import {CctpTxEvidence, NobleAddress, PendingTxKey, PendingTx} from '../types.js';
* @import {CctpTxEvidence, NobleAddress, SeenTxKey, PendingTxKey, PendingTx} from '../types.js';
*/

/**
Expand All @@ -29,6 +29,16 @@ const getPendingTxKey = evidence => {
return toPendingTxKey(forwardingAddress, amount);
};

/**
* Get the key for the seenTxs SetStore
* @param {CctpTxEvidence} evidence
* @returns {SeenTxKey}
*/
const getSeenKey = evidence => {
const { txHash, chainId } = evidence;
return `${txHash}+${chainId}`;
};

/**
* The `StatusManager` keeps track of Pending and Seen Transactions
* via {@link TxStatus} states, aiding in coordination between the `Advancer`
Expand All @@ -45,6 +55,25 @@ export const prepareStatusManager = zone => {
valueShape: M.arrayOf(PendingTxShape),
});

/** @type {SetStore<SeenTxKey>} */
const seenTxs = zone.setStore('SeenTxs', {
keyShape: M.string(),
});

/**
* Ensures that `txHash+chainId` has not been processed
* and adds `txHash+chainId` to `seenTxs` set.
*
* @param {CctpTxEvidence} evidence
*/
const assertNotSeen = evidence => {
const seenKey = getSeenKey(evidence);
if (seenTxs.has(seenKey)) {
throw makeError(`Transaction already seen: ${q(seenKey)}`);
}
seenTxs.add(seenKey);
};

return zone.exo(
'Fast USDC Status Manager',
M.interface('StatusManagerI', {
Expand All @@ -60,6 +89,7 @@ export const prepareStatusManager = zone => {
* @param {CctpTxEvidence} evidence
*/
advance(evidence) {
assertNotSeen(evidence);
const key = getPendingTxKey(evidence);
const entry = { ...evidence, status: TxStatus.Advanced };

Expand All @@ -76,6 +106,7 @@ export const prepareStatusManager = zone => {
* @param {CctpTxEvidence} evidence
*/
observe(evidence) {
assertNotSeen(evidence);
const key = getPendingTxKey(evidence);
const entry = { ...evidence, status: TxStatus.Observed };

Expand Down
2 changes: 2 additions & 0 deletions packages/fast-usdc/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ export interface PendingTx extends CctpTxEvidence {
/** composite of NobleAddress and transaction amount value */
export type PendingTxKey = `"["${NobleAddress}",${bigint}]"`;

export type SeenTxKey = `${EvmHash}+${number}`;

export type * from './constants.js';

0 comments on commit 444aedd

Please sign in to comment.