1

What should happen when I modify an array, afterwards in the same execution the function fails?

Should I expect the pushed value to be presented or does it get deleted?

for example:

contract MyToken is ERC721Token("My Token", "MYTKN"), Ownable {

    struct TokenInfo {
       // ...
    }

    TokenInfo[] public tokens;

    // ...

    function mint(string userId) public payable {

        uint256 tokenId = tokens.push(newToken) - 1;
        string tokenUri = concat(BASE_URI, tokenId);

        // >>>>>>>>> Maybe function fails here <<<<<<<<

        // ...

        _mint(msg.sender, tokenId);
        _setTokenURI(tokenId, tokenUri);


        // >>>>>>>>> Maybe function fails here <<<<<<<<    
    }
}
1

If the transaction fails,and causes to run REVERT opcode, all the modifications will be reverted as they never happened, except the gas cost to execute upto that will be reduced from your account.

For e.g. Out of gas error or require or revert statement in Solidity will revert the changes.

Solidity documentation here provides a comprehensive guide on error handling and the answers to Why does a Solidity throw consume all gas? provide more details.

  • "except the gas cost to execute upto that will be reduced from your account" this is only if the failure calls the REVERT opcode, as in a require or revert statement in Solidity. If the failure is because of pretty much anything else, it'll eat all available gas. – flygoing Jul 21 '18 at 16:05
  • 1
    @flygoing Thanks for pointing out, when it says a failure I only had Out of gas error in my mind. I edited the answer :) – Achala Dissanayake Jul 23 '18 at 20:17
1

Out of gas error will revert all changes as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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