Given a modifier:

modifier yetNotCalculated() {
    require(calculated == false);

and then using it in my function:

function calculateResult() public yetNotCalculated {
    calculated = true;

When the modifier rejects further processing (calculated is set to true), it consumes all my gas :( ! is this a desired behaviour ?

Here I am invoking that function using web3 library, and that what I get as receipt:

    { blockHash: '0xc8219ac506e4020f61e199126d8b578c8d8219222286d684661415593c251495',
  blockNumber: 12476,
  contractAddress: null,
  cumulativeGasUsed: 2100000,
  from: '0xe6cb4d38ae1f3458f5095652a68b304b3aa7f82c',
  gasUsed: 2100000,
  logs: [],
  logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
  root: '0x6047a5fe941496e089cf73e0c2cbd7db1608794bac0a73368fcfcd34833a1aef',
  to: '0xf8b8741024621ee0936bafac55ff544421d7924b',
  transactionHash: '0xf8b40eeb956b425fc4cfadec1c1cc2a479addbc635c74fb6000945fedd41696a',
  transactionIndex: 0 }

as you can see 2100000 gas was used which is my gas limit. why is that :) ? Thanks

  • Gas should be returned with require(false);. See stackoverflow.com/a/48085107/2318492 Mar 14, 2018 at 10:40
  • What solidity version are you using in your contract? The revert() opcode is different pre-byzantium.
    – valdi.k
    Mar 14, 2018 at 11:59
  • I am using geth 1.7.3 on my local private test network. solc --version solc, the solidity compiler commandline interface Version: 0.4.19+commit.c4cbbb05.Linux.g++ @Valdi_K
    – friko
    Mar 14, 2018 at 14:00

2 Answers 2


It's not the modifier, it's the require. require reverts/errors if the condition is false, so, if calculated==true then revert.

This might not be a great application for modifier or require. Have you considered if(!calculated) calculate(); return true;?

Hope it helps

  • what I would like to achieve is when a certain condition is not true, and that condition can be checked right at the very begining, there should be no gas consumed. I am not sure if it's possible that the user's transactions took 0 gas when function was rejected on modifier, but that would be great if possible....
    – friko
    Mar 14, 2018 at 20:50
  • Separate concerns. You can make calculated public so anyone can see ahead of time if it's already done. Then decide if you want to return true, false or revert() in the case they ask for it again. I might incline to "ask a silly question, lose all your gas" given that you provided a "free" getter they could check first if they weren't so lazy. It is not possible to return a false for free. Although the cost to check and return would be tiny, the user will pay for the computation to run the logic. Mar 14, 2018 at 21:17

“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.” (By Adam Kipnis)

As far as I know this is the right answer.

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.