-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathtest-crashingContract.js
209 lines (186 loc) · 7.17 KB
/
test-crashingContract.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
/* global __dirname */
// @ts-check
// TODO Remove babel-standalone preinitialization
// https://github.com/endojs/endo/issues/768
import '@agoric/babel-standalone';
// eslint-disable-next-line import/no-extraneous-dependencies
import '@agoric/install-ses';
// eslint-disable-next-line import/no-extraneous-dependencies
import test from 'ava';
import { loadBasedir, buildVatController } from '@agoric/swingset-vat';
import bundleSource from '@agoric/bundle-source';
import fs from 'fs';
const CONTRACT_FILES = ['crashingAutoRefund'];
const generateBundlesP = Promise.all(
CONTRACT_FILES.map(async contract => {
const bundle = await bundleSource(`${__dirname}/${contract}`);
const obj = { bundle, contract };
fs.writeFileSync(
`${__dirname}/bundle-${contract}.js`,
`export default ${JSON.stringify(obj)};`,
);
}),
);
async function main(argv) {
const config = await loadBasedir(__dirname);
config.defaultManagerType = 'xs-worker';
await generateBundlesP;
const controller = await buildVatController(config, argv);
await controller.run();
return controller.dump();
}
const meterExceededInOfferLog = [
'=> alice is set up',
'=> alice.doMeterExceptionInHook called',
'outcome correctly resolves to broken: RangeError: Allocate meter exceeded',
'aliceMoolaPurse: balance {"brand":{},"value":3}',
'aliceSimoleanPurse: balance {"brand":{},"value":0}',
'contract no longer responds: RangeError: Allocate meter exceeded',
'counter: 2',
];
// TODO: Unskip. See https://github.com/Agoric/agoric-sdk/issues/1625
test.skip('ZCF metering crash on invitation exercise', async t => {
const dump = await main(['meterInOfferHook', [3, 0, 0]]);
t.deepEqual(dump.log, meterExceededInOfferLog);
});
const meterExceededInSecondOfferLog = [
'=> alice is set up',
'=> alice.doMeterExceptionInHook called',
'Swap outcome resolves to an invitation: [Alleged: presence o-73]',
'aliceMoolaPurse: balance {"brand":{},"value":0}',
'aliceSimoleanPurse: balance {"brand":{},"value":0}',
'aliceMoolaPurse: balance {"brand":{},"value":5}',
'aliceSimoleanPurse: balance {"brand":{},"value":0}',
'swap value, 5',
'outcome correctly resolves to broken: RangeError: Allocate meter exceeded',
'contract no longer responds: RangeError: Allocate meter exceeded',
'aliceMoolaPurse: balance {"brand":{},"value":8}',
'aliceSimoleanPurse: balance {"brand":{},"value":0}',
'refund value, 8',
'counter: 2',
];
// TODO: Unskip. See https://github.com/Agoric/agoric-sdk/issues/1625
test.skip('ZCF metering crash on second invitation', async t => {
const dump = await main(['meterInSecondInvitation', [8, 0, 0]]);
t.deepEqual(dump.log, meterExceededInSecondOfferLog);
});
const throwInOfferLog = [
'=> alice is set up',
'=> alice.doThrowInHook called',
'counter: 2',
'outcome correctly resolves to broken: Error: someException',
'counter: 4',
'aliceMoolaPurse: balance {"brand":{},"value":3}',
'aliceSimoleanPurse: balance {"brand":{},"value":0}',
'counter: 5',
'newCounter: 2',
'Successful refund: The offer was accepted',
'new Purse: balance {"brand":{},"value":3}',
'aliceSimoleanPurse: balance {"brand":{},"value":0}',
'counter: 7',
];
test('ZCF throwing on invitation exercise', async t => {
const dump = await main(['throwInOfferHook', [3, 0, 0]]);
t.deepEqual(dump.log, throwInOfferLog);
});
const throwInAPILog = [
'=> alice is set up',
'=> alice.doThrowInApiCall called',
'counter: 3',
'throwingAPI should throw Error: someException',
'counter: 5',
'counter: 6',
'Swap outcome is an invitation (true).',
'newCounter: 2',
'counter: 7',
'outcome correctly resolves: "The offer has been accepted. Once the contract has been completed, please check your payout"',
'aliceMoolaPurse: balance {"brand":{},"value":3}',
'aliceSimoleanPurse: balance {"brand":{},"value":8}',
'second moolaPurse: balance {"brand":{},"value":2}',
'second simoleanPurse: balance {"brand":{},"value":4}',
];
test('ZCF throwing in API call', async t => {
const dump = await main(['throwInApiCall', [5, 12, 0]]);
t.deepEqual(dump.log, throwInAPILog);
});
const meteringExceededInAPILog = [
'=> alice is set up',
'=> alice.doMeterInApiCall called',
'counter: 2',
'counter: 3',
'counter: 5',
'Vat correctly died for RangeError: Allocate meter exceeded',
'outcome correctly resolves to "The offer was accepted"',
'aliceMoolaPurse: balance {"brand":{},"value":3}',
'aliceSimoleanPurse: balance {"brand":{},"value":0}',
'contract no longer responds: RangeError: Allocate meter exceeded',
'newCounter: 2',
];
// TODO: Unskip. See https://github.com/Agoric/agoric-sdk/issues/1625
test.skip('ZCF metering crash in API call', async t => {
const dump = await main(['meterInApiCall', [3, 0, 0]]);
t.deepEqual(dump.log, meteringExceededInAPILog);
});
const meteringExceptionInMakeContractILog = [
'=> alice is set up',
'=> alice.doMeterExceptionInMakeContract called',
'contract creation failed: RangeError: Allocate meter exceeded',
'newCounter: 2',
];
// TODO: Unskip. See https://github.com/Agoric/agoric-sdk/issues/1625
test.skip('ZCF metering crash in makeContract call', async t => {
const dump = await main(['meterInMakeContract', [3, 0, 0]]);
t.deepEqual(dump.log, meteringExceptionInMakeContractILog);
});
const thrownExceptionInMakeContractILog = [
'=> alice is set up',
'=> alice.doThrowInMakeContract called',
'contract creation failed: Error: blowup in makeContract',
'newCounter: 2',
];
test('throw in makeContract call', async t => {
const dump = await main(['throwInMakeContract', [3, 0, 0]]);
t.deepEqual(dump.log, thrownExceptionInMakeContractILog);
});
const happyTerminationLog = [
'=> alice is set up',
'=> alice.doHappyTermination called',
'happy termination saw "Success"',
];
test('happy termination path', async t => {
const dump = await main(['happyTermination', [3, 0, 0]]);
t.deepEqual(dump.log, happyTerminationLog);
});
const happyTerminationWOffersLog = [
'=> alice is set up',
'=> alice.doHappyTerminationWOffers called',
'seat has been exited: [object Promise]',
'Swap outcome rejected before fulfillment: "Error: vat terminated"',
'happy termination saw "Success"',
'second moolaPurse: balance {"brand":{},"value":5}',
'second simoleanPurse: balance {"brand":{},"value":0}',
];
test('happy termination with offers path', async t => {
const dump = await main(['happyTerminationWOffers', [5, 0, 0]]);
t.deepEqual(dump.log, happyTerminationWOffersLog);
});
const doHappyTerminationRefusesContactLog = [
'=> alice is set up',
'=> alice.doHappyTerminationWOffers called',
'offer correctly refused: "Error: No further offers are accepted"',
'happy termination saw "Success"',
'can\'t make more invitations because "Error: vat terminated"',
];
test('happy termination refuses contact path', async t => {
const dump = await main(['doHappyTerminationRefusesContact', [5, 0, 0]]);
t.deepEqual(dump.log, doHappyTerminationRefusesContactLog);
});
const sadTerminationLog = [
'=> alice is set up',
'=> alice.doSadTermination called',
'sad termination saw reject "Sadness"',
];
test('sad termination path', async t => {
const dump = await main(['sadTermination', [3, 0, 0]]);
t.deepEqual(dump.log, sadTerminationLog);
});