Skip to content

Commit

Permalink
fix(zoe): teach fakeVatAdmin to createVatByName(zcf)
Browse files Browse the repository at this point in the history
Zoe needs a way to create a new ZCF vat to host each contract instance.
Inside swingset, zoe uses `vatAdminService`, and this is used from unit
tests (zoe and other packages that use zoe) when they are willing to take the
time to run a full swingset environment.

Unit tests that use zoe, but not swingset, use `tools/fakeVatAdmin.js` as a
replacement, which implements `createVat(bundle)` but not
`createVatByName(name)`. This is used to evaluate the ZCF bundle in a new
Compartment (using `importBundle` and `evalContractBundle`).

The primary export of `@agoric/zoe` is `makeZoeKit()`, which is called with
`vatAdminService` and an optional `zcfBundleName`. This function runs
`setupCreateZCFVat(vatAdminService)` to attenuate the vat-making power into
one that can only create ZCF vats. Previously, `setupCreateZCFVat` closed
over a copy of the ZCF bundle, and passed it to
`vatAdminService~.createVat(zcfBundle)`. This hides the existence of the ZCF
bundle from other packages that just want to use Zoe.

However, to remove these large code bundles from userspace messages (#4372),
we need the ZCF bundle to be installed "off to the side", by the code that
prepares the swingset environment (usually as `config.bundles.zcf=`).

This commit changes `fakeVatAdmin.js` to implement `createVatByName('zcf')`,
and to move the copy of the zcfBundle out of `makeZoeKit()` and into
`fakeVatAdmin.js` . In addition, it changes `createZCFVat.js` to provide a
default bundle name of 'zcf'. Any unit test that uses `fakeVatAdmin.js` can
continue to do so without changes, and the fake service will magically know
how to create Zoe's ZCF vat without any additional configuration.

Unit tests that use swingset, however, need to be updated to install the ZCF
bundle into the kernel, with something like:

```
import zcfBundle from `@agoric/zoe/bundles/bundle-contractFacet.js`;
...
config.bundles.zcf = { bundle: zcfBundle };
```

Note: if we used `{ sourceSpec: '@agoric/zoe/contractFacet.js' }`, then we
would not depend upon a recent `cd packages/zoe && yarn build`, and each
kernel instance would bundle its own copy. This would be slower, but perhaps
less prone to stale-bundle surprises, and might be a nicer export
commitment.

refs #4487
  • Loading branch information
warner committed Feb 9, 2022
1 parent 9d7286e commit 6f88e2b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
10 changes: 3 additions & 7 deletions packages/zoe/src/zoeService/createZCFVat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { E } from '@agoric/eventual-send';

import zcfContractBundle from '../../bundles/bundle-contractFacet.js';

/**
* Attenuate the power of vatAdminSvc by restricting it such that only
* ZCF Vats can be created.
Expand All @@ -10,13 +8,11 @@ import zcfContractBundle from '../../bundles/bundle-contractFacet.js';
* @param {string=} zcfBundleName
* @returns {CreateZCFVat}
*/
export const setupCreateZCFVat = (vatAdminSvc, zcfBundleName = undefined) => {
export const setupCreateZCFVat = (vatAdminSvc, zcfBundleName = 'zcf') => {
/** @type {CreateZCFVat} */
const createZCFVat = async () => {
const rootAndAdminNodeP =
typeof zcfBundleName === 'string'
? E(vatAdminSvc).createVatByName(zcfBundleName)
: E(vatAdminSvc).createVat(zcfContractBundle);
assert.typeof(zcfBundleName, 'string');
const rootAndAdminNodeP = E(vatAdminSvc).createVatByName(zcfBundleName);
const rootAndAdminNode = await rootAndAdminNodeP;
return rootAndAdminNode;
};
Expand Down
8 changes: 5 additions & 3 deletions packages/zoe/tools/fakeVatAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { E } from '@agoric/eventual-send';
import { makePromiseKit } from '@agoric/promise-kit';
import { Far } from '@endo/marshal';

import { assert, details as X } from '@agoric/assert';
import { assert } from '@agoric/assert';
import { evalContractBundle } from '../src/contractFacet/evalContractCode.js';
import { handlePKitWarning } from '../src/handleWarning.js';
import zcfContractBundle from '../bundles/bundle-contractFacet.js';

/**
* @param { (...args) => unknown } [testContextSetter]
Expand Down Expand Up @@ -54,8 +55,9 @@ function makeFakeVatAdmin(testContextSetter = undefined, makeRemote = x => x) {
}),
});
},
createVatByName: _name => {
assert.fail(X`createVatByName not supported in fake mode`);
createVatByName: name => {
assert.equal(name, 'zcf', `only name='zcf' accepted, not ${name}`);
return admin.createVat(zcfContractBundle);
},
});
const vatAdminState = {
Expand Down

0 comments on commit 6f88e2b

Please sign in to comment.