-
Notifications
You must be signed in to change notification settings - Fork 215
/
upgrade-vats.test.ts
551 lines (484 loc) · 17.3 KB
/
upgrade-vats.test.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
/* eslint-disable @jessie.js/safe-await-separator -- test */
import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava.js';
import { BridgeId } from '@agoric/internal';
import { buildVatController } from '@agoric/swingset-vat';
import { makeRunUtils } from '@agoric/swingset-vat/tools/run-utils.js';
import { Fail } from '@endo/errors';
import { makeTagged } from '@endo/marshal';
import { resolve as importMetaResolve } from 'import-meta-resolve';
import type { IssuerKit } from '@agoric/ertp/src/types.js';
import { matchAmount, matchIter, matchRef } from '../../tools/supports.js';
import type { buildRootObject as buildTestMintVat } from './vat-mint.js';
const bfile = name => new URL(name, import.meta.url).pathname;
const importSpec = spec =>
importMetaResolve(spec, import.meta.url).then(u => new URL(u).pathname);
const makeCallOutbound = t => (srcID, obj) => {
t.log(`callOutbound(${srcID}, ${obj})`);
return obj;
};
const makeScenario = async (
t: any,
kernelConfigOverrides: Partial<SwingSetConfig> = {},
deviceEndowments: Record<string, unknown> = {},
) => {
const config: SwingSetConfig = {
includeDevDependencies: true, // for vat-data
bootstrap: 'bootstrap',
defaultReapInterval: 'never',
vats: {
bootstrap: {
sourceSpec: await importSpec(
'@agoric/swingset-vat/tools/bootstrap-relay.js',
),
},
},
bundleCachePath: 'bundles',
...kernelConfigOverrides,
};
const c = await buildVatController(
config,
undefined,
undefined,
deviceEndowments,
);
t.teardown(c.shutdown);
c.pinVatRoot('bootstrap');
const runUtils = makeRunUtils(c);
return runUtils;
};
test('upgrade vat-board', async t => {
const bundles = {
board: {
sourceSpec: await importSpec('@agoric/vats/src/vat-board.js'),
},
};
const { EV } = await makeScenario(t, { bundles });
t.log('create initial version');
const boardVatConfig = {
name: 'board',
bundleCapName: 'board',
};
const boardRoot = await EV.vat('bootstrap').createVat(boardVatConfig);
const board = await EV(boardRoot).getBoard();
const thing = await EV.vat('bootstrap').makeRemotable('Thing', {});
const thingId = await EV(board).getId(thing);
t.regex(thingId, /^board0[0-9]+$/);
t.log('now perform the null upgrade');
const { incarnationNumber } =
await EV.vat('bootstrap').upgradeVat(boardVatConfig);
t.is(incarnationNumber, 1, 'Board vat must be upgraded');
const board2 = await EV(boardRoot).getBoard();
matchRef(t, board2, board, 'must get the same board reference');
const actualThing = await EV(board2).getValue(thingId);
matchRef(t, actualThing, thing, 'must get original value back');
});
test.failing('upgrade bootstrap vat', async t => {
const bundles = {
chain: {
sourceSpec: await importSpec('@agoric/vats/src/core/boot-chain.js'),
},
};
// @ts-expect-error error in skipped test
const { EV } = await makeScenario(t, bundles);
t.log('create initial version');
const chainVatConfig = {
name: 'chain',
bundleCapName: 'chain',
};
const chainRoot = await EV.vat('bootstrap').createVat(chainVatConfig);
await EV(chainRoot)
.bootstrap({}, {})
.catch(problem => t.log('TODO: address problem:', problem));
t.log('now perform the null upgrade');
const { incarnationNumber } =
await EV.vat('bootstrap').upgradeVat(chainVatConfig);
t.is(incarnationNumber, 1, 'vat must be upgraded');
});
test('upgrade vat-bridge', async t => {
const bundles = {
bridge: { sourceSpec: await importSpec('@agoric/vats/src/vat-bridge.js') },
};
const devices = {
bridge: { sourceSpec: bfile('./device-bridge.js') },
};
const expectedStorageValues = ['abc', 'def'];
const { EV } = await makeScenario(
t,
{ bundles, devices },
{
bridge: {
t,
callOutbound: makeCallOutbound(t),
bridgeBackends: {
[BridgeId.STORAGE](obj) {
const { method, args } = obj;
t.is(method, 'set', `storage bridge method must be 'set'`);
t.deepEqual(args, [['root1', expectedStorageValues.shift()]]);
return undefined;
},
},
},
},
);
t.log('create initial version');
const bridgeVatConfig = {
name: 'bridge',
bundleCapName: 'bridge',
};
const bridgeRoot = await EV.vat('bootstrap').createVat(bridgeVatConfig);
await t.throwsAsync(
() => EV(bridgeRoot).provideManagerForBridge(1),
{
message: /must be given a device node/,
},
'rejects non-device node',
);
const dev = await EV.vat('bootstrap').getDevice('bridge');
const bridge1 = await EV(bridgeRoot).provideManagerForBridge(dev);
const bridge2 = await EV(bridgeRoot).provideManagerForBridge(dev);
matchRef(t, bridge1, bridge2, 'must get the same bridge reference');
const storageBridge = await EV(bridge1).register(BridgeId.STORAGE);
const storageRoot = await EV(bridgeRoot).makeBridgedChainStorageRoot(
storageBridge,
'root1',
{ sequence: false },
);
const path1 = await EV(storageRoot).getPath();
t.is(path1, 'root1', 'must get the same path back');
const ret = await EV(storageRoot).setValue('abc');
t.is(ret, undefined, 'setValue must return undefined');
await t.throwsAsync(() => EV(storageBridge).fromBridge('no handler'), {
message: /No inbound handler for/,
});
const storageHandler = await EV.vat('bootstrap').makeRemotable(
'loggingStorageHandler',
{
fromBridge: undefined,
},
);
await EV(storageBridge).initHandler(storageHandler);
t.deepEqual(
await EV.vat('bootstrap').getLogForRemotable(storageHandler),
[],
'log must start empty',
);
await EV(storageBridge).fromBridge('storage handler is good');
t.deepEqual(
await EV.vat('bootstrap').getLogForRemotable(storageHandler),
[['fromBridge', 'storage handler is good']],
'log must show one handler call',
);
t.log('now perform the null upgrade');
const { incarnationNumber } =
await EV.vat('bootstrap').upgradeVat(bridgeVatConfig);
t.is(incarnationNumber, 1, 'Bridge vat must be upgraded');
const path1b = await EV(storageRoot).getPath();
t.is(path1b, 'root1', 'must get the same path back');
const bridge1b = await EV(bridgeRoot).provideManagerForBridge(dev);
matchRef(t, bridge1b, bridge1, 'must get the same bridge reference');
const ret2 = await EV(storageRoot).setValue('def');
t.is(ret2, undefined, 'setValue must return undefined');
await EV(storageBridge).fromBridge('storage handler is still good');
t.deepEqual(
await EV.vat('bootstrap').getLogForRemotable(storageHandler),
[
['fromBridge', 'storage handler is good'],
['fromBridge', 'storage handler is still good'],
],
'log must show next handler call',
);
});
test('upgrade vat-bank', async t => {
const bundles = {
bank: { sourceSpec: await importSpec('@agoric/vats/src/vat-bank.js') },
bridge: { sourceSpec: await importSpec('@agoric/vats/src/vat-bridge.js') },
mint: { sourceSpec: bfile('./vat-mint.js') },
};
const devices = {
bridge: { sourceSpec: bfile('./device-bridge.js') },
};
const { EV } = await makeScenario(
t,
{ bundles, devices },
{
bridge: {
t,
callOutbound: makeCallOutbound(t),
bridgeBackends: {
[BridgeId.BANK](obj) {
switch (obj.type) {
case 'VBANK_GET_BALANCE': {
return '1000';
}
default:
Fail`unexpected call to bank handler ${obj}`;
}
},
},
},
},
);
t.log('create initial version');
const bridgeVatConfig = {
name: 'bridge',
bundleCapName: 'bridge',
};
const bankVatConfig = {
name: 'bank',
bundleCapName: 'bank',
};
const mintVatConfig = {
name: 'mint',
bundleCapName: 'mint',
};
const bridgeRoot = await EV.vat('bootstrap').createVat(bridgeVatConfig);
const bankRoot: BankVat = await EV.vat('bootstrap').createVat(bankVatConfig);
const mintRoot: ReturnType<typeof buildTestMintVat> =
await EV.vat('bootstrap').createVat(mintVatConfig);
t.log(`create a non-bridged bank manager`);
const noBridgeMgr = await EV(bankRoot).makeBankManager();
t.log(`create a bridged bank manager`);
const dev = await EV.vat('bootstrap').getDevice('bridge');
const bridge1 = await EV(bridgeRoot).provideManagerForBridge(dev);
const bankBridge = await EV(bridge1).register(BridgeId.BANK);
const bridgedMgr = await EV(bankRoot).makeBankManager(bankBridge);
t.log('subscribe to no bridge asset lists');
const noBridgeAssetSub1 = await EV(noBridgeMgr).getAssetSubscription();
const noBridgeIterator = await EV(noBridgeAssetSub1)[Symbol.asyncIterator]();
t.log('add an asset to both');
const abcKit = (await EV(mintRoot).makeIssuerKit('ABC')) as IssuerKit<'nat'>;
const defKit = (await EV(mintRoot).makeIssuerKit('DEF')) as IssuerKit<'nat'>;
await EV(noBridgeMgr).addAsset('uabc', 'ABC', 'A Bank Coin', abcKit);
await EV(bridgedMgr).addAsset('uabc', 'ABC', 'A Bank Coin', abcKit);
await EV(bridgedMgr).addAsset('udef', 'DEF', 'Definitely a coin', defKit);
const banks = {
agoric1234: await EV(noBridgeMgr).getBankForAddress('agoric1234'),
agoric1bbb: await EV(bridgedMgr).getBankForAddress('agoric1bbb'),
};
const abcPurse1 = await EV(banks.agoric1234).getPurse(abcKit.brand);
const abcPurse2 = await EV(banks.agoric1234).getPurse(abcKit.brand);
matchRef(t, abcPurse1, abcPurse2, 'must get the same purse back');
const defPurse1 = await EV(banks.agoric1bbb).getPurse(defKit.brand);
const defPurse2 = await EV(banks.agoric1bbb).getPurse(defKit.brand);
matchRef(t, defPurse1, defPurse2, 'must get the same purse back');
const pmt1 = await EV(abcKit.mint).mintPayment({
brand: abcKit.brand,
value: 1000n,
});
await EV(abcPurse1).deposit(pmt1);
const abcAmount = await EV(abcPurse1).getCurrentAmount();
matchAmount(t, abcAmount, abcKit.brand, 1000n, 'purse must have 1000n');
const abcAsset = harden({
brand: abcKit.brand,
denom: 'uabc',
issuer: abcKit.issuer,
issuerName: 'ABC',
proposedName: 'A Bank Coin',
});
const defAsset = harden({
brand: defKit.brand,
denom: 'udef',
issuer: defKit.issuer,
issuerName: 'DEF',
proposedName: 'Definitely a coin',
});
t.log('subscribe to bridged asset list');
const bridgedAssetSub1 = await EV(bridgedMgr).getAssetSubscription();
t.not(
bridgedAssetSub1,
noBridgeAssetSub1,
'differently-bridged asset subscriptions must be different',
);
const bridgedIterator = await EV(bridgedAssetSub1)[Symbol.asyncIterator]();
matchIter(t, await EV(bridgedIterator).next(), abcAsset);
t.log('now perform the null upgrade');
const { incarnationNumber } =
await EV.vat('bootstrap').upgradeVat(bankVatConfig);
t.is(incarnationNumber, 1, 'Bank vat must be upgraded');
await t.throwsAsync(() => EV(noBridgeIterator).next(), {
message: 'vat terminated',
});
await t.throwsAsync(() => EV(bridgedIterator).next(), {
message: 'vat terminated',
});
const abcPurse3 = await EV(banks.agoric1234).getPurse(abcKit.brand);
matchRef(t, abcPurse3, abcPurse1, 'must get the same purse back');
matchAmount(
t,
await EV(abcPurse3).getCurrentAmount(),
abcKit.brand,
1000n,
'purse must have 1000n',
);
const noBridgeAssetSub2 = await EV(noBridgeMgr).getAssetSubscription();
matchRef(
t,
noBridgeAssetSub1,
noBridgeAssetSub2,
'must get the same subscription back',
);
const noBridgeIterator2 = await EV(noBridgeAssetSub2)[Symbol.asyncIterator]();
matchIter(t, await EV(noBridgeIterator2).next(), abcAsset);
const bridgedAssetSub2 = await EV(bridgedMgr).getAssetSubscription();
matchRef(
t,
bridgedAssetSub1,
bridgedAssetSub2,
'must get the same subscription back',
);
const bridgedIteratorpub = await EV(bridgedAssetSub2).subscribeAfter();
matchIter(t, bridgedIteratorpub.head, abcAsset);
t.like(bridgedIteratorpub, {
publishCount: 1n,
tail: bridgedIteratorpub.tail,
});
const bridgedIteratorpub2 = await EV.vat('bootstrap').awaitVatObject(
bridgedIteratorpub.tail,
);
matchIter(t, bridgedIteratorpub2.head, defAsset);
t.like(bridgedIteratorpub2, {
publishCount: 2n,
tail: bridgedIteratorpub2.tail,
});
await EV(noBridgeMgr).addAsset('udef', 'DEF', 'Definitely a coin', defKit);
matchIter(t, await EV(noBridgeIterator2).next(), defAsset);
});
test('upgrade vat-priceAuthority', async t => {
const bundles = {
priceAuthority: {
sourceSpec: await importSpec('@agoric/vats/src/vat-priceAuthority.js'),
},
};
const { EV } = await makeScenario(t, { bundles });
t.log('create initial version');
const priceAuthorityVatConfig = {
name: 'priceAuthority',
bundleCapName: 'priceAuthority',
};
const priceAuthorityRoot: PriceAuthorityVat = await EV.vat(
'bootstrap',
).createVat(priceAuthorityVatConfig);
const registry = await EV(priceAuthorityRoot).getRegistry();
// Ideally we'd also test registering a PA and verifying the same one comes out the def end.
// But we don't have a way to produce one through the kser that EV does here
// so we'll have to test that elsewhere.
t.log('now perform the null upgrade');
const { incarnationNumber } = await EV.vat('bootstrap').upgradeVat(
priceAuthorityVatConfig,
);
t.is(incarnationNumber, 1, 'vat must be reincarnated');
t.log('get again');
const reincarnatedRegistry = await EV(priceAuthorityRoot).getRegistry();
// facet holder object changes but the members have the same identity
t.not(reincarnatedRegistry, registry);
matchRef(t, reincarnatedRegistry.priceAuthority, registry.priceAuthority);
matchRef(t, reincarnatedRegistry.adminFacet, registry.adminFacet);
});
const dataOnly = obj => JSON.parse(JSON.stringify(obj));
test('upgrade vat-vow', async t => {
const bundles = {
vow: {
sourceSpec: bfile('./vat-vow.js'),
},
};
const { EV } = await makeScenario(t, { bundles });
t.log('create initial version, metered');
const vatAdmin = await EV.vat('bootstrap').getVatAdmin();
const meter = await EV(vatAdmin).createUnlimitedMeter();
const vowVatConfig = {
name: 'vow',
bundleCapName: 'vow',
};
const vatOptions = { managerType: 'xs-worker', meter };
const vowRoot = await EV.vat('bootstrap').createVat(vowVatConfig, vatOptions);
const makeFakeVowKit = async () => {
const internalPromiseKit = await EV.vat('bootstrap').makePromiseKit();
const fakeVowV0 = await EV.vat('bootstrap').makeRemotable('fakeVowV0', {
shorten: internalPromiseKit.promise,
});
const fakeVow = makeTagged('Vow', harden({ vowV0: fakeVowV0 }));
return harden({ resolver: internalPromiseKit.resolver, vow: fakeVow });
};
t.log('test incarnation 0');
const localPromises = {
promiseForever: [],
promiseFulfilled: ['hello'],
promiseRejected: ['goodbye', true],
} as Record<string, [settlementValue?: unknown, isRejection?: boolean]>;
const promiseKit = await EV.vat('bootstrap').makePromiseKit();
const fakeVowKit = await makeFakeVowKit();
const localVows = {
vowForever: [],
vowFulfilled: ['hello'],
vowRejected: ['goodbye', true],
vowPostUpgrade: [],
vowExternalPromise: [promiseKit.promise],
vowExternalVow: [fakeVowKit.vow],
vowPromiseForever: [undefined, false, true],
};
await EV(vowRoot).makeLocalPromiseWatchers(localPromises);
await EV(vowRoot).makeLocalVowWatchers(localVows);
t.deepEqual(dataOnly(await EV(vowRoot).getWatcherResults()), {
promiseForever: { status: 'unsettled' },
promiseFulfilled: { status: 'fulfilled', value: 'hello' },
promiseRejected: { status: 'rejected', reason: 'goodbye' },
vowForever: {
status: 'unsettled',
resolver: {},
},
vowFulfilled: { status: 'fulfilled', value: 'hello' },
vowRejected: { status: 'rejected', reason: 'goodbye' },
vowPostUpgrade: {
status: 'unsettled',
resolver: {},
},
vowExternalPromise: {
status: 'unsettled',
resolver: {},
},
vowExternalVow: {
status: 'unsettled',
resolver: {},
},
vowPromiseForever: {
status: 'unsettled',
resolver: {},
},
});
t.log('restart');
const { incarnationNumber } =
await EV.vat('bootstrap').upgradeVat(vowVatConfig);
t.is(incarnationNumber, 1, 'vat must be reincarnated');
t.log('test incarnation 1');
const localVowsUpdates = {
vowPostUpgrade: ['bonjour'],
};
await EV(vowRoot).resolveVowWatchers(localVowsUpdates);
await EV(promiseKit.resolver).resolve('ciao');
t.timeout(10_000);
const upgradeRejection = harden({
status: 'rejected',
reason: {
name: 'vatUpgraded',
upgradeMessage: 'vat upgraded',
incarnationNumber: 0,
},
});
await EV(fakeVowKit.resolver).reject(upgradeRejection.reason);
t.timeout(600_000); // t.timeout.clear() not yet available in our ava version
t.deepEqual(dataOnly(await EV(vowRoot).getWatcherResults()), {
promiseForever: upgradeRejection,
promiseFulfilled: { status: 'fulfilled', value: 'hello' },
promiseRejected: { status: 'rejected', reason: 'goodbye' },
vowForever: {
status: 'unsettled',
resolver: {},
},
vowFulfilled: { status: 'fulfilled', value: 'hello' },
vowRejected: { status: 'rejected', reason: 'goodbye' },
vowPostUpgrade: { status: 'fulfilled', value: 'bonjour' },
vowExternalPromise: { status: 'fulfilled', value: 'ciao' },
vowExternalVow: upgradeRejection,
vowPromiseForever: upgradeRejection,
});
});