From 4d31d85ff83ce41a48dc3b8b867fc517e0251ca9 Mon Sep 17 00:00:00 2001 From: vsmk98 Date: Thu, 4 Oct 2018 03:03:22 +0000 Subject: [PATCH] comments added to contract --- controls/cluster/Clusterkeys.sol | 103 +++++++++++++++++++------------ 1 file changed, 64 insertions(+), 39 deletions(-) diff --git a/controls/cluster/Clusterkeys.sol b/controls/cluster/Clusterkeys.sol index a9cfaaade..8f7dbcd66 100644 --- a/controls/cluster/Clusterkeys.sol +++ b/controls/cluster/Clusterkeys.sol @@ -2,6 +2,7 @@ pragma solidity ^0.4.23; contract Clusterkeys { + // Struct for managing the org details enum Operation {None, Add, Delete} struct OrgDetails { string orgId; @@ -10,9 +11,9 @@ contract Clusterkeys { Operation pendingOp; } OrgDetails [] private orgList; - mapping(bytes32 => uint) private OrgIndex; + // Struct for managing the voter accounst for the org struct OrgVoterDetails { string orgId; address [] orgVoterAccount; @@ -20,32 +21,50 @@ contract Clusterkeys { OrgVoterDetails [] private voterList; mapping(bytes32 => uint) private VoterOrgIndex; + // mapping to monitor the voting status for each acount and + // overall voting count mapping (uint => mapping (address => bool)) private voteStatus; mapping (uint => uint) private voteCount; uint private numberOfOrgs = 0; - uint private orgVoterNum = 0; + // events related to Org level key management event OrgKeyAdded(string _orgId, string _privateKey); event OrgKeyDeleted(string _orgId, string _privateKey); - event orgVoterAdded(string _orgId, string _voterAccount); event KeyNotFound(string _privateKey); - event OrgNotFound(string _orgId); event KeyExists(string _orgId, string _privateKey); - event VoterAdded(string _orgId, address _address); - event VoterExists(string _orgId, address _address); - event VoterNotFound(string _orgId, address _address); - event VoterAccountDeleted(string _orgId, address _address); - event NoVotingAccount(string _orgId); + event OrgNotFound(string _orgId); + + // events related to org level approval process event PendingApproval(string _orgId); event ItemForApproval(string _orgId, Operation _pendingOp, string _privateKey); event NothingToApprove(string _orgId); + // events related to managing voting accounts for the org + event NoVotingAccount(string _orgId); + event VoterAdded(string _orgId, address _address); + event VoterNotFound(string _orgId, address _address); + event VoterAccountDeleted(string _orgId, address _address); + event VoterExists(string _orgId, address _address); + + // events related to helper functions to print all org keys and voter keys event PrintAll(string _orgId, string _privateKey); event PrintVoter(string _orgId, address _voterAccount); - event PrintKey(string _orgId, Operation _pendingOp, string _pendingKey); + // returns the org index for the org list + function getOrgIndex(string _orgId) internal view returns (uint) + { + return OrgIndex[keccak256(abi.encodePacked(_orgId))] - 1; + } + + // returns the voter index for the org from voter list + function getOrgIndexVoter(string _orgId) internal view returns (uint) + { + return VoterOrgIndex[keccak256(abi.encodePacked(_orgId))] - 1; + } + + // checks if the sender is one of the registered voter account for the org modifier canVote(string _orgId){ bool flag = false; uint orgIndex = getOrgIndexVoter(_orgId); @@ -59,16 +78,8 @@ contract Clusterkeys { _; } - function getOrgIndex(string _orgId) internal view returns (uint) - { - return OrgIndex[keccak256(abi.encodePacked(_orgId))] - 1; - } - - function getOrgIndexVoter(string _orgId) internal view returns (uint) - { - return VoterOrgIndex[keccak256(abi.encodePacked(_orgId))] - 1; - } + // checks if the org has any voter accounts set up or not function checkIfVoterExists(string _orgId, address _address) internal view returns (bool, uint){ bool keyExists = false; uint voterIndex = getOrgIndexVoter(_orgId); @@ -81,6 +92,7 @@ contract Clusterkeys { return (keyExists, i); } + // checks if the voter account is already in the voter accounts list for the org function checkVotingAccountExists(string _orgId) internal returns (bool) { if (VoterOrgIndex[keccak256(abi.encodePacked(_orgId))] == 0){ @@ -95,6 +107,7 @@ contract Clusterkeys { return true; } + // checks if there are any pending unapproved actions for the org function checkingPendingOp(string _orgId) internal view returns (bool) { if (OrgIndex[keccak256(abi.encodePacked(_orgId))] == 0){ @@ -107,6 +120,7 @@ contract Clusterkeys { return false; } + // checks if there the key is already in the list of private keys for the org function checkIfKeyExists(string _orgId, string _privateKey) internal view returns (bool, uint){ bool keyExists = false; uint orgIndex = getOrgIndex(_orgId); @@ -119,6 +133,7 @@ contract Clusterkeys { return (keyExists, i); } + // function for adding a voter account to a org function addVoter(string _orgId, address _address) external { if (VoterOrgIndex[keccak256(abi.encodePacked(_orgId))] == 0) { @@ -143,6 +158,7 @@ contract Clusterkeys { } } + // function for deleting a voter account to a org function deleteVoter(string _orgId, address _address) external { if (VoterOrgIndex[keccak256(abi.encodePacked(_orgId))] == 0) { @@ -150,9 +166,6 @@ contract Clusterkeys { } else { uint voterIndex = getOrgIndexVoter(_orgId); - // uint i = 0; - //bool keyExists = false; - (bool voterExists, uint i) = checkIfVoterExists(_orgId, _address); if (voterExists == true) { @@ -169,6 +182,8 @@ contract Clusterkeys { } } + // function for adding a private key for the org. Thsi will be added once + // approval process is complete function addOrgKey(string _orgId, string _privateKey) external { if (checkVotingAccountExists(_orgId)){ @@ -193,7 +208,6 @@ contract Clusterkeys { else { uint orgIndex; orgIndex = getOrgIndex(_orgId); - // orgList[orgIndex].privateKey.push(_privateKey); orgList[orgIndex].pendingKey = _privateKey; orgList[orgIndex].pendingOp = Operation.Add; voterInit(_orgId); @@ -204,6 +218,8 @@ contract Clusterkeys { } } + // function for deleting a private key for the org. Thsi will be deleted once + // approval process is complete function deleteOrgKey(string _orgId, string _privateKey) external { if (checkVotingAccountExists(_orgId)){ @@ -235,22 +251,7 @@ contract Clusterkeys { } } - function voterInit(string _orgId) internal { - uint orgIndex = getOrgIndexVoter(_orgId); - for (uint i = 0; i < voterList[orgIndex].orgVoterAccount.length; i++){ - voteStatus[orgIndex][voterList[orgIndex].orgVoterAccount[i]] = false; - } - voteCount[orgIndex] = 0; - } - - function processVote (string _orgId) internal { - uint orgIndex = getOrgIndexVoter(_orgId); - if (voteStatus[orgIndex][msg.sender] == false ){ - voteStatus[orgIndex][msg.sender] = true; - voteCount[orgIndex]++; - } - } - + // function for approving key add or delete operations function approvePendingOp(string _orgId) external canVote(_orgId) { if (checkingPendingOp(_orgId)){ @@ -263,6 +264,27 @@ contract Clusterkeys { } } + // initialize the voter account votes to false. This will be called when a + // new item is initiated for approval + function voterInit(string _orgId) internal { + uint orgIndex = getOrgIndexVoter(_orgId); + for (uint i = 0; i < voterList[orgIndex].orgVoterAccount.length; i++){ + voteStatus[orgIndex][voterList[orgIndex].orgVoterAccount[i]] = false; + } + voteCount[orgIndex] = 0; + } + + // processes the vote from the voter account. + function processVote (string _orgId) internal { + uint orgIndex = getOrgIndexVoter(_orgId); + if (voteStatus[orgIndex][msg.sender] == false ){ + voteStatus[orgIndex][msg.sender] = true; + voteCount[orgIndex]++; + } + } + + // checks if enough votes have been cast for the pending operation. If yes + // returns true function checkEnoughVotes (string _orgId) internal view returns (bool) { uint orgIndex = getOrgIndexVoter(_orgId); if (voteCount[orgIndex] > voterList[orgIndex].orgVoterAccount.length / 2 ){ @@ -271,6 +293,7 @@ contract Clusterkeys { return false; } + // function to process the approavl for add or delete function processApproval(uint _orgIndex) internal { if(checkEnoughVotes(orgList[_orgIndex].orgId)){ string storage locKey = orgList[_orgIndex].pendingKey; @@ -294,6 +317,7 @@ contract Clusterkeys { } } + // helper function to print all privates keys for an org function printAllOrg () public { for (uint i = 0; i < orgList.length; i++){ for (uint j = 0; j < orgList[i].privateKey.length ; j++){ @@ -302,6 +326,7 @@ contract Clusterkeys { } } + // helper function to print all voters accounts for an org function printAllVoter () public { for (uint i = 0; i < voterList.length; i++){ for (uint j = 0; j < voterList[i].orgVoterAccount.length ; j++){