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 25, 2024
1 parent 653bde3 commit 98f44fb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/orchestration/src/examples/sendAnywhere.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ export const start = async (zcf, privateArgs, baggage) => {
const { chainName, destAddr } = offerArgs;
const { give } = seat.getProposal();
const [[kw, amt]] = entries(give);
// TODO remove V.when() when integrating with asyncFlow
const { denom } = await V.when(
// XXX when() until membrane
const { denom } = await E.when(
agoricNamesTools.findBrandInVBank(amt.brand),
);
const chain = await orch.getChain(chainName);
Expand Down
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 */
46 changes: 31 additions & 15 deletions packages/orchestration/test/exos/agoric-names-tools.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';

import { E } from '@endo/far';
import { V } from '@agoric/vow/vat.js';
import { heapVowE as E } 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 All @@ -20,33 +20,49 @@ test('agoric names tools', async t => {
vowTools,
});

const chainEntry = await V.when(agNamesTools.lookup('chain', 'celestia'));
const chainEntry = await E.when(agNamesTools.lookup('chain', 'celestia'));
t.like(chainEntry, { chainId: 'celestia' });

const istDenom = await V.when(agNamesTools.findBrandInVBank(ist.brand));
const istDenom = await E.when(agNamesTools.findBrandInVBank(ist.brand));
t.like(istDenom, { denom: 'uist' });

const moolah = withAmountUtils(makeIssuerKit('MOO'));

await t.throwsAsync(V.when(agNamesTools.findBrandInVBank(moolah.brand)), {
await t.throwsAsync(E.when(agNamesTools.findBrandInVBank(moolah.brand)), {
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 E.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)),
await E.when(agNamesTools.findBrandInVBank(moolah.brand)),
{ denom: 'umoo' },
'refresh stale cache for new assets',
'old AssetInfo is cached',
);
t.like(
await E.when(agNamesTools.findBrandInVBank(moolah.brand, true)),
{ denom: 'umoo2' },
'new AssetInfo is fetched when refetch=true',
);
});

0 comments on commit 98f44fb

Please sign in to comment.