Merge remote-tracking branch 'OpenZeppelin/master'

This commit is contained in:
AugustoL
2016-12-29 15:54:19 -03:00
14 changed files with 410 additions and 14 deletions

72
test/DayLimit.js Normal file
View File

@ -0,0 +1,72 @@
contract('DayLimit', function(accounts) {
it('should construct with the passed daily limit', async function() {
let initLimit = 10;
let dayLimit = await DayLimitMock.new(initLimit);
let dailyLimit = await dayLimit.dailyLimit();
assert.equal(initLimit, dailyLimit);
});
it('should be able to spend if daily limit is not reached', async function() {
let limit = 10;
let dayLimit = await DayLimitMock.new(limit);
await dayLimit.attemptSpend(8);
let spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8);
await dayLimit.attemptSpend(2);
spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 10);
});
it('should prevent spending if daily limit is reached', async function() {
let limit = 10;
let dayLimit = await DayLimitMock.new(limit);
await dayLimit.attemptSpend(8);
let spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8);
await dayLimit.attemptSpend(3);
spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8);
});
it('should allow spending if daily limit is reached and then set higher', async function() {
let limit = 10;
let dayLimit = await DayLimitMock.new(limit);
await dayLimit.attemptSpend(8);
let spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8);
await dayLimit.attemptSpend(3);
spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8);
await dayLimit.setDailyLimit(15);
await dayLimit.attemptSpend(3);
spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 11);
});
it('should allow spending if daily limit is reached and then amount spent is reset', async function() {
let limit = 10;
let dayLimit = await DayLimitMock.new(limit);
await dayLimit.attemptSpend(8);
let spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8);
await dayLimit.attemptSpend(3);
spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8);
await dayLimit.resetSpentToday(15);
await dayLimit.attemptSpend(3);
spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 3);
});
});

163
test/MultisigWallet.js Normal file
View File

@ -0,0 +1,163 @@
contract('MultisigWallet', function(accounts) {
//from https://gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6
web3.eth.getTransactionReceiptMined = function (txnHash, interval) {
var transactionReceiptAsync;
interval = interval ? interval : 500;
transactionReceiptAsync = function(txnHash, resolve, reject) {
try {
var receipt = web3.eth.getTransactionReceipt(txnHash);
if (receipt == null) {
setTimeout(function () {
transactionReceiptAsync(txnHash, resolve, reject);
}, interval);
} else {
resolve(receipt);
}
} catch(e) {
reject(e);
}
};
if (Array.isArray(txnHash)) {
var promises = [];
txnHash.forEach(function (oneTxHash) {
promises.push(web3.eth.getTransactionReceiptMined(oneTxHash, interval));
});
return Promise.all(promises);
} else {
return new Promise(function (resolve, reject) {
transactionReceiptAsync(txnHash, resolve, reject);
});
}
};
it('should send balance to passed address upon death', async function() {
//Give account[0] 20 ether
web3.eth.sendTransaction({from: web3.eth.coinbase, to: accounts[0], value: web3.toWei('20','ether')}, function(err, result) {
if(err)
console.log("ERROR:" + err);
});
let dailyLimit = 10;
let ownersRequired = 2;
//Create MultisigWallet contract with 10 ether
let wallet = await MultisigWalletMock.new(accounts, ownersRequired, dailyLimit, {value: web3.toWei('10', 'ether')});
//Get balances of owner and wallet after wallet creation.
let ownerBalance = web3.eth.getBalance(accounts[0]);
let walletBalance = web3.eth.getBalance(wallet.address);
let hash = 1234;
//Call kill function from two different owner accounts, satisfying owners required
await wallet.kill(accounts[0], {data: hash});
let txnHash = await wallet.kill(accounts[0], {from: accounts[1], data: hash});
let receiptMined = await web3.eth.getTransactionReceiptMined(txnHash);
//Get balances of owner and wallet after kill function is complete, compare with previous values
let newOwnerBalance = web3.eth.getBalance(accounts[0]);
let newWalletBalance = web3.eth.getBalance(wallet.address);
assert.isTrue(newOwnerBalance > ownerBalance);
assert.isTrue(newWalletBalance < walletBalance);
});
it('should execute transaction if below daily limit', async function() {
//Give account[0] 20 ether
web3.eth.sendTransaction({from: web3.eth.coinbase, to: accounts[0], value: web3.toWei('20','ether')}, function(err, result) {
if(err)
console.log("ERROR:" + err);
});
let dailyLimit = 10;
let ownersRequired = 2;
//Create MultisigWallet contract with 10 ether
let wallet = await MultisigWalletMock.new(accounts, ownersRequired, dailyLimit, {value: web3.toWei('10', 'ether')});
let accountBalance = web3.eth.getBalance(accounts[2]);
let hash = 1234;
//Owner account0 commands wallet to send 9 wei to account2
let txnHash = await wallet.execute(accounts[2], 9, hash);
let receiptMined = await web3.eth.getTransactionReceiptMined(txnHash);
//Balance of account2 should have increased
let newAccountBalance = web3.eth.getBalance(accounts[2]);
assert.isTrue(newAccountBalance > accountBalance);
});
it('should prevent execution of transaction if above daily limit', async function() {
//Give account[0] 20 ether
web3.eth.sendTransaction({from: web3.eth.coinbase, to: accounts[0], value: web3.toWei('20','ether')}, function(err, result) {
if(err)
console.log("ERROR:" + err);
});
let dailyLimit = 10;
let ownersRequired = 2;
//Create MultisigWallet contract with 10 ether
let wallet = await MultisigWalletMock.new(accounts, ownersRequired, dailyLimit, {value: web3.toWei('10', 'ether')});
let accountBalance = web3.eth.getBalance(accounts[2]);
let hash = 1234;
//Owner account0 commands wallet to send 9 wei to account2
let txnHash = await wallet.execute(accounts[2], 9, hash);
let receiptMined = await web3.eth.getTransactionReceiptMined(txnHash);
//Balance of account2 should have increased
let newAccountBalance = web3.eth.getBalance(accounts[2]);
assert.isTrue(newAccountBalance > accountBalance);
accountBalance = newAccountBalance;
hash = 4567;
//Owner account0 commands wallet to send 2 more wei to account2, going over the daily limit of 10
txnHash = await wallet.execute(accounts[2], 2, hash);
receiptMined = await web3.eth.getTransactionReceiptMined(txnHash);
//Balance of account2 should not change
newAccountBalance = web3.eth.getBalance(accounts[2]);
assert.equal(newAccountBalance.toString(), accountBalance.toString());
});
it('should execute transaction if above daily limit and enough owners approve', async function() {
//Give account[0] 20 ether
web3.eth.sendTransaction({from: web3.eth.coinbase, to: accounts[0], value: web3.toWei('20','ether')}, function(err, result) {
if(err)
console.log("ERROR:" + err);
});
let dailyLimit = 10;
let ownersRequired = 2;
//Create MultisigWallet contract with 10 ether
let wallet = await MultisigWalletMock.new(accounts, ownersRequired, dailyLimit, {value: web3.toWei('10', 'ether')});
let accountBalance = web3.eth.getBalance(accounts[2]);
let hash = 1234;
//Owner account0 commands wallet to send 11 wei to account2
let txnHash = await wallet.execute(accounts[2], 11, hash);
let receiptMined = await web3.eth.getTransactionReceiptMined(txnHash);
//Balance of account2 should not change
let newAccountBalance = web3.eth.getBalance(accounts[2]);
assert.equal(newAccountBalance.toString(), accountBalance.toString());
accountBalance = newAccountBalance;
//Owner account1 commands wallet to send 11 wei to account2
txnHash = await wallet.execute(accounts[2], 2, hash);
receiptMined = await web3.eth.getTransactionReceiptMined(txnHash);
//Balance of account2 should change
newAccountBalance = web3.eth.getBalance(accounts[2]);
assert.isTrue(newAccountBalance > accountBalance);
});
});

