43

What is the difference between transaction cost and execution cost as seen after contract instantiation in remix?

Remix

I don't think it matters, but here is my contract:

contract DepositCounter {
    uint deposits = 0;
    function() {
        deposits++;
    }
}
3
  • 2
    Some related comments from a gitter conversation that I found helpful: "The difference between execution cost and transaction cost is the overhead of creating a transaction, which includes the cost of the payload, AFAIK. One is useful if you want to know how much it will cost for someone to call you from another contract, the other if you want to know how much it will cost to send a transaction to that method." Jan 16, 2017 at 17:29
  • 1
    "Transaction cost isn't specific to deployment. Every transaction has a transaction cost, deployment just happens to include the cost of the full bytecode of the contract." Jan 16, 2017 at 17:29
  • Transactions cost "68 gas per nonzero byte and 4 gas per zero byte in the transaction payload. ...[Comparing transactions that deploy contracts vs normal contracts,] there's an additional fee of 32k gas for contracts that create transactions, plus a fee for the storage of the contract data in state. Other than that, they're the same." Jan 16, 2017 at 17:31

3 Answers 3

16

Transaction costs are the costs for sending the contract code to the ethereum blockchain, they depend on the size of the contract.

Check out the source code:

 var getGasUsedOutput = function (result, vmResult) {
        var $gasUsed = $('<div class="gasUsed">');
        var caveat = lookupOnly ? '<em>(<span class="caveat" title="Cost only applies when called by a contract">caveat</span>)</em>' : '';
        if (result.gasUsed) {
            var gas = result.gasUsed.toString(10);
            $gasUsed.html('<strong>Transaction cost:</strong> ' + gas + ' gas. ' + caveat);
        }
        if (vmResult.gasUsed) {
            var $callGasUsed = $('<div class="gasUsed">');
            var gas = vmResult.gasUsed.toString(10);
            $callGasUsed.append('<strong>Execution cost:</strong> ' + gas + ' gas.');
            $gasUsed.append($callGasUsed);
        }
        return $gasUsed;
    };

Execution costs should be really the vm execution costs, if I interprete the parameter vmResult correctly. Is this maybe because a default constructor will be called any way?

4
  • I renamed it for the question but didn't update the screenshot. Same function though. That's quite strange that you don't see the same thing that I see. Jun 8, 2016 at 19:17
  • Screenshot updated. Jun 8, 2016 at 19:21
  • Ok, I've used an older version locally I think Jun 8, 2016 at 19:27
  • can confirm after update it works the same Jun 8, 2016 at 19:39
39

I hope this answer helps to provide a more complete picture alongside the accepted answer.

Transaction costs are based on the cost of sending data to the blockchain. There are 4 items which make up the full transaction cost:

  1. the base cost of a transaction (21000 gas)
  2. the cost of a contract deployment (32000 gas)
  3. the cost for every zero byte of data or code for a transaction.
  4. the cost of every non-zero byte of data or code for a transaction.

Execution costs are based on the cost of computational operations which are executed as a result of the transaction.

You can see the breakdown in the yellow paper, Appendix G.

enter image description here

9

This post is an addition to the previous answer from Roland Kofler in June 2016.

Execution costs should be really the vm execution costs, if I interprete the parameter vmResult correctly. Is this maybe because a default constructor will be called any way? In addition to Roland:

Execution cost has everything to do with the costs for storing global variables and the runtime of the method calls.

Storing global variables seems rather expensive to me. In the code below you see me testing the mathematical efficiency of exponentiation. All the computations together cost roughly 500. Adding the line "result = res;" changes this from 500 to roughly 20,000 execution cost.

uint256 result;
    function modPow2() returns (uint res){
        res = 2341**4 % 3456;
        res = res**8 % 3456;
        res = res**2 % 3456;
        res = res**2 % 3456;
        res = res**2 % 3456;
        res = res**4 % 3456;
        result = res;
    }

If you run this method twice in a row, it costs respectively 20,000 and then 5,000. The third time I run it, it costs 5,000 again. My guess is that changing the bit-size of the global variable result is expensive. I tested this by resetting the value with result=0; or do result=result+1; between the runs.

Then Transaction cost has everything to do with the size of the compiled contract PLUS the execution cost. Comments don't change the transaction costs and the naming of the variables also don't. But adding a (non-called) method does. You can call some methods within your contract and it appears that transaction cost - execution cost = constant for the compiled contract you are currently working with.

3
  • 1
    shouls i have to pay (Transaction Cost + Execution Cost) or only above the transaction cost Jul 27, 2017 at 8:20
  • 6
    You have to pay transaction cost, which includes execution cost.
    – skozin
    Feb 1, 2018 at 15:21
  • Actually I feel this is the best answer rather than the most voted and the one accepted as correct answer. Apr 28, 2021 at 22:50

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.