-
Notifications
You must be signed in to change notification settings - Fork 225
/
Copy pathsupports.ts
558 lines (505 loc) · 17 KB
/
supports.ts
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/* eslint-disable jsdoc/require-param, @jessie.js/safe-await-separator */
/* global process */
import childProcessAmbient from 'child_process';
import { promises as fsAmbientPromises } from 'fs';
import { resolve as importMetaResolve } from 'import-meta-resolve';
import { basename, join } from 'path';
import { inspect } from 'util';
import { Fail, NonNullish } from '@agoric/assert';
import { buildSwingset } from '@agoric/cosmic-swingset/src/launch-chain.js';
import { BridgeId, VBankAccount, makeTracer } from '@agoric/internal';
import { unmarshalFromVstorage } from '@agoric/internal/src/marshal.js';
import { makeFakeStorageKit } from '@agoric/internal/src/storage-test-utils.js';
import { krefOf } from '@agoric/kmarshal';
import { initSwingStore } from '@agoric/swing-store';
import { loadSwingsetConfigFile } from '@agoric/swingset-vat';
import { makeSlogSender } from '@agoric/telemetry';
import { TimeMath, Timestamp } from '@agoric/time';
import {
boardSlottingMarshaller,
slotToBoardRemote,
} from '@agoric/vats/tools/board-utils.js';
import { makeRunUtils } from '@agoric/swingset-vat/tools/run-utils.js';
import type { ExecutionContext as AvaT } from 'ava';
import type { CoreEvalSDKType } from '@agoric/cosmic-proto/swingset/swingset.js';
import type { BridgeHandler, IBCMethod } from '@agoric/vats';
import { icaMocks, protoMsgMocks } from './ibc/mocks.js';
const trace = makeTracer('BSTSupport', false);
const keysToObject = <K extends PropertyKey, V>(
keys: K[],
valueMaker: (key: K, i: number) => V,
) => {
return Object.fromEntries(keys.map((key, i) => [key, valueMaker(key, i)]));
};
/**
* AVA's default t.deepEqual() is nearly unreadable for sorted arrays of
* strings.
*/
export const keyArrayEqual = (
t: AvaT,
a: PropertyKey[],
b: PropertyKey[],
message?: string,
) => {
const aobj = keysToObject(a, () => 1);
const bobj = keysToObject(b, () => 1);
return t.deepEqual(aobj, bobj, message);
};
export const getNodeTestVaultsConfig = async (
bundleDir = 'bundles',
specifier = '@agoric/vm-config/decentral-itest-vaults-config.json',
defaultManagerType = 'local' as ManagerType,
) => {
const fullPath = await importMetaResolve(specifier, import.meta.url).then(
u => new URL(u).pathname,
);
const config: SwingSetConfig & { coreProposals?: any[] } = NonNullish(
await loadSwingsetConfigFile(fullPath),
);
// Manager types:
// 'local':
// - much faster (~3x speedup)
// - much easier to use debugger
// - exhibits inconsistent GC behavior from run to run
// 'xs-worker'
// - timing results more accurately reflect production
config.defaultManagerType = defaultManagerType;
// speed up build (60s down to 10s in testing)
config.bundleCachePath = bundleDir;
await fsAmbientPromises.mkdir(bundleDir, { recursive: true });
if (config.coreProposals) {
// remove Pegasus because it relies on IBC to Golang that isn't running
config.coreProposals = config.coreProposals.filter(
v => v !== '@agoric/pegasus/scripts/init-core.js',
);
}
const testConfigPath = `${bundleDir}/${basename(specifier)}`;
await fsAmbientPromises.writeFile(
testConfigPath,
JSON.stringify(config),
'utf-8',
);
return testConfigPath;
};
interface Powers {
childProcess: Pick<typeof import('node:child_process'), 'execFileSync'>;
fs: typeof import('node:fs/promises');
}
export const makeProposalExtractor = ({ childProcess, fs }: Powers) => {
const getPkgPath = (pkg, fileName = '') =>
new URL(`../../${pkg}/${fileName}`, import.meta.url).pathname;
const importSpec = spec =>
importMetaResolve(spec, import.meta.url).then(u => new URL(u).pathname);
const runPackageScript = (
outputDir: string,
scriptPath: string,
env: NodeJS.ProcessEnv,
) => {
console.info('running package script:', scriptPath);
const out = childProcess.execFileSync('yarn', ['bin', 'agoric'], {
cwd: outputDir,
env,
});
return childProcess.execFileSync(
out.toString().trim(),
['run', scriptPath],
{
cwd: outputDir,
env,
},
);
};
const loadJSON = async filePath =>
harden(JSON.parse(await fs.readFile(filePath, 'utf8')));
// XXX parses the output to find the files but could write them to a path that can be traversed
const parseProposalParts = (txt: string) => {
const evals = [
...txt.matchAll(/swingset-core-eval (?<permit>\S+) (?<script>\S+)/g),
].map(m => {
if (!m.groups) throw Fail`Invalid proposal output ${m[0]}`;
const { permit, script } = m.groups;
return { permit, script };
});
evals.length ||
Fail`No swingset-core-eval found in proposal output: ${txt}`;
const bundles = [
...txt.matchAll(/swingset install-bundle @([^\n]+)/gm),
].map(([, bundle]) => bundle);
bundles.length || Fail`No bundles found in proposal output: ${txt}`;
return { evals, bundles };
};
const buildAndExtract = async (builderPath: string) => {
const tmpDir = await fsAmbientPromises.mkdtemp(
join(getPkgPath('builders'), 'proposal-'),
);
const built = parseProposalParts(
runPackageScript(
tmpDir,
await importSpec(builderPath),
process.env,
).toString(),
);
const loadPkgFile = fileName => fs.readFile(join(tmpDir, fileName), 'utf8');
const evalsP = Promise.all(
built.evals.map(async ({ permit, script }) => {
const [permits, code] = await Promise.all([
loadPkgFile(permit),
loadPkgFile(script),
]);
// Fire and forget. There's a chance the Node process could terminate
// before the deletion completes. This is a minor inconvenience to clean
// up manually and not worth slowing down the test execution to prevent.
void fsAmbientPromises.rm(tmpDir, { recursive: true, force: true });
return { json_permits: permits, js_code: code } as CoreEvalSDKType;
}),
);
const bundlesP = Promise.all(
built.bundles.map(
async bundleFile =>
loadJSON(bundleFile) as Promise<EndoZipBase64Bundle>,
),
);
return Promise.all([evalsP, bundlesP]).then(([evals, bundles]) => ({
evals,
bundles,
}));
};
return buildAndExtract;
};
harden(makeProposalExtractor);
export const matchRef = (
t: AvaT,
ref1: unknown,
ref2: unknown,
message?: string,
) => t.is(krefOf(ref1), krefOf(ref2), message);
export const matchAmount = (
t: AvaT,
amount: Amount,
refBrand: Brand,
refValue,
message?: string,
) => {
matchRef(t, amount.brand, refBrand);
t.is(amount.value, refValue, message);
};
export const matchValue = (t: AvaT, value, ref) => {
matchRef(t, value.brand, ref.brand);
t.is(value.denom, ref.denom);
matchRef(t, value.issuer, ref.issuer);
t.is(value.issuerName, ref.issuerName);
t.is(value.proposedName, ref.proposedName);
};
export const matchIter = (t: AvaT, iter, valueRef) => {
t.is(iter.done, false);
matchValue(t, iter.value, valueRef);
};
/**
* Start a SwingSet kernel to be used by tests and benchmarks.
*
* In the case of Ava tests, this kernel is expected to be shared across all
* tests in a given test module. By default Ava tests run in parallel, so be
* careful to avoid ordering dependencies between them. For example, test
* accounts balances using separate wallets or test vault factory metrics using
* separate collateral managers. (Or use test.serial)
*
* The shutdown() function _must_ be called after the test or benchmarks are
* complete, else V8 will see the xsnap workers still running, and will never
* exit (leading to a timeout error). Ava tests should use
* t.after.always(shutdown), because the normal t.after() hooks are not run if a
* test fails.
*
* @param log
* @param bundleDir directory to write bundles and config to
* @param [options]
* @param [options.configSpecifier] bootstrap config specifier
* @param [options.storage]
* @param [options.verbose]
* @param [options.slogFile]
* @param [options.profileVats]
* @param [options.debugVats]
* @param [options.defaultManagerType]
*/
export const makeSwingsetTestKit = async (
log: (..._: any[]) => void,
bundleDir = 'bundles',
{
configSpecifier = undefined as string | undefined,
storage = makeFakeStorageKit('bootstrapTests'),
verbose = false,
slogFile = undefined as string | undefined,
profileVats = [] as string[],
debugVats = [] as string[],
defaultManagerType = 'local' as ManagerType,
} = {},
) => {
console.time('makeBaseSwingsetTestKit');
const configPath = await getNodeTestVaultsConfig(
bundleDir,
configSpecifier,
defaultManagerType,
);
const swingStore = initSwingStore();
const { kernelStorage, hostStorage } = swingStore;
const { fromCapData } = boardSlottingMarshaller(slotToBoardRemote);
const readLatest = (path: string): any => {
const data = unmarshalFromVstorage(storage.data, path, fromCapData, -1);
trace('readLatest', path, 'returning', inspect(data, false, 20, true));
return data;
};
let lastNonce = 0n;
const outboundMessages = new Map();
let inbound;
let ibcSequenceNonce = 0;
const makeAckEvent = (obj: IBCMethod<'sendPacket'>, ack: string) => {
ibcSequenceNonce += 1;
const msg = icaMocks.ackPacket(obj, ibcSequenceNonce, ack);
inbound(BridgeId.DIBC, msg);
return msg.packet;
};
/**
* Mock the bridge outbound handler. The real one is implemented in Golang so
* changes there will sometimes require changes here.
*/
const bridgeOutbound = (bridgeId: string, obj: any) => {
// store all messages for querying by tests
if (!outboundMessages.has(bridgeId)) {
outboundMessages.set(bridgeId, []);
}
outboundMessages.get(bridgeId).push(obj);
switch (bridgeId) {
case BridgeId.BANK: {
trace(
'bridgeOutbound BANK',
obj.type,
obj.recipient,
obj.amount,
obj.denom,
);
// bridgeOutbound bank : {
// moduleName: 'vbank/reserve',
// type: 'VBANK_GET_MODULE_ACCOUNT_ADDRESS'
// }
switch (obj.type) {
case 'VBANK_GET_MODULE_ACCOUNT_ADDRESS': {
const { moduleName } = obj;
const moduleDescriptor = Object.values(VBankAccount).find(
({ module }) => module === moduleName,
);
if (!moduleDescriptor) {
return 'undefined';
}
return moduleDescriptor.address;
}
// Observed message:
// address: 'agoric1megzytg65cyrgzs6fvzxgrcqvwwl7ugpt62346',
// denom: 'ibc/toyatom',
// type: 'VBANK_GET_BALANCE'
case 'VBANK_GET_BALANCE': {
// TODO consider letting config specify vbank assets
// empty balances for test.
return '0';
}
case 'VBANK_GRAB':
case 'VBANK_GIVE': {
lastNonce += 1n;
// Also empty balances.
return harden({
type: 'VBANK_BALANCE_UPDATE',
nonce: `${lastNonce}`,
updated: [],
});
}
default: {
return 'undefined';
}
}
}
case BridgeId.CORE:
case BridgeId.DIBC:
switch (obj.type) {
case 'IBC_METHOD':
switch (obj.method) {
case 'startChannelOpenInit':
inbound(BridgeId.DIBC, icaMocks.channelOpenAck(obj));
return undefined;
case 'sendPacket':
switch (obj.packet.data) {
case protoMsgMocks.delegate.msg: {
return makeAckEvent(obj, protoMsgMocks.delegate.ack);
}
case protoMsgMocks.delegateWithOpts.msg: {
return makeAckEvent(
obj,
protoMsgMocks.delegateWithOpts.ack,
);
}
case protoMsgMocks.queryBalance.msg: {
return makeAckEvent(obj, protoMsgMocks.queryBalance.ack);
}
case protoMsgMocks.queryUnknownPath.msg: {
return makeAckEvent(
obj,
protoMsgMocks.queryUnknownPath.ack,
);
}
case protoMsgMocks.queryBalanceMulti.msg: {
return makeAckEvent(
obj,
protoMsgMocks.queryBalanceMulti.ack,
);
}
case protoMsgMocks.queryBalanceUnknownDenom.msg: {
return makeAckEvent(
obj,
protoMsgMocks.queryBalanceUnknownDenom.ack,
);
}
default: {
return makeAckEvent(obj, protoMsgMocks.error.ack);
}
}
default:
return undefined;
}
default:
return undefined;
}
case BridgeId.PROVISION:
case BridgeId.PROVISION_SMART_WALLET:
case BridgeId.VTRANSFER:
case BridgeId.WALLET:
console.warn('Bridge returning undefined for', bridgeId, ':', obj);
return undefined;
case BridgeId.STORAGE:
return storage.toStorage(obj);
case BridgeId.VLOCALCHAIN:
switch (obj.type) {
case 'VLOCALCHAIN_ALLOCATE_ADDRESS':
return 'agoric1mockVlocalchainAddress';
case 'VLOCALCHAIN_EXECUTE_TX':
// returns one empty object per message
return obj.messages.map(() => ({}));
default:
throw Error(`VLOCALCHAIN message of unknown type ${obj.type}`);
}
default:
throw Error(`unknown bridgeId ${bridgeId}`);
}
};
let slogSender;
if (slogFile) {
slogSender = await makeSlogSender({
stateDir: '.',
env: {
...process.env,
SLOGFILE: slogFile,
SLOGSENDER: '',
},
});
}
const { controller, timer, bridgeInbound } = await buildSwingset(
new Map(),
bridgeOutbound,
kernelStorage,
configPath,
[],
{},
{
callerWillEvaluateCoreProposals: false,
debugName: 'TESTBOOT',
verbose,
slogSender,
profileVats,
debugVats,
},
);
inbound = bridgeInbound;
console.timeLog('makeBaseSwingsetTestKit', 'buildSwingset');
const runUtils = makeRunUtils(controller);
const buildProposal = makeProposalExtractor({
childProcess: childProcessAmbient,
fs: fsAmbientPromises,
});
const evalProposal = async (
proposalP: ERef<Awaited<ReturnType<typeof buildProposal>>>,
) => {
const { EV } = runUtils;
const proposal = harden(await proposalP);
for await (const bundle of proposal.bundles) {
await controller.validateAndInstallBundle(bundle);
}
log('installed', proposal.bundles.length, 'bundles');
log('executing proposal');
const bridgeMessage = {
type: 'CORE_EVAL',
evals: proposal.evals,
};
log({ bridgeMessage });
const coreEvalBridgeHandler: BridgeHandler = await EV.vat(
'bootstrap',
).consumeItem('coreEvalBridgeHandler');
await EV(coreEvalBridgeHandler).fromBridge(bridgeMessage);
log(`proposal executed`);
};
console.timeEnd('makeBaseSwingsetTestKit');
let currentTime = 0n;
const updateTimer = async time => {
await timer.poll(time);
};
const jumpTimeTo = (targetTime: Timestamp) => {
targetTime = TimeMath.absValue(targetTime);
targetTime >= currentTime ||
Fail`cannot reverse time :-( (${targetTime} < ${currentTime})`;
currentTime = targetTime;
trace('jumpTimeTo', currentTime);
return runUtils.queueAndRun(() => updateTimer(currentTime), true);
};
const advanceTimeTo = async (targetTime: Timestamp) => {
targetTime = TimeMath.absValue(targetTime);
targetTime >= currentTime ||
Fail`cannot reverse time :-( (${targetTime} < ${currentTime})`;
while (currentTime < targetTime) {
trace('stepping time from', currentTime, 'towards', targetTime);
currentTime += 1n;
await runUtils.queueAndRun(() => updateTimer(currentTime), true);
}
};
const advanceTimeBy = (
n: number,
unit: 'seconds' | 'minutes' | 'hours' | 'days',
) => {
const multiplier = {
seconds: 1,
minutes: 60,
hours: 60 * 60,
days: 60 * 60 * 24,
};
const targetTime = currentTime + BigInt(multiplier[unit] * n);
trace('advanceTimeBy', n, unit, 'to', targetTime);
return advanceTimeTo(targetTime);
};
const shutdown = async () =>
Promise.all([controller.shutdown(), hostStorage.close()]).then(() => {});
const getCrankNumber = () => Number(kernelStorage.kvStore.get('crankNumber'));
const getOutboundMessages = (bridgeId: string) =>
harden([...outboundMessages.get(bridgeId)]);
return {
advanceTimeBy,
advanceTimeTo,
buildProposal,
bridgeInbound,
controller,
evalProposal,
getCrankNumber,
getOutboundMessages,
jumpTimeTo,
readLatest,
runUtils,
shutdown,
storage,
swingStore,
timer,
};
};
export type SwingsetTestKit = Awaited<ReturnType<typeof makeSwingsetTestKit>>;