get candidates list

This commit is contained in:
Nguyen Sy Thanh Son 2018-04-17 07:23:26 +00:00
parent 53e2f7cd46
commit 246d187294
4 changed files with 23 additions and 117 deletions

View file

@ -7,60 +7,45 @@ contract TomoValidator is IValidator {
using SafeMath for uint256; using SafeMath for uint256;
struct ValidatorState { struct ValidatorState {
bool isValidator;
bool isCandidate; bool isCandidate;
uint256 cap; uint256 cap;
mapping(address => uint256) voters; mapping(address => uint256) voters;
} }
mapping(address => ValidatorState) validatorsState; mapping(address => ValidatorState) validatorsState;
address[] public validators;
address[] public candidates; address[] public candidates;
uint256 threshold = 1000 * 10** 18; // 1000 TOMO uint256 candidateCount = 0;
function TomoValidator(address[] _validators, uint256[] _caps) public { function TomoValidator(address[] _candidates, uint256[] _caps) public {
validators = _validators; candidates = _candidates;
candidates = _validators;
for (uint256 i = 0; i < _validators.length; i++) { for (uint256 i = 0; i < _candidates.length; i++) {
validatorsState[_validators[i]] = ValidatorState({ validatorsState[_candidates[i]] = ValidatorState({
isValidator: true,
isCandidate: true, isCandidate: true,
cap: _caps[i] cap: _caps[i]
}); });
candidateCount = candidateCount + 1;
} }
} }
function propose(address _candidate) external payable { function propose(address _candidate) external payable {
// only validator can propose a candidate // TOMO: only validator can propose a candidate
require(validatorsState[msg.sender].isValidator);
if (!validatorsState[_candidate].isCandidate) { if (!validatorsState[_candidate].isCandidate) {
candidates.push(_candidate); candidates.push(_candidate);
} }
validatorsState[_candidate] = ValidatorState({ validatorsState[_candidate] = ValidatorState({
isValidator: false,
isCandidate: true, isCandidate: true,
cap: msg.value cap: msg.value
}); });
candidateCount = candidateCount + 1;
} }
function vote(address _candidate) public payable { function vote(address _candidate) public payable {
// only vote for candidate proposed by a validator // only vote for candidate proposed by a validator
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) { validatorsState[_candidate].voters[msg.sender] = validatorsState[_candidate].voters[msg.sender].add(msg.value);
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[]) { function getCandidates() public view returns(address[]) {
@ -75,17 +60,11 @@ contract TomoValidator is IValidator {
return validatorsState[_candidate].voters[_voter]; 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) { function isCandidate(address _candidate) public view returns(bool) {
return validatorsState[_candidate].isCandidate; return validatorsState[_candidate].isCandidate;
} }
function unvote(address _candidate, uint256 _cap) public { 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].isCandidate);
require(validatorsState[_candidate].voters[msg.sender] >= _cap); require(validatorsState[_candidate].voters[msg.sender] >= _cap);
validatorsState[_candidate].cap = validatorsState[_candidate].cap.sub(_cap); validatorsState[_candidate].cap = validatorsState[_candidate].cap.sub(_cap);

File diff suppressed because one or more lines are too long

View file

@ -4,6 +4,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/contracts/validator/contract" "github.com/ethereum/go-ethereum/contracts/validator/contract"
"math/big"
) )
type Validator struct { type Validator struct {
@ -26,8 +27,8 @@ func NewValidator(transactOpts *bind.TransactOpts, contractAddr common.Address,
}, nil }, nil
} }
func DeployValidator(transactOpts *bind.TransactOpts, contractBackend bind.ContractBackend) (common.Address, *Validator, error) { func DeployValidator(transactOpts *bind.TransactOpts, contractBackend bind.ContractBackend, candidates []common.Address, caps []*big.Int) (common.Address, *Validator, error) {
validatorAddr, _, _, err := contract.DeployTomoValidator(transactOpts, contractBackend, nil, nil) validatorAddr, _, _, err := contract.DeployTomoValidator(transactOpts, contractBackend, candidates, caps)
if err != nil { if err != nil {
return validatorAddr, nil, err return validatorAddr, nil, err
} }

View file

@ -4,8 +4,10 @@ import (
"math/big" "math/big"
"testing" "testing"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends" "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/core"
"github.com/ethereum/go-ethereum/crypto" "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)}}) contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}})
transactOpts := bind.NewKeyedTransactor(key) 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 { if err != nil {
t.Fatalf("can't deploy root registry: %v", err) t.Fatalf("can't deploy root registry: %v", err)
} }
contractBackend.Commit() contractBackend.Commit()
if _, err := validator.GetValidators(); err != nil { candidates, err := validator.GetCandidates()
t.Fatalf("can't get validators: %v", err) if err != nil {
t.Fatalf("can't get candidates: %v", err)
} }
fmt.Println("candidates", candidates)
contractBackend.Commit() contractBackend.Commit()
} }