坚固循环再入

发布于 2025-01-21 16:33:53 字数 1419 浏览 0 评论 0原文

我的函数可以根据循环的结果将值分配给令牌。

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);

        }

此功能由任何人都可以访问的公共功能调用。

这里的每个Tokenclass [_tokenclassID]是一个struct,able_list是一个阵列,它具有[0,0,0,50,20,30]之类的值,

此循环只是返回第一个非零值并将其更改为0。

但是,我的问题是重新进入风险。如果循环同时有2个条目,会发生什么……它会返回相同的ext_id,因为当它们都进入时,第一个非零的时间将相同。还是内部有任何防止这种情况发生的措施?

一个简单的重新进入警卫会保护或会产生更多问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鱼忆七猫命九 2025-01-28 16:33:53

如果同时有2个循环条目

您的功能不会实现任何方法来超越它(例如,一旦开始执行,请呼叫其他功能或另一个合同),这使其免受重新入侵的攻击。至少在此问题所示的上下文中。

EVM以串联执行所有交易 - 不平行。因此,即使有2个用户同时提交了执行此功能的交易,并且他们的两个交易都在同一块中开采,矿工仍将分别执行其交易,而不会相互影响。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文