This repository was archived by the owner on Jan 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathpush.test.js
More file actions
561 lines (454 loc) · 23.3 KB
/
Copy pathpush.test.js
File metadata and controls
561 lines (454 loc) · 23.3 KB
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
559
560
561
'use strict'
require('../setup')
import sinon from 'sinon'
import { Contracts, App, Package } from 'zos-lib'
import push from '../../src/scripts/push.js';
import freeze from '../../src/scripts/freeze';
import add from '../../src/scripts/add';
import bumpVersion from '../../src/scripts/bump';
import ZosPackageFile from '../../src/models/files/ZosPackageFile';
import remove from '../../src/scripts/remove';
import Dependency from '../../src/models/dependency/Dependency';
import CaptureLogs from '../helpers/captureLogs';
import { promisify } from 'util';
const should = require('chai').should();
const ImplV1 = Contracts.getFromLocal('ImplV1');
const WithLibraryImplV1 = Contracts.getFromLocal('WithLibraryImplV1');
const PackageContract = Contracts.getFromNodeModules('zos-lib', 'Package');
const ImplementationDirectory = Contracts.getFromNodeModules('zos-lib', 'ImplementationDirectory');
contract('push script', function([_, owner]) {
const network = 'test';
const txParams = { from: owner }
const defaultVersion = '1.1.0';
const shouldDeployPackage = function () {
it('should create a network file with version info', async function() {
this.networkFile.isCurrentVersion(defaultVersion).should.be.true;
});
it('should include deployment addresses', async function () {
this.networkFile.packageAddress.should.be.nonzeroAddress;
this.networkFile.providerAddress.should.be.nonzeroAddress;
});
it('should deploy package at specified address', async function () {
const _package = await Package.fetch(this.networkFile.packageAddress);
(await _package.hasVersion(defaultVersion)).should.be.true;
});
};
const shouldDeployProvider = function () {
it('should deploy provider at specified address', async function () {
const directory = await ImplementationDirectory.at(this.networkFile.providerAddress);
(await directory.getImplementation('foo')).should.be.zeroAddress;
});
};
const shouldDeployApp = function () {
shouldDeployPackage();
it('should deploy app at specified address', async function () {
const address = this.networkFile.appAddress;
address.should.be.nonzeroAddress;
const app = await App.fetch(address);
const hasPackage = await app.hasPackage(this.networkFile.packageFile.name, defaultVersion)
hasPackage.should.be.true
});
};
const shouldDeployLib = function () {
shouldDeployPackage();
it('should not be frozen by default', async function() {
this.networkFile.frozen.should.be.false;
});
};
const shouldDeployContracts = function () {
it('should record contracts in network file', async function () {
const contract = this.networkFile.contract('Impl');
contract.address.should.be.nonzeroAddress;
contract.localBytecodeHash.should.not.be.empty;
contract.storage.should.not.be.empty;
contract.types.should.not.be.empty;
const deployed = await ImplV1.at(contract.address);
(await deployed.say()).should.eq('V1');
});
it('should deploy contract instance', async function () {
const address = this.networkFile.contract('Impl').address;
const deployed = await ImplV1.at(address);
(await deployed.say()).should.eq('V1');
});
it('should deploy required libraries', async function () {
const address = this.networkFile.solidityLib('UintLib').address;
const code = await promisify(web3.eth.getCode.bind(web3.eth))(address);
const uintLib = Contracts.getFromLocal('UintLib');
code.length.should.eq(uintLib.deployedBytecode.length).and.be.greaterThan(40);
});
it('should deploy and link contracts that require libraries', async function () {
const address = this.networkFile.contract('WithLibraryImpl').address;
const deployed = await WithLibraryImplV1.at(address);
const result = await deployed.double(10);
result.toNumber().should.eq(20);
});
};
const shouldRegisterContractsInDirectory = function () {
it('should register instances in directory', async function () {
const address = this.networkFile.contract('Impl').address;
const _package = await Package.fetch(this.networkFile.package.address);
(await _package.getImplementation(defaultVersion, 'Impl')).should.eq(address);
});
};
const shouldRedeployContracts = function () {
beforeEach('loading previous addresses', function () {
this.previousAddress = this.networkFile.contract('Impl').address
this.withLibraryPreviousAddress = this.networkFile.contract('WithLibraryImpl').address
})
it('should not redeploy contracts if unmodified', async function () {
await push({ networkFile: this.networkFile, network, txParams });
this.networkFile.contract('Impl').address.should.eq(this.previousAddress);
});
it('should redeploy unmodified contract if forced', async function () {
await push({ networkFile: this.networkFile, network, txParams, reupload: true });
this.networkFile.contract('Impl').address.should.not.eq(this.previousAddress);
});
it('should redeploy contracts if modified', async function () {
modifyBytecode.call(this, 'Impl');
await push({ networkFile: this.networkFile, network, txParams });
this.networkFile.contract('Impl').address.should.not.eq(this.previousAddress);
});
it('should redeploy contracts if library is modified', async function () {
modifyLibraryBytecode.call(this, 'UintLib');
await push({ networkFile: this.networkFile, network, txParams });
this.networkFile.contract('WithLibraryImpl').address.should.not.eq(this.withLibraryPreviousAddress);
});
it('should not redeploy contracts if library is unmodified', async function () {
await push({ networkFile: this.networkFile, network, txParams });
this.networkFile.contract('WithLibraryImpl').address.should.eq(this.withLibraryPreviousAddress);
});
it('should refuse to redeploy a contract if storage is incompatible', async function () {
modifyBytecode.call(this, 'Impl');
modifyStorageInfo.call(this, 'Impl');
await push({ networkFile: this.networkFile, network, txParams }).should.be.rejectedWith(/have validation errors/)
this.networkFile.contract('Impl').address.should.eq(this.previousAddress);
});
it('should redeploy contract ignoring warnings', async function () {
modifyBytecode.call(this, 'Impl');
modifyStorageInfo.call(this, 'Impl');
await push({ force: true, networkFile: this.networkFile, network, txParams });
this.networkFile.contract('Impl').address.should.not.eq(this.previousAddress);
});
}
const shouldValidateContracts = function () {
describe('validations', function () {
beforeEach('capturing log output', function () {
this.logs = new CaptureLogs();
});
afterEach(function () {
this.logs.restore();
});
it('should refuse to push a contract with validation error', async function () {
add({ contractsData: ['WithConstructor'], packageFile: this.networkFile.packageFile });
await push({ networkFile: this.networkFile, network, txParams }).should.be.rejectedWith(/One or more contracts have validation errors/i)
this.logs.errors.should.have.lengthOf(1);
this.logs.errors[0].should.match(/constructor/i);
})
it('should push a contract with validation error if forced', async function () {
add({ contractsData: ['WithConstructor'], packageFile: this.networkFile.packageFile });
await push({ networkFile: this.networkFile, network, txParams, force: true });
this.logs.errors.should.have.lengthOf(1);
this.logs.errors[0].should.match(/constructor/i);
const contract = this.networkFile.contract('WithConstructor');
contract.address.should.be.nonzeroAddress;
})
it('should only report new validation errors', async function () {
add({ contractsData: ['WithConstructor'], packageFile: this.networkFile.packageFile });
await push({ networkFile: this.networkFile, network, txParams, force: true });
const previousAddress = this.networkFile.contract('WithConstructor').address;
this.logs.clear();
modifyBytecode.call(this, 'WithConstructor');
await push({ networkFile: this.networkFile, network, txParams});
this.logs.errors.should.have.lengthOf(0);
const contract = this.networkFile.contract('WithConstructor');
contract.address.should.not.eq(previousAddress);
})
it('should only validate modified contracts', async function () {
add({ contractsData: ['WithConstructor'], packageFile: this.networkFile.packageFile });
await push({ networkFile: this.networkFile, network, txParams, force: true });
const previousAddress = this.networkFile.contract('Impl').address;
this.logs.clear();
modifyBytecode.call(this, 'Impl');
await push({ networkFile: this.networkFile, network, txParams});
this.logs.errors.should.have.lengthOf(0);
this.networkFile.contract('Impl').address.should.not.eq(previousAddress);
})
});
}
const shouldBumpVersion = function () {
it('should keep package address when bumping version', async function () {
const previousPackage = this.networkFile.packageAddress
await bumpVersion({ version: '1.2.0', packageFile: this.networkFile.packageFile });
await push({ networkFile: this.networkFile, network, txParams });
this.networkFile.packageAddress.should.eq(previousPackage)
});
it('should update provider address when bumping version', async function () {
await bumpVersion({ version: '1.2.0', packageFile: this.networkFile.packageFile });
await push({ networkFile: this.networkFile, network, txParams });
const _package = await Package.fetch(this.networkFile.package.address);
(await _package.getDirectory('1.2.0')).address.should.eq(this.networkFile.providerAddress);
});
it('should upload contracts to new directory when bumping version', async function () {
await bumpVersion({ version: '1.2.0', packageFile: this.networkFile.packageFile });
await push({ networkFile: this.networkFile, network, txParams });
const implementationAddress = this.networkFile.contract('Impl').address;
const _package = await Package.fetch(this.networkFile.package.address);
(await _package.getImplementation('1.2.0', 'Impl')).should.eq(implementationAddress);
});
it('should set frozen back to false', async function() {
await bumpVersion({ version: '1.1.0', packageFile: this.newNetworkFile.packageFile });
await push({ network, txParams, networkFile: this.newNetworkFile })
await freeze({ network, txParams, networkFile: this.newNetworkFile })
this.newNetworkFile.frozen.should.be.true;
await bumpVersion({ version: '1.2.0', packageFile: this.newNetworkFile.packageFile });
await add({ contractsData: [{ name: 'ImplV1', alias: 'Impl' }], packageFile: this.newNetworkFile.packageFile });
await push({ network, txParams, networkFile: this.newNetworkFile })
this.newNetworkFile.frozen.should.be.false;
});
};
const shouldDeleteContracts = function ({ unregisterFromDirectory }) {
it('should delete contracts', async function () {
await remove({ contracts: ['Impl'], packageFile: this.networkFile.packageFile });
await push({ network, txParams, networkFile: this.networkFile });
if (unregisterFromDirectory) {
const _package = await Package.fetch(this.networkFile.package.address);
(await _package.getImplementation(defaultVersion, 'Impl')).should.be.zeroAddress;
}
should.not.exist(this.networkFile.contract('Impl'));
});
};
const shouldSetDependency = function () {
const libName = 'mock-stdlib';
const libVersion = '1.1.0';
it('should set dependency in deployed app', async function () {
if (!this.networkFile.appAddress) return;
const app = await App.fetch(this.networkFile.appAddress);
const packageInfo = await app.getPackage(libName)
packageInfo.version.should.be.semverEqual(libVersion)
packageInfo.package.address.should.eq(this.dependencyPackage.address)
});
it('should set address and version in network file', async function () {
const dependency = this.networkFile.getDependency(libName)
dependency.version.should.be.semverEqual(libVersion)
dependency.package.should.eq(this.dependencyPackage.address)
});
};
const shouldUpdateDependency = function () {
describe('updating dependency', function () {
const newVersion = '1.2.0';
beforeEach('deploying new dependency version', async function () {
const mockStdlibPackage = new ZosPackageFile('test/mocks/mock-stdlib/zos.json');
mockStdlibPackage.version = newVersion;
sinon.stub(Dependency.prototype, 'getPackageFile').callsFake(() => mockStdlibPackage);
sinon.stub(Dependency.prototype, 'getNpmFile').callsFake(() => ({ version: '1.2.0' }));
await this.dependencyPackage.newVersion(newVersion)
this.dependencyGetNetworkFileStub.callsFake(() => ({ packageAddress: this.dependencyPackage.address, version: newVersion }));
this.networkFile.packageFile.setDependency('mock-stdlib', newVersion);
})
beforeEach('running new push', async function () {
await push({ networkFile: this.networkFile, network, txParams });
})
it('should update dependency to new version in network file', async function () {
const dependency = this.networkFile.getDependency('mock-stdlib');
dependency.package.should.eq(this.dependencyPackage.address);
dependency.version.should.be.semverEqual(newVersion);
});
it('should update dependency to new version in app', async function () {
if (!this.networkFile.appAddress) return;
const app = await App.fetch(this.networkFile.appAddress);
const packageInfo = await app.getPackage('mock-stdlib');
packageInfo.package.address.should.eq(this.dependencyPackage.address);
packageInfo.version.should.be.semverEqual(newVersion);
});
});
};
const shouldNotPushWhileFrozen = function () {
it('should refuse to push when frozen upon modified contracts', async function() {
await freeze({ network, txParams, networkFile: this.networkFile })
modifyBytecode.call(this, 'Impl');
await push({ network, txParams, networkFile: this.networkFile }).should.be.rejectedWith(/frozen/i)
});
it('should refuse to push when frozen upon modified libraries', async function() {
await freeze({ network, txParams, networkFile: this.networkFile })
modifyLibraryBytecode.call(this, 'UintLib');
await push({ network, txParams, networkFile: this.networkFile }).should.be.rejectedWith(/frozen/i)
});
};
const deployingDependency = function () {
beforeEach('deploying dependency', async function () {
const dependency = Dependency.fromNameWithVersion('mock-stdlib@1.1.0')
this.dependencyProject = await dependency.deploy()
this.dependencyPackage = await this.dependencyProject.getProjectPackage()
this.dependencyGetNetworkFileStub = sinon.stub(Dependency.prototype, 'getNetworkFile');
this.dependencyGetNetworkFileStub.callsFake(() => ({ packageAddress: this.dependencyPackage.address, version: '1.1.0' }))
})
afterEach('unstub dependency network file stub', function () {
sinon.restore()
})
};
describe('an empty app', function() {
beforeEach('pushing package-empty', async function () {
const packageFile = new ZosPackageFile('test/mocks/packages/package-empty.zos.json')
this.networkFile = packageFile.networkFile(network)
await push({ network, txParams, networkFile: this.networkFile })
});
shouldDeployApp();
shouldDeployProvider();
});
describe('an app with contracts', function() {
beforeEach('pushing package-with-contracts', async function () {
const packageFile = new ZosPackageFile('test/mocks/packages/package-with-contracts.zos.json')
this.networkFile = packageFile.networkFile(network)
await push({ network, txParams, networkFile: this.networkFile })
const newPackageFile = new ZosPackageFile('test/mocks/packages/package-with-contracts-v2.zos.json')
this.newNetworkFile = newPackageFile.networkFile(network)
});
shouldDeployApp();
shouldDeployProvider();
shouldDeployContracts();
shouldRegisterContractsInDirectory();
shouldRedeployContracts();
shouldValidateContracts();
shouldBumpVersion();
shouldNotPushWhileFrozen();
shouldDeleteContracts({ unregisterFromDirectory: true });
});
describe('an app with invalid contracts', function() {
beforeEach('pushing package-with-invalid-contracts', async function () {
const packageFile = new ZosPackageFile('test/mocks/packages/package-with-invalid-contracts.zos.json')
this.networkFile = packageFile.networkFile(network)
await push({ networkFile: this.networkFile, network, txParams, force: true }).should.be.rejectedWith(/WithFailingConstructor deployment failed/);
});
shouldDeployApp();
shouldDeployProvider();
});
describe('an app with dependency', function () {
deployingDependency();
describe('when using a valid dependency', function () {
beforeEach('pushing package-stdlib', async function () {
const packageFile = new ZosPackageFile('test/mocks/packages/package-with-stdlib.zos.json')
this.networkFile = packageFile.networkFile(network)
await push({ networkFile: this.networkFile, network, txParams })
});
shouldDeployApp();
shouldSetDependency();
shouldUpdateDependency();
})
describe('when using a dependency with a version range', function () {
beforeEach('pushing package-stdlib-range', async function () {
const packageFile = new ZosPackageFile('test/mocks/packages/package-with-stdlib-range.zos.json')
this.networkFile = packageFile.networkFile(network)
await push({ networkFile: this.networkFile, network, txParams })
});
shouldDeployApp();
shouldSetDependency();
shouldUpdateDependency();
})
});
describe('an app with invalid dependency', function () {
describe('when using an invalid dependency', function () {
beforeEach('building network file', async function () {
const packageFile = new ZosPackageFile('test/mocks/packages/package-with-invalid-stdlib.zos.json')
this.networkFile = packageFile.networkFile(network)
});
it('should fail to push', async function () {
await push({ network, txParams, networkFile: this.networkFile })
.should.be.rejectedWith(/requires package version 1.0.0, but 1.1.0 was found/)
});
})
describe('when using an undeployed dependency', function () {
beforeEach('building network file', async function () {
const packageFile = new ZosPackageFile('test/mocks/packages/package-with-undeployed-stdlib.zos.json')
this.networkFile = packageFile.networkFile(network)
});
it('should fail to push', async function () {
await push({ network, txParams, networkFile: this.networkFile })
.should.be.rejectedWith(/Could not find a zos file for network 'test' for 'mock-stdlib-undeployed'/)
});
})
});
describe('an empty lib', function() {
beforeEach('pushing package-empty', async function () {
const packageFile = new ZosPackageFile('test/mocks/packages/package-empty-lib.zos.json')
this.networkFile = packageFile.networkFile(network)
await push({ network, txParams, networkFile: this.networkFile })
});
shouldDeployLib(this.networkFile);
shouldDeployProvider(this.networkFile);
});
describe('a lib with contracts', function() {
beforeEach('pushing package-with-contracts', async function () {
const packageFile = new ZosPackageFile('test/mocks/packages/package-lib-with-contracts.zos.json')
this.networkFile = packageFile.networkFile(network)
await push({ network, txParams, networkFile: this.networkFile })
const newPackageFile = new ZosPackageFile('test/mocks/packages/package-lib-with-contracts-v2.zos.json')
this.newNetworkFile = newPackageFile.networkFile(network)
});
shouldDeployLib();
shouldDeployProvider();
shouldDeployContracts();
shouldRegisterContractsInDirectory();
shouldValidateContracts();
shouldRedeployContracts();
shouldBumpVersion();
shouldNotPushWhileFrozen();
shouldDeleteContracts({ unregisterFromDirectory: true });
});
describe('an empty lightweight app', function() {
beforeEach('pushing package-empty-lite', async function () {
const packageFile = new ZosPackageFile('test/mocks/packages/package-empty-lite.zos.json')
this.networkFile = packageFile.networkFile(network)
});
it('should run push', async function () {
await push({ network, txParams, networkFile: this.networkFile })
});
});
describe('a lightweight app with contracts', function() {
beforeEach('pushing package-with-contracts', async function () {
const packageFile = new ZosPackageFile('test/mocks/packages/package-with-contracts.zos.json')
packageFile.publish = false
this.networkFile = packageFile.networkFile(network)
await push({ network, txParams, networkFile: this.networkFile })
});
shouldDeployContracts();
shouldValidateContracts();
shouldRedeployContracts();
shouldDeleteContracts({ unregisterFromDirectory: false });
it('should not reupload contracts after version bump', async function () {
const previousAddress = this.networkFile.contract('Impl').address
await bumpVersion({ version: '1.2.0', packageFile: this.networkFile.packageFile });
await push({ networkFile: this.networkFile, network, txParams });
this.networkFile.version.should.eq('1.2.0')
this.networkFile.contract('Impl').address.should.eq(previousAddress)
})
});
describe('a lightweight app with dependencies', function() {
deployingDependency();
beforeEach('pushing package-with-stdlib', async function () {
const packageFile = new ZosPackageFile('test/mocks/packages/package-with-stdlib.zos.json')
packageFile.publish = false
this.networkFile = packageFile.networkFile(network)
await push({ network, txParams, networkFile: this.networkFile })
});
shouldSetDependency();
shouldUpdateDependency();
});
});
async function getImplementationFromApp(contractAlias) {
const app = await App.fetch(this.networkFile.appAddress);
return await app.getImplementation(this.networkFile.packageFile.name, contractAlias);
}
function modifyBytecode(contractAlias) {
const contractData = this.networkFile.contract(contractAlias);
this.networkFile.setContract(contractAlias, { ... contractData, localBytecodeHash: '0xabcdef' })
}
function modifyLibraryBytecode(contractAlias) {
const contractData = this.networkFile.solidityLib(contractAlias);
this.networkFile.setSolidityLib(contractAlias, { ... contractData, localBytecodeHash: '0xabcdef' })
}
function modifyStorageInfo(contractAlias) {
const contractData = this.networkFile.contract(contractAlias);
const fakeVariable = {label: 'deleted', type: 't_uint256', contract: 'ImplV1'};
const modifiedStorage = [fakeVariable, ... contractData.storage]
this.networkFile.setContract(contractAlias, { ... contractData, storage: modifiedStorage })
}