101
test/Shareable.js Normal file
View File

@ -0,0 +1,101 @@
contract('Shareable', function(accounts) {
it('should construct with correct owners and number of sigs required', async function() {
let requiredSigs = 2;
let owners = accounts.slice(1,4);
let shareable = await ShareableMock.new(owners, requiredSigs);
let required = await shareable.required();
assert.equal(required, requiredSigs);
let owner = await shareable.getOwner(0);
assert.equal(owner, accounts[0]);
for(let i = 0; i < accounts.length; i++) {
let owner = await shareable.getOwner(i);
let isowner = await shareable.isOwner(accounts[i]);
if(i <= owners.length) {
assert.equal(accounts[i], owner);
assert.isTrue(isowner);
} else {
assert.notEqual(accounts[i], owner);
assert.isFalse(isowner);
}
}
});
it('should only perform multisig function with enough sigs', async function() {
let requiredSigs = 3;
let owners = accounts.slice(1,4);
let shareable = await ShareableMock.new(owners, requiredSigs);
let hash = 1234;
let initCount = await shareable.count();
initCount = initCount.toString();
for(let i = 0; i < requiredSigs; i++) {
await shareable.increaseCount(hash, {from: accounts[i]});
let count = await shareable.count();
if(i == requiredSigs - 1) {
assert.equal(Number(initCount)+1, count.toString());
} else {
assert.equal(initCount, count.toString());
}
}
});
it('should require approval from different owners', async function() {
let requiredSigs = 2;
let owners = accounts.slice(1,4);
let shareable = await ShareableMock.new(owners, requiredSigs);
let hash = 1234;
let initCount = await shareable.count();
initCount = initCount.toString();
//Count shouldn't increase when the same owner calls repeatedly
for(let i = 0; i < 2; i++) {
await shareable.increaseCount(hash);
let count = await shareable.count();
assert.equal(initCount, count.toString());
}
});
it('should reset sig count after operation is approved', async function() {
let requiredSigs = 3;
let owners = accounts.slice(1,4);
let shareable = await ShareableMock.new(owners, requiredSigs);
let hash = 1234;
let initCount = await shareable.count();
for(let i = 0; i < requiredSigs * 3; i++) {
await shareable.increaseCount(hash, {from: accounts[i % 4]});
let count = await shareable.count();
if((i%(requiredSigs)) == requiredSigs - 1) {
initCount = Number(initCount)+1;
assert.equal(initCount, count);
} else {
assert.equal(initCount.toString(), count);
}
}
});
it('should not perform multisig function after an owner revokes', async function() {
let requiredSigs = 3;
let owners = accounts.slice(1,4);
let shareable = await ShareableMock.new(owners, requiredSigs);
let hash = 1234;
let initCount = await shareable.count();
for(let i = 0; i < requiredSigs; i++) {
if(i == 1) {
await shareable.revoke(hash, {from: accounts[i-1]});
}
await shareable.increaseCount(hash, {from: accounts[i]});
let count = await shareable.count();
assert.equal(initCount.toString(), count);
}
});
});