-
Notifications
You must be signed in to change notification settings - Fork 206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(orchestrate): membrane friendly wrapper for agoricNames #9551
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { VowShape } from '@agoric/vow'; | ||
import { E } from '@endo/far'; | ||
import { M, makeCopyMap } from '@endo/patterns'; | ||
import { BrandShape } from '@agoric/ertp'; | ||
|
||
const { Fail } = assert; | ||
|
||
/** | ||
* @import {NameHub} from '@agoric/vats'; | ||
* @import {AssetInfo} from '@agoric/vats/src/vat-bank.js'; | ||
* @import {Remote} from '@agoric/internal'; | ||
* @import {Vow, VowTools} from '@agoric/vow'; | ||
* @import {Zone} from '@agoric/zone'; | ||
*/ | ||
|
||
/** | ||
* 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. | ||
* | ||
* XXX only works once per zone. | ||
* | ||
* XXX consider exposing `has`, `entries`, `keys`, `values` from `NameHub` | ||
* | ||
* @param {Zone} zone | ||
* @param {{ agoricNames: Remote<NameHub>; vowTools: VowTools }} powers | ||
*/ | ||
export const makeResumableAgoricNamesHack = ( | ||
zone, | ||
{ agoricNames, vowTools: { watch, asVow } }, | ||
) => { | ||
const makeResumableAgoricNamesHackKit = zone.exoClassKit( | ||
'ResumableAgoricNamesHack', | ||
{ | ||
public: M.interface('ResumableAgoricNamesHackI', { | ||
lookup: M.call().rest(M.arrayOf(M.string())).returns(VowShape), | ||
findBrandInVBank: M.call(BrandShape) | ||
.optional(M.boolean()) | ||
.returns(VowShape), | ||
}), | ||
vbankAssetEntriesWatcher: M.interface('vbankAssetEntriesWatcher', { | ||
onFulfilled: M.call(M.arrayOf(M.record())) | ||
.optional({ brand: BrandShape }) | ||
.returns(VowShape), | ||
}), | ||
}, | ||
() => ({ | ||
vbankAssetsByBrand: zone.mapStore('vbankAssetsByBrand', { | ||
keyShape: BrandShape, | ||
valueShape: M.any(), | ||
}), | ||
}), | ||
{ | ||
vbankAssetEntriesWatcher: { | ||
/** | ||
* @param {AssetInfo[]} assets | ||
* @param {{ brand: Brand<'nat'> }} ctx | ||
*/ | ||
onFulfilled(assets, { brand }) { | ||
return asVow(() => { | ||
const { vbankAssetsByBrand } = this.state; | ||
vbankAssetsByBrand.addAll( | ||
makeCopyMap(assets.map(a => [a.brand, a])), | ||
); | ||
vbankAssetsByBrand.has(brand) || | ||
Fail`brand ${brand} not in agoricNames.vbankAsset`; | ||
return vbankAssetsByBrand.get(brand); | ||
}); | ||
}, | ||
}, | ||
public: { | ||
/** @param {...string} args */ | ||
lookup(...args) { | ||
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, refetch) { | ||
return asVow(() => { | ||
const { vbankAssetsByBrand } = this.state; | ||
if (vbankAssetsByBrand.has(brand) && !refetch) { | ||
return vbankAssetsByBrand.get(brand); | ||
} | ||
const vbankAssetNameHubP = E(agoricNames).lookup('vbankAsset'); | ||
const vbankAssetEntriesP = E(vbankAssetNameHubP).values(); | ||
return watch( | ||
vbankAssetEntriesP, | ||
this.facets.vbankAssetEntriesWatcher, | ||
{ brand }, | ||
); | ||
}); | ||
}, | ||
}, | ||
}, | ||
); | ||
// XXX only works once per zone. | ||
return makeResumableAgoricNamesHackKit().public; | ||
}; | ||
/** @typedef {ReturnType<typeof makeResumableAgoricNamesHack>} AgNamesTools */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,8 +45,9 @@ export const provideOrchestration = ( | |
marshaller, | ||
) => { | ||
const zone = makeDurableZone(baggage); | ||
const { agoricNames, timerService } = remotePowers; | ||
|
||
const chainHub = makeChainHub(remotePowers.agoricNames); | ||
const chainHub = makeChainHub(agoricNames); | ||
|
||
const vowTools = prepareVowTools(zone.subZone('vows')); | ||
|
||
|
@@ -55,7 +56,7 @@ export const provideOrchestration = ( | |
zone, | ||
makeRecorderKit, | ||
zcf, | ||
remotePowers.timerService, | ||
timerService, | ||
vowTools, | ||
chainHub, | ||
Comment on lines
56
to
61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aside: do we really have a function with that many positional args? If it gets >3, they should be named. |
||
); | ||
|
@@ -103,6 +104,6 @@ export const provideOrchestration = ( | |
vowTools, | ||
...remotePowers, | ||
}); | ||
return { ...facade, chainHub, zone }; | ||
return { ...facade, chainHub, vowTools, zone }; | ||
}; | ||
harden(provideOrchestration); |
68 changes: 68 additions & 0 deletions
68
packages/orchestration/test/exos/agoric-names-tools.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { test } from '@agoric/zoe/tools/prepare-test-env-ava.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'; | ||
|
||
test('agoric names tools', async t => { | ||
const { | ||
bootstrap: { agoricNames, agoricNamesAdmin, bankManager, vowTools }, | ||
brands: { ist }, | ||
} = await commonSetup(t); | ||
|
||
const zone = makeHeapZone(); | ||
const agNamesTools = makeResumableAgoricNamesHack(zone, { | ||
agoricNames, | ||
vowTools, | ||
}); | ||
|
||
const chainEntry = await E.when(agNamesTools.lookup('chain', 'celestia')); | ||
t.like(chainEntry, { chainId: 'celestia' }); | ||
|
||
const istDenom = await E.when(agNamesTools.findBrandInVBank(ist.brand)); | ||
t.like(istDenom, { denom: 'uist' }); | ||
|
||
const moolah = withAmountUtils(makeIssuerKit('MOO')); | ||
|
||
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', | ||
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 E.when(agNamesTools.findBrandInVBank(moolah.brand)), | ||
{ denom: 'umoo' }, | ||
'old AssetInfo is cached', | ||
); | ||
t.like( | ||
await E.when(agNamesTools.findBrandInVBank(moolah.brand, true)), | ||
{ denom: 'umoo2' }, | ||
'new AssetInfo is fetched when refetch=true', | ||
); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will fail if
makeResumableAgoricNamesHack
is called more than once in the same zone.Add that to the docstring? or just leave it implicit in the
*Hack
name?