From bc8bb217eed4f7143efce62cb799f8a02c677884 Mon Sep 17 00:00:00 2001 From: Hemant Chaudhary <104961126+hemant933@users.noreply.github.com> Date: Sat, 23 Dec 2023 11:32:51 +0530 Subject: [PATCH 1/2] Update Contest.sol make it more optimal , optimizations such as making state variable private , simplifying initializations , removing reductant mapping and improving functions name for clarity --- contracts/Contest.sol | 106 ++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 55 deletions(-) diff --git a/contracts/Contest.sol b/contracts/Contest.sol index 84f6918..c7cb61d 100644 --- a/contracts/Contest.sol +++ b/contracts/Contest.sol @@ -1,69 +1,65 @@ pragma solidity 0.5.16; -contract Contest{ - - struct Contestant{ - uint id; - string name; - uint voteCount; - string party; - uint age; - string qualification; - } +contract Contest { + address private admin; + uint private contestantsCount; + PHASE private state; + enum PHASE { Reg, Voting, Done } + + struct Contestant { + uint id; + string name; + uint voteCount; + string party; + uint age; + string qualification; + } - struct Voter{ - bool hasVoted; - uint vote; - bool isRegistered; - } + struct Voter { + bool hasVoted; + uint vote; + bool isRegistered; + } - address admin; - mapping(uint => Contestant) public contestants; - // mapping(address => bool) public voters; - mapping(address => Voter) public voters; - uint public contestantsCount; - // uint public counter; - enum PHASE{reg, voting , done} - PHASE public state; + mapping(uint => Contestant) private contestants; + mapping(address => Voter) private voters; - modifier onlyAdmin(){ - require(msg.sender==admin); - _; - } - - modifier validState(PHASE x){ - require(state==x); - _; - } + modifier onlyAdmin() { + require(msg.sender == admin, "Only admin can call this function"); + _; + } - constructor() public{ - admin=msg.sender; - state=PHASE.reg; - // counter = 0; + modifier validState(PHASE x) { + require(state == x, "Invalid state transition"); + _; + } - } + constructor() public { + admin = msg.sender; + state = PHASE.Reg; + } - function changeState(PHASE x) onlyAdmin public{ - require(x > state); + function transitionToPhase(PHASE x) public onlyAdmin { + require(uint(x) > uint(state), "Invalid state transition"); state = x; } - function addContestant(string memory _name , string memory _party , uint _age , string memory _qualification) public onlyAdmin validState(PHASE.reg){ - contestantsCount++; - contestants[contestantsCount]=Contestant(contestantsCount,_name,0,_party,_age,_qualification); - } + function addContestant(string memory _name, string memory _party, uint _age, string memory _qualification) public onlyAdmin validState(PHASE.Reg) { + contestantsCount++; + contestants[contestantsCount] = Contestant(contestantsCount, _name, 0, _party, _age, _qualification); + } - function voterRegisteration(address user) public onlyAdmin validState(PHASE.reg){ - voters[user].isRegistered=true; - } + function registerVoter(address user) public onlyAdmin validState(PHASE.Reg) { + voters[user].isRegistered = true; + } - function vote(uint _contestantId) public validState(PHASE.voting){ + function vote(uint _contestantId) public validState(PHASE.Voting) { + require(voters[msg.sender].isRegistered, "Voter is not registered"); + require(!voters[msg.sender].hasVoted, "Voter has already voted"); + require(_contestantId > 0 && _contestantId <= contestantsCount, "Invalid contestant ID"); - require(voters[msg.sender].isRegistered); - require(!voters[msg.sender].hasVoted); - require(_contestantId > 0 && _contestantId<=contestantsCount); - contestants[_contestantId].voteCount++; - voters[msg.sender].hasVoted=true; - voters[msg.sender].vote=_contestantId; - } -} \ No newline at end of file + contestants[_contestantId].voteCount++; + voters[msg.sender].hasVoted = true; + voters[msg.sender].vote = _contestantId; + } +} From d5150d1bbe4fcf7271314db88c3c7165feabd6fb Mon Sep 17 00:00:00 2001 From: Hemant Chaudhary <104961126+hemant933@users.noreply.github.com> Date: Sat, 23 Dec 2023 11:36:01 +0530 Subject: [PATCH 2/2] Update Migrations.sol change restricted modifier to onlyOwner --- contracts/Migrations.sol | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/contracts/Migrations.sol b/contracts/Migrations.sol index f4661c6..a34f82f 100644 --- a/contracts/Migrations.sol +++ b/contracts/Migrations.sol @@ -1,19 +1,15 @@ -// SPDX-License-Identifier: MIT -pragma solidity >=0.4.22 <0.8.0; +pragma solidity ^0.8.0; contract Migrations { - address public owner = msg.sender; - uint public last_completed_migration; + address public owner = msg.sender; + uint public lastCompletedMigration; - modifier restricted() { - require( - msg.sender == owner, - "This function is restricted to the contract's owner" - ); - _; - } + modifier onlyOwner() { + require(msg.sender == owner, "Restricted to the contract's owner"); + _; + } - function setCompleted(uint completed) public restricted { - last_completed_migration = completed; - } + function setCompleted(uint completed) external onlyOwner { + lastCompletedMigration = completed; + } }