0

Starting from OpenZeppelin's ERC721.sol, _mint(to, tokenId) requires a unique token id.

function _mint(address to, uint256 tokenId) internal virtual {
    require(to != address(0), "ERC721: mint to the zero address");
    require(!_exists(tokenId), "ERC721: token already minted");
    ...

The uniqueness of a token id can be ensured by a counter, as applied in ERC721PresetMinterPauserAutoId.sol.

Counters.Counter private _tokenIdTracker;
...
function mint(address to) public virtual {
    ...
    _mint(to, _tokenIdTracker.current());
    _tokenIdTracker.increment();
}

I am not familiar with Solidity but it seems like no synchronous lock exists. Therefore, if there are others who also execute mint() at the same time, duplicated _mint(to, _tokenIdTracker.current()) can be executed before _tokenIdTracker.increment() is executed, thereby resulting a uniqueness problem.

I think this problem can be prevented by managing token ids with an external database and directly calling _mint() function with a unique value from AUTO_INCREMENT field. However, I want to know if there is any method that can be applied to internally manage unique token ids.

3
  • the transactions are processed after each other and not in parallel. Therefore the counter will work correctly
    – Majd TL
    Apr 9, 2022 at 17:10
  • If 2 people send a transaction at the same time. The miner will decide based on some parameters and gas fees which transaction is processed first
    – Majd TL
    Apr 9, 2022 at 17:11
  • @MajdTL Thanks for your wonderful comment!
    – J. Choi
    Apr 10, 2022 at 7:26

1 Answer 1

0

In Ethereum (account based Blockchain) the transactions are processed after each other and not in parallel. Therefore, the counter will work correctly.

As example:

If 2 users send transactions to same contract at the same second then the miner will decide based on some parameters e.g. gas fees which transaction is processed first and the first tx will get the number i and the second after that i+1 (Even if both included in the same block)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.