solidityreentrancy

Solidity Loop Reentrancy


I have a function that assigns a value to a token based on a result of a loop.

pragma solidity ^0.8.0;

        function generateId(uint myclassid) public
        {
            (found_,id_)=getAvailableId(myclassid);
             importantMapping[_id]=important_value;                
        }

        function getAvailableId(uint256 _tokenClassId) internal  returns (bool found,uint256 ext_id) 
        {
            //loop the avialble
            //if it is not 0, break and return 
            // if 0
            ext_id=0;
            uint256[] storage availables=tokenClass[_tokenClassId].available_list;
            found=false;
            for (uint i ; i < availables.length;) 
            {
                uint256 val=tokenClass[_tokenClassId].available_list[i];
                if (val!=0)
                {
                    tokenClass[_tokenClassId].available_list[i]=0;
                    ext_id=val;
                    found=true;
                    break;
                }
                unchecked { ++i ;}

            }
            return (found,ext_id);

        }

This function is called by a public function that is accessible by anyone.

Here each tokenClass[_tokenClassId] is a struct and available_list is an array that carries values like [0,0,50,20,30]

This loop just returns the first non-zero value and changes it to 0.

However, ,my question is reentrancy risk. What happens if there are 2 entries to the loop at the same time... Will it return the same ext_id since when they both entered, the first non-zero will be the same. Or is there any measure within Solidty to prevent that from happening?

Will a simple reentrancy guard protect or will it generate more problems?


Solution

  • What happens if there are 2 entries to the loop at the same time

    Your function doesn't implement any way to step outside of it (e.g. a call to another function or another contract) once it starts executing, which makes it safe from reentrancy attack. At least within the context shown in this question.

    EVM executes all transactions in series - not in parallel. So even if 2 users submitted the transaction executing this function at the same time, and both their transactions were mined in the same block, the miner would still execute their transactions separately, not affecting each other.