From b5b907bfca2a0e47d83ae6a0c8be581ea1993ee0 Mon Sep 17 00:00:00 2001 From: perissology Date: Mon, 23 Oct 2017 14:26:38 -0700 Subject: [PATCH 1/3] added test --- test/DelegationChain.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/DelegationChain.js b/test/DelegationChain.js index ead93d0..d3e2a21 100644 --- a/test/DelegationChain.js +++ b/test/DelegationChain.js @@ -148,4 +148,21 @@ describe('LiquidPledging test', function () { assert.equal(st.pledges[2].delegates.length, 1); assert.equal(st.pledges[2].delegates[0].id, 2); }); + + it('delegation chain should remain the same when owner veto\'s delegation', async () => { + // delegate1 add delegate2 to chain + await liquidPledging.transfer(2, 2, 1000, 3, {from: delegate1, $extraGas: 100000}); + // delegate2 delegate to project1 + await liquidPledging.transfer(3, 3, 1000, 5, {from: delegate2, $extraGas: 100000}); + // owner veto delegation to project1 + await liquidPledging.transfer(1, 8, 1000, 3, {from: giver1, $extraGas: 100000}); + + printState(liquidPledgingState); + const st = await liquidPledgingState.getState(); + assert.equal(st.pledges[8].amount, 0); + assert.equal(st.pledges[3].amount, 1000); + assert.equal(st.pledges[3].delegates.length, 2); + assert.equal(st.pledges[3].delegates[0].id, 2); + assert.equal(st.pledges[3].delegates[0].id, 3); + }) }); From 5d15fc6f6294d66ab3ac8bcef56ef1272d0bea28 Mon Sep 17 00:00:00 2001 From: perissology Date: Fri, 10 Nov 2017 08:39:58 -0800 Subject: [PATCH 2/3] preserve delegationChain on delegation veto --- contracts/LiquidPledging.sol | 27 +++++++++++++++++++++++++-- test/DelegationChain.js | 34 ++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/contracts/LiquidPledging.sol b/contracts/LiquidPledging.sol index 0c72df7..f17a52e 100644 --- a/contracts/LiquidPledging.sol +++ b/contracts/LiquidPledging.sol @@ -81,8 +81,31 @@ contract LiquidPledging is LiquidPledgingBase { } else if (receiver.adminType == PledgeAdminType.Project) { transferOwnershipToProject(idPledge, amount, idReceiver); } else if (receiver.adminType == PledgeAdminType.Delegate) { - idPledge = undelegate(idPledge, amount, n.delegationChain.length); - appendDelegate(idPledge, amount, idReceiver); + + uint recieverDIdx = getDelegateIdx(n, idReceiver); + if (n.intendedProject > 0 && recieverDIdx != NOTFOUND) { + // if there is an intendedProject and the receiver is in the delegationChain, + // then we want to preserve the delegationChain as this is a veto of the + // intendedProject by the owner + + if (recieverDIdx == n.delegationChain.length - 1) { + uint64 toPledge = findOrCreatePledge( + n.owner, + n.delegationChain, + 0, + 0, + n.oldPledge, + PaymentState.Pledged); + doTransfer(idPledge, toPledge, amount); + } else { + undelegate(idPledge, amount, n.delegationChain.length - receiverDIdx - 1); + } + } else { + // owner is transferring pledge to a new delegate, so we want to reset + // the delegationChain + idPledge = undelegate(idPledge, amount, n.delegationChain.length); + appendDelegate(idPledge, amount, idReceiver); + } } else { assert(false); } diff --git a/test/DelegationChain.js b/test/DelegationChain.js index 9cd5048..229e40c 100644 --- a/test/DelegationChain.js +++ b/test/DelegationChain.js @@ -181,19 +181,41 @@ describe('DelegationChain test', function () { }); it('delegation chain should remain the same when owner veto\'s delegation', async () => { - // delegate1 add delegate2 to chain - await liquidPledging.transfer(2, 2, 1000, 3, { from: delegate1, $extraGas: 100000 }); - // delegate2 delegate to project1 - await liquidPledging.transfer(3, 3, 1000, 5, { from: delegate2, $extraGas: 100000 }); // owner veto delegation to project1 await liquidPledging.transfer(1, 8, 1000, 3, { from: giver1, $extraGas: 100000 }); - printState(liquidPledgingState); const st = await liquidPledgingState.getState(); assert.equal(st.pledges[ 8 ].amount, 0); assert.equal(st.pledges[ 3 ].amount, 1000); assert.equal(st.pledges[ 3 ].delegates.length, 2); assert.equal(st.pledges[ 3 ].delegates[ 0 ].id, 2); - assert.equal(st.pledges[ 3 ].delegates[ 0 ].id, 3); + assert.equal(st.pledges[ 3 ].delegates[ 1 ].id, 3); + }); + + it('delegation chain should remain the same upto delegate of reciever when owner veto\'s delegation', async () => { + // propose project1 delegation + await liquidPledging.transfer(3, 3, 1000, 5, { from: delegate2, $extraGas: 100000 }); + // owner veto delegation to project1 and remove delegate2 + await liquidPledging.transfer(1, 8, 1000, 2, { from: giver1, $extraGas: 100000 }); + + const pledge = await liquidPledging.getPledge(2); + assert.equal(pledge.amount, 1000); + }); + + it('owner should be able to transfer pledge to a new delegate at any time', async () => { + // propose project1 delegation + await liquidPledging.transfer(2, 2, 1000, 5, { from: delegate1, $extraGas: 100000 }); + // owner veto delegation to project1 and assign new delgate + await liquidPledging.transfer(1, 9, 1000, 3, { from: giver1, $extraGas: 100000 }); + + const pledge = await liquidPledging.getPledge(10); + assert.equal(pledge.amount, 1000); + assert.equal(pledge.nDelegates, 1); + + // owner assign new delegate w/o vetoing intendedProject + await liquidPledging.transfer(1, 10, 1000, 2, { from: giver1, $extraGas: 100000 }); + const pledge2 = await liquidPledging.getPledge(2); + assert.equal(pledge2.amount, 1000); + await printState(liquidPledgingState); }); }); From 0687776b68ed43925203386a54d78b6750463588 Mon Sep 17 00:00:00 2001 From: perissology Date: Tue, 5 Dec 2017 12:23:56 -0800 Subject: [PATCH 3/3] update comment --- contracts/LiquidPledging.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/LiquidPledging.sol b/contracts/LiquidPledging.sol index f825287..6bf9f64 100644 --- a/contracts/LiquidPledging.sol +++ b/contracts/LiquidPledging.sol @@ -137,8 +137,8 @@ contract LiquidPledging is LiquidPledgingBase { undelegate(idPledge, amount, n.delegationChain.length - receiverDIdx - 1); } } else { - // owner is transferring pledge to a new delegate, so we want to reset - // the delegationChain + // owner is not vetoing an intendedProject and is transferring the pledge to a delegate, + // so we want to reset the delegationChain idPledge = undelegate(idPledge, amount, n.delegationChain.length); appendDelegate(idPledge, amount, idReceiver); }