Skip to content

Commit 232db52

Browse files
authored
Testing rebase invocation from constructor (#146)
* additional check for rebase tx origin * code review fix
1 parent d205660 commit 232db52

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
pragma solidity 0.4.24;
2+
3+
import "../UFragmentsPolicy.sol";
4+
5+
6+
contract ConstructorRebaseCallerContract {
7+
constructor(address policy) public {
8+
// Take out a flash loan.
9+
// Do something funky...
10+
UFragmentsPolicy(policy).rebase(); // should fail
11+
// pay back flash loan.
12+
}
13+
}

contracts/mocks/RebaseCallerContract.sol

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,11 @@ import "../UFragmentsPolicy.sol";
44

55

66
contract RebaseCallerContract {
7-
UFragmentsPolicy private _policy;
8-
9-
constructor(address policy) public {
10-
_policy = UFragmentsPolicy(policy);
11-
}
12-
13-
function callRebase() public returns (bool) {
7+
function callRebase(address policy) public returns (bool) {
148
// Take out a flash loan.
159
// Do something funky...
16-
_policy.rebase(); // should fail
10+
UFragmentsPolicy(policy).rebase(); // should fail
1711
// pay back flash loan.
18-
1912
return true;
2013
}
2114
}

test/unit/UFragmentsPolicy.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const UFragmentsPolicy = artifacts.require('UFragmentsPolicy.sol');
22
const MockUFragments = artifacts.require('MockUFragments.sol');
33
const MockOracle = artifacts.require('MockOracle.sol');
44
const RebaseCallerContract = artifacts.require('RebaseCallerContract.sol');
5+
const ConstructorRebaseCallerContract = artifacts.require('ConstructorRebaseCallerContract.sol');
56

67
const encodeCall = require('zos-lib/lib/helpers/encodeCall').default;
78
const BigNumber = web3.BigNumber;
@@ -13,7 +14,7 @@ require('chai')
1314
.use(require('chai-bignumber')(BigNumber))
1415
.should();
1516

16-
let uFragmentsPolicy, mockUFragments, mockMarketOracle, mockCpiOracle, rebaseCallerContract;
17+
let uFragmentsPolicy, mockUFragments, mockMarketOracle, mockCpiOracle;
1718
let r, prevEpoch, prevTime;
1819
let deployer, user;
1920

@@ -46,7 +47,6 @@ async function setupContracts () {
4647
});
4748
await uFragmentsPolicy.setMarketOracle(mockMarketOracle.address);
4849
await uFragmentsPolicy.setCpiOracle(mockCpiOracle.address);
49-
rebaseCallerContract = await RebaseCallerContract.new(uFragmentsPolicy.address);
5050
}
5151

5252
async function setupContractsWithOpenRebaseWindow () {
@@ -271,7 +271,7 @@ contract('UFragments:setRebaseTimingParameters:accessControl', function (account
271271
});
272272

273273
contract('UFragmentsPolicy:Rebase:accessControl', async function (accounts) {
274-
before('setup UFragmentsPolicy contract', async function () {
274+
beforeEach('setup UFragmentsPolicy contract', async function () {
275275
await setupContractsWithOpenRebaseWindow();
276276
await mockExternalData(INITIAL_RATE_30P_MORE, INITIAL_CPI, 1000, true);
277277
await chain.waitForSomeTime(60);
@@ -286,9 +286,18 @@ contract('UFragmentsPolicy:Rebase:accessControl', async function (accounts) {
286286
});
287287

288288
describe('when rebase called by a contract', function () {
289+
it('should fail', async function () {
290+
const rebaseCallerContract = await RebaseCallerContract.new();
291+
expect(
292+
await chain.isEthException(rebaseCallerContract.callRebase(uFragmentsPolicy.address))
293+
).to.be.true;
294+
});
295+
});
296+
297+
describe('when rebase called by a contract which is being constructed', function () {
289298
it('should fail', async function () {
290299
expect(
291-
await chain.isEthException(rebaseCallerContract.callRebase())
300+
await chain.isEthException(ConstructorRebaseCallerContract.new(uFragmentsPolicy.address))
292301
).to.be.true;
293302
});
294303
});

0 commit comments

Comments
 (0)