From 6d8c259f1dd8ec391b3568ac5989b74729085ed0 Mon Sep 17 00:00:00 2001 From: perissology Date: Mon, 23 Oct 2017 14:25:06 -0700 Subject: [PATCH] added tests for delegations --- package.json | 4 +- test/DelegationChain.js | 151 +++++++++++++++++++++++++++++++++++++ test/NormalOperation.js | 55 +++++++------- test/VetoDelegation.js | 6 +- test/helpers/assertFail.js | 2 +- 5 files changed, 187 insertions(+), 31 deletions(-) create mode 100644 test/DelegationChain.js diff --git a/package.json b/package.json index 7068705..7b8e50a 100644 --- a/package.json +++ b/package.json @@ -39,12 +39,12 @@ "random-bytes": "^1.0.0", "mocha": "^3.5.0", "solcpiler": "0.0.4", - "web3": "git://github.com/perissology/web3.js.git#all_fixes" + "web3": "1.0.0-beta.24" }, "homepage": "https://github.com/Giveth/liquidpledging#readme", "dependencies": { "async": "^2.4.0", "chai": "^4.1.0", - "eth-contract-class": "0.0.4" + "eth-contract-class": "0.0.6" } } diff --git a/test/DelegationChain.js b/test/DelegationChain.js new file mode 100644 index 0000000..ead93d0 --- /dev/null +++ b/test/DelegationChain.js @@ -0,0 +1,151 @@ +/* eslint-env mocha */ +/* eslint-disable no-await-in-loop */ +const TestRPC = require('ethereumjs-testrpc'); +const Web3 = require('web3'); +const chai = require('chai'); +const liquidpledging = require('../index.js'); + +const LiquidPledging = liquidpledging.LiquidPledgingMock; +const LiquidPledgingState = liquidpledging.LiquidPledgingState; +const Vault = liquidpledging.Vault; +const assertFail = require('./helpers/assertFail'); +const assert = chai.assert; + +const printState = async (liquidPledgingState) => { + const st = await liquidPledgingState.getState(); + console.log(JSON.stringify(st, null, 2)); +}; + +describe('LiquidPledging test', function () { + this.timeout(0); + + let testrpc; + let web3; + let accounts; + let liquidPledging; + let liquidPledgingState; + let vault; + let giver1; + let giver2; + let delegate1; + let delegate2; + let delegate3; + let adminProject1; + + before(async () => { + testrpc = TestRPC.server({ + ws: true, + gasLimit: 5800000, + total_accounts: 10, + }); + + testrpc.listen(8546, '127.0.0.1'); + + web3 = new Web3('ws://localhost:8546'); + accounts = await web3.eth.getAccounts(); + giver1 = accounts[1]; + delegate1 = accounts[2]; + delegate2 = accounts[3]; + delegate3 = accounts[4]; + adminProject1 = accounts[5]; + giver2 = accounts[6]; + }); + + after((done) => { + testrpc.close(); + done(); + }); + + it('Should deploy LiquidPledging contract', async () => { + vault = await Vault.new(web3); + liquidPledging = await LiquidPledging.new(web3, vault.$address, { gas: 5800000 }); + await vault.setLiquidPledging(liquidPledging.$address); + liquidPledgingState = new LiquidPledgingState(liquidPledging); + }); + + it('Should add pledgeAdmins', async () => { + await liquidPledging.addGiver('Giver1', 'URLGiver1', 0, 0, { from: giver1 }); // pledgeAdmin 1 + await liquidPledging.addDelegate('Delegate1', 'URLDelegate1', 0, 0, { from: delegate1 }); // pledgeAdmin 2 + await liquidPledging.addDelegate('Delegate2', 'URLDelegate2', 0, 0, { from: delegate2 }); // pledgeAdmin 3 + await liquidPledging.addDelegate('Delegate3', 'URLDelegate3', 0, 0, { from: delegate3 }); // pledgeAdmin 4 + await liquidPledging.addProject('Project1', 'URLProject1', adminProject1, 0, 0, 0, { from: adminProject1 }); // pledgeAdmin 5 + await liquidPledging.addGiver('Giver2', 'URLGiver2', 0, 0, { from: giver2 }); // pledgeAdmin 6 + + const nAdmins = await liquidPledging.numberOfPledgeAdmins(); + assert.equal(nAdmins, 6); + }); + + it('Should allow previous delegate to transfer pledge', async () => { + await liquidPledging.donate(1, 2, {from: giver1, value: 1000}); + // add delegate2 to chain + await liquidPledging.transfer(2, 2, 1000, 3, {from: delegate1}); + // delegate 1 transfer pledge back to self, thus undelegating delegate2 + await liquidPledging.transfer(2, 3, 1000, 2, {from: delegate1, gas: 100000}); + + const st = await liquidPledgingState.getState(); + assert.equal(st.pledges[2].amount, 1000); + assert.equal(st.pledges[3].amount, 0); + }); + + it('Should allow any delegate in chain to transfer pledge and undelegate all delegates occurring later in chain', async () => { + // add delegate2 to chain + await liquidPledging.transfer(2, 2, 1000, 3, {from: delegate1, $extraGas: 100000}); + // add delegate3 to chain + await liquidPledging.transfer(3, 3, 1000, 4, {from: delegate2, $extraGas: 100000}); + // delegate 1 transfer pledge to project1. should also undelegate all delegates occurring later in chain + await liquidPledging.transfer(2, 4, 1000, 5, {from: delegate1, $extraGas: 200000}); + + const st = await liquidPledgingState.getState(); + assert.equal(st.pledges[5].amount, 1000); + assert.equal(st.pledges[5].intendedProject, 5); + assert.equal(st.pledges[5].delegates.length, 1); + assert.equal(st.pledges[5].delegates[0].id, 2); + assert.equal(st.pledges[3].amount, 0); + assert.equal(st.pledges[4].amount, 0); + }); + + it('Should throw if delegate2 is not in delegationChain', async () => { + await assertFail(async () => await liquidPledging.transfer(3, 5, 1000, 1, {from: delegate2})); + }); + + it('Delegate1 should not be able to transfer to another giver', async () => { + await assertFail(async () => await liquidPledging.transfer(2, 5, 1000, 6, {from: delegate1})); + }); + + it('Delegate1 should be able to transfer pledge back to owner', async () => { + await liquidPledging.transfer(2, 5, 1000, 1, {from: delegate1, $extraGas: 100000}); + const st = await liquidPledgingState.getState(); + assert.equal(st.pledges[1].amount, 1000); + assert.equal(st.pledges[1].delegates.length, 0); + assert.equal(st.pledges[5].amount, 0); + }); + + it('Delegate1 should be able to change delegation', async () => { + // add delegate1 to chain + await liquidPledging.transfer(1, 1, 1000, 2, {from: giver1, $extraGas: 100000}); + // delegate1 add delegate2 to chain + await liquidPledging.transfer(2, 2, 1000, 3, {from: delegate1, $extraGas: 100000}); + // delegate1 remove delegate2 and add delegate3 to chain + await liquidPledging.transfer(2, 3, 1000, 4, {from: delegate1, $extraGas: 100000}); + + const st = await liquidPledgingState.getState(); + assert.equal(st.pledges[1].amount, 0); + assert.equal(st.pledges[6].amount, 1000); + assert.equal(st.pledges[6].delegates.length, 2); + assert.equal(st.pledges[6].delegates[0].id, 2); + assert.equal(st.pledges[6].delegates[1].id, 4); + }); + + it('delegate in chain should be able to delegate to previous delegate, thus undelegating themselves and any delegate after the previous delegate', async () => { + // add delegate2 to chain + await liquidPledging.transfer(4, 6, 1000, 3, {from: delegate3, $extraGas: 100000}); + // delegate2 transfer back to delegate1, thus undelegating delegate2 & delegate3 + await liquidPledging.transfer(3, 7, 1000, 2, {from: delegate2, $extraGas: 100000}); + + const st = await liquidPledgingState.getState(); + assert.equal(st.pledges[7].amount, 0); + assert.equal(st.pledges[2].amount, 1000); + assert.equal(st.pledges[2].delegates.length, 1); + assert.equal(st.pledges[2].delegates[0].id, 2); + }); +}); diff --git a/test/NormalOperation.js b/test/NormalOperation.js index 719b4d3..5ccd49c 100644 --- a/test/NormalOperation.js +++ b/test/NormalOperation.js @@ -19,7 +19,9 @@ const printState = async (liquidPledgingState) => { console.log(JSON.stringify(st, null, 2)); }; -describe('LiquidPledging test', () => { +describe('LiquidPledging test', function () { + this.timeout(0); + let testrpc; let web3; let accounts; @@ -59,12 +61,13 @@ describe('LiquidPledging test', () => { testrpc.close(); done(); }); + it('Should deploy LiquidPledging contract', async () => { vault = await Vault.new(web3); liquidPledging = await LiquidPledging.new(web3, vault.$address, { gas: 5800000 }); await vault.setLiquidPledging(liquidPledging.$address); liquidPledgingState = new LiquidPledgingState(liquidPledging); - }).timeout(6000); + }); it('Should create a giver', async () => { await liquidPledging.addGiver('Giver1', 'URLGiver1', 86400, 0, { from: giver1 }); const nAdmins = await liquidPledging.numberOfPledgeAdmins(); @@ -75,13 +78,13 @@ describe('LiquidPledging test', () => { assert.equal(res[2], 'Giver1'); assert.equal(res[3], 'URLGiver1'); assert.equal(res[4], 86400); - }).timeout(6000); + }); it('Should make a donation', async () => { await liquidPledging.donate(1, 1, { from: giver1, value: utils.toWei(1) }); const nPledges = await liquidPledging.numberOfPledges(); assert.equal(nPledges, 1); await liquidPledging.getPledge(1); - }).timeout(6000); + }); it('Should create a delegate', async () => { await liquidPledging.addDelegate('Delegate1', 'URLDelegate1', 0, 0, { from: delegate1 }); const nAdmins = await liquidPledging.numberOfPledgeAdmins(); @@ -92,7 +95,7 @@ describe('LiquidPledging test', () => { assert.equal(res[2], 'Delegate1'); assert.equal(res[3], 'URLDelegate1'); assert.equal(res[4], 0); - }).timeout(6000); + }); it('Giver should delegate on the delegate', async () => { await liquidPledging.transfer(1, 1, utils.toWei(0.5), 2, { from: giver1 }); const nPledges = await liquidPledging.numberOfPledges(); @@ -107,7 +110,7 @@ describe('LiquidPledging test', () => { assert.equal(d[0], 2); assert.equal(d[1], delegate1); assert.equal(d[2], 'Delegate1'); - }).timeout(6000); + }); it('Should create a 2 projects', async () => { await liquidPledging.addProject('Project1', 'URLProject1', adminProject1, 0, 86400, 0, { from: adminProject1 }); @@ -134,7 +137,7 @@ describe('LiquidPledging test', () => { assert.equal(res4[4], 86400); assert.equal(res4[5], 0); assert.equal(res4[6], false); - }).timeout(6000); + }); it('Delegate should assign to project1', async () => { const n = Math.floor(new Date().getTime() / 1000); await liquidPledging.transfer(2, 2, utils.toWei(0.2), 3, { from: delegate1 }); @@ -148,7 +151,7 @@ describe('LiquidPledging test', () => { assert.isAbove(utils.toDecimal(res3[4]), n + 86000); assert.equal(res3[5], 0); // Old Node assert.equal(res3[6], 0); // Not Paid - }).timeout(6000); + }); it('Giver should change his mind and assign half of it to project2', async () => { await liquidPledging.transfer(1, 3, utils.toWei(0.1), 4, { from: giver1 }); const nPledges = await liquidPledging.numberOfPledges(); @@ -162,7 +165,7 @@ describe('LiquidPledging test', () => { assert.equal(res4[4], 0); assert.equal(res4[5], 2); // Old Node assert.equal(res4[6], 0); // Not Paid - }).timeout(6000); + }); it('After the time, the project1 should be able to spend part of it', async () => { const n = Math.floor(new Date().getTime() / 1000); await liquidPledging.setMockedTime(n + 86401); @@ -185,7 +188,7 @@ describe('LiquidPledging test', () => { assert.equal(res6[4], 0); // commit time assert.equal(res6[5], 2); // Old Node assert.equal(res6[6], 1); // Peinding paid Paid - }).timeout(6000); + }); it('Should collect the Ether', async () => { const initialBalance = await web3.eth.getBalance(adminProject1); @@ -206,19 +209,19 @@ describe('LiquidPledging test', () => { assert.equal(res7[4], 0); // commit time assert.equal(res7[5], 2); // Old Node assert.equal(res7[6], 2); // Peinding paid Paid - }).timeout(6000); + }); it('Admin of the project1 should be able to cancel project1', async () => { await liquidPledging.cancelProject(3, { from: adminProject1 }); const st = await liquidPledgingState.getState(liquidPledging); assert.equal(st.admins[3].canceled, true); - }).timeout(6000); + }); it('Should not allow to withdraw from a canceled project', async () => { const st = await liquidPledgingState.getState(liquidPledging); assert.equal(utils.fromWei(st.pledges[5].amount), 0.05); await assertFail(async () => { await liquidPledging.withdraw(5, utils.toWei(0.01), { from: adminProject1 }); }); - }).timeout(6000); + }); it('Delegate should send part of this ETH to project2', async () => { await liquidPledging.transfer(2, 5, utils.toWei(0.03), 4, { $extraGas: 100000, @@ -231,34 +234,34 @@ describe('LiquidPledging test', () => { assert.equal(st.pledges[8].delegates.length, 1); assert.equal(st.pledges[8].delegates[0].id, 2); assert.equal(st.pledges[8].intendedProject, 4); - }).timeout(6000); + }); it('Giver should be able to send the remaining to project2', async () => { await liquidPledging.transfer(1, 5, utils.toWei(0.02), 4, { from: giver1, $extraGas: 100000 }); const st = await liquidPledgingState.getState(liquidPledging); assert.equal(st.pledges.length, 9); assert.equal(utils.fromWei(st.pledges[5].amount), 0); assert.equal(utils.fromWei(st.pledges[4].amount), 0.12); - }).timeout(6000); + }); it('A subproject 2a and a delegate2 is created', async () => { await liquidPledging.addProject('Project2a', 'URLProject2a', adminProject2a, 4, 86400, 0, { from: adminProject2 }); await liquidPledging.addDelegate('Delegate2', 'URLDelegate2', 0, 0, { from: delegate2 }); const nAdmins = await liquidPledging.numberOfPledgeAdmins(); assert.equal(nAdmins, 6); - }).timeout(6000); + }); it('Project 2 delegate in delegate2', async () => { await liquidPledging.transfer(4, 4, utils.toWei(0.02), 6, { from: adminProject2 }); const st = await liquidPledgingState.getState(liquidPledging); assert.equal(st.pledges.length, 10); assert.equal(utils.fromWei(st.pledges[9].amount), 0.02); assert.equal(utils.fromWei(st.pledges[4].amount), 0.1); - }).timeout(6000); + }); it('delegate2 assigns to projec2a', async () => { await liquidPledging.transfer(6, 9, utils.toWei(0.01), 5, { from: delegate2 }); const st = await liquidPledgingState.getState(liquidPledging); assert.equal(st.pledges.length, 11); assert.equal(utils.fromWei(st.pledges[9].amount), 0.01); assert.equal(utils.fromWei(st.pledges[10].amount), 0.01); - }).timeout(4000); + }); it('project2a authorize to spend a litle', async () => { const n = Math.floor(new Date().getTime() / 1000); await liquidPledging.setMockedTime(n + (86401 * 3)); @@ -268,20 +271,20 @@ describe('LiquidPledging test', () => { assert.equal(utils.fromWei(st.pledges[10].amount), 0); assert.equal(utils.fromWei(st.pledges[11].amount), 0.005); assert.equal(utils.fromWei(st.pledges[12].amount), 0.005); - }).timeout(4000); + }); it('project2 is canceled', async () => { await liquidPledging.cancelProject(4, { from: adminProject2 }); - }).timeout(6000); + }); it('project2 should not be able to confirm payment', async () => { await assertFail(async () => { await vault.confirmPayment(1); }); - }).timeout(6000); + }); it('Should not be able to withdraw it', async () => { await assertFail(async () => { await liquidPledging.withdraw(12, utils.toWei(0.005), { from: giver1 }); }); - }).timeout(6000); + }); it('Should be able to cancel payment', async () => { // bug somewhere which will throw invalid op_code if we don't provide gas or extraGas await vault.cancelPayment(1, { $extraGas: 100000 }); @@ -290,7 +293,7 @@ describe('LiquidPledging test', () => { assert.equal(utils.fromWei(st.pledges[2].amount), 0.31); assert.equal(utils.fromWei(st.pledges[11].amount), 0); assert.equal(utils.fromWei(st.pledges[12].amount), 0); - }).timeout(6000); + }); it('original owner should recover the remaining funds', async () => { await liquidPledging.withdraw(1, utils.toWei(0.5), { from: giver1 }); await liquidPledging.withdraw(2, utils.toWei(0.31), { from: giver1 }); @@ -306,7 +309,7 @@ describe('LiquidPledging test', () => { const collected = utils.fromWei(utils.toBN(finalBalance).sub(utils.toBN(initialBalance))); assert.equal(collected, 0.95); - }).timeout(12000); + }); it('Should make a donation and create giver', async () => { const oldNPledges = await liquidPledging.numberOfPledges(); const oldNAdmins = await liquidPledging.numberOfPledgeAdmins(); @@ -321,11 +324,11 @@ describe('LiquidPledging test', () => { assert.equal(res[2], ''); assert.equal(res[3], ''); assert.equal(res[4], 259200); // default to 3 day commitTime - }).timeout(6000); + }); it('Should allow childProject with different parentProject owner', async () => { const nAdminsBefore = await liquidPledging.numberOfPledgeAdmins(); await liquidPledging.addProject('Project3', 'URLProject3', adminProject3, 4, 86400, 0, { from: adminProject3 }); const nAdmins = await liquidPledging.numberOfPledgeAdmins(); assert.equal(nAdmins, utils.toDecimal(nAdminsBefore) + 1); - }).timeout(6000); + }); }); diff --git a/test/VetoDelegation.js b/test/VetoDelegation.js index 2617e8f..fcfe338 100644 --- a/test/VetoDelegation.js +++ b/test/VetoDelegation.js @@ -15,7 +15,9 @@ const printState = async (liquidPledgingState) => { console.log(JSON.stringify(st, null, 2)); }; -describe('LiquidPledging test', () => { +describe('LiquidPledging test', function () { + this.timeout(0); + let testrpc; let web3; let accounts; @@ -64,7 +66,7 @@ describe('LiquidPledging test', () => { assert.equal(res[2], 'Delegate1'); assert.equal(res[3], 'URLDelegate1'); assert.equal(res[4], 0); - }).timeout(6000); + }); it('Should make a donation and create giver', async () => { await liquidPledging.donate(0, 1, { from: giver1, value: '1000', gas: 2000000 }); diff --git a/test/helpers/assertFail.js b/test/helpers/assertFail.js index 6633b60..123c3df 100644 --- a/test/helpers/assertFail.js +++ b/test/helpers/assertFail.js @@ -6,7 +6,7 @@ module.exports = async function(callback) { try { await callback(); } catch (error) { - if (error.message.search("invalid opcode")) web3_error_thrown = true; + if (error.message.includes("invalid opcode")) web3_error_thrown = true; } assert.ok(web3_error_thrown, "Transaction should fail"); };