Skip to content

Commit

Permalink
feat(findBrandInVBank): add optional parameter to invalidate cache
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpatrickdev committed Jun 21, 2024
1 parent bc75c92 commit aa53b01
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
20 changes: 16 additions & 4 deletions packages/orchestration/src/exos/agoric-names-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const { Fail } = assert;
* Perform remote calls to agoricNames in membrane-friendly way. This is an
* interim approach until https://github.com/Agoric/agoric-sdk/issues/9541,
* https://github.com/Agoric/agoric-sdk/pull/9322, or
* https://github.com/Agoric/agoric-sdk/pull/9519
* https://github.com/Agoric/agoric-sdk/pull/9519.
*
* XXX only works once per zone.
*
* XXX consider exposing `has`, `entries`, `keys`, `values` from `NameHub`
*
Expand All @@ -33,7 +35,9 @@ export const makeResumableAgoricNamesHack = (
{
public: M.interface('ResumableAgoricNamesHackI', {
lookup: M.call().rest(M.arrayOf(M.string())).returns(VowShape),
findBrandInVBank: M.call(BrandShape).returns(VowShape),
findBrandInVBank: M.call(BrandShape)
.optional(M.boolean())
.returns(VowShape),
}),
vbankAssetEntriesWatcher: M.interface('vbankAssetEntriesWatcher', {
onFulfilled: M.call(M.arrayOf(M.record()))
Expand Down Expand Up @@ -72,12 +76,19 @@ export const makeResumableAgoricNamesHack = (
return watch(E(agoricNames).lookup(...args));
},
/**
* Look up asset info, like denom, in agoricNames.vbankAsset using a
* Brand.
*
* Caches the query to agoricNames in the first call. Subsequent lookups
* are via cache unless a refetch is specified or a brand is not found.
*
* @param {Brand<'nat'>} brand
* @param {boolean} [refetch] if true, will invalidate the cache
* @returns {Vow<AssetInfo>}
*/
findBrandInVBank(brand) {
findBrandInVBank(brand, refetch) {
const { vbankAssetsByBrand } = this.state;
if (vbankAssetsByBrand.has(brand)) {
if (vbankAssetsByBrand.has(brand) && !refetch) {
return watch(vbankAssetsByBrand.get(brand));
}
const vbankAssetNameHubP = E(agoricNames).lookup('vbankAsset');
Expand All @@ -91,6 +102,7 @@ export const makeResumableAgoricNamesHack = (
},
},
);
// XXX only works once per zone.
return makeResumableAgoricNamesHackKit().public;
};
/** @typedef {ReturnType<typeof makeResumableAgoricNamesHack>} AgNamesTools */
35 changes: 26 additions & 9 deletions packages/orchestration/test/exos/agoric-names-tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { V } from '@agoric/vow/vat.js';
import { makeHeapZone } from '@agoric/zone';
import { withAmountUtils } from '@agoric/zoe/tools/test-utils.js';
import { makeIssuerKit } from '@agoric/ertp';
import { AssetInfo } from '@agoric/vats/src/vat-bank.js';
import { makeResumableAgoricNamesHack } from '../../src/exos/agoric-names-tools.js';
import { commonSetup } from '../supports.js';

Expand Down Expand Up @@ -32,21 +33,37 @@ test('agoric names tools', async t => {
message: /brand(.*?)not in agoricNames.vbankAsset/,
});

const mooToken: AssetInfo = {
brand: moolah.brand,
issuer: moolah.issuer,
issuerName: 'MOO',
denom: 'umoo',
proposedName: 'MOO',
displayInfo: { decimalPlaces: 6, assetKind: 'nat' },
};

await E(E(agoricNamesAdmin).lookupAdmin('vbankAsset')).update(
'umoo',
/** @type {AssetInfo} */ harden({
brand: moolah.brand,
issuer: moolah.issuer,
issuerName: 'MOO',
denom: 'umoo',
proposedName: 'MOO',
displayInfo: { decimals: 6, symbol: 'MOO' },
}),
harden(mooToken),
);
t.like(
await V.when(agNamesTools.findBrandInVBank(moolah.brand)),
{ denom: 'umoo' },
'vbankAssets are refetched if brand is not found',
);

await E(E(agoricNamesAdmin).lookupAdmin('vbankAsset')).update(
'umoo',
harden({ ...mooToken, denom: 'umoo2' }),
);
t.like(
await V.when(agNamesTools.findBrandInVBank(moolah.brand)),
{ denom: 'umoo' },
'refresh stale cache for new assets',
'old AssetInfo is cached',
);
t.like(
await V.when(agNamesTools.findBrandInVBank(moolah.brand, true)),
{ denom: 'umoo2' },
'new AssetInfo is fetched when refetch=true',
);
});

0 comments on commit aa53b01

Please sign in to comment.