1

So, I'm trying to implement openzeppelin ERC721Enumerable contract. I want to override _beforeTokenTransfer method. Because it was a Hook, I have to call super method like this

function _beforeTokenTransfer( address from, address to, uint256 tokenId ) internal virtual override { super._beforeTokenTransfer(from, to, tokenId); // some requirement require(someRequirement(_msgSender()), 'MyERC721: Sender has not met the requirement to proceed') // some stuff } 

The problem is, inside _beforeTokenTransfer of ERC721Enumerable, there are some changing state variables. I am worried that when my method reverted, the state variables there are changed before are not going back to their previous state. Usually, something like this will happened

>>> class SomeClass: ... def __init__(self): ... self.state = 0 ... def change_state(self, x): ... self.state += x ... raise Exception('method reverted') >>> an_instance = SomeClass() >>> an_instance.state 0 >>> an_instance.change_state(10) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 6, in change_state Exception: method reverted >>> an_instance.state 10 

The state variable inside SomeClass is changed because the exception is raised after the state changed. Does this also valid for SmartContract? How does SmartContract handle changing state before exception is raised?

1 Answer 1

0

In this context it doesn't make much difference when the exception is raised. Transactions are atomic: either they succeed fully or they are fully reverted back.

So if there is a revert anywhere in the execution path, the whole transaction is reverted back, and all changes made by it are reverted. (This is not 100% correct: external contracts' revertions can be for example caught by try-catch blocks.)

So you don't really need to worry about some residual state changes.

5
  • "external contracts' revertions can be for example caught by try-catch blocks". Could you please elaborate this. Does this mean that if you try-catch the exception, the state could be changed? Commented Sep 28, 2021 at 5:53
  • With try-catch the whole transaction doesn't need to revert, if an external call reverts. It's up to you to decide. blog.ethereum.org/2020/01/29/solidity-0.6-try-catch Commented Sep 28, 2021 at 5:56
  • What I mean is if someone calls my contract from his contract dan does try-catch inside it, does the state from my contract would change because the exception caught by his contract? Commented Sep 28, 2021 at 6:39
  • I'm sorry, I didn't understand the question. And in any case, please post a new question if you have more question - or search among the existing questions Commented Sep 28, 2021 at 6:51
  • Following up to @fahadh4ilyas, the state would only change if the 'try' within the try/catch passed requirements needed in order to fulfill the tx. Commented Dec 10, 2022 at 22:16

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.