mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 09:53:48 +00:00
get candidates list
This commit is contained in:
parent
53e2f7cd46
commit
246d187294
4 changed files with 23 additions and 117 deletions
|
|
@ -7,61 +7,46 @@ contract TomoValidator 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 TOMO
|
||||
uint256 candidateCount = 0;
|
||||
|
||||
function TomoValidator(address[] _validators, uint256[] _caps) public {
|
||||
validators = _validators;
|
||||
candidates = _validators;
|
||||
function TomoValidator(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);
|
||||
// TOMO: 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;
|
||||
}
|
||||
|
||||
function getCandidates() public view returns(address[]) {
|
||||
return candidates;
|
||||
|
|
@ -75,17 +60,11 @@ contract TomoValidator 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);
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -4,6 +4,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/contracts/validator/contract"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
type Validator struct {
|
||||
|
|
@ -26,8 +27,8 @@ func NewValidator(transactOpts *bind.TransactOpts, contractAddr common.Address,
|
|||
}, nil
|
||||
}
|
||||
|
||||
func DeployValidator(transactOpts *bind.TransactOpts, contractBackend bind.ContractBackend) (common.Address, *Validator, error) {
|
||||
validatorAddr, _, _, err := contract.DeployTomoValidator(transactOpts, contractBackend, nil, nil)
|
||||
func DeployValidator(transactOpts *bind.TransactOpts, contractBackend bind.ContractBackend, candidates []common.Address, caps []*big.Int) (common.Address, *Validator, error) {
|
||||
validatorAddr, _, _, err := contract.DeployTomoValidator(transactOpts, contractBackend, candidates, caps)
|
||||
if err != nil {
|
||||
return validatorAddr, nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,8 +4,10 @@ import (
|
|||
"math/big"
|
||||
"testing"
|
||||
|
||||
"fmt"
|
||||
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
||||
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
)
|
||||
|
|
@ -19,14 +21,16 @@ func TestENS(t *testing.T) {
|
|||
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}})
|
||||
transactOpts := bind.NewKeyedTransactor(key)
|
||||
|
||||
_, validator, err := DeployValidator(transactOpts, contractBackend)
|
||||
_, validator, err := DeployValidator(transactOpts, contractBackend, []common.Address{addr}, []*big.Int{big.NewInt(0)})
|
||||
if err != nil {
|
||||
t.Fatalf("can't deploy root registry: %v", err)
|
||||
}
|
||||
contractBackend.Commit()
|
||||
|
||||
if _, err := validator.GetValidators(); err != nil {
|
||||
t.Fatalf("can't get validators: %v", err)
|
||||
candidates, err := validator.GetCandidates()
|
||||
if err != nil {
|
||||
t.Fatalf("can't get candidates: %v", err)
|
||||
}
|
||||
fmt.Println("candidates", candidates)
|
||||
contractBackend.Commit()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue