comments added to contract

This commit is contained in:
vsmk98 2018-10-04 03:03:22 +00:00
parent ef1708fbc0
commit 4d31d85ff8
1 changed files with 64 additions and 39 deletions

View File

@ -2,6 +2,7 @@ pragma solidity ^0.4.23;
contract Clusterkeys { contract Clusterkeys {
// Struct for managing the org details
enum Operation {None, Add, Delete} enum Operation {None, Add, Delete}
struct OrgDetails { struct OrgDetails {
string orgId; string orgId;
@ -10,9 +11,9 @@ contract Clusterkeys {
Operation pendingOp; Operation pendingOp;
} }
OrgDetails [] private orgList; OrgDetails [] private orgList;
mapping(bytes32 => uint) private OrgIndex; mapping(bytes32 => uint) private OrgIndex;
// Struct for managing the voter accounst for the org
struct OrgVoterDetails { struct OrgVoterDetails {
string orgId; string orgId;
address [] orgVoterAccount; address [] orgVoterAccount;
@ -20,32 +21,50 @@ contract Clusterkeys {
OrgVoterDetails [] private voterList; OrgVoterDetails [] private voterList;
mapping(bytes32 => uint) private VoterOrgIndex; 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 => mapping (address => bool)) private voteStatus;
mapping (uint => uint) private voteCount; mapping (uint => uint) private voteCount;
uint private numberOfOrgs = 0; uint private numberOfOrgs = 0;
uint private orgVoterNum = 0; uint private orgVoterNum = 0;
// events related to Org level key management
event OrgKeyAdded(string _orgId, string _privateKey); event OrgKeyAdded(string _orgId, string _privateKey);
event OrgKeyDeleted(string _orgId, string _privateKey); event OrgKeyDeleted(string _orgId, string _privateKey);
event orgVoterAdded(string _orgId, string _voterAccount);
event KeyNotFound(string _privateKey); event KeyNotFound(string _privateKey);
event OrgNotFound(string _orgId);
event KeyExists(string _orgId, string _privateKey); event KeyExists(string _orgId, string _privateKey);
event VoterAdded(string _orgId, address _address); event OrgNotFound(string _orgId);
event VoterExists(string _orgId, address _address);
event VoterNotFound(string _orgId, address _address); // events related to org level approval process
event VoterAccountDeleted(string _orgId, address _address);
event NoVotingAccount(string _orgId);
event PendingApproval(string _orgId); event PendingApproval(string _orgId);
event ItemForApproval(string _orgId, Operation _pendingOp, string _privateKey); event ItemForApproval(string _orgId, Operation _pendingOp, string _privateKey);
event NothingToApprove(string _orgId); 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 PrintAll(string _orgId, string _privateKey);
event PrintVoter(string _orgId, address _voterAccount); 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){ modifier canVote(string _orgId){
bool flag = false; bool flag = false;
uint orgIndex = getOrgIndexVoter(_orgId); 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){ function checkIfVoterExists(string _orgId, address _address) internal view returns (bool, uint){
bool keyExists = false; bool keyExists = false;
uint voterIndex = getOrgIndexVoter(_orgId); uint voterIndex = getOrgIndexVoter(_orgId);
@ -81,6 +92,7 @@ contract Clusterkeys {
return (keyExists, i); 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) function checkVotingAccountExists(string _orgId) internal returns (bool)
{ {
if (VoterOrgIndex[keccak256(abi.encodePacked(_orgId))] == 0){ if (VoterOrgIndex[keccak256(abi.encodePacked(_orgId))] == 0){
@ -95,6 +107,7 @@ contract Clusterkeys {
return true; return true;
} }
// checks if there are any pending unapproved actions for the org
function checkingPendingOp(string _orgId) internal view returns (bool) function checkingPendingOp(string _orgId) internal view returns (bool)
{ {
if (OrgIndex[keccak256(abi.encodePacked(_orgId))] == 0){ if (OrgIndex[keccak256(abi.encodePacked(_orgId))] == 0){
@ -107,6 +120,7 @@ contract Clusterkeys {
return false; 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){ function checkIfKeyExists(string _orgId, string _privateKey) internal view returns (bool, uint){
bool keyExists = false; bool keyExists = false;
uint orgIndex = getOrgIndex(_orgId); uint orgIndex = getOrgIndex(_orgId);
@ -119,6 +133,7 @@ contract Clusterkeys {
return (keyExists, i); return (keyExists, i);
} }
// function for adding a voter account to a org
function addVoter(string _orgId, address _address) external function addVoter(string _orgId, address _address) external
{ {
if (VoterOrgIndex[keccak256(abi.encodePacked(_orgId))] == 0) { 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 function deleteVoter(string _orgId, address _address) external
{ {
if (VoterOrgIndex[keccak256(abi.encodePacked(_orgId))] == 0) { if (VoterOrgIndex[keccak256(abi.encodePacked(_orgId))] == 0) {
@ -150,9 +166,6 @@ contract Clusterkeys {
} }
else { else {
uint voterIndex = getOrgIndexVoter(_orgId); uint voterIndex = getOrgIndexVoter(_orgId);
// uint i = 0;
//bool keyExists = false;
(bool voterExists, uint i) = checkIfVoterExists(_orgId, _address); (bool voterExists, uint i) = checkIfVoterExists(_orgId, _address);
if (voterExists == true) { 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 function addOrgKey(string _orgId, string _privateKey) external
{ {
if (checkVotingAccountExists(_orgId)){ if (checkVotingAccountExists(_orgId)){
@ -193,7 +208,6 @@ contract Clusterkeys {
else { else {
uint orgIndex; uint orgIndex;
orgIndex = getOrgIndex(_orgId); orgIndex = getOrgIndex(_orgId);
// orgList[orgIndex].privateKey.push(_privateKey);
orgList[orgIndex].pendingKey = _privateKey; orgList[orgIndex].pendingKey = _privateKey;
orgList[orgIndex].pendingOp = Operation.Add; orgList[orgIndex].pendingOp = Operation.Add;
voterInit(_orgId); 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 function deleteOrgKey(string _orgId, string _privateKey) external
{ {
if (checkVotingAccountExists(_orgId)){ if (checkVotingAccountExists(_orgId)){
@ -235,22 +251,7 @@ contract Clusterkeys {
} }
} }
function voterInit(string _orgId) internal { // function for approving key add or delete operations
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 approvePendingOp(string _orgId) external canVote(_orgId) function approvePendingOp(string _orgId) external canVote(_orgId)
{ {
if (checkingPendingOp(_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) { function checkEnoughVotes (string _orgId) internal view returns (bool) {
uint orgIndex = getOrgIndexVoter(_orgId); uint orgIndex = getOrgIndexVoter(_orgId);
if (voteCount[orgIndex] > voterList[orgIndex].orgVoterAccount.length / 2 ){ if (voteCount[orgIndex] > voterList[orgIndex].orgVoterAccount.length / 2 ){
@ -271,6 +293,7 @@ contract Clusterkeys {
return false; return false;
} }
// function to process the approavl for add or delete
function processApproval(uint _orgIndex) internal { function processApproval(uint _orgIndex) internal {
if(checkEnoughVotes(orgList[_orgIndex].orgId)){ if(checkEnoughVotes(orgList[_orgIndex].orgId)){
string storage locKey = orgList[_orgIndex].pendingKey; 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 { function printAllOrg () public {
for (uint i = 0; i < orgList.length; i++){ for (uint i = 0; i < orgList.length; i++){
for (uint j = 0; j < orgList[i].privateKey.length ; j++){ 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 { function printAllVoter () public {
for (uint i = 0; i < voterList.length; i++){ for (uint i = 0; i < voterList.length; i++){
for (uint j = 0; j < voterList[i].orgVoterAccount.length ; j++){ for (uint j = 0; j < voterList[i].orgVoterAccount.length ; j++){