-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f8e4ff8
Showing
1 changed file
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
contract MyCustomConsensusAlgorithm { | ||
|
||
// We proceed with the declaration of our Solidity version | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
// Statement of variables | ||
|
||
struct Validator { | ||
address validatorAddress; | ||
uint stakedAmount; | ||
bool exists; | ||
} | ||
|
||
mapping(address => Validator) private validators; | ||
address[] private validatorcount; | ||
|
||
uint public mycustomconsensusThreshold; | ||
uint public totalStakeAmount; | ||
|
||
// Our Data structures | ||
|
||
struct Proposal { | ||
uint Id; | ||
uint votes; | ||
bool executed; | ||
mapping(address => bool) votecasted; | ||
} | ||
mapping(uint => Proposal) public proposals; | ||
uint public proposalCount; | ||
|
||
// Modifiers | ||
modifier onlyValidator() { | ||
require(isValidator(msg.sender), "Only the validators can perform this action"); | ||
_; | ||
} | ||
|
||
// Events | ||
event ValidatorAdded(address indexed validator); | ||
event ValidatorRemoved(address indexed validator); | ||
event ProposalCreated(uint indexed proposal Id); | ||
event VoteCasted(uint indexed proposal Id, address indexed validator, uint votes); | ||
event ProposalExecuted(uint indexed proposal Id); | ||
event Slashed(address indexed validator, uint amount); | ||
|
||
// Constructor and setup functions | ||
|
||
constructor() { | ||
mycustomconsensusThreshold = 1; | ||
} | ||
|
||
// Smart contract functions | ||
|
||
// 1. Adding a new validator to the consensus algorithm | ||
|
||
function addValidator(address _validator, uint _stakeAmount) external { | ||
require(isValidator(_validator), "Validator already exists"); | ||
validators[_validator] = Validator(_validator, _stakeAmount, true); | ||
validatorcount.push(_validator); | ||
totalStakeAmount += _stakeAmount; | ||
emit ValidatorAdded(_validator); | ||
} | ||
|
||
// 2. Remove a validator from the consensus algorithm and slashing their stake | ||
|
||
function removeValidator(address _validator) external onlyValidator { | ||
require(isValidator(_validator), "Validator does not exist"); | ||
|
||
uint slashedAmount = validators[_validator].stakeAmount; | ||
validators[_validator].exists = false; | ||
totalStakeAmount -= slashedAmount; | ||
emit Slashed(_validator, slashedAmount); | ||
|
||
for (uint i = 0; i < validatorcount.length; i++) { | ||
if (validatorcount[i] == _validator) { | ||
validatorcount[i] = validatorcount[validatorcount.length - 1]; | ||
validatorcount.pop(); | ||
break; | ||
} | ||
} | ||
|
||
emit ValidatorRemoved(_validator); | ||
} | ||
|
||
// Creating a new proposal | ||
|
||
function createProposal() external onlyValidator returns (uint) { | ||
proposalCount++; | ||
proposals[proposalCount] = Proposal(proposalCount, 0, false); | ||
emit ProposalCreated(proposalCount); | ||
return proposalCount; | ||
} | ||
|
||
// Voting on new proposal | ||
|
||
function vote(uint _proposalId, uint _votes) external onlyValidator { | ||
require(proposals[_proposalId].id != 0, 'Invalid proposal ID'); | ||
require(!proposals[_proposalId].executed, 'Proposal already executed'); | ||
require(_votes > 1, 'Votes must be greater than one'); | ||
require(!proposals[_proposalId].votecasted[msg.sender], 'Already voted on this proposal'); | ||
require(validators[msg.sender].stakedAmount >= _votes, 'stake not enough'); | ||
|
||
validators[msg.sender].stakedAmount -= _votes; | ||
totalStakeAmount -= _votes; | ||
|
||
proposals[_proposalId].votes += _votes; | ||
proposals[_proposalId].votecasted[msg.sender] = true; | ||
emit VoteCasted(_proposalId, msg.sender, _votes); | ||
|
||
if (proposals[_proposalId].votes >= mycustomconsensusThreshold) { | ||
executeProposal(_proposalId); | ||
} | ||
} | ||
|
||
// Executing a proposal | ||
|
||
function executeProposal(uint _proposalId) internal { | ||
proposals[_proposalId].executed = true; | ||
emit ProposalExecuted(_proposalId); | ||
// Perform proposal execution logic | ||
} | ||
|
||
// Some Utility functions | ||
|
||
// 1. Checking if an address is a validator | ||
|
||
function isValidator(address _validator) public view returns (bool) { | ||
return validators[_validator].exists; | ||
} | ||
|
||
//2. Getting a list of validators | ||
|
||
function getValidators() public view returns (address[] memory) { | ||
return validatorcount; | ||
} | ||
|
||
// 3. Getting the staked amount for a validator | ||
|
||
function getStakedAmount(address _validator) public view returns (uint) { | ||
return validators[_validator].stakedAmount; | ||
} | ||
} |