mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-16 09:50:45 +00:00
Changes added in Smart-Contract
This commit is contained in:
parent
5a667983c3
commit
ed2f00b61c
3 changed files with 459 additions and 779 deletions
|
|
@ -1,52 +1,6 @@
|
||||||
pragma solidity ^0.4.21;
|
pragma solidity ^0.4.21;
|
||||||
|
|
||||||
// This contract is under development.
|
import "./libs/SafeMath.sol";
|
||||||
// Refer to readme for further details.
|
|
||||||
|
|
||||||
|
|
||||||
library SafeMath {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev Multiplies two numbers, throws on overflow.
|
|
||||||
*/
|
|
||||||
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
||||||
if (a == 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
uint256 c = a * b;
|
|
||||||
assert(c / a == b);
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev Integer division of two numbers, truncating the quotient.
|
|
||||||
*/
|
|
||||||
function div(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
||||||
// assert(b > 0); // Solidity automatically throws when dividing by 0
|
|
||||||
// uint256 c = a / b;
|
|
||||||
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
|
|
||||||
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) {
|
|
||||||
assert(b <= a);
|
|
||||||
return a - b;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev Adds two numbers, throws on overflow.
|
|
||||||
*/
|
|
||||||
function add(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
||||||
uint256 c = a + b;
|
|
||||||
assert(c >= a);
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
contract XDCValidator {
|
contract XDCValidator {
|
||||||
using SafeMath for uint256;
|
using SafeMath for uint256;
|
||||||
|
|
@ -73,14 +27,6 @@ contract XDCValidator {
|
||||||
|
|
||||||
mapping(address => ValidatorState) validatorsState;
|
mapping(address => ValidatorState) validatorsState;
|
||||||
mapping(address => address[]) voters;
|
mapping(address => address[]) voters;
|
||||||
|
|
||||||
// Mapping structures added for KYC feature.
|
|
||||||
mapping(address => string) public KYCString;
|
|
||||||
mapping(address => uint) public invalidKYCCount;
|
|
||||||
mapping(address => mapping(address => bool)) public hasVotedInvalid;
|
|
||||||
mapping(address => address[]) public ownerToCandidate;
|
|
||||||
address[] public owners;
|
|
||||||
|
|
||||||
address[] public candidates;
|
address[] public candidates;
|
||||||
|
|
||||||
uint256 public candidateCount = 0;
|
uint256 public candidateCount = 0;
|
||||||
|
|
@ -97,20 +43,10 @@ contract XDCValidator {
|
||||||
}
|
}
|
||||||
|
|
||||||
modifier onlyValidVoterCap {
|
modifier onlyValidVoterCap {
|
||||||
|
|
||||||
require(msg.value >= minVoterCap);
|
require(msg.value >= minVoterCap);
|
||||||
_;
|
_;
|
||||||
}
|
}
|
||||||
|
|
||||||
modifier onlyKYCWhitelisted {
|
|
||||||
if(bytes(KYCString[msg.sender]).length != 0)
|
|
||||||
{_;}
|
|
||||||
else{
|
|
||||||
if (ownerToCandidate[msg.sender].length > 0)
|
|
||||||
{_;}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
modifier onlyOwner(address _candidate) {
|
modifier onlyOwner(address _candidate) {
|
||||||
require(validatorsState[_candidate].owner == msg.sender);
|
require(validatorsState[_candidate].owner == msg.sender);
|
||||||
_;
|
_;
|
||||||
|
|
@ -163,7 +99,7 @@ contract XDCValidator {
|
||||||
candidateWithdrawDelay = _candidateWithdrawDelay;
|
candidateWithdrawDelay = _candidateWithdrawDelay;
|
||||||
voterWithdrawDelay = _voterWithdrawDelay;
|
voterWithdrawDelay = _voterWithdrawDelay;
|
||||||
candidateCount = _candidates.length;
|
candidateCount = _candidates.length;
|
||||||
owners.push(_firstOwner);
|
|
||||||
for (uint256 i = 0; i < _candidates.length; i++) {
|
for (uint256 i = 0; i < _candidates.length; i++) {
|
||||||
candidates.push(_candidates[i]);
|
candidates.push(_candidates[i]);
|
||||||
validatorsState[_candidates[i]] = ValidatorState({
|
validatorsState[_candidates[i]] = ValidatorState({
|
||||||
|
|
@ -172,20 +108,11 @@ contract XDCValidator {
|
||||||
cap: _caps[i]
|
cap: _caps[i]
|
||||||
});
|
});
|
||||||
voters[_candidates[i]].push(_firstOwner);
|
voters[_candidates[i]].push(_firstOwner);
|
||||||
ownerToCandidate[_firstOwner].push(_candidates[i]);
|
|
||||||
validatorsState[_candidates[i]].voters[_firstOwner] = minCandidateCap;
|
validatorsState[_candidates[i]].voters[_firstOwner] = minCandidateCap;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function propose(address _candidate) external payable onlyValidCandidateCap onlyNotCandidate(_candidate) {
|
||||||
// uploadKYC : anyone can upload a KYC; its not equivalent to becoming an owner.
|
|
||||||
function uploadKYC(string kychash) external {
|
|
||||||
require(bytes(KYCString[msg.sender]).length==0);
|
|
||||||
KYCString[msg.sender]=kychash;
|
|
||||||
}
|
|
||||||
|
|
||||||
// propose : any non-candidate who has uploaded its KYC can become an owner by proposing a candidate.
|
|
||||||
function propose(address _candidate) external payable onlyValidCandidateCap onlyKYCWhitelisted onlyNotCandidate(_candidate) {
|
|
||||||
uint256 cap = validatorsState[_candidate].cap.add(msg.value);
|
uint256 cap = validatorsState[_candidate].cap.add(msg.value);
|
||||||
candidates.push(_candidate);
|
candidates.push(_candidate);
|
||||||
validatorsState[_candidate] = ValidatorState({
|
validatorsState[_candidate] = ValidatorState({
|
||||||
|
|
@ -195,11 +122,6 @@ contract XDCValidator {
|
||||||
});
|
});
|
||||||
validatorsState[_candidate].voters[msg.sender] = validatorsState[_candidate].voters[msg.sender].add(msg.value);
|
validatorsState[_candidate].voters[msg.sender] = validatorsState[_candidate].voters[msg.sender].add(msg.value);
|
||||||
candidateCount = candidateCount.add(1);
|
candidateCount = candidateCount.add(1);
|
||||||
if (ownerToCandidate[msg.sender].length ==0){
|
|
||||||
owners.push(msg.sender);
|
|
||||||
|
|
||||||
}
|
|
||||||
ownerToCandidate[msg.sender].push(_candidate);
|
|
||||||
voters[_candidate].push(msg.sender);
|
voters[_candidate].push(msg.sender);
|
||||||
emit Propose(msg.sender, _candidate, msg.value);
|
emit Propose(msg.sender, _candidate, msg.value);
|
||||||
}
|
}
|
||||||
|
|
@ -276,59 +198,6 @@ contract XDCValidator {
|
||||||
emit Resign(msg.sender, _candidate);
|
emit Resign(msg.sender, _candidate);
|
||||||
}
|
}
|
||||||
|
|
||||||
// voteInvalidKYC : any candidate can vote for invalid KYC i.e. a particular candidate's owner has uploaded a bad KYC.
|
|
||||||
// On securing 75% votes against an owner ( not candidate ), owner & all its candidates will lose their funds.
|
|
||||||
function voteInvalidKYC(address _invalidCandidate) onlyValidCandidate(msg.sender) onlyValidCandidate(_invalidCandidate) public {
|
|
||||||
address candidateOwner = getCandidateOwner(msg.sender);
|
|
||||||
address _invalidMasternode = getCandidateOwner(_invalidCandidate);
|
|
||||||
require(!hasVotedInvalid[candidateOwner][_invalidMasternode]);
|
|
||||||
hasVotedInvalid[candidateOwner][_invalidMasternode] = true;
|
|
||||||
invalidKYCCount[_invalidMasternode] += 1;
|
|
||||||
if( invalidKYCCount[_invalidMasternode]*100/getOwnerCount() >= 75 ){
|
|
||||||
// 75% owners say that the KYC is invalid
|
|
||||||
for (uint i=0;i<candidates.length;i++){
|
|
||||||
if (getCandidateOwner(candidates[i])==_invalidMasternode){
|
|
||||||
// logic to remove cap.
|
|
||||||
candidateCount = candidateCount.sub(1);
|
|
||||||
delete candidates[i];
|
|
||||||
delete validatorsState[candidates[i]];
|
|
||||||
delete KYCString[_invalidMasternode];
|
|
||||||
delete ownerToCandidate[_invalidMasternode];
|
|
||||||
delete invalidKYCCount[_invalidMasternode];
|
|
||||||
for(uint k=0;k<owners.length;k++){
|
|
||||||
if (owners[k]==_invalidMasternode){
|
|
||||||
delete owners[k];
|
|
||||||
owners.length--;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// invalidPercent : get votes against an owner in percentage.
|
|
||||||
function invalidPercent(address _invalidCandidate) onlyValidCandidate(_invalidCandidate) view public returns(uint){
|
|
||||||
address _invalidMasternode = getCandidateOwner(_invalidCandidate);
|
|
||||||
return (invalidKYCCount[_invalidMasternode]*100/getOwnerCount());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// getOwnerCount : get count of total owners; accounts who own atleast one masternode.
|
|
||||||
function getOwnerCount() view public returns (uint){
|
|
||||||
return owners.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
// getKYC : get KYC uploaded of the owner of the given masternode or the owner themselves
|
|
||||||
function getKYC(address _address) view public returns (string) {
|
|
||||||
if(isCandidate(_address)){
|
|
||||||
return KYCString[getCandidateOwner(_address)];
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
return KYCString[_address];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function withdraw(uint256 _blockNumber, uint _index) public onlyValidWithdraw(_blockNumber, _index) {
|
function withdraw(uint256 _blockNumber, uint _index) public onlyValidWithdraw(_blockNumber, _index) {
|
||||||
uint256 cap = withdrawsState[msg.sender].caps[_blockNumber];
|
uint256 cap = withdrawsState[msg.sender].caps[_blockNumber];
|
||||||
delete withdrawsState[msg.sender].caps[_blockNumber];
|
delete withdrawsState[msg.sender].caps[_blockNumber];
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
pragma solidity ^0.4.21;
|
pragma solidity ^0.4.21;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @title SafeMath
|
* @title SafeMath
|
||||||
* @dev Math operations with safety checks that throw on error
|
* @dev Math operations with safety checks that throw on error
|
||||||
*/
|
*/
|
||||||
library SafeMath {
|
library SafeMath {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Multiplies two numbers, throws on overflow.
|
* @dev Multiplies two numbers, throws on overflow.
|
||||||
*/
|
*/
|
||||||
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
|
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
|
||||||
|
|
@ -18,7 +18,7 @@ library SafeMath {
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Integer division of two numbers, truncating the quotient.
|
* @dev Integer division of two numbers, truncating the quotient.
|
||||||
*/
|
*/
|
||||||
function div(uint256 a, uint256 b) internal pure returns (uint256) {
|
function div(uint256 a, uint256 b) internal pure returns (uint256) {
|
||||||
|
|
@ -28,7 +28,7 @@ library SafeMath {
|
||||||
return a / b;
|
return a / b;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
|
* @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) {
|
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
|
||||||
|
|
@ -36,7 +36,7 @@ library SafeMath {
|
||||||
return a - b;
|
return a - b;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Adds two numbers, throws on overflow.
|
* @dev Adds two numbers, throws on overflow.
|
||||||
*/
|
*/
|
||||||
function add(uint256 a, uint256 b) internal pure returns (uint256) {
|
function add(uint256 a, uint256 b) internal pure returns (uint256) {
|
||||||
|
|
@ -44,4 +44,4 @@ library SafeMath {
|
||||||
assert(c >= a);
|
assert(c >= a);
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue