0

If I have a function that changes the state of the contract, and further down in the same function, there is a require statement. What happens to the contract state if something fails to pass the require statement? As in: if the function changes the variable x, then fails the require on the next line, will x remain changed since it happened before the failure? or will it revert because the entire txn fails?

example: calling changeX(1, 5);

Contract:

uint x;

function changeX(uint _x, uint _y){ x = _x; require(_y != 5, "_y was 5"); }

In this simple example I am aware I should just put the require statement at the top but I am using this as a metaphor for a more complex contract I am writing. I just need to know: if a txn fails, do any of the state changes remain? Or do they all revert regardless of how many there were before the failure.

1 Answer 1

0

All changes are reverted to maintain atomicity. Read more here. Relevant excerpt -

Internally, Solidity performs a revert operation (instruction 0xfd). This causes the EVM to revert all changes made to the state. The reason for reverting is that there is no safe way to continue execution, because an expected effect did not occur. Because we want to keep the atomicity of transactions, the safest action is to revert all changes and make the whole transaction (or at least call) without effect.

Your Answer

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

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