Skip to content

Commit ed9de93

Browse files
committed
Extract reputation functionality into VotingReputation
1 parent 08df73f commit ed9de93

File tree

4 files changed

+286
-215
lines changed

4 files changed

+286
-215
lines changed

contracts/extensions/VotingBase.sol

Lines changed: 34 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ contract VotingBase is ColonyExtension, PatriciaTreeProofs {
9090
// Modifiers
9191

9292
modifier onlyRoot() {
93-
require(colony.hasUserRole(msg.sender, 1, ColonyDataTypes.ColonyRole.Root), "voting-rep-caller-not-root");
93+
require(colony.hasUserRole(msg.sender, 1, ColonyDataTypes.ColonyRole.Root), "voting-base-caller-not-root");
9494
_;
9595
}
9696

@@ -140,18 +140,18 @@ contract VotingBase is ColonyExtension, PatriciaTreeProofs {
140140
public
141141
onlyRoot
142142
{
143-
require(state == ExtensionState.Deployed, "voting-rep-already-initialised");
143+
require(state == ExtensionState.Deployed, "voting-base-already-initialised");
144144

145-
require(_totalStakeFraction <= WAD / 2, "voting-rep-greater-than-half-wad");
146-
require(_voterRewardFraction <= WAD / 2, "voting-rep-greater-than-half-wad");
145+
require(_totalStakeFraction <= WAD / 2, "voting-base-greater-than-half-wad");
146+
require(_voterRewardFraction <= WAD / 2, "voting-base-greater-than-half-wad");
147147

148-
require(_userMinStakeFraction <= WAD, "voting-rep-greater-than-wad");
149-
require(_maxVoteFraction <= WAD, "voting-rep-greater-than-wad");
148+
require(_userMinStakeFraction <= WAD, "voting-base-greater-than-wad");
149+
require(_maxVoteFraction <= WAD, "voting-base-greater-than-wad");
150150

151-
require(_stakePeriod <= 365 days, "voting-rep-period-too-long");
152-
require(_submitPeriod <= 365 days, "voting-rep-period-too-long");
153-
require(_revealPeriod <= 365 days, "voting-rep-period-too-long");
154-
require(_escalationPeriod <= 365 days, "voting-rep-period-too-long");
151+
require(_stakePeriod <= 365 days, "voting-base-period-too-long");
152+
require(_submitPeriod <= 365 days, "voting-base-period-too-long");
153+
require(_revealPeriod <= 365 days, "voting-base-period-too-long");
154+
require(_escalationPeriod <= 365 days, "voting-base-period-too-long");
155155

156156
state = ExtensionState.Active;
157157

@@ -190,7 +190,7 @@ contract VotingBase is ColonyExtension, PatriciaTreeProofs {
190190
bytes32 rootHash;
191191
uint256 domainId;
192192
uint256 skillId;
193-
uint256 skillRep;
193+
uint256 maxVotes;
194194
uint256 totalVotes;
195195
uint256 paidVoterComp;
196196
uint256[2] pastVoterComp; // [nay, yay]
@@ -213,62 +213,7 @@ contract VotingBase is ColonyExtension, PatriciaTreeProofs {
213213

214214
// Public functions (interface)
215215

216-
/// @notice Create a motion in the root domain
217-
/// @param _altTarget The contract to which we send the action (0x0 for the colony)
218-
/// @param _action A bytes array encoding a function call
219-
/// @param _key Reputation tree key for the root domain
220-
/// @param _value Reputation tree value for the root domain
221-
/// @param _branchMask The branchmask of the proof
222-
/// @param _siblings The siblings of the proof
223-
function createRootMotion(
224-
address _altTarget,
225-
bytes memory _action,
226-
bytes memory _key,
227-
bytes memory _value,
228-
uint256 _branchMask,
229-
bytes32[] memory _siblings
230-
)
231-
public
232-
{
233-
uint256 rootSkillId = colony.getDomain(1).skillId;
234-
createMotion(_altTarget, _action, 1, rootSkillId, _key, _value, _branchMask, _siblings);
235-
}
236-
237-
/// @notice Create a motion in any domain
238-
/// @param _domainId The domain where we vote on the motion
239-
/// @param _childSkillIndex The childSkillIndex pointing to the domain of the action
240-
/// @param _action A bytes array encoding a function call
241-
/// @param _key Reputation tree key for the domain
242-
/// @param _value Reputation tree value for the domain
243-
/// @param _branchMask The branchmask of the proof
244-
/// @param _siblings The siblings of the proof
245-
function createDomainMotion(
246-
uint256 _domainId,
247-
uint256 _childSkillIndex,
248-
bytes memory _action,
249-
bytes memory _key,
250-
bytes memory _value,
251-
uint256 _branchMask,
252-
bytes32[] memory _siblings
253-
)
254-
public
255-
{
256-
// Check the function requires a non-root permission (and thus a domain proof)
257-
require(
258-
colony.getCapabilityRoles(getSig(_action)) | ROOT_ROLES != ROOT_ROLES,
259-
"voting-rep-invalid-function"
260-
);
261-
262-
uint256 domainSkillId = colony.getDomain(_domainId).skillId;
263-
uint256 actionDomainSkillId = getActionDomainSkillId(_action);
264-
265-
if (domainSkillId != actionDomainSkillId) {
266-
uint256 childSkillId = colonyNetwork.getChildSkillId(domainSkillId, _childSkillIndex);
267-
require(childSkillId == actionDomainSkillId, "voting-rep-invalid-domain-id");
268-
}
269-
270-
createMotion(address(0x0), _action, _domainId, domainSkillId, _key, _value, _branchMask, _siblings);
271-
}
216+
// MOTION CREATION & VOTING IMPLEMENTED IN SUBCLASSES
272217

273218
/// @notice Stake on a motion
274219
/// @param _motionId The id of the motion
@@ -294,23 +239,23 @@ contract VotingBase is ColonyExtension, PatriciaTreeProofs {
294239
public
295240
{
296241
Motion storage motion = motions[_motionId];
297-
require(_vote <= 1, "voting-rep-bad-vote");
298-
require(getMotionState(_motionId) == MotionState.Staking, "voting-rep-motion-not-staking");
242+
require(_vote <= 1, "voting-base-bad-vote");
243+
require(getMotionState(_motionId) == MotionState.Staking, "voting-base-motion-not-staking");
299244

300245
uint256 requiredStake = getRequiredStake(_motionId);
301246
uint256 amount = min(_amount, sub(requiredStake, motion.stakes[_vote]));
302-
require(amount > 0, "voting-rep-bad-amount");
247+
require(amount > 0, "voting-base-bad-amount");
303248

304249
uint256 stakerTotalAmount = add(stakes[_motionId][msg.sender][_vote], amount);
305250

306251
require(
307252
stakerTotalAmount <= getReputationFromProof(_motionId, msg.sender, _key, _value, _branchMask, _siblings),
308-
"voting-rep-insufficient-rep"
253+
"voting-base-insufficient-rep"
309254
);
310255
require(
311256
stakerTotalAmount >= wmul(requiredStake, userMinStakeFraction) ||
312257
add(motion.stakes[_vote], amount) == requiredStake, // To prevent a residual stake from being un-stakable
313-
"voting-rep-insufficient-stake"
258+
"voting-base-insufficient-stake"
314259
);
315260

316261
colony.obligateStake(msg.sender, motion.domainId, amount);
@@ -333,7 +278,7 @@ contract VotingBase is ColonyExtension, PatriciaTreeProofs {
333278
expenditureMotionCounts[structHash] = add(expenditureMotionCounts[structHash], 1);
334279
// Set to UINT256_MAX / 3 to avoid overflow (finalizedTimestamp + globalClaimDelay + claimDelay)
335280
bytes memory claimDelayAction = createClaimDelayAction(motion.action, UINT256_MAX / 3);
336-
require(executeCall(_motionId, claimDelayAction), "voting-rep-expenditure-lock-failed");
281+
require(executeCall(_motionId, claimDelayAction), "voting-base-expenditure-lock-failed");
337282
}
338283

339284
emit MotionStaked(_motionId, msg.sender, _vote, amount);
@@ -364,90 +309,6 @@ contract VotingBase is ColonyExtension, PatriciaTreeProofs {
364309

365310
}
366311

367-
/// @notice Submit a vote secret for a motion
368-
/// @param _motionId The id of the motion
369-
/// @param _voteSecret The hashed vote secret
370-
/// @param _key Reputation tree key for the staker/domain
371-
/// @param _value Reputation tree value for the staker/domain
372-
/// @param _branchMask The branchmask of the proof
373-
/// @param _siblings The siblings of the proof
374-
function submitVote(
375-
uint256 _motionId,
376-
bytes32 _voteSecret,
377-
bytes memory _key,
378-
bytes memory _value,
379-
uint256 _branchMask,
380-
bytes32[] memory _siblings
381-
)
382-
public
383-
{
384-
Motion storage motion = motions[_motionId];
385-
require(getMotionState(_motionId) == MotionState.Submit, "voting-rep-motion-not-open");
386-
require(_voteSecret != bytes32(0), "voting-rep-invalid-secret");
387-
388-
uint256 userRep = getReputationFromProof(_motionId, msg.sender, _key, _value, _branchMask, _siblings);
389-
390-
// Count reputation if first submission
391-
if (voteSecrets[_motionId][msg.sender] == bytes32(0)) {
392-
motion.totalVotes = add(motion.totalVotes, userRep);
393-
}
394-
395-
voteSecrets[_motionId][msg.sender] = _voteSecret;
396-
397-
emit MotionVoteSubmitted(_motionId, msg.sender);
398-
399-
if (motion.totalVotes >= wmul(motion.skillRep, maxVoteFraction)) {
400-
motion.events[SUBMIT_END] = uint64(block.timestamp);
401-
motion.events[REVEAL_END] = uint64(block.timestamp + revealPeriod);
402-
403-
emit MotionEventSet(_motionId, SUBMIT_END);
404-
}
405-
}
406-
407-
/// @notice Reveal a vote secret for a motion
408-
/// @param _motionId The id of the motion
409-
/// @param _salt The salt used to hash the vote
410-
/// @param _vote The side being supported (0 = NAY, 1 = YAY)
411-
/// @param _key Reputation tree key for the staker/domain
412-
/// @param _value Reputation tree value for the staker/domain
413-
/// @param _branchMask The branchmask of the proof
414-
/// @param _siblings The siblings of the proof
415-
function revealVote(
416-
uint256 _motionId,
417-
bytes32 _salt,
418-
uint256 _vote,
419-
bytes memory _key,
420-
bytes memory _value,
421-
uint256 _branchMask,
422-
bytes32[] memory _siblings
423-
)
424-
public
425-
{
426-
Motion storage motion = motions[_motionId];
427-
require(getMotionState(_motionId) == MotionState.Reveal, "voting-rep-motion-not-reveal");
428-
require(_vote <= 1, "voting-rep-bad-vote");
429-
430-
uint256 userRep = getReputationFromProof(_motionId, msg.sender, _key, _value, _branchMask, _siblings);
431-
motion.votes[_vote] = add(motion.votes[_vote], userRep);
432-
433-
bytes32 voteSecret = voteSecrets[_motionId][msg.sender];
434-
require(voteSecret == getVoteSecret(_salt, _vote), "voting-rep-secret-no-match");
435-
delete voteSecrets[_motionId][msg.sender];
436-
437-
uint256 voterReward = getVoterReward(_motionId, userRep);
438-
motion.paidVoterComp = add(motion.paidVoterComp, voterReward);
439-
440-
emit MotionVoteRevealed(_motionId, msg.sender, _vote);
441-
442-
// See if reputation revealed matches reputation submitted
443-
if (add(motion.votes[NAY], motion.votes[YAY]) == motion.totalVotes) {
444-
motion.events[REVEAL_END] = uint64(block.timestamp);
445-
446-
emit MotionEventSet(_motionId, REVEAL_END);
447-
}
448-
tokenLocking.transfer(token, voterReward, msg.sender, true);
449-
}
450-
451312
/// @notice Escalate a motion to a higher domain
452313
/// @param _motionId The id of the motion
453314
/// @param _newDomainId The desired domain of escalation
@@ -468,16 +329,16 @@ contract VotingBase is ColonyExtension, PatriciaTreeProofs {
468329
public
469330
{
470331
Motion storage motion = motions[_motionId];
471-
require(getMotionState(_motionId) == MotionState.Closed, "voting-rep-motion-not-closed");
332+
require(getMotionState(_motionId) == MotionState.Closed, "voting-base-motion-not-closed");
472333

473334
uint256 newDomainSkillId = colony.getDomain(_newDomainId).skillId;
474335
uint256 childSkillId = colonyNetwork.getChildSkillId(newDomainSkillId, _childSkillIndex);
475-
require(childSkillId == motion.skillId, "voting-rep-invalid-domain-proof");
336+
require(childSkillId == motion.skillId, "voting-base-invalid-domain-proof");
476337

477338
uint256 domainId = motion.domainId;
478339
motion.domainId = _newDomainId;
479340
motion.skillId = newDomainSkillId;
480-
motion.skillRep = getReputationFromProof(_motionId, address(0x0), _key, _value, _branchMask, _siblings);
341+
motion.maxVotes = getReputationFromProof(_motionId, address(0x0), _key, _value, _branchMask, _siblings);
481342

482343
uint256 loser = (motion.votes[NAY] < motion.votes[YAY]) ? NAY : YAY;
483344
motion.stakes[loser] = sub(motion.stakes[loser], motion.paidVoterComp);
@@ -502,7 +363,7 @@ contract VotingBase is ColonyExtension, PatriciaTreeProofs {
502363

503364
function finalizeMotion(uint256 _motionId) public {
504365
Motion storage motion = motions[_motionId];
505-
require(getMotionState(_motionId) == MotionState.Finalizable, "voting-rep-motion-not-finalizable");
366+
require(getMotionState(_motionId) == MotionState.Finalizable, "voting-base-motion-not-finalizable");
506367

507368
assert(
508369
motion.stakes[YAY] == getRequiredStake(_motionId) ||
@@ -567,12 +428,12 @@ contract VotingBase is ColonyExtension, PatriciaTreeProofs {
567428
require(
568429
getMotionState(_motionId) == MotionState.Finalized ||
569430
getMotionState(_motionId) == MotionState.Failed,
570-
"voting-rep-motion-not-claimable"
431+
"voting-base-motion-not-claimable"
571432
);
572433

573434
(uint256 stakerReward, uint256 repPenalty) = getStakerReward(_motionId, _staker, _vote);
574435

575-
require(stakerReward > 0, "voting-rep-nothing-to-claim");
436+
require(stakerReward > 0, "voting-base-nothing-to-claim");
576437
delete stakes[_motionId][_staker][_vote];
577438

578439
tokenLocking.transfer(token, stakerReward, _staker, true);
@@ -737,7 +598,7 @@ contract VotingBase is ColonyExtension, PatriciaTreeProofs {
737598
/// @return The voter reward
738599
function getVoterReward(uint256 _motionId, uint256 _voterRep) public view returns (uint256) {
739600
Motion storage motion = motions[_motionId];
740-
uint256 fractionUserReputation = wdiv(_voterRep, motion.skillRep);
601+
uint256 fractionUserReputation = wdiv(_voterRep, motion.maxVotes);
741602
uint256 totalStake = add(motion.stakes[YAY], motion.stakes[NAY]);
742603
return wmul(wmul(fractionUserReputation, totalStake), voterRewardFraction);
743604
}
@@ -817,18 +678,13 @@ contract VotingBase is ColonyExtension, PatriciaTreeProofs {
817678
function createMotion(
818679
address _altTarget,
819680
bytes memory _action,
820-
uint256 _domainId,
821-
uint256 _skillId,
822-
bytes memory _key,
823-
bytes memory _value,
824-
uint256 _branchMask,
825-
bytes32[] memory _siblings
681+
uint256 _domainId
826682
)
827683
internal
828684
notDeprecated
829685
{
830-
require(state == ExtensionState.Active, "voting-rep-not-active");
831-
require(_altTarget != address(colony), "voting-rep-alt-target-cannot-be-base-colony");
686+
require(state == ExtensionState.Active, "voting-base-not-active");
687+
require(_altTarget != address(colony), "voting-base-alt-target-cannot-be-base-colony");
832688

833689
motionCount += 1;
834690
Motion storage motion = motions[motionCount];
@@ -839,9 +695,8 @@ contract VotingBase is ColonyExtension, PatriciaTreeProofs {
839695

840696
motion.rootHash = colonyNetwork.getReputationRootHash();
841697
motion.domainId = _domainId;
842-
motion.skillId = _skillId;
698+
motion.skillId = colony.getDomain(_domainId).skillId;
843699

844-
motion.skillRep = getReputationFromProof(motionCount, address(0x0), _key, _value, _branchMask, _siblings);
845700
motion.altTarget = _altTarget;
846701
motion.action = _action;
847702

@@ -853,7 +708,7 @@ contract VotingBase is ColonyExtension, PatriciaTreeProofs {
853708
}
854709

855710
function getRequiredStake(uint256 _motionId) internal view returns (uint256) {
856-
return wmul(motions[_motionId].skillRep, totalStakeFraction);
711+
return wmul(motions[_motionId].maxVotes, totalStakeFraction);
857712
}
858713

859714
function flip(uint256 _vote) internal pure returns (uint256) {
@@ -871,7 +726,7 @@ contract VotingBase is ColonyExtension, PatriciaTreeProofs {
871726
internal view returns (uint256)
872727
{
873728
bytes32 impliedRoot = getImpliedRootHashKey(_key, _value, _branchMask, _siblings);
874-
require(motions[_motionId].rootHash == impliedRoot, "voting-rep-invalid-root-hash");
729+
require(motions[_motionId].rootHash == impliedRoot, "voting-base-invalid-root-hash");
875730

876731
uint256 reputationValue;
877732
address keyColonyAddress;
@@ -885,9 +740,9 @@ contract VotingBase is ColonyExtension, PatriciaTreeProofs {
885740
keyUserAddress := mload(add(_key, 72))
886741
}
887742

888-
require(keyColonyAddress == address(colony), "voting-rep-invalid-colony-address");
889-
require(keySkill == motions[_motionId].skillId, "voting-rep-invalid-skill-id");
890-
require(keyUserAddress == _who, "voting-rep-invalid-user-address");
743+
require(keyColonyAddress == address(colony), "voting-base-invalid-colony-address");
744+
require(keySkill == motions[_motionId].skillId, "voting-base-invalid-skill-id");
745+
require(keyUserAddress == _who, "voting-base-invalid-user-address");
891746

892747
return reputationValue;
893748
}

0 commit comments

Comments
 (0)