Skip to content

Commit

Permalink
refactor(status-manager): delete empty entries
Browse files Browse the repository at this point in the history
- if we settle and there's no additional entries for a key, delete it instead
  of setting to an empty array
- ensure  always return 'No unsettled entry' error instead of sometimes 'key not found'
  • Loading branch information
0xpatrickdev committed Nov 20, 2024
1 parent f025034 commit 657bc34
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
22 changes: 13 additions & 9 deletions packages/fast-usdc/src/exos/status-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const prepareStatusManager = zone => {
);
};

return zone.exo(
const statusManager = zone.exo(
'Fast USDC Status Manager',
M.interface('StatusManagerI', {
advance: M.call(CctpTxEvidenceShape).returns(M.undefined()),
Expand Down Expand Up @@ -148,35 +148,39 @@ export const prepareStatusManager = zone => {
* @throws {Error} if a pending settlement was not found for the address + amount
*/
settle(address, amount) {
const key = makePendingTxKey(address, amount);
const pending = pendingTxs.get(key);

const pending = statusManager.lookupPending(address, amount);
if (!pending.length) {
throw makeError(`No unsettled entry for ${q(key)}`);
throw makeError(`No unsettled entry for ${q([address, amount])}`);
}

const pendingCopy = [...pending];
pendingCopy.shift();
// TODO, vstorage update for `TxStatus.Settled`
pendingTxs.set(key, harden(pendingCopy));

const key = makePendingTxKey(address, amount);
if (pendingCopy.length) {
return pendingTxs.set(key, harden(pendingCopy));
}
return pendingTxs.delete(key);
},

/**
* Lookup all pending entries for a given address and amount
* Lookup all pending entries for a given address and amount.
*
* @param {NobleAddress} address
* @param {bigint} amount
* @returns {PendingTx[]}
* @returns {PendingTx[]} pendingTxs or empty array if nothing is found
*/
lookupPending(address, amount) {
const key = makePendingTxKey(address, amount);
if (!pendingTxs.has(key)) {
throw makeError(`Key ${q(key)} not yet observed`);
return harden([]);
}
return pendingTxs.get(key);
},
},
);
return statusManager;
};
harden(prepareStatusManager);

Expand Down
10 changes: 4 additions & 6 deletions packages/fast-usdc/test/exos/status-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ test('cannot SETTLE without an ADVANCED or OBSERVED entry', t => {
statusManager.settle(evidence.tx.forwardingAddress, evidence.tx.amount),
{
message:
'key "pendingTx:[\\"noble1x0ydg69dh6fqvr27xjvp6maqmrldam6yfelqkd\\",\\"150000000\\"]" not found in collection "PendingTxs"',
'No unsettled entry for ["noble1x0ydg69dh6fqvr27xjvp6maqmrldam6yfelqkd","[150000000n]"]',
},
);
});
Expand Down Expand Up @@ -139,19 +139,17 @@ test('settle SETTLES first matched entry', t => {
statusManager.settle(evidence.tx.forwardingAddress, evidence.tx.amount),
{
message:
'No unsettled entry for "pendingTx:[\\"noble1x0ydg69dh6fqvr27xjvp6maqmrldam6yfelqkd\\",\\"150000000\\"]"',
'No unsettled entry for ["noble1x0ydg69dh6fqvr27xjvp6maqmrldam6yfelqkd","[150000000n]"]',
},
'No more matches to settle',
);
});

test('lookup throws when presented a key it has not seen', t => {
test('lookingPending returns an empty array when presented a key it has not seen', t => {
const zone = provideDurableZone('status-test');
const statusManager = prepareStatusManager(zone.subZone('status-manager'));

t.throws(() => statusManager.lookupPending('noble123', 1n), {
message: 'Key "pendingTx:[\\"noble123\\",\\"1\\"]" not yet observed',
});
t.deepEqual(statusManager.lookupPending('noble123', 1n), []);
});

test('StatusManagerKey logic handles addresses with hyphens', async t => {
Expand Down

0 comments on commit 657bc34

Please sign in to comment.