From 523e4d1a308962e93c72cf9b7bf432775cc6afd3 Mon Sep 17 00:00:00 2001 From: Pradyumna Kattel <126577744+pradyumna14@users.noreply.github.com> Date: Sun, 20 Oct 2024 09:53:49 +0530 Subject: [PATCH] Prevent duplicate registrations and add input validation Modified registerParticipant function to prevent duplicate entries from the same address and ensure accurate participant count. Added input validation using require statements for mandatory fields and introduced events for registration and updates. --- contracts/event_registration.sol | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/contracts/event_registration.sol b/contracts/event_registration.sol index 27da3aa..7342f2c 100644 --- a/contracts/event_registration.sol +++ b/contracts/event_registration.sol @@ -14,6 +14,17 @@ contract EventRegistration { mapping(address => Participant) public participants; address[] public participantAddresses; + event ParticipantRegistered( + address indexed participantAddress, + string fullname, + string eventName + ); + event ParticipantUpdated( + address indexed participantAddress, + string fullname, + string eventName + ); + function registerParticipant( string memory _fullname, string memory _stream, @@ -22,7 +33,21 @@ contract EventRegistration { string memory _phno, string memory _roll ) public { - Participant memory newParticipant = Participant({ + require(bytes(_fullname).length > 0, "Full name is required."); + require(bytes(_stream).length > 0, "Stream is required."); + require(bytes(_eventName).length > 0, "Event name is required."); + require(bytes(_email).length > 0, "Email is required."); + require(bytes(_phno).length > 0, "Phone number is required."); + require(bytes(_roll).length > 0, "Roll number is required."); + + if (bytes(participants[msg.sender].fullname).length == 0) { + participantAddresses.push(msg.sender); + emit ParticipantRegistered(msg.sender, _fullname, _eventName); + } else { + emit ParticipantUpdated(msg.sender, _fullname, _eventName); + } + + participants[msg.sender] = Participant({ fullname: _fullname, stream: _stream, eventName: _eventName, @@ -30,8 +55,6 @@ contract EventRegistration { phno: _phno, roll: _roll }); - participants[msg.sender] = newParticipant; - participantAddresses.push(msg.sender); } function getParticipant(address _addr)