few bug fixes & all tests passing

This commit is contained in:
perissology 2018-01-22 11:00:30 -08:00
parent 4f2bf61dd2
commit a178396df6
12 changed files with 181 additions and 44 deletions

View File

@ -1,9 +1,8 @@
pragma solidity ^0.4.0;
// change to Escapable
import "node_modules/giveth-common-contracts/contracts/Owned.sol";
import "node_modules/giveth-common-contracts/contracts/Escapable.sol";
contract EternalStorage is Owned {
contract EternalStorage is Escapable {
mapping(bytes32 => uint) UIntStorage;
mapping(bytes32 => int) IntStorage;
@ -13,6 +12,10 @@ contract EternalStorage is Owned {
mapping(bytes32 => bytes) BytesStorage;
mapping(bytes32 => bytes32) Bytes32Storage;
function EternalStorage(address _escapeHatchCaller, address _escapeHatchDestination)
Escapable(_escapeHatchCaller, _escapeHatchDestination) public {
}
/// UInt Storage
function getUIntValue(bytes32 record) public view returns (uint) {

View File

@ -567,21 +567,23 @@ contract LiquidPledging is LiquidPledgingBase {
/// @param _amount The amount of value that will be transfered.
function doTransfer(uint64 from, uint64 to, uint _amount) internal {
uint amount = callPlugins(true, from, to, _amount);
// uint amount = _amount;
if (from == to) {
return;
}
if (amount == 0) {
return;
}
Pledges.Pledge memory nFrom = _storage.findPledge(from);
Pledges.Pledge memory nTo = _storage.findPledge(to);
require(nFrom.amount >= amount);
nFrom.amount -= amount;
nTo.amount += amount;
Pledges.Pledge memory pFrom = _storage.findPledge(from);
Pledges.Pledge memory pTo = _storage.findPledge(to);
require(pFrom.amount >= amount);
pFrom.amount -= amount;
_storage.setPledgeAmount(pFrom.id, pFrom.amount);
pTo.amount += amount;
_storage.setPledgeAmount(pTo.id, pTo.amount);
Transfer(from, to, amount);
// callPlugins(false, from, to, amount);
callPlugins(false, from, to, amount);
}
/// @notice Only affects pledges with the Pledged Pledges.PledgeState for 2 things:

View File

@ -36,7 +36,7 @@ library PledgeAdmins {
idGiver = _storage.stgCollectionAddItem(admins);
// Save the fields
_storage.stgObjectSetUInt(class, idGiver, "adminType", uint(PledgeAdminType.Giver));
// don't set adminType to save gas, b/c 0 is Giver
_storage.stgObjectSetAddress(class, idGiver, "addr", msg.sender);
_storage.stgObjectSetString(class, idGiver, "name", name);
_storage.stgObjectSetString(class, idGiver, "url", url);

View File

@ -68,7 +68,9 @@ library Pledges {
if (oldPledge > 0) {
_storage.stgObjectSetUInt(class, id, "oldPledge", oldPledge);
}
if (state != PledgeState.Pledged) {
_storage.stgObjectSetUInt(class, id, "state", uint(state));
}
if (delegationChain.length > 0) {
_storage.setUIntValue(keccak256("delegationChain", id, "length"), delegationChain.length);

View File

@ -31,7 +31,7 @@ class LiquidPledgingState {
}
const promises = [];
for (let i = 1; i <= res.nDelegates; i += 1) {
for (let i = 0; i < res.nDelegates; i += 1) {
promises.push(
this.$lp.getPledgeDelegate(idPledge, i)
.then(r => ({

View File

@ -4,6 +4,9 @@ const TestRPC = require('ethereumjs-testrpc');
const Web3 = require('web3');
const chai = require('chai');
const liquidpledging = require('../index.js');
const lpData = require('../build/LiquidPledgingMock.sol');
const EternalStorage = require('../js/eternalStorage');
const PledgeAdmins = require('../js/pledgeAdmins');
const assertFail = require('./helpers/assertFail');
const LiquidPledging = liquidpledging.LiquidPledgingMock;
@ -35,7 +38,7 @@ describe('LiquidPledging plugins test', function () {
before(async () => {
testrpc = TestRPC.server({
ws: true,
gasLimit: 6500000,
gasLimit: 6700000,
total_accounts: 10,
});
@ -55,7 +58,25 @@ describe('LiquidPledging plugins test', function () {
it('Should deploy LiquidPledging contract', async function() {
vault = await Vault.new(web3, accounts[0], accounts[1]);
liquidPledging = await LiquidPledging.new(web3, vault.$address, accounts[0], accounts[1], { gas: 6500000 });
let storage = await EternalStorage.new(web3, accounts[0], accounts[1]);
let admins = await PledgeAdmins.new(web3);
let bytecode = lpData.LiquidPledgingMockByteCode;
bytecode = bytecode.replace(/__contracts\/PledgeAdmins\.sol:PledgeAdm__/g, admins.$address.slice(2)); // solc library
bytecode = bytecode.replace(/__:PledgeAdmins_________________________/g, admins.$address.slice(2)); // yarn sol-compile library
let lp = await new web3.eth.Contract(lpData.LiquidPledgingMockAbi).deploy({
arguments: [storage.$address, vault.$address, accounts[0], accounts[0]],
data: bytecode,
}).send({
from: accounts[0],
gas: 6700000,
gasPrice: 1,
});
liquidPledging = new LiquidPledging(web3, lp._address);
await storage.changeOwnership(liquidPledging.$address);
await vault.setLiquidPledging(liquidPledging.$address);
liquidPledgingState = new LiquidPledgingState(liquidPledging);
});
@ -96,10 +117,11 @@ describe('LiquidPledging plugins test', function () {
data: simpleProjectPluginFactoryByteCode,
arguments: []
}).send({ from: adminProject1, gas: 5000000 });
factoryContract.setProvider(web3.currentProvider);
await factoryContract.methods
.deploy(liquidPledging.$address, "SimplePlugin1", "", 0)
.send({ from: adminProject1, gas: 4000000 });
.send({ from: adminProject1, gas: 5000000 })
const nAdmins = await liquidPledging.numberOfPledgeAdmins();
assert.equal(nAdmins, 2);

View File

@ -4,6 +4,9 @@ const TestRPC = require('ethereumjs-testrpc');
const Web3 = require('web3');
const chai = require('chai');
const liquidpledging = require('../index.js');
const lpData = require('../build/LiquidPledgingMock.sol');
const EternalStorage = require('../js/eternalStorage');
const PledgeAdmins = require('../js/pledgeAdmins');
const assertFail = require('./helpers/assertFail');
const LiquidPledging = liquidpledging.LiquidPledgingMock;
@ -32,7 +35,7 @@ describe('LiquidPledging cancelPledge normal scenario', function () {
before(async () => {
testrpc = TestRPC.server({
ws: true,
gasLimit: 5800000,
gasLimit: 6700000,
total_accounts: 10,
});
@ -52,14 +55,32 @@ describe('LiquidPledging cancelPledge normal scenario', function () {
it('Should deploy LiquidPledging contract', async () => {
vault = await Vault.new(web3, accounts[0], accounts[1]);
liquidPledging = await LiquidPledging.new(web3, vault.$address, accounts[0], accounts[1], { gas: 5800000 });
let storage = await EternalStorage.new(web3, accounts[0], accounts[1]);
let admins = await PledgeAdmins.new(web3);
let bytecode = lpData.LiquidPledgingMockByteCode;
bytecode = bytecode.replace(/__contracts\/PledgeAdmins\.sol:PledgeAdm__/g, admins.$address.slice(2)); // solc library
bytecode = bytecode.replace(/__:PledgeAdmins_________________________/g, admins.$address.slice(2)); // yarn sol-compile library
let lp = await new web3.eth.Contract(lpData.LiquidPledgingMockAbi).deploy({
arguments: [storage.$address, vault.$address, accounts[0], accounts[0]],
data: bytecode,
}).send({
from: accounts[0],
gas: 6700000,
gasPrice: 1,
});
liquidPledging = new LiquidPledging(web3, lp._address);
await storage.changeOwnership(liquidPledging.$address);
await vault.setLiquidPledging(liquidPledging.$address);
liquidPledgingState = new LiquidPledgingState(liquidPledging);
});
it('Should add project and donate ', async () => {
await liquidPledging.addProject('Project1', 'URLProject1', adminProject1, 0, 0, '0x0', { from: adminProject1 });
await liquidPledging.donate(0, 1, { from: giver1, value: '1000', gas: 500000 });
await liquidPledging.donate(0, 1, { from: giver1, value: '1000' });
const nAdmins = await liquidPledging.numberOfPledgeAdmins();
assert.equal(nAdmins, 2);
@ -67,12 +88,12 @@ describe('LiquidPledging cancelPledge normal scenario', function () {
it('Should only allow pledge owner to cancel pledge', async () => {
await assertFail(async () => {
await liquidPledging.cancelPledge(2, 1000, { from: giver1, gas: 500000 });
await liquidPledging.cancelPledge(2, 1000, { from: giver1 });
});
});
it('Should cancel pledge and return to oldPledge', async () => {
await liquidPledging.cancelPledge(2, 1000, { from: adminProject1, gas: 500000 });
await liquidPledging.cancelPledge(2, 1000, { from: adminProject1 });
const st = await liquidPledgingState.getState();
@ -82,7 +103,7 @@ describe('LiquidPledging cancelPledge normal scenario', function () {
it('Should not allow to cancel pledge if oldPledge === 0', async () => {
await assertFail(async () => {
await liquidPledging.cancelPledge(1, 1000, { from: giver1, gas: 500000 });
await liquidPledging.cancelPledge(1, 1000, { from: giver1 });
});
})
});

View File

@ -4,6 +4,9 @@ const TestRPC = require('ethereumjs-testrpc');
const Web3 = require('web3');
const chai = require('chai');
const liquidpledging = require('../index.js');
const lpData = require('../build/LiquidPledgingMock.sol');
const EternalStorage = require('../js/eternalStorage');
const PledgeAdmins = require('../js/pledgeAdmins');
const LiquidPledging = liquidpledging.LiquidPledgingMock;
const LiquidPledgingState = liquidpledging.LiquidPledgingState;
@ -32,16 +35,17 @@ describe('DelegationChain test', function () {
let delegate3;
let adminProject1;
const gasUsage = {};
before(async () => {
testrpc = TestRPC.server({
ws: true,
gasLimit: 5800000,
gasLimit: 6700000,
total_accounts: 10,
});
testrpc.listen(8546, '127.0.0.1');
testrpc.listen(8545, '127.0.0.1');
web3 = new Web3('ws://localhost:8546');
web3 = new Web3('ws://localhost:8545');
accounts = await web3.eth.getAccounts();
giver1 = accounts[1];
delegate1 = accounts[2];
@ -53,12 +57,31 @@ describe('DelegationChain test', function () {
after((done) => {
testrpc.close();
// console.log(gasUsage);
done();
});
it('Should deploy LiquidPledging contract', async () => {
vault = await Vault.new(web3, accounts[0], accounts[1]);
liquidPledging = await LiquidPledging.new(web3, vault.$address, accounts[0], accounts[1], { gas: 5800000 });
let storage = await EternalStorage.new(web3, accounts[0], accounts[1]);
let admins = await PledgeAdmins.new(web3);
let bytecode = lpData.LiquidPledgingMockByteCode;
bytecode = bytecode.replace(/__contracts\/PledgeAdmins\.sol:PledgeAdm__/g, admins.$address.slice(2)); // solc library
bytecode = bytecode.replace(/__:PledgeAdmins_________________________/g, admins.$address.slice(2)); // yarn sol-compile library
let lp = await new web3.eth.Contract(lpData.LiquidPledgingMockAbi).deploy({
arguments: [storage.$address, vault.$address, accounts[0], accounts[0]],
data: bytecode,
}).send({
from: accounts[0],
gas: 6700000,
gasPrice: 1,
});
liquidPledging = new LiquidPledging(web3, lp._address);
await storage.changeOwnership(liquidPledging.$address);
await vault.setLiquidPledging(liquidPledging.$address);
liquidPledgingState = new LiquidPledgingState(liquidPledging);
});
@ -76,11 +99,11 @@ describe('DelegationChain test', function () {
});
it('Should allow previous delegate to transfer pledge', async () => {
await liquidPledging.donate(1, 2, {from: giver1, value: 1000, $extraGas: 50000});
await liquidPledging.donate(1, 2, {from: giver1, value: 1000});
// add delegate2 to chain
await liquidPledging.transfer(2, 2, 1000, 3, {from: delegate1, $extraGas: 100000});
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, $extraGas: 100000});
await liquidPledging.transfer(2, 3, 1000, 2, {from: delegate1});
const st = await liquidPledgingState.getState();
assert.equal(st.pledges[2].amount, 1000);
@ -151,13 +174,13 @@ describe('DelegationChain test', function () {
it('Should not append delegate on veto delegation', async () => {
// propose the delegation
await liquidPledging.transfer(2, 2, 1000, 5, { from: delegate1, $extraGas: 100000 });
await liquidPledging.transfer(2, 2, 1000, 5, { from: delegate1 });
const origPledge = await liquidPledging.getPledge(2);
assert.equal(origPledge.amount, '0');
// veto the delegation
await liquidPledging.transfer(1, 5, 1000, 2, { from: giver1, $extraGas: 100000 });
await liquidPledging.transfer(1, 5, 1000, 2, { from: giver1 });
const currentPledge = await liquidPledging.getPledge(2);

View File

@ -4,6 +4,9 @@ const TestRPC = require('ethereumjs-testrpc');
const Web3 = require('web3');
const chai = require('chai');
const liquidpledging = require('../index.js');
const lpData = require('../build/LiquidPledgingMock.sol');
const EternalStorage = require('../js/eternalStorage');
const PledgeAdmins = require('../js/pledgeAdmins');
const assertFail = require('./helpers/assertFail');
const { utils } = Web3;
@ -38,13 +41,13 @@ describe('LiquidPledging test', function () {
before(async () => {
testrpc = TestRPC.server({
ws: true,
gasLimit: 5800000,
gasLimit: 6700000,
total_accounts: 10,
});
testrpc.listen(8546, '127.0.0.1');
testrpc.listen(8545, '127.0.0.1');
web3 = new Web3('ws://localhost:8546');
web3 = new Web3('ws://localhost:8545');
accounts = await web3.eth.getAccounts();
giver1 = accounts[1];
delegate1 = accounts[2];
@ -57,13 +60,31 @@ describe('LiquidPledging test', function () {
});
after((done) => {
testrpc.close();
// testrpc.close();
done();
});
it('Should deploy LiquidPledging contract', async () => {
vault = await LPVault.new(web3, accounts[0], accounts[1]);
liquidPledging = await LiquidPledging.new(web3, vault.$address, accounts[0], accounts[1], { gas: 5800000 });
let storage = await EternalStorage.new(web3, accounts[0], accounts[1]);
let admins = await PledgeAdmins.new(web3);
let bytecode = lpData.LiquidPledgingMockByteCode;
bytecode = bytecode.replace(/__contracts\/PledgeAdmins\.sol:PledgeAdm__/g, admins.$address.slice(2)); // solc library
bytecode = bytecode.replace(/__:PledgeAdmins_________________________/g, admins.$address.slice(2)); // yarn sol-compile library
let lp = await new web3.eth.Contract(lpData.LiquidPledgingMockAbi).deploy({
arguments: [storage.$address, vault.$address, accounts[0], accounts[0]],
data: bytecode,
}).send({
from: accounts[0],
gas: 6700000,
gasPrice: 1,
});
liquidPledging = new LiquidPledging(web3, lp._address);
await storage.changeOwnership(liquidPledging.$address);
await vault.setLiquidPledging(liquidPledging.$address);
liquidPledgingState = new LiquidPledgingState(liquidPledging);
});
@ -105,7 +126,7 @@ describe('LiquidPledging test', function () {
assert.equal(res2[0], utils.toWei('0.5'));
assert.equal(res2[1], 1); // One delegate
const d = await liquidPledging.getPledgeDelegate(2, 1);
const d = await liquidPledging.getPledgeDelegate(2, 0);
assert.equal(d[0], 2);
assert.equal(d[1], delegate1);
assert.equal(d[2], 'Delegate1');
@ -215,8 +236,9 @@ describe('LiquidPledging test', function () {
assert.equal(st.admins[3].canceled, true);
});
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);
const p = await liquidPledging.getPledge(5);
assert.equal(utils.fromWei(p.amount), 0.05);
await assertFail(async () => {
await liquidPledging.withdraw(5, utils.toWei('0.01'), { from: adminProject1 });
});

View File

@ -4,6 +4,9 @@ const TestRPC = require('ethereumjs-testrpc');
const Web3 = require('web3');
const chai = require('chai');
const liquidpledging = require('../index.js');
const lpData = require('../build/LiquidPledgingMock.sol');
const EternalStorage = require('../js/eternalStorage');
const PledgeAdmins = require('../js/pledgeAdmins');
const LiquidPledging = liquidpledging.LiquidPledgingMock;
const LiquidPledgingState = liquidpledging.LiquidPledgingState;
@ -35,7 +38,7 @@ describe('NormalizePledge test', function () {
before(async () => {
testrpc = TestRPC.server({
ws: true,
gasLimit: 5800000,
gasLimit: 6700000,
total_accounts: 10,
});
@ -58,7 +61,25 @@ describe('NormalizePledge test', function () {
it('Should deploy LiquidPledging contract', async () => {
vault = await Vault.new(web3, accounts[0], accounts[1]);
liquidPledging = await LiquidPledging.new(web3, vault.$address, accounts[0], accounts[1], { gas: 5800000 });
let storage = await EternalStorage.new(web3, accounts[0], accounts[1]);
let admins = await PledgeAdmins.new(web3);
let bytecode = lpData.LiquidPledgingMockByteCode;
bytecode = bytecode.replace(/__contracts\/PledgeAdmins\.sol:PledgeAdm__/g, admins.$address.slice(2)); // solc library
bytecode = bytecode.replace(/__:PledgeAdmins_________________________/g, admins.$address.slice(2)); // yarn sol-compile library
let lp = await new web3.eth.Contract(lpData.LiquidPledgingMockAbi).deploy({
arguments: [storage.$address, vault.$address, accounts[0], accounts[0]],
data: bytecode,
}).send({
from: accounts[0],
gas: 6700000,
gasPrice: 1,
});
liquidPledging = new LiquidPledging(web3, lp._address);
await storage.changeOwnership(liquidPledging.$address);
await vault.setLiquidPledging(liquidPledging.$address);
liquidPledgingState = new LiquidPledgingState(liquidPledging);
});

View File

@ -4,6 +4,9 @@ const TestRPC = require('ethereumjs-testrpc');
const Web3 = require('web3');
const chai = require('chai');
const liquidpledging = require('../index.js');
const lpData = require('../build/LiquidPledgingMock.sol');
const EternalStorage = require('../js/eternalStorage');
const PledgeAdmins = require('../js/pledgeAdmins');
const assertFail = require('./helpers/assertFail');
const LiquidPledging = liquidpledging.LiquidPledgingMock;
@ -29,7 +32,7 @@ describe('Vault test', function () {
before(async () => {
testrpc = TestRPC.server({
ws: true,
gasLimit: 6500000,
gasLimit: 6700000,
total_accounts: 10,
});
@ -51,7 +54,25 @@ describe('Vault test', function () {
it('Should deploy Vault contract', async function () {
vault = await Vault.new(web3, escapeHatchCaller, escapeHatchDestination, { from: vaultOwner });
liquidPledging = await LiquidPledging.new(web3, vault.$address, escapeHatchCaller, escapeHatchDestination, { gas: 6500000 });
let storage = await EternalStorage.new(web3, accounts[0], accounts[1]);
let admins = await PledgeAdmins.new(web3);
let bytecode = lpData.LiquidPledgingMockByteCode;
bytecode = bytecode.replace(/__contracts\/PledgeAdmins\.sol:PledgeAdm__/g, admins.$address.slice(2)); // solc library
bytecode = bytecode.replace(/__:PledgeAdmins_________________________/g, admins.$address.slice(2)); // yarn sol-compile library
let lp = await new web3.eth.Contract(lpData.LiquidPledgingMockAbi).deploy({
arguments: [storage.$address, vault.$address, accounts[0], accounts[0]],
data: bytecode,
}).send({
from: accounts[0],
gas: 6700000,
gasPrice: 1,
});
liquidPledging = new LiquidPledging(web3, lp._address);
await storage.changeOwnership(liquidPledging.$address);
await vault.setLiquidPledging(liquidPledging.$address, { from: vaultOwner });
liquidPledgingState = new LiquidPledgingState(liquidPledging);

View File

@ -6,7 +6,7 @@ module.exports = async function(callback) {
try {
await callback();
} catch (error) {
if (error.message.includes("invalid opcode")) web3_error_thrown = true;
if (error.message.includes("invalid opcode") || error.message.includes('revert')) web3_error_thrown = true;
}
assert.ok(web3_error_thrown, "Transaction should fail");
};