From 5d15fc6f6294d66ab3ac8bcef56ef1272d0bea28 Mon Sep 17 00:00:00 2001 From: perissology Date: Fri, 10 Nov 2017 08:39:58 -0800 Subject: [PATCH] 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); }); });