Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ethereum Solidity - Does require() use any gas?

Google has failed to give me an concrete answer, does using the require() function in Solidity use up any gas? Even if the statement in the function is evaluated as true?

like image 896
mrsulaj Avatar asked Jan 03 '18 12:01

mrsulaj


People also ask

Does solidity use gas?

Each transaction in the ethereum blockchain requires computational resources to execute. Therefore, each transaction requires a fee, which is called gas (or gas fee). A gas fee is required while executing the solidity code.

What is difference between require and if in solidity?

The difference is that require() is an error handling statement like you had stated, but if this statement fails, the transaction is reverted. Whereas if you had an if else statement, you would have to make sure to revert the transaction yourself.

What costs gas in solidity?

Every transaction costs at least 21,000 gas (“base fee”). Each CREATE operation (creating a smart contract) costs 32,000 gas.

How can I reduce my gas fee in Smart Contract?

Optimize Storage usage The number of Storage slots and the ways you represent your data in your Smart Contract affects the gas usage heavily. Here are some recommendations: Limiting Storage use: Since Storage is the most expensive type of Memory, you need to use it as little as possible, which isn't always easy.


1 Answers

I'm not quite sure if you're asking if the OPCODE itself consumes gas or if gas is consumed if the statement evaluates to true.

If you're asking about the OPCODE itself, I agree with you that the answer is unclear. I don't see the REVERT OPCODE (which is what require() is compiled to) in the (now very deprecated) Google OPCODE gas usage spreadsheet or in the yellowpaper (Appendix G).

Running a test in Remix, it looks like it does consume a very small amount of gas. Simply adding a require(true) call at the top of this method increased gas usage by 23.

contract GasUsage {
    uint val;

    function someFunc() public returns (bool) {
        require(true);

        delete val;
    }
}

Execution cost when included:5230

Execution cost when commented out: 5207


If you're asking about gas consumption up until the require statement, then the answer is yes. As of the Byzantium release, all gas consumed up to the point of a require statement is consumed, but any remaining gas is returned. Prior to Byzantium, require() and assert() were identical and all gas would be consumed.

From the Solidity docs:

Internally, Solidity performs a revert operation (instruction 0xfd) for a require-style exception and executes an invalid operation (instruction 0xfe) to throw an assert-style exception. In both cases, this causes the EVM to revert all changes made to the state...Note that assert-style exceptions consume all gas available to the call, while require-style exceptions will not consume any gas starting from the Metropolis release.

like image 173
Adam Kipnis Avatar answered Sep 19 '22 21:09

Adam Kipnis