Skip to content

get candidates list #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 9 additions & 30 deletions contracts/validator/contract/xdcValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,45 @@ contract xdcValidator is IValidator {
using SafeMath for uint256;

struct ValidatorState {
bool isValidator;
bool isCandidate;
uint256 cap;
mapping(address => uint256) voters;
}

mapping(address => ValidatorState) validatorsState;
address[] public validators;
address[] public candidates;
uint256 threshold = 1000 * 10** 18; // 1000 xdc
uint256 candidateCount = 0;

function xdcValidator(address[] _validators, uint256[] _caps) public {
validators = _validators;
candidates = _validators;
function xdcValidator(address[] _candidates, uint256[] _caps) public {
candidates = _candidates;

for (uint256 i = 0; i < _validators.length; i++) {
validatorsState[_validators[i]] = ValidatorState({
isValidator: true,
for (uint256 i = 0; i < _candidates.length; i++) {
validatorsState[_candidates[i]] = ValidatorState({
isCandidate: true,
cap: _caps[i]
});
candidateCount = candidateCount + 1;
}

}

function propose(address _candidate) external payable {
// only validator can propose a candidate
require(validatorsState[msg.sender].isValidator);
// xdc: only validator can propose a candidate
if (!validatorsState[_candidate].isCandidate) {
candidates.push(_candidate);
}
validatorsState[_candidate] = ValidatorState({
isValidator: false,
isCandidate: true,
cap: msg.value
});

candidateCount = candidateCount + 1;
}

function vote(address _candidate) public payable {
// only vote for candidate proposed by a validator
require(validatorsState[_candidate].isCandidate);
validatorsState[_candidate].cap = validatorsState[_candidate].cap.add(msg.value);
if (validatorsState[_candidate].cap >= threshold) {
if (!validatorsState[_candidate].isValidator) {
validators.push(_candidate);
}
validatorsState[_candidate].isValidator = true;
validatorsState[_candidate].voters[msg.sender] = validatorsState[_candidate].voters[msg.sender].add(msg.value);
}
}

function getValidators() public view returns(address[]) {
return validators;
validatorsState[_candidate].voters[msg.sender] = validatorsState[_candidate].voters[msg.sender].add(msg.value);
}

function getCandidates() public view returns(address[]) {
Expand All @@ -75,17 +60,11 @@ contract xdcValidator is IValidator {
return validatorsState[_candidate].voters[_voter];
}

function isValidator(address _candidate) public view returns(bool) {
return validatorsState[_candidate].isValidator;
}

function isCandidate(address _candidate) public view returns(bool) {
return validatorsState[_candidate].isCandidate;
}

function unvote(address _candidate, uint256 _cap) public {
// only unvote for candidate who does not become validator yet
require(!validatorsState[_candidate].isValidator);
require(validatorsState[_candidate].isCandidate);
require(validatorsState[_candidate].voters[msg.sender] >= _cap);
validatorsState[_candidate].cap = validatorsState[_candidate].cap.sub(_cap);
Expand Down