当要求当前 solidity 计数器的多个调用获得相同的值时会发生什么?

What happens when multiple call asking for the current counter in solidity gets the same value?

我正在学习创建 NFT markertplace (openZeppelin-ERC721) 并卡在了柜台。我想知道执行下面这段代码时会发生什么。

pragma solidity ^0.8.0;

library Counters {
    struct Counter { uint256 _value; }

    function current(Counter storage counter) internal view returns (uint256) {
        return counter._value;
    }

    function increment(Counter storage counter) internal {
        unchecked { counter._value += 1; }
    }

    function decrement(Counter storage counter) internal {
        uint256 value = counter._value;
        require(value > 0, "Counter: decrement overflow");
        unchecked { counter._value = value - 1; }
    }

    function reset(Counter storage counter) internal {
        counter._value = 0;
    }
}

假设 A 和 B 都在尝试创建 NFT,并且计数器应该递增并为他们的 NFT 提供 ID。但是,如果他们都试图同时创建 NFT,我的意思是当然有很大的机会发生这种情况。另一个 NFT 是否会被丢弃,获得一个新的 ID,如果是这样,是否需要比预期更长的时间?那GAS费呢???

很多问题希望你明白我想说什么!

我没有在这段代码中找到更改 NFT id 的地方。它是一个可以递增或递减值的计数器。如果两个人同时创建一个 nft,处理 nft id 的函数将根据以太坊区块中的哪笔交易先来确定谁获得什么 id。除非智能合约设置为这样做,否则不会丢弃任何 nft。如果你能解释一下 gas 费是什么意思,你可能会找到答案。

But, what if they both try to create NFT at the same time, I mean ofcourse there is a lot of chance of happening this.

您的假设不正确。不做假设,时刻批判思考。

所有以太坊交易都是按顺序执行的,理论上没有办法让两个交易同时发生。请.

最先选择的交易将获得第一个令牌(计数器),另一个在它之后。