-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathwriteCoreEvalParts.js
228 lines (199 loc) · 7.08 KB
/
writeCoreEvalParts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// @ts-check
import fs from 'fs';
import { E } from '@endo/far';
import { deeplyFulfilled } from '@endo/marshal';
import { createBundles } from '@agoric/internal/src/node/createBundles.js';
import { defangAndTrim, mergePermits, stringify } from './code-gen.js';
import {
makeCoreProposalBehavior,
permits as defaultPermits,
} from './coreProposalBehavior.js';
/**
* @import {BundleSource, BundleSourceResult} from '@endo/bundle-source';
* @import {AgSoloHome, CanonicalHome, CommonHome, CoreEvalBuilder, CoreEvalDescriptor, ManifestBundleRef} from './externalTypes.js';
*/
/**
* @typedef CoreEvalPlan
* @property {string} name
* @property {string} permit
* @property {string} script
* @property {{entrypoint: string, bundleID: string, fileName: string}[]} bundles
*/
/**
* @callback WriteCoreEval write to disk the files needed for a CoreEval (js code to`${filePrefix}.js`, permits to `${filePrefix}-permit.json`, an overall
* summary to `${filePrefix}-plan.json), plus whatever bundles bundles the code loads)
* see CoreEval in {@link '/golang/cosmos/x/swingset/types/swingset.pb.go'}
* @param {string} filePrefix name on disk
* @param {CoreEvalBuilder} builder
* @returns {Promise<CoreEvalPlan>}
*/
/**
*
* @param {Promise<CommonHome | AgSoloHome>} homeP
* @param {{
* bundleSource: BundleSource,
* pathResolve: (path: string) => string,
* }} endowments
* @param {{
* getBundlerMaker: () => Promise<import('./getBundlerMaker.js').BundleMaker>,
* getBundleSpec: (bundle: Promise<BundleSourceResult<'endoZipBase64'>>, getBundle: () => import('./getBundlerMaker.js').Bundler, opts?: any) => Promise<ManifestBundleRef>,
* log?: typeof console.log,
* writeFile?: typeof fs.promises.writeFile
* }} io
* @returns {WriteCoreEval}
*/
export const makeWriteCoreEval = (
homeP,
endowments,
{
getBundlerMaker,
getBundleSpec,
log = console.log,
writeFile = fs.promises.writeFile,
},
) => {
const { bundleSource, pathResolve } = endowments;
let bundlerCache;
/** @returns {import('./getBundlerMaker.js').Bundler} */
const getBundler = () => {
if (!bundlerCache) {
bundlerCache = E(getBundlerMaker()).makeBundler({
// @ts-expect-error lazily resolved for AgSoloHome
zoe: E.get(homeP).zoe,
});
}
return bundlerCache;
};
/**
*
* @param {CoreEvalDescriptor} coreEval
* @param {*} additionalPermits
*/
const mergeEvalPermit = async (coreEval, additionalPermits) => {
const {
sourceSpec,
getManifestCall: [manifestGetterName, ...manifestGetterArgs],
} = coreEval;
const moduleNamespace = await import(pathResolve(sourceSpec));
// We only care about the manifest, not any restoreRef calls.
const { manifest } = await moduleNamespace[manifestGetterName](
harden({ restoreRef: x => `restoreRef:${x}` }),
...manifestGetterArgs,
);
const mergedPermits = mergePermits(manifest);
return {
manifest,
permits: mergePermits({ mergedPermits, additionalPermits }),
};
};
let mutex = /** @type {Promise<ManifestBundleRef | undefined>} */ (
Promise.resolve()
);
/** @type {WriteCoreEval} */
const writeCoreEval = async (filePrefix, builder) => {
/**
*
* @param {string} entrypoint
* @param {string} [bundlePath]
* @returns {Promise<BundleSourceResult<'endoZipBase64'>>}
*/
const getBundle = async (entrypoint, bundlePath) => {
if (!bundlePath) {
return bundleSource(pathResolve(entrypoint));
}
const bundleCache = pathResolve(bundlePath);
await createBundles([[pathResolve(entrypoint), bundleCache]]);
const ns = await import(bundleCache);
return ns.default;
};
const bundles = [];
/**
* Install an entrypoint.
*
* @param {string} entrypoint
* @param {string} [bundlePath]
* @param {unknown} [opts]
* @returns {Promise<ManifestBundleRef>}
*/
const install = async (entrypoint, bundlePath, opts) => {
const bundle = getBundle(entrypoint, bundlePath);
// Serialise the installations.
mutex = E.when(mutex, async () => {
// console.log('installing', { filePrefix, entrypoint, bundlePath });
const spec = await getBundleSpec(bundle, getBundler, opts);
bundles.push({
entrypoint,
...spec,
});
return spec;
});
// @ts-expect-error xxx mutex type narrowing
return mutex;
};
// Await a reference then publish to the board.
const cmds = [];
/** @param {Promise<ManifestBundleRef>} refP */
const publishRef = async refP => {
const { fileName, ...ref } = await refP;
if (fileName) {
cmds.push(`agd tx swingset install-bundle @${fileName}`);
}
return harden(ref);
};
// Create the eval structure.
const evalDescriptor = await deeplyFulfilled(
harden(builder({ publishRef, install })),
);
const { sourceSpec, getManifestCall } = evalDescriptor;
// console.log('created', { filePrefix, sourceSpec, getManifestCall });
// Extract the top-level permit.
const { permits: evalPermits, manifest: customManifest } =
await mergeEvalPermit(evalDescriptor, defaultPermits);
// Get an install
const manifestBundleRef = await publishRef(install(sourceSpec));
// console.log('writing', { filePrefix, manifestBundleRef, sourceSpec });
const code = `\
// This is generated by writeCoreEval; please edit!
/* eslint-disable */
const manifestBundleRef = ${stringify(manifestBundleRef)};
const getManifestCall = harden(${stringify(getManifestCall, true)});
const customManifest = ${stringify(customManifest, true)};
// Make a behavior function and "export" it by way of script completion value.
// It is constructed by an anonymous invocation to ensure the absence of a global binding
// for makeCoreProposalBehavior, which may not be necessary but preserves behavior pre-dating
// https://github.com/Agoric/agoric-sdk/pull/8712 .
const behavior = (${makeCoreProposalBehavior})({ manifestBundleRef, getManifestCall, customManifest, E });
behavior;
`;
const trimmed = defangAndTrim(code);
const permitFile = `${filePrefix}-permit.json`;
log(`creating ${permitFile}`);
await writeFile(permitFile, JSON.stringify(evalPermits, null, 2));
const codeFile = `${filePrefix}.js`;
log(`creating ${codeFile}`);
await writeFile(codeFile, trimmed);
/** @type {CoreEvalPlan} */
const plan = {
name: filePrefix,
script: codeFile,
permit: permitFile,
bundles,
};
await writeFile(
`${filePrefix}-plan.json`,
`${JSON.stringify(plan, null, 2)}\n`,
);
log(`\
You can now run a governance submission command like:
agd tx gov submit-proposal swingset-core-eval ${permitFile} ${codeFile} \\
--title="Enable <something>" --description="Evaluate ${codeFile}" --deposit=1000000ubld \\
--gas=auto --gas-adjustment=1.2
Remember to install bundles before submitting the proposal:
${cmds.join('\n ')}
`);
return plan;
};
return writeCoreEval;
};
/** @deprecated use makeWriteCoreEval */
export const makeWriteCoreProposal = makeWriteCoreEval;