mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 09:53:48 +00:00
update list of voters
This commit is contained in:
parent
101b4e3840
commit
51bac6108a
2 changed files with 324 additions and 3 deletions
|
|
@ -8,6 +8,8 @@ contract TomoValidator is IValidator {
|
|||
|
||||
event Vote(address _candidate, uint256 _cap);
|
||||
event Unvote(address _candidate, uint256 _cap);
|
||||
event Propose(address _candidate, uint256 _cap);
|
||||
event Retire(address _candidate, uint256 _cap);
|
||||
|
||||
struct ValidatorState {
|
||||
bool isCandidate;
|
||||
|
|
@ -16,6 +18,7 @@ contract TomoValidator is IValidator {
|
|||
}
|
||||
|
||||
mapping(address => ValidatorState) validatorsState;
|
||||
mapping(address => address[]) voters;
|
||||
address[] public candidates;
|
||||
uint256 candidateCount = 0;
|
||||
uint256 public constant minCandidateCap = 10000 ether;
|
||||
|
|
@ -45,13 +48,16 @@ contract TomoValidator is IValidator {
|
|||
isCandidate: true,
|
||||
cap: msg.value
|
||||
});
|
||||
validatorsState[msg.sender].voters[msg.sender] = msg.value;
|
||||
candidateCount = candidateCount + 1;
|
||||
emit Propose(msg.sender, msg.value);
|
||||
}
|
||||
|
||||
function vote(address _candidate) external payable {
|
||||
require(validatorsState[_candidate].isCandidate);
|
||||
validatorsState[_candidate].cap = validatorsState[_candidate].cap.add(msg.value);
|
||||
validatorsState[_candidate].voters[msg.sender] = validatorsState[_candidate].voters[msg.sender].add(msg.value);
|
||||
voters[_candidate].push(msg.sender);
|
||||
emit Vote(_candidate, msg.value);
|
||||
}
|
||||
|
||||
|
|
@ -67,12 +73,15 @@ contract TomoValidator is IValidator {
|
|||
return validatorsState[_candidate].voters[_voter];
|
||||
}
|
||||
|
||||
function getVoters(address _candidate) public view returns(address[]) {
|
||||
return voters[_candidate];
|
||||
}
|
||||
|
||||
function isCandidate(address _candidate) public view returns(bool) {
|
||||
return validatorsState[_candidate].isCandidate;
|
||||
}
|
||||
|
||||
function unvote(address _candidate, uint256 _cap) public {
|
||||
require(validatorsState[_candidate].isCandidate);
|
||||
require(validatorsState[_candidate].voters[msg.sender] >= _cap);
|
||||
validatorsState[_candidate].cap = validatorsState[_candidate].cap.sub(_cap);
|
||||
validatorsState[_candidate].voters[msg.sender] = validatorsState[_candidate].voters[msg.sender].sub(_cap);
|
||||
|
|
@ -80,4 +89,23 @@ contract TomoValidator is IValidator {
|
|||
msg.sender.transfer(_cap);
|
||||
emit Unvote(_candidate, _cap);
|
||||
}
|
||||
|
||||
function retire() public {
|
||||
require(validatorsState[msg.sender].isCandidate);
|
||||
uint256 cap = validatorsState[msg.sender].voters[msg.sender];
|
||||
validatorsState[msg.sender].cap = validatorsState[msg.sender].cap.sub(cap);
|
||||
validatorsState[msg.sender].voters[msg.sender] = 0;
|
||||
validatorsState[msg.sender].isCandidate = false;
|
||||
candidateCount = candidateCount - 1;
|
||||
for (uint256 i = 0; i < candidates.length; i++) {
|
||||
if (candidates[i] == msg.sender) {
|
||||
delete candidates[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
// refunding to user after retiring
|
||||
msg.sender.transfer(cap);
|
||||
emit Retire(msg.sender, cap);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue