Thursday, May 24, 2018

Ethereum: How Transaction Costs are Calculated

This article on Ethereum Transaction Costs was originally published at Bruno’s Bitfalls website, and is reproduced here with permission.

When sending a Bitcoin transaction, its fee is proportionate to its size. The more inputs and outputs, the more expensive it is. Add to that the factor of pending transactions, and transaction fees can skyrocket based on those two factors alone.


It’s recommended you read the following materials before diving into the rest of this post in order to better understand the terminology we’ll be mentioning.

Basic knowledge of programming terms (variables, loops) might also come in handy.


With Ethereum, given that we’re talking about a programming language within the protocol, it’s possible to be very computationally demanding with very little text or code (something which would be very cheap in the BTC-verse). Let’s look at this loop, for example:

while (i++ < 1000) {
    j = j + i;
}

This loop means “for as long as i is smaller than 1000, increase it by 1 and then sum up i and j and write the result into j, then do it all again.” This loop will execute 1000 times if i is 0, or more if it’s a negative number.

To pay for this computational cost in a fair way — since it has to be executed on all miners’ machines at once and they spend their resources and time on it — the concept of gas was introduced. Gas is used to pay for the execution of these so-called smart contracts (Ethereum programs) inside the EVM. For example, i + j above is a summation operation which costs 3 gas every time it’s executed, so 3000 gas if executed 1000 times.

To explain gas properly, let’s first cover the EVM.

EVM

EVM stands for Ethereum Virtual Machine. But what is a virtual machine anyway?

Virtual Machine

A virtual machine is software running on a specific computer which contains another operating system completely encapsulated inside the main one. For example, a virtual machine allows you to run Windows inside Linux, Linux inside Windows, Windows on macOS like in the image below, or any other combination.

Windows inside macOS

We use virtual machines to separate the environment in which we do our everyday computer use from the environment we work or program in. This lets us keep viruses at bay (they have no way of breaching the virtual machine and getting to the main operating system), helps prevent infinite loops from crashing our main operating system, and holds hard-drive corruptions like the infamous WannaCry ransomware at bay. Additionally, VMs let us use Windows games on Linux, for example, or allow us to program in different versions of the same programming language’s environment easily, without mixing them up.

EVM

The Ethereum Virtual Machine is built into the software running on the Ethereum protocol. It executes smart contracts — Ethereum programs written in the Solidity language. The EVM is contained in the full nodes of the Ethereum network, inside of which it executes these Ethereum-user-written programs.

Any miner of Ethereum simultaneously executes smart contract code. What this means is that Ethereum programs (dapps — decentralized apps) are executed on everyone’s computer at the same time (decentralized).

Execution of these programs isn’t free, however. Miners spend their own electricity, time, and hardware to do this. To pay them for their effort of executing computer instructions (like “store the value 5 into the variable X”), the concept of gas was introduced.

Continue reading %Ethereum: How Transaction Costs are Calculated%


by Bruno Skvorc via SitePoint

No comments:

Post a Comment