The removeTeacher function in LevelOne has a scalability issue due to its linear search through the listOfTeachers array. As more teachers are added, gas costs increase significantly when removing a teacher. Additionally, there's a logical flaw until the teacher is actually removed, they can still interact with the contract (e.g., submit reviews), potentially leading to unauthorized actions.
The LevelOne::removeTeacher
function has a removal checking mechanism that loops through the listOfTeachers
array to search for the teacher. However, the longer the LevelOne::listOfTeachers
array is, the more time it takes to find and remove the teacher. This means that the time taken will increase and also the gas increased as the number of teachers added. Meanwhile, the teacher can still review the student even though the teacher should have been removed.
Write the below code in `levelOneAndGraduateTest.t.sol` file.
High Gas Costs: As the number of teachers grows, removing a teacher becomes more expensive due to the linear search and shift operations in the array. This can lead to inefficient contract performance and potential transaction failures if gas limits are exceeded.
Unauthorized Actions: Since a teacher is not immediately restricted upon initiation of the removal process, they can still perform actions (like submitting reviews), which violates expected access controls and could lead to trust or integrity issues in the system.
Manual testing
Use a Mapping Instead of Array: Replace the listOfTeachers
array with a mapping(address => bool)
or a mapping(address => Teacher)
structure for constant-time lookup and removal. This avoids expensive iteration and reduces gas costs significantly.
Implement Soft Delete with Access Control: Instead of fully removing the teacher immediately, introduce a isTeacher[teacher] = false
before the loop. This allows you to instantly restrict access to functions (like reviewing) by checking this flag, even before full cleanup.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.