mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 09:53:48 +00:00
update validator smart contract
This commit is contained in:
parent
1d24f2f33d
commit
29a7cf8f2b
2 changed files with 279 additions and 3 deletions
|
|
@ -10,12 +10,17 @@ contract TomoValidator is IValidator {
|
||||||
bool isValidator;
|
bool isValidator;
|
||||||
bool isCandidate;
|
bool isCandidate;
|
||||||
uint256 cap;
|
uint256 cap;
|
||||||
|
mapping(address => uint256) voters;
|
||||||
}
|
}
|
||||||
|
|
||||||
mapping(address => ValidatorState) validatorsState;
|
mapping(address => ValidatorState) validatorsState;
|
||||||
|
address[] public validators;
|
||||||
|
address[] public candidates;
|
||||||
uint256 threshold = 1000 * 10** 18; // 1000 TOMO
|
uint256 threshold = 1000 * 10** 18; // 1000 TOMO
|
||||||
|
|
||||||
function TomoValidator(address[] _validators, uint256[] _caps) public {
|
function TomoValidator(address[] _validators, uint256[] _caps) public {
|
||||||
|
validators = _validators;
|
||||||
|
candidates = _validators;
|
||||||
|
|
||||||
for (uint256 i = 0; i < _validators.length; i++) {
|
for (uint256 i = 0; i < _validators.length; i++) {
|
||||||
validatorsState[_validators[i]] = ValidatorState({
|
validatorsState[_validators[i]] = ValidatorState({
|
||||||
|
|
@ -30,6 +35,9 @@ contract TomoValidator is IValidator {
|
||||||
function propose(address _candidate) external payable {
|
function propose(address _candidate) external payable {
|
||||||
// only validator can propose a candidate
|
// only validator can propose a candidate
|
||||||
require(validatorsState[msg.sender].isValidator);
|
require(validatorsState[msg.sender].isValidator);
|
||||||
|
if (!validatorsState[_candidate].isCandidate) {
|
||||||
|
candidates.push(_candidate);
|
||||||
|
}
|
||||||
validatorsState[_candidate] = ValidatorState({
|
validatorsState[_candidate] = ValidatorState({
|
||||||
isValidator: false,
|
isValidator: false,
|
||||||
isCandidate: true,
|
isCandidate: true,
|
||||||
|
|
@ -43,7 +51,46 @@ contract TomoValidator is IValidator {
|
||||||
require(validatorsState[_candidate].isCandidate);
|
require(validatorsState[_candidate].isCandidate);
|
||||||
validatorsState[_candidate].cap = validatorsState[_candidate].cap.add(msg.value);
|
validatorsState[_candidate].cap = validatorsState[_candidate].cap.add(msg.value);
|
||||||
if (validatorsState[_candidate].cap >= threshold) {
|
if (validatorsState[_candidate].cap >= threshold) {
|
||||||
validatorsState[_candidate].isValidator = true;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCandidates() public view returns(address[]) {
|
||||||
|
return candidates;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCandidateCap(address _candidate) public view returns(uint256) {
|
||||||
|
return validatorsState[_candidate].cap;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getVoterCap(address _candidate, address _voter) public view returns(uint256) {
|
||||||
|
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);
|
||||||
|
validatorsState[_candidate].voters[msg.sender] = validatorsState[_candidate].voters[msg.sender].sub(_cap);
|
||||||
|
// refunding to user after unvoting
|
||||||
|
msg.sender.transfer(_cap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue