update new Tomo Validator

This commit is contained in:
Nguyen Sy Thanh Son 2018-07-16 08:03:44 +00:00
parent cbb706e435
commit 4cc12d3ed6
4 changed files with 26 additions and 288 deletions

View file

@ -1,9 +1,8 @@
pragma solidity ^0.4.21;
import "./interfaces/IValidator.sol";
import "./libs/SafeMath.sol";
contract TomoValidator is IValidator {
contract TomoValidator {
using SafeMath for uint256;
event Vote(address _voter, address _candidate, uint256 _cap);
@ -18,7 +17,6 @@ contract TomoValidator is IValidator {
string nodeUrl;
bool isCandidate;
uint256 cap;
uint256 withdrawBlockNumber;
mapping(address => uint256) voters;
}
@ -31,13 +29,8 @@ contract TomoValidator is IValidator {
mapping(address => ValidatorState) validatorsState;
mapping(address => address[]) voters;
address[] public candidates = [
0xf99805B536609cC03AcBB2604dFaC11E9E54a448,
0x31b249fE6F267aa2396Eb2DC36E9c79351d97Ec5,
0xfC5571921c6d3672e13B58EA23DEA534f2b35fA0
];
address[] public candidates;
address public firstOwner = 0x487d62d33467c4842c5e54Eb370837E4E88BBA0F;
uint256 public candidateCount = 3;
uint256 public minCandidateCap;
uint256 public maxValidatorNumber;
@ -84,6 +77,9 @@ contract TomoValidator is IValidator {
}
function TomoValidator (
address[] _candidates,
uint256[] _caps,
address _firstOwner,
uint256 _minCandidateCap,
uint256 _maxValidatorNumber,
uint256 _candidateWithdrawDelay,
@ -94,16 +90,16 @@ contract TomoValidator is IValidator {
candidateWithdrawDelay = _candidateWithdrawDelay;
voterWithdrawDelay = _voterWithdrawDelay;
for (uint256 i = 0; i < candidates.length; i++) {
validatorsState[candidates[i]] = ValidatorState({
owner: firstOwner,
for (uint256 i = 0; i < _candidates.length; i++) {
candidates.push(_candidates[i]);
validatorsState[_candidates[i]] = ValidatorState({
owner: _firstOwner,
nodeUrl: '',
isCandidate: true,
withdrawBlockNumber: 0,
cap: minCandidateCap
cap: _caps[i]
});
voters[candidates[i]].push(firstOwner);
validatorsState[candidates[i]].voters[firstOwner] = minCandidateCap;
voters[_candidates[i]].push(_firstOwner);
validatorsState[candidates[i]].voters[_firstOwner] = minCandidateCap;
}
}
@ -113,7 +109,6 @@ contract TomoValidator is IValidator {
owner: msg.sender,
nodeUrl: _nodeUrl,
isCandidate: true,
withdrawBlockNumber: 0,
cap: msg.value
});
validatorsState[_candidate].voters[msg.sender] = msg.value;
@ -147,10 +142,6 @@ contract TomoValidator is IValidator {
return validatorsState[_candidate].owner;
}
function getCandidateWithdrawBlockNumber(address _candidate) public view returns(uint256) {
return validatorsState[_candidate].withdrawBlockNumber;
}
function getVoterCap(address _candidate, address _voter) public view returns(uint256) {
return validatorsState[_candidate].voters[_voter];
}

File diff suppressed because one or more lines are too long

View file

@ -1,7 +1,6 @@
package validator
import (
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/contracts/validator/contract"
@ -28,11 +27,10 @@ func NewValidator(transactOpts *bind.TransactOpts, contractAddr common.Address,
}, nil
}
func DeployValidator(transactOpts *bind.TransactOpts, contractBackend bind.ContractBackend) (common.Address, *Validator, error) {
func DeployValidator(transactOpts *bind.TransactOpts, contractBackend bind.ContractBackend, validatorAddress []common.Address, caps []*big.Int, ownerAddress common.Address) (common.Address, *Validator, error) {
minDeposit := new(big.Int)
minDeposit.SetString("50000000000000000000000", 10)
fmt.Println("--->", common.BigToHash(minDeposit).Hex())
validatorAddr, _, _, err := contract.DeployTomoValidator(transactOpts, contractBackend, minDeposit, big.NewInt(99), big.NewInt(100), big.NewInt(100))
validatorAddr, _, _, err := contract.DeployTomoValidator(transactOpts, contractBackend, validatorAddress, caps, ownerAddress, minDeposit, big.NewInt(99), big.NewInt(100), big.NewInt(100))
if err != nil {
return validatorAddr, nil, err
}

View file

@ -9,11 +9,11 @@ import (
"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/contracts"
"github.com/ethereum/go-ethereum/contracts/validator/contract"
// "github.com/ethereum/go-ethereum/contracts"
// "github.com/ethereum/go-ethereum/contracts/validator/contract"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
"math/rand"
// "math/rand"
)
var (
@ -33,7 +33,9 @@ func TestValidator(t *testing.T) {
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}})
transactOpts := bind.NewKeyedTransactor(key)
validatorAddress, validator, err := DeployValidator(transactOpts, contractBackend)
validatorCap := new(big.Int)
validatorCap.SetString("50000000000000000000000", 10)
validatorAddress, validator, err := DeployValidator(transactOpts, contractBackend, []common.Address{addr}, []*big.Int{validatorCap}, addr)
if err != nil {
t.Fatalf("can't deploy root registry: %v", err)
}
@ -63,6 +65,7 @@ func TestValidator(t *testing.T) {
contractBackend.Commit()
}
/*
func TestRewardBalance(t *testing.T) {
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{
acc1Addr: {Balance: new(big.Int).SetUint64(10000000)},
@ -143,3 +146,4 @@ func TestRewardBalance(t *testing.T) {
}
}
*/