Skip to content

Commit

Permalink
refactor: simplify db keys
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Dec 10, 2024
1 parent c7a3167 commit 5751bf5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 9 additions & 4 deletions packages/fast-usdc/src/exos/status-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ import { PendingTxStatus, TxStatus } from '../constants.js';
*/

/**
* @typedef {`pendingTx:${string}`} PendingTxKey
* @typedef {`seenTx:${string}`} SeenTxKey
* @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 @@ -31,7 +34,8 @@ import { PendingTxStatus, TxStatus } from '../constants.js';
* @returns {PendingTxKey}
*/
const makePendingTxKey = (addr, amount) =>
`pendingTx:${JSON.stringify([addr, String(amount)])}`;
// amount can't contain colon
`pendingTx:${amount}:${addr}`;

/**
* Get the key for the pendingTxs MapStore.
Expand All @@ -54,7 +58,8 @@ const pendingTxKeyOf = evidence => {
*/
const seenTxKeyOf = evidence => {
const { txHash, chainId } = evidence;
return `seenTx:${JSON.stringify([txHash, chainId])}`;
// chainId can't contain colon
return `seenTx:${chainId}:${txHash}`;
};

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

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

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

// new txHash should not throw
Expand Down

0 comments on commit 5751bf5

Please sign in to comment.