#Day2 The Ethereum Virtual Machine

Shubham Sonthalia
3 min readJul 20, 2022
Image Credit: Tech-Fi

The Ethereum Virtual Machine

The EVM is the part of Ethereum that handles smart contract deployment and execution. Its job is to update the Ethereum state by computing valid state transitions as a result of smart contract code execution.

This aspect is the reason for defining Ethereum as a transaction-based state machine. I’ve already explained state machines in my previous blog. You can check that blog here.

As we know, state machines switch states based on the input data. In case of Ethereum, transactions directed to a smart contract function execution changes its state.

Each smart contract has its contract address, and in turn, each Ethereum address represents an account comprising of:

  1. an ether balance
  2. a nonce
  3. the account’s storage
  4. the account’s program code

The ether balance represents the wei owned by the account. The nonce represents the number of contracts created by the contract, the account’s storage is a permanent data store of all the ‘storage’ variables declared inside the contract, and finally the program code stores the byte-code version of the smart contract.

Think of Ethereum blockchain as a permanent storage of all these information related to an Ethereum account — no more, no less.

And if any transaction results in a smart contract code execution i.e., aims to call a function of a deployed smart contract, an EVM is instantiated with all the required information (related to the block, and the specific transaction). The EVM then loads the program code of the targeted contract, set its program counter to zero (a program counter contains the location of the instruction being executed currently), and sets all the environment variables. A key element of this process is the estimation of the gas supplied for this execution — which is set equal to the amount of gas paid by the sender at the start of the transaction (remember how we approve the gas cost using Metamask).

I’ll discuss gas in my next blog. But for now, you can think of gas as the currency that you must pay to use the resources of the Ethereum blockchain. You “buy” this currency using Ether or the native network token.

If at any point, the gas supply is reduced to zero, the EVM throws an “Out of Gas” exception and the transaction is reverted. The Ethereum goes back to the initial state with only two changes —

  1. the sender’s nonce that represents the number of successful transactions sent from the account is incremented by one.
  2. the sender’s ether balance is reduced by the amount of gas spent until the ‘halting’ point because the miner’s effort to include the transaction to the block, and the use of Ethereum resources must be compensated.

Excepting the above mentioned values, everything about the state of Ethereum remains same — as if the transaction never happened.

I hope the blog gave you an idea about the EVM. Think of EVM as a computational engine like that of Microsoft’s .NET framework, or JAVA interpreters, etc. which is responsible for changing states of the Ethereum state machine.

Thank You

--

--

Shubham Sonthalia

Backend Software Engineer. Learning and building scalable systems. Languages - C#, Java, Python, English (for blogging ofc)