From 5a667983c3291f3efe6c3b252a85e406ea52555f Mon Sep 17 00:00:00 2001 From: AnilChinchawale Date: Tue, 15 Jan 2019 08:37:24 +0530 Subject: [PATCH] Updated SafeMath & blockSigner, Test added for Validator --- contracts/blocksigner/blocksigner.go | 17 +++- .../blocksigner/contract/libs/SafeMath.sol | 12 +-- contracts/validator/validator_test.go | 99 ++++--------------- 3 files changed, 42 insertions(+), 86 deletions(-) diff --git a/contracts/blocksigner/blocksigner.go b/contracts/blocksigner/blocksigner.go index 34a13d6091..557045f45c 100644 --- a/contracts/blocksigner/blocksigner.go +++ b/contracts/blocksigner/blocksigner.go @@ -1,3 +1,18 @@ +// Copyright (c) 2018 XDCchain +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . + package blocksigner import ( @@ -39,4 +54,4 @@ func DeployBlockSigner(transactOpts *bind.TransactOpts, contractBackend bind.Con } return blockSignerAddr, blockSigner, nil -} \ No newline at end of file +} diff --git a/contracts/blocksigner/contract/libs/SafeMath.sol b/contracts/blocksigner/contract/libs/SafeMath.sol index 316726acf5..9c9a2bd22f 100644 --- a/contracts/blocksigner/contract/libs/SafeMath.sol +++ b/contracts/blocksigner/contract/libs/SafeMath.sol @@ -1,12 +1,12 @@ pragma solidity ^0.4.21; - /** +/** * @title SafeMath * @dev Math operations with safety checks that throw on error */ library SafeMath { - /** + /** * @dev Multiplies two numbers, throws on overflow. */ function mul(uint256 a, uint256 b) internal pure returns (uint256) { @@ -18,7 +18,7 @@ library SafeMath { return c; } - /** + /** * @dev Integer division of two numbers, truncating the quotient. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { @@ -28,7 +28,7 @@ library SafeMath { return a / b; } - /** + /** * @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend). */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { @@ -36,7 +36,7 @@ library SafeMath { return a - b; } - /** + /** * @dev Adds two numbers, throws on overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { @@ -44,4 +44,4 @@ library SafeMath { assert(c >= a); return c; } -} \ No newline at end of file +} diff --git a/contracts/validator/validator_test.go b/contracts/validator/validator_test.go index e0106bd8df..0f74d9b029 100644 --- a/contracts/validator/validator_test.go +++ b/contracts/validator/validator_test.go @@ -1,20 +1,34 @@ +// Copyright (c) 2018 XDCchain +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . + package validator import ( "context" - "encoding/json" "math/big" - "math/rand" "testing" "time" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind/backends" "github.com/ethereum/go-ethereum/common" - contractValidator "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" - "github.com/ethereum/go-ethereum/log" + "math/rand" ) var ( @@ -80,7 +94,7 @@ func TestRewardBalance(t *testing.T) { // validatorAddr, _, baseValidator, err := contract.DeployXDCValidator(transactOpts, contractBackend, big.NewInt(50000), big.NewInt(99), big.NewInt(100), big.NewInt(100)) validatorCap := new(big.Int) validatorCap.SetString("50000000000000000000000", 10) - validatorAddr, _, baseValidator, err := contractValidator.DeployXDCValidator( + validatorAddr, _, baseValidator, err := contract.DeployXDCValidator( transactOpts, contractBackend, []common.Address{addr}, @@ -130,7 +144,7 @@ func TestRewardBalance(t *testing.T) { foundationAddr := common.HexToAddress(common.FoudationAddr) totalReward := new(big.Int).SetInt64(15 * 1000) - rewards, err := GetRewardBalancesRate(foundationAddr, acc3Addr, totalReward, baseValidator) + rewards, err := contracts.GetRewardBalancesRate(foundationAddr, acc3Addr, totalReward, baseValidator) if err != nil { t.Error("Fail to get reward balances rate.", err) } @@ -161,76 +175,3 @@ func TestRewardBalance(t *testing.T) { } } - -func GetRewardBalancesRate(foudationWalletAddr common.Address, masterAddr common.Address, totalReward *big.Int, validator *contractValidator.XDCValidator) (map[common.Address]*big.Int, error) { - owner := GetCandidatesOwnerBySigner(validator, masterAddr) - balances := make(map[common.Address]*big.Int) - rewardMaster := new(big.Int).Mul(totalReward, new(big.Int).SetInt64(common.RewardMasterPercent)) - rewardMaster = new(big.Int).Div(rewardMaster, new(big.Int).SetInt64(100)) - balances[owner] = rewardMaster - // Get voters for masternode. - opts := new(bind.CallOpts) - voters, err := validator.GetVoters(opts, masterAddr) - if err != nil { - log.Crit("Fail to get voters", "error", err) - return nil, err - } - - if len(voters) > 0 { - totalVoterReward := new(big.Int).Mul(totalReward, new(big.Int).SetUint64(common.RewardVoterPercent)) - totalVoterReward = new(big.Int).Div(totalVoterReward, new(big.Int).SetUint64(100)) - totalCap := new(big.Int) - // Get voters capacities. - voterCaps := make(map[common.Address]*big.Int) - for _, voteAddr := range voters { - var voterCap *big.Int - - voterCap, err = validator.GetVoterCap(opts, masterAddr, voteAddr) - if err != nil { - log.Crit("Fail to get vote capacity", "error", err) - } - - totalCap.Add(totalCap, voterCap) - voterCaps[voteAddr] = voterCap - } - if totalCap.Cmp(new(big.Int).SetInt64(0)) > 0 { - for addr, voteCap := range voterCaps { - // Only valid voter has cap > 0. - if voteCap.Cmp(new(big.Int).SetInt64(0)) > 0 { - rcap := new(big.Int).Mul(totalVoterReward, voteCap) - rcap = new(big.Int).Div(rcap, totalCap) - if balances[addr] != nil { - balances[addr].Add(balances[addr], rcap) - } else { - balances[addr] = rcap - } - } - } - } - } - - foudationReward := new(big.Int).Mul(totalReward, new(big.Int).SetInt64(common.RewardFoundationPercent)) - foudationReward = new(big.Int).Div(foudationReward, new(big.Int).SetInt64(100)) - balances[foudationWalletAddr] = foudationReward - - jsonHolders, err := json.Marshal(balances) - if err != nil { - log.Error("Fail to parse json holders", "error", err) - return nil, err - } - log.Info("Holders reward", "holders", string(jsonHolders), "master node", masterAddr.String()) - - return balances, nil -} - -func GetCandidatesOwnerBySigner(validator *contractValidator.XDCValidator, signerAddr common.Address) common.Address { - owner := signerAddr - opts := new(bind.CallOpts) - owner, err := validator.GetCandidateOwner(opts, signerAddr) - if err != nil { - log.Error("Fail get candidate owner", "error", err) - return owner - } - - return owner -} \ No newline at end of file