Git Product home page Git Product logo

ethereum-virtual-machine's Introduction

Ethereum Virtual Machine (EVM)

The Ethereum network exists solely for the purpose of keeping the single, continuous, uninterrupted, and immutable operation of the state machine that is the Ethereum blockchain. It is the environment in which all Ethereum accounts and smart contracts and data live. At any given block, Ethereum has one and only one globally accepted 'state'. The Ethereum Virtual Machine (EVM) is what defines the rules for computing a new valid state from block to block.

Prerequisites

Some basic understanding of bytes, memory, and the stack is required to understand the EVM.

It might also be helpful to familiarize yourself with some cryptography like hash functions.

Ethereum as a State Machine

Blockchains like Bitcoin are often described as 'distributed ledgers' which enable the existence of a decentralized currency using fundamental tools of cryptography.

A cryptocurrency can behave like a 'normal' currency because of the rules which govern what one can and cannot do to modify this ledger. For example, a Bitcoin address cannot spend more Bitcoin than it has previously received. These rules underpin all transactions that take place on Bitcoin, and similarly other blockchains.

While Ethereum also has its native cryptocurrency, the Ether, it also enables a much more powerful function that we have seen - Smart Contracts. For this more complex feature, we need a more powerful analogy than just 'distributed ledger'.

Instead of a distributed ledger, Ethereum can be described as a distributed state machine. A state machine is essentially any machine that can change from one state to another in response to certain inputs.

A simple state machine is a coin-operated turnstile, commonly found in subways or train stations, to prevent people from entering unless they pay using a coin or have a ticket.

The initial state for a turnstile is locked. In the locked state, if you keep pushing it, it remains locked. If you insert a coin, it moves to the unlocked state. If you keep inserting coins, it remains in the unlocked state. Once you push in the unlocked state (and someone passes through), it becomes locked again.

For Ethereum, the state is much more complex. It is described using a large data structure which contains all the state of the blockchain. The specific rules of how state can change from block to block is defined by the EVM.

Ethereum State Transition

On a high level, the EVM behaves similar to a mathematical state transition function. Given the current state, and a new set of valid transactions, it produces a new state. The output is deterministic, which means that for the same input, it will always produce the same output.

Y(S, T) = S'

Given the old valid state S, and a new set of valid transactions T, the state transition function Y produces the new valid state S'.

The state in Ethereum is stored as a really large data structure called a Merkle Patricia Trie. You do not need to understand exactly how it is structured, but if you want to, you can read the given link.

EVM Layer

The EVM lives as a layer in the software stack of Ethereum.

Ethereum nodes contain implementations of the EVM, and the EVM can then execute EVM code on it. EVM code is compiled smart contract bytecode that can be executed.

EVM Code Generation

EVM Instructions (OPCODES)

The EVM itself behaves as a stack machine with a maximum depth of 1024 items on the stack. Each item in the stack is a 256-bit (32 bytes) word.

During execution, the EVM maintains a transient memory, as a 32 byte addressed byte array, which does not persist between transactions. The transient memory is cleared when a new transaction is being executed.

Smart contracts, however, do maintain their own state in the blockchain. This state is also modeled as a Merkle Patricia Trie. This is commonly refered to as the EVM storage during transaction execution.

The EVM has logic present that allows it to execute EVM Opcodes, which perform standard operations on the stack like XOR, ADD, AND, SUB, MUL etc. The EVM also implements a number of blockchain-specific stack operations, such as BALANCE and BLOCKHASH.

When a smart contract is compiled into bytecode (represented in hexadecimal), it compiles down to EVM opcodes. These opcodes are what get executed on the EVM.

EVM Implementations

All implementations of the EVM must adhere to the specification described in the Ethereum Yellowpaper. Over Ethereum's history, the EVM has undergone multiple revisions, and there now exist multiple implementations of the EVM in various programming languages.

All Ethereum clients include an EVM implementation. In addition to those, there are multiple standalone implementations as well.

Ethereum Clients (with EVM)

  • Geth | Programming Language = Go
  • OpenEthereum | Programming Language = Rust
  • Nethermind | Programming Language = C# (.NET)
  • Besu | Programming Language = Java
  • Erigon | Programming Language = Go

Standalone EVM Implementations

Resources

The following are recommended, but optional, readings/viewings for learning more about the EVM.

ethereum-virtual-machine's People

Contributors

1agi avatar haardikk21 avatar kacieahmed avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.