Skip to content

Commit

Permalink
Adapt to last changes in Staking app
Browse files Browse the repository at this point in the history
Locks now have start date too.
Modified events in tests.
`unlockAndMoveTokens` renamed to `unlockPartialAndMoveTokens`.
  • Loading branch information
bingen committed Jul 24, 2018
1 parent 77b5c57 commit 1248fb7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 40 deletions.
14 changes: 8 additions & 6 deletions contracts/PLCR.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ contract PLCR is AragonApp, IVoting {
require(getTimestamp() <= vote.commitEndDate);

// check lock and get amount
uint256 stake = checkLock(msg.sender, _lockId, vote.revealEndDate);
uint256 stake = checkLock(msg.sender, _lockId, vote.commitEndDate, vote.revealEndDate);

// move slash proportion to here
uint256 slashStake = stake.mul(minorityBlocSlash) / PCT_BASE;
staking.unlockAndMoveTokens(msg.sender, _lockId, address(this), slashStake);
staking.unlockPartialAndMoveTokens(msg.sender, _lockId, address(this), slashStake);
vote.slashPool = vote.slashPool.add(slashStake);

vote.userVotes[msg.sender] = UserVote({
Expand Down Expand Up @@ -154,7 +154,7 @@ contract PLCR is AragonApp, IVoting {
require(getTimestamp() <= vote.revealEndDate);

// check salt
require(userVote.secretHash == keccak256(keccak256(_voteOption ? '1' : '0'), keccak256(_salt)));
require(userVote.secretHash == keccak256(keccak256(_voteOption ? "1" : "0"), keccak256(_salt)));

// make sure it's not revealed twice
require(!userVote.revealed);
Expand Down Expand Up @@ -352,7 +352,7 @@ contract PLCR is AragonApp, IVoting {
return 0;
}

function checkLock(address user, uint256 _lockId, uint64 _date) internal returns (uint256) {
function checkLock(address user, uint256 _lockId, uint64 _startDate, uint64 _endDate) internal returns (uint256) {
// check lockId was not used before
require(!usedLocks[user][_lockId]);
// mark it as used
Expand All @@ -361,14 +361,16 @@ contract PLCR is AragonApp, IVoting {
// get the lock
uint256 amount;
uint8 lockUnit;
uint64 lockStarts;
uint64 lockEnds;
address unlocker;
(amount, lockUnit, lockEnds, unlocker, ) = staking.getLock(msg.sender, _lockId);
(amount, lockUnit, lockStarts, lockEnds, unlocker, ) = staking.getLock(msg.sender, _lockId);
// check unlocker
require(unlocker == address(this));
// check time
require(lockUnit == uint8(TimeUnit.Seconds));
require(lockEnds >= _date);
require(lockStarts <= _startDate);
require(lockEnds >= _endDate);

return amount;
}
Expand Down
34 changes: 26 additions & 8 deletions test/mocks/StakingMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,54 @@ import "@aragon/apps-staking/contracts/interfaces/IStaking.sol";
contract StakingMock is IStaking {
uint256 amount;
uint8 lockUnit;
uint64 lockStarts;
uint64 lockEnds;
address unlocker;
bytes32 metadata;

event Unlock(address indexed account, uint256 indexed lockId);
event UnlockPartial(address indexed account, uint256 indexed lockId, uint256 amount);
event Unlocked(address indexed account, address indexed unlocker, uint256 oldLockId);
event UnlockedPartial(address indexed account, address indexed unlocker, uint256 indexed lockId, uint256 amount);
event MovedTokens(address indexed from, address indexed to, uint256 amount);

function setLock(uint256 _amount, uint8 _lockUnit, uint64 _lockEnds, address _unlocker, bytes32 _metadata) public {
function setLock(uint256 _amount, uint8 _lockUnit, uint64 _lockStarts, uint64 _lockEnds, address _unlocker, bytes32 _metadata) public {
amount = _amount;
lockUnit = _lockUnit;
lockStarts = _lockStarts;
lockEnds = _lockEnds;
unlocker = _unlocker;
}

function unlock(address _acct, uint256 _lockId) public {
Unlock(_acct, _lockId);
Unlocked(_acct, msg.sender, _lockId);
}

function moveTokens(address _from, address _to, uint256 _amount) public {
MovedTokens(_from, _to, _amount);
}

function unlockAndMoveTokens(address _from, uint256 _lockId, address _to, uint256 _amount) external {
UnlockPartial(_from, _lockId, _amount);
function unlockPartialAndMoveTokens(address _from, uint256 _lockId, address _to, uint256 _amount) external {
UnlockedPartial(_from, msg.sender, _lockId, _amount);
MovedTokens(_from, _to, _amount);
}

function getLock(address _acct, uint256 _lockId) public view returns (uint256, uint8, uint64, address, bytes32) {
return (amount, lockUnit, lockEnds, unlocker, metadata);
function getLock(
address acct,
uint256 lockId
)
public
view
returns (
uint256 _amount,
uint8 _lockUnit,
uint64 _lockStarts,
uint64 _lockEnds,
address _unlocker,
bytes32 _metadata,
uint256 _prevUnlockerLockId,
uint256 _nextUnlockerLockId
)
{
return (amount, lockUnit, lockStarts, lockEnds, unlocker, metadata, 0, 0);
}


Expand Down
62 changes: 36 additions & 26 deletions test/plcr.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ contract('PLCR', ([owner, voter1, voter2, _]) => {
const commitVote = async (voter, voteOption) => {
const voteId = await createVote()
// mock lock
const endLock = (await app.getTimestampExt.call()).add(commitDuration + revealDuration + 1)
await staking.setLock(stake, TIME_UNIT_SECONDS, endLock, app.address, "")
const startLock = await app.getTimestampExt.call()
const endLock = startLock.add(commitDuration + revealDuration + 1)
await staking.setLock(stake, TIME_UNIT_SECONDS, startLock, endLock, app.address, "")

const receipt = await app.commitVote(voteId, secretHash(voteOption), lockId, { from: voter })

Expand All @@ -78,23 +79,25 @@ contract('PLCR', ([owner, voter1, voter2, _]) => {
}

// checks if a lockId for a given account was unlocked
const checkUnlocked = (_receipt, _account, _lockId) => {
const checkUnlocked = (_receipt, _account, _unlocker, _lockId) => {
const logs = _receipt.receipt.logs.filter(
l =>
l.topics[0] == web3.sha3('Unlock(address,uint256)') &&
l.topics[0] == web3.sha3('Unlocked(address,address,uint256)') &&
'0x' + l.topics[1].slice(26) == _account &&
web3.toDecimal(l.topics[2]) == _lockId
'0x' + l.topics[2].slice(26) == _unlocker &&
web3.toDecimal(l.topics[3]) == _lockId
)
return logs.length == 1
}

// checks if a lockId for a given account was partially unlocked for certain amount
const checkUnlockedPartial = (_receipt, _account, _lockId, _amount) => {
const checkUnlockedPartial = (_receipt, _account, _unlocker, _lockId, _amount) => {
const logs = _receipt.receipt.logs.filter(
l =>
l.topics[0] == web3.sha3('UnlockPartial(address,uint256,uint256)') &&
l.topics[0] == web3.sha3('UnlockedPartial(address,address,uint256,uint256)') &&
'0x' + l.topics[1].slice(26) == _account &&
web3.toDecimal(l.topics[2]) == _lockId &&
'0x' + l.topics[2].slice(26) == _unlocker &&
web3.toDecimal(l.topics[3]) == _lockId &&
web3.toDecimal(l.data) == _amount
)
return logs.length == 1
Expand Down Expand Up @@ -138,7 +141,7 @@ contract('PLCR', ([owner, voter1, voter2, _]) => {
const {voteId, receipt} = await commitVote(voter1, voteOption)
// checks
const slashPool = minorityBlocSlash.mul(stake).dividedToIntegerBy(pct16(100))
assert.isTrue(checkUnlockedPartial(receipt, voter1, lockId, slashPool))
assert.isTrue(checkUnlockedPartial(receipt, voter1, app.address, lockId, slashPool))
assert.isTrue(checkMovedTokens(receipt, voter1, app.address, slashPool))
const userVote = await app.getUserVote.call(voteId, voter1)
assert.equal(userVote[0].toString(), lockId, "lockId should match")
Expand All @@ -160,7 +163,7 @@ contract('PLCR', ([owner, voter1, voter2, _]) => {
const {voteId, receipt} = await commitVote(voter1, voteOption)
// checks
const slashPool = minorityBlocSlash.mul(stake).dividedToIntegerBy(pct16(100))
assert.isTrue(checkUnlockedPartial(receipt, voter1, lockId, slashPool))
assert.isTrue(checkUnlockedPartial(receipt, voter1, app.address, lockId, slashPool))
assert.isTrue(checkMovedTokens(receipt, voter1, app.address, slashPool))
const userVote = await app.getUserVote.call(voteId, voter1)
assert.equal(userVote[0].toString(), lockId, "lockId should match")
Expand All @@ -180,8 +183,9 @@ contract('PLCR', ([owner, voter1, voter2, _]) => {
it('fails if trying to commit after commit period', async () => {
const voteId = await createVote()
// mock lock
const endLock = (await app.getTimestampExt.call()).add(commitDuration + revealDuration + 1)
await staking.setLock(stake, TIME_UNIT_SECONDS, endLock, app.address, "")
const startLock = await app.getTimestampExt.call()
const endLock = startLock.add(commitDuration + revealDuration + 1)
await staking.setLock(stake, TIME_UNIT_SECONDS, startLock, endLock, app.address, "")

await app.addTime(commitDuration + 1)

Expand All @@ -193,8 +197,9 @@ contract('PLCR', ([owner, voter1, voter2, _]) => {
it('fails if lock has wrong unlocker', async () => {
const voteId = await createVote()
// mock lock
const endLock = (await app.getTimestampExt.call()).add(commitDuration + revealDuration + 1)
await staking.setLock(stake, TIME_UNIT_SECONDS, endLock, owner, "")
const startLock = await app.getTimestampExt.call()
const endLock = startLock.add(commitDuration + revealDuration + 1)
await staking.setLock(stake, TIME_UNIT_SECONDS, startLock, endLock, owner, "")

return assertRevert(async () => {
await app.commitVote(voteId, secretHash(true), lockId, { from: voter1 })
Expand All @@ -204,8 +209,9 @@ contract('PLCR', ([owner, voter1, voter2, _]) => {
it('fails if lock has wrong unit', async () => {
const voteId = await createVote()
// mock lock
const endLock = (await app.getTimestampExt.call()).add(commitDuration + revealDuration + 1)
await staking.setLock(stake, TIME_UNIT_BLOCKS, endLock, app.address, "")
const startLock = await app.getTimestampExt.call()
const endLock = startLock.add(commitDuration + revealDuration + 1)
await staking.setLock(stake, TIME_UNIT_BLOCKS, startLock, endLock, app.address, "")

return assertRevert(async () => {
await app.commitVote(voteId, secretHash(true), lockId, { from: voter1 })
Expand All @@ -215,8 +221,9 @@ contract('PLCR', ([owner, voter1, voter2, _]) => {
it('fails if lock has wrong end date', async () => {
const voteId = await createVote()
// mock lock
const endLock = await app.getTimestampExt.call()
await staking.setLock(stake, TIME_UNIT_SECONDS, endLock, app.address, "")
const startLock = await app.getTimestampExt.call()
const endLock = startLock
await staking.setLock(stake, TIME_UNIT_SECONDS, startLock, endLock, app.address, "")

return assertRevert(async () => {
await app.commitVote(voteId, secretHash(true), lockId, { from: voter1 })
Expand All @@ -226,8 +233,9 @@ contract('PLCR', ([owner, voter1, voter2, _]) => {
it('fails if lock was already used', async () => {
const voteId = await createVote()
// mock lock
const endLock = (await app.getTimestampExt.call()).add(commitDuration + revealDuration + 1)
await staking.setLock(stake, TIME_UNIT_SECONDS, endLock, app.address, "")
const startLock = await app.getTimestampExt.call()
const endLock = startLock.add(commitDuration + revealDuration + 1)
await staking.setLock(stake, TIME_UNIT_SECONDS, startLock, endLock, app.address, "")
await app.commitVote(voteId, secretHash(true), lockId, { from: voter1 })

return assertRevert(async () => {
Expand Down Expand Up @@ -328,12 +336,13 @@ contract('PLCR', ([owner, voter1, voter2, _]) => {
// commit vote
// Voter 1
// mock lock
const endLock = (await app.getTimestampExt.call()).add(commitDuration + revealDuration + 1)
await staking.setLock(stake1, TIME_UNIT_SECONDS, endLock, app.address, "")
const startLock = await app.getTimestampExt.call()
const endLock = startLock.add(commitDuration + revealDuration + 1)
await staking.setLock(stake1, TIME_UNIT_SECONDS, startLock, endLock, app.address, "")
await app.commitVote(voteId, secretHash(voteOption1), lockId1, { from: voter1 })
// Voter 2
// mock lock
await staking.setLock(stake2, TIME_UNIT_SECONDS, endLock, app.address, "")
await staking.setLock(stake2, TIME_UNIT_SECONDS, startLock, endLock, app.address, "")
await app.commitVote(voteId, secretHash(voteOption2), lockId2, { from: voter2 })

// Reveal votes
Expand Down Expand Up @@ -490,12 +499,13 @@ contract('PLCR', ([owner, voter1, voter2, _]) => {
// commit vote
// Voter 1
// mock lock
const endLock = (await app.getTimestampExt.call()).add(commitDuration + revealDuration + 1)
await staking.setLock(stake1, TIME_UNIT_SECONDS, endLock, app.address, "")
const startLock = await app.getTimestampExt.call()
const endLock = startLock.add(commitDuration + revealDuration + 1)
await staking.setLock(stake1, TIME_UNIT_SECONDS, startLock, endLock, app.address, "")
await app.commitVote(voteId, secretHash(voteOption1), lockId1, { from: voter1 })
// Voter 2
// mock lock
await staking.setLock(stake2, TIME_UNIT_SECONDS, endLock, app.address, "")
await staking.setLock(stake2, TIME_UNIT_SECONDS, startLock, endLock, app.address, "")
await app.commitVote(voteId, secretHash(voteOption2), lockId2, { from: voter2 })

// Reveal votes
Expand Down

0 comments on commit 1248fb7

Please sign in to comment.