Contract logic change for voting

This commit is contained in:
vsmk98 2018-10-03 06:48:20 +00:00
parent e41fd1c194
commit b806571e9f
1 changed files with 108 additions and 49 deletions

View File

@ -2,9 +2,12 @@ pragma solidity ^0.4.23;
contract Clusterkeys {
enum Operation {None, Add, Delete}
struct OrgDetails {
string orgId;
string [] privateKey;
string pendingKey;
Operation pendingOp;
}
OrgDetails [] private orgList;
@ -26,7 +29,6 @@ contract Clusterkeys {
event orgVoterAdded(string _orgId, string _voterAccount);
event KeyNotFound(string _privateKey);
event OrgNotFound(string _orgId);
event PrintAll(string _orgId, string _privateKey);
event KeyExists(string _orgId, string _privateKey);
event Dummy(uint _orgId, bool _keyExists, uint loopCnt );
event VoterAdded(string _orgId, address _address);
@ -34,6 +36,34 @@ contract Clusterkeys {
event VoterNotFound(string _orgId, address _address);
event VoterAccountDeleted(string _orgId, address _address);
event NoVotingAccount(string _orgId);
event PendingApproval(string _orgId);
event ItemForApproval(string _orgId, Operation _pendingOp, string _privateKey);
event NothingToApprove(string _orgId);
event PrintAll(string _orgId, string _privateKey);
event PrintKey(string _orgId, Operation _pendingOp, string _pendingKey);
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;
}
function checkIfVoterExists(string _orgId, address _address) internal view returns (bool, uint){
bool keyExists = false;
uint voterIndex = getOrgIndexVoter(_orgId);
for (uint i = 0; i < voterList[voterIndex].orgVoterAccount.length; i++){
if(keccak256(abi.encodePacked(voterList[voterIndex].orgVoterAccount[i])) == keccak256(abi.encodePacked(_address))){
keyExists = true;
break;
}
}
return (keyExists, i);
}
function checkVotingAccountExists(string _orgId) internal returns (bool)
{
@ -49,6 +79,18 @@ contract Clusterkeys {
return true;
}
function checkingPendingOp(string _orgId) internal view returns (bool)
{
if (OrgIndex[keccak256(abi.encodePacked(_orgId))] == 0){
return false;
}
uint orgIndex = getOrgIndex(_orgId);
if (orgList[orgIndex].pendingOp != Operation.None) {
return true;
}
return false;
}
function checkIfKeyExists(string _orgId, string _privateKey) internal view returns (bool, uint){
bool keyExists = false;
uint orgIndex = getOrgIndex(_orgId);
@ -61,28 +103,6 @@ contract Clusterkeys {
return (keyExists, i);
}
function checkIfVoterExists(string _orgId, address _address) internal view returns (bool, uint){
bool keyExists = false;
uint voterIndex = getOrgIndexVoter(_orgId);
for (uint i = 0; i < voterList[voterIndex].orgVoterAccount.length; i++){
if(keccak256(abi.encodePacked(voterList[voterIndex].orgVoterAccount[i])) == keccak256(abi.encodePacked(_address))){
keyExists = true;
break;
}
}
return (keyExists, i);
}
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;
}
function addVoter(string _orgId, address _address) external
{
if (VoterOrgIndex[keccak256(abi.encodePacked(_orgId))] == 0) {
@ -139,22 +159,28 @@ contract Clusterkeys {
if (OrgIndex[keccak256(abi.encodePacked(_orgId))] == 0) {
numberOfOrgs++;
OrgIndex[keccak256(abi.encodePacked(_orgId))] = numberOfOrgs;
orgList.push( OrgDetails(_orgId, new string[](0)));
orgList[numberOfOrgs-1].privateKey.push(_privateKey);
emit OrgKeyAdded(_orgId, _privateKey);
orgList.push( OrgDetails(_orgId, new string[](0), _privateKey, Operation.Add));
emit ItemForApproval(_orgId, Operation.Add, _privateKey);
}
else {
bool keyExists = false;
uint i = 0;
(keyExists, i) = checkIfKeyExists(_orgId, _privateKey);
if (keyExists) {
emit KeyExists(_orgId, _privateKey);
if (checkingPendingOp(_orgId)){
emit PendingApproval(_orgId);
}
else {
uint orgIndex;
orgIndex = getOrgIndex(_orgId);
orgList[orgIndex].privateKey.push(_privateKey);
emit OrgKeyAdded(_orgId, _privateKey);
bool keyExists = false;
uint i = 0;
(keyExists, i) = checkIfKeyExists(_orgId, _privateKey);
if (keyExists) {
emit KeyExists(_orgId, _privateKey);
}
else {
uint orgIndex;
orgIndex = getOrgIndex(_orgId);
// orgList[orgIndex].privateKey.push(_privateKey);
orgList[orgIndex].pendingKey = _privateKey;
orgList[orgIndex].pendingOp = Operation.Add;
emit ItemForApproval(_orgId,Operation.Add, _privateKey);
}
}
}
}
@ -167,29 +193,62 @@ contract Clusterkeys {
emit OrgNotFound(_orgId);
}
else {
uint orgIndex = getOrgIndex(_orgId);
uint i = 0;
bool keyExists = false;
(keyExists, i) = checkIfKeyExists (_orgId, _privateKey);
if (keyExists == true) {
for (uint j = i; j < orgList[orgIndex].privateKey.length -1; j++){
orgList[orgIndex].privateKey[j] = orgList[orgIndex].privateKey[j+1];
}
delete orgList[orgIndex].privateKey[orgList[orgIndex].privateKey.length -1];
orgList[orgIndex].privateKey.length --;
emit OrgKeyDeleted(_orgId, _privateKey);
if (checkingPendingOp(_orgId)){
emit PendingApproval(_orgId);
}
else {
emit KeyNotFound(_privateKey);
uint orgIndex = getOrgIndex(_orgId);
uint i = 0;
bool keyExists = false;
(keyExists, i) = checkIfKeyExists (_orgId, _privateKey);
if (keyExists == true) {
orgList[orgIndex].pendingKey = _privateKey;
orgList[orgIndex].pendingOp = Operation.Delete;
emit ItemForApproval(_orgId, Operation.Delete, _privateKey);
}
else {
emit KeyNotFound(_privateKey);
}
}
}
}
}
function approvePendingOp(string _orgId) external
{
if (checkingPendingOp(_orgId)){
uint orgIndex = getOrgIndex(_orgId);
string storage locKey = orgList[orgIndex].pendingKey;
if (orgList[orgIndex].pendingOp == Operation.Add){
orgList[orgIndex].pendingOp = Operation.None;
orgList[orgIndex].privateKey.push(orgList[orgIndex].pendingKey);
orgList[orgIndex].pendingKey = "";
emit OrgKeyAdded(_orgId, locKey);
}
else {
bool keyExists = false;
uint i = 0;
(keyExists, i) = checkIfKeyExists (_orgId, locKey);
for (uint j = i; j < orgList[orgIndex].privateKey.length -1; j++){
orgList[orgIndex].privateKey[j] = orgList[orgIndex].privateKey[j+1];
}
delete orgList[orgIndex].privateKey[orgList[orgIndex].privateKey.length -1];
orgList[orgIndex].privateKey.length --;
orgList[orgIndex].pendingOp = Operation.None;
orgList[orgIndex].pendingKey = "";
emit OrgKeyDeleted(_orgId, locKey);
}
}
else {
emit NothingToApprove(_orgId);
}
}
function printAll () public {
for (uint i = 0; i < orgList.length; i++){
emit PrintKey(orgList[i].orgId, orgList[i].pendingOp, orgList[i].pendingKey);
for (uint j = 0; j < orgList[i].privateKey.length ; j++){
emit PrintAll(orgList[i].orgId, orgList[i].privateKey[j]);
}