Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
Support solidity 0.6 in Initializable contract
Browse files Browse the repository at this point in the history
Modifies Initializable so it is compatible with Solidity 0.5 and 0.6. Adds a set of tests that run on the 0.6 version to verify its behaviour (note that the 0.6 compiled contracts are committed into source control to simplify the setup).
  • Loading branch information
spalladino committed Jan 28, 2020
1 parent 8b23a74 commit 65993b0
Show file tree
Hide file tree
Showing 16 changed files with 11,532 additions and 73 deletions.
10 changes: 5 additions & 5 deletions packages/cli/test/utils/solidity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ require('../setup');

import { getImports } from '../../src/utils/solidity';

describe('utils.solidity', function () {
describe('getImports', function () {
it('returns no imports from empty file', function () {
describe('utils.solidity', function() {
describe('getImports', function() {
it('returns no imports from empty file', function() {
getImports('').should.be.empty;
});

it.skip('returns no imports from commented out file', function () {
it.skip('returns no imports from commented out file', function() {
getImports(`// import "Foo.sol";`).should.be.empty;
});
});
});
});
5 changes: 3 additions & 2 deletions packages/lib/contracts/Initializable.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity >=0.4.24 <0.6.0;
pragma solidity >=0.4.24 <0.7.0;


/**
Expand Down Expand Up @@ -51,8 +51,9 @@ contract Initializable {
// deployed when running a constructor, any checks on its code size will
// yield zero, making it an effective way to detect if a contract is
// under construction or not.
address self = address(this);
uint256 cs;
assembly { cs := extcodesize(address) }
assembly { cs := extcodesize(self) }
return cs == 0;
}

Expand Down
120 changes: 63 additions & 57 deletions packages/lib/test/contracts/Initializable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,81 +6,87 @@ import assertRevert from '../../src/test/helpers/assertRevert';

import { assert } from 'chai';

const InitializableMock = Contracts.getFromLocal('InitializableMock');
const SampleMother = Contracts.getFromLocal('SampleMother');
const SampleGramps = Contracts.getFromLocal('SampleGramps');
const SampleFather = Contracts.getFromLocal('SampleFather');
const SampleChild = Contracts.getFromLocal('SampleChild');

describe('Initializable', function() {
describe('basic testing without inheritance', function() {
beforeEach('deploying', async function() {
this.contract = await InitializableMock.new();
});
testInitializable(`Initializable on solc 0.5`, {
InitializableMock: Contracts.getFromLocal('InitializableMock'),
SampleChild: Contracts.getFromLocal('SampleChild'),
});

context('before initialize', function() {
it('initializer has not run', async function() {
assert.isFalse(await this.contract.methods.initializerRan().call());
});
});
testInitializable(`Initializable on solc 0.6`, {
InitializableMock: Contracts.getFromNodeModules('mock-solc-0.6', 'InitializableMock'),
SampleChild: Contracts.getFromNodeModules('mock-solc-0.6', 'SampleChild'),
});

context('after initialize', function() {
beforeEach('initializing', async function() {
await this.contract.methods.initialize().send();
function testInitializable(description, { InitializableMock, SampleChild }) {
describe(description, function() {
describe('basic testing without inheritance', function() {
beforeEach('deploying', async function() {
this.contract = await InitializableMock.new();
});

it('initializer has run', async function() {
assert.isTrue(await this.contract.methods.initializerRan().call());
context('before initialize', function() {
it('initializer has not run', async function() {
assert.isFalse(await this.contract.methods.initializerRan().call());
});
});

it('initializer does not run again', async function() {
await assertRevert(this.contract.methods.initialize().send());
});
});
context('after initialize', function() {
beforeEach('initializing', async function() {
await this.contract.methods.initialize().send();
});

context('after nested initialize', function() {
beforeEach('initializing', async function() {
await this.contract.methods.initializeNested().send();
it('initializer has run', async function() {
assert.isTrue(await this.contract.methods.initializerRan().call());
});

it('initializer does not run again', async function() {
await assertRevert(this.contract.methods.initialize().send());
});
});

it('initializer has run', async function() {
assert.isTrue(await this.contract.methods.initializerRan().call());
context('after nested initialize', function() {
beforeEach('initializing', async function() {
await this.contract.methods.initializeNested().send();
});

it('initializer has run', async function() {
assert.isTrue(await this.contract.methods.initializerRan().call());
});
});
});
});

describe('complex testing with inheritance', function() {
const mother = 12;
const gramps = 56;
const father = 34;
const child = 78;
describe('complex testing with inheritance', function() {
const mother = 12;
const gramps = 56;
const father = 34;
const child = 78;

beforeEach('deploying', async function() {
this.contract = await SampleChild.new();
});
beforeEach('deploying', async function() {
this.contract = await SampleChild.new();
});

beforeEach('initializing', async function() {
await this.contract.methods.initialize(mother, gramps, father, child).send();
});
beforeEach('initializing', async function() {
await this.contract.methods.initialize(mother, gramps, father, child).send();
});

it('initializes human', async function() {
assert.equal(await this.contract.methods.isHuman().call(), true);
});
it('initializes human', async function() {
assert.equal(await this.contract.methods.isHuman().call(), true);
});

it('initializes mother', async function() {
assert.equal(await this.contract.methods.mother().call(), mother);
});
it('initializes mother', async function() {
assert.equal(await this.contract.methods.mother().call(), mother);
});

it('initializes gramps', async function() {
assert.equal(await this.contract.methods.gramps().call(), gramps);
});
it('initializes gramps', async function() {
assert.equal(await this.contract.methods.gramps().call(), gramps);
});

it('initializes father', async function() {
assert.equal(await this.contract.methods.father().call(), father);
});
it('initializes father', async function() {
assert.equal(await this.contract.methods.father().call(), father);
});

it('initializes child', async function() {
assert.equal(await this.contract.methods.child().call(), child);
it('initializes child', async function() {
assert.equal(await this.contract.methods.child().call(), child);
});
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
"telemetryOptIn": false,
"compiler": {
"manager": "openzeppelin",
"solcVersion": "0.6.0",
"solcVersion": "0.6.2",
"compilerSettings": {
"optimizer": {
"enabled": false,
"runs": "200"
}
}
},
"artifactsDir": "build/contracts",
"contractsDir": "contracts",
"typechain": {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -653,11 +653,11 @@
],
"src": "0:462:0"
},
"bytecode": "0x6080604052348015600f57600080fd5b5060818061001e6000396000f3fe60806040526004361060265760003560e01c806310c82ddf14602b578063f77df40a14603f575b600080fd5b348015603657600080fd5b50603d6047565b005b60456049565b005b565b56fea26469706673582212201883a9831b1f8f6f26caaa345bbf964728ba05ae549373d0f00a92006fd05bff64736f6c63430006000033",
"deployedBytecode": "0x60806040526004361060265760003560e01c806310c82ddf14602b578063f77df40a14603f575b600080fd5b348015603657600080fd5b50603d6047565b005b60456049565b005b565b56fea26469706673582212201883a9831b1f8f6f26caaa345bbf964728ba05ae549373d0f00a92006fd05bff64736f6c63430006000033",
"bytecode": "0x6080604052348015600f57600080fd5b5060818061001e6000396000f3fe60806040526004361060265760003560e01c806310c82ddf14602b578063f77df40a14603f575b600080fd5b348015603657600080fd5b50603d6047565b005b60456049565b005b565b56fea2646970667358221220b27de1792583c12c3ac075a9c535f5d05aa936d0924b6652561ab809b1252a4064736f6c63430006020033",
"deployedBytecode": "0x60806040526004361060265760003560e01c806310c82ddf14602b578063f77df40a14603f575b600080fd5b348015603657600080fd5b50603d6047565b005b60456049565b005b565b56fea2646970667358221220b27de1792583c12c3ac075a9c535f5d05aa936d0924b6652561ab809b1252a4064736f6c63430006020033",
"compiler": {
"name": "solc",
"version": "0.6.0+commit.26b70077.Emscripten.clang",
"version": "0.6.2+commit.bacdbe57.Linux.g++",
"optimizer": {
"enabled": false,
"runs": 200
Expand Down
Loading

0 comments on commit 65993b0

Please sign in to comment.