Commit 3d6988cf by Fabio Berger

Update modifiers so that they fail "loudly" by throwing errors rather then…

Update modifiers so that they fail "loudly" by throwing errors rather then silently no-oping. Updated tests to remain compatible with these changes
parent c10a2cf1
...@@ -58,8 +58,9 @@ contract DayLimit { ...@@ -58,8 +58,9 @@ contract DayLimit {
// simple modifier for daily limit. // simple modifier for daily limit.
modifier limitedDaily(uint _value) { modifier limitedDaily(uint _value) {
if (underLimit(_value)) { if (!underLimit(_value)) {
_; throw;
} }
_;
} }
} }
...@@ -13,8 +13,10 @@ contract Claimable is Ownable { ...@@ -13,8 +13,10 @@ contract Claimable is Ownable {
address public pendingOwner; address public pendingOwner;
modifier onlyPendingOwner() { modifier onlyPendingOwner() {
if (msg.sender == pendingOwner) if (msg.sender != pendingOwner) {
_; throw;
}
_;
} }
function transferOwnership(address newOwner) onlyOwner { function transferOwnership(address newOwner) onlyOwner {
......
...@@ -15,9 +15,10 @@ contract Ownable { ...@@ -15,9 +15,10 @@ contract Ownable {
} }
modifier onlyOwner() { modifier onlyOwner() {
if (msg.sender == owner) { if (msg.sender != owner) {
_; throw;
} }
_;
} }
function transferOwnership(address newOwner) onlyOwner { function transferOwnership(address newOwner) onlyOwner {
......
...@@ -39,9 +39,10 @@ contract Shareable { ...@@ -39,9 +39,10 @@ contract Shareable {
// simple single-sig function modifier. // simple single-sig function modifier.
modifier onlyOwner { modifier onlyOwner {
if (isOwner(msg.sender)) { if (!isOwner(msg.sender)) {
_; throw;
} }
_;
} }
// multi-sig function modifier: the operation must have an intrinsic hash in order // multi-sig function modifier: the operation must have an intrinsic hash in order
......
'use strict'; 'use strict';
const assertJump = require('./helpers/assertJump');
var Claimable = artifacts.require('../contracts/ownership/Claimable.sol'); var Claimable = artifacts.require('../contracts/ownership/Claimable.sol');
...@@ -23,17 +24,22 @@ contract('Claimable', function(accounts) { ...@@ -23,17 +24,22 @@ contract('Claimable', function(accounts) {
}); });
it('should prevent to claimOwnership from no pendingOwner', async function() { it('should prevent to claimOwnership from no pendingOwner', async function() {
claimable.claimOwnership({from: accounts[2]}); try {
let owner = await claimable.owner(); await claimable.claimOwnership({from: accounts[2]});
} catch(error) {
assert.isTrue(owner !== accounts[2]); assertJump(error);
}
}); });
it('should prevent non-owners from transfering', async function() { it('should prevent non-owners from transfering', async function() {
await claimable.transferOwnership(accounts[2], {from: accounts[2]}); const other = accounts[2];
let pendingOwner = await claimable.pendingOwner(); const owner = await claimable.owner.call();
assert.isTrue(owner !== other);
assert.isFalse(pendingOwner === accounts[2]); try {
await claimable.transferOwnership(other, {from: other});
} catch(error) {
assertJump(error);
}
}); });
describe('after initiating a transfer', function () { describe('after initiating a transfer', function () {
......
'use strict'; 'use strict';
const assertJump = require('./helpers/assertJump');
var DayLimitMock = artifacts.require('helpers/DayLimitMock.sol'); var DayLimitMock = artifacts.require('helpers/DayLimitMock.sol');
...@@ -32,9 +33,11 @@ contract('DayLimit', function(accounts) { ...@@ -32,9 +33,11 @@ contract('DayLimit', function(accounts) {
let spentToday = await dayLimit.spentToday(); let spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8); assert.equal(spentToday, 8);
await dayLimit.attemptSpend(3); try {
spentToday = await dayLimit.spentToday(); await dayLimit.attemptSpend(3);
assert.equal(spentToday, 8); } catch(error) {
assertJump(error);
}
}); });
it('should allow spending if daily limit is reached and then set higher', async function() { it('should allow spending if daily limit is reached and then set higher', async function() {
...@@ -45,7 +48,11 @@ contract('DayLimit', function(accounts) { ...@@ -45,7 +48,11 @@ contract('DayLimit', function(accounts) {
let spentToday = await dayLimit.spentToday(); let spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8); assert.equal(spentToday, 8);
await dayLimit.attemptSpend(3); try {
await dayLimit.attemptSpend(3);
} catch(error) {
assertJump(error);
}
spentToday = await dayLimit.spentToday(); spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8); assert.equal(spentToday, 8);
...@@ -63,7 +70,11 @@ contract('DayLimit', function(accounts) { ...@@ -63,7 +70,11 @@ contract('DayLimit', function(accounts) {
let spentToday = await dayLimit.spentToday(); let spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8); assert.equal(spentToday, 8);
await dayLimit.attemptSpend(3); try {
await dayLimit.attemptSpend(3);
} catch(error) {
assertJump(error);
}
spentToday = await dayLimit.spentToday(); spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8); assert.equal(spentToday, 8);
......
'use strict'; 'use strict';
const assertJump = require('./helpers/assertJump');
var Ownable = artifacts.require('../contracts/ownership/Ownable.sol'); var Ownable = artifacts.require('../contracts/ownership/Ownable.sol');
...@@ -23,11 +24,14 @@ contract('Ownable', function(accounts) { ...@@ -23,11 +24,14 @@ contract('Ownable', function(accounts) {
}); });
it('should prevent non-owners from transfering', async function() { it('should prevent non-owners from transfering', async function() {
let other = accounts[2]; const other = accounts[2];
await ownable.transferOwnership(other, {from: accounts[2]}); const owner = await ownable.owner.call();
let owner = await ownable.owner(); assert.isTrue(owner !== other);
try {
assert.isFalse(owner === other); await ownable.transferOwnership(other, {from: other});
} catch(error) {
assertJump(error);
}
}); });
it('should guard ownership against stuck state', async function() { it('should guard ownership against stuck state', async function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment