Ethereum & EVM

M-Zohaib Nasir
4 min readMar 3, 2022

--

“Bitcoin” -the evergreen red-hot topic, but how is eth different from bitcoin. Bitcoin is primarily designed to be an alternative to traditional currencies and hence a medium of exchange and store of value. Ethereum is believed to be better off than bitcoin because of its real-world applications that can store great value. Ethereum enables the creation of new applications on its infrastructure. While bitcoin is a digital currency, Ethereum — on the other hand — is a blockchain-based network that also runs the currency by the same name.

What are Smart Contracts?

At their core, smart contracts are basically codified agreements between parties. This means that certain (pre-defined) conditions must be met for certain (pre-defined) executions to take place. Moreover, it is thanks to smart contracts that Ethereum has so many use cases. In simpler words. Smart contracts are simply programs stored on a blockchain that run when predetermined conditions are met. Smart contracts permit trusted transactions and agreements to be carried out among anonymous parties without the need for a central authority or intimidatory.

What is EVM?

As Ethereum Virtual Machine has been mentioned above too, so, let’s see what exactly it is.

It’s the environment in which all Ethereum accounts and smart contracts live. At any given block in the chain, Ethereum has one and only one ‘canonical’ state, and the EVM is what defines the rules for computing a new valid state from block to block. ~ ethereum.org

First, you need to understand what a virtual machine is. Virtual machines are machines that attempt a higher level of abstraction than your usual operating system or OS. Unlike Windows or iOS, virtual machines or VMs are created on top of the usual operating systems so they can function similarly to a physical computing machine. They can run on many different types of OS’s and hardware, making them the perfect engine for a decentralized ecosystem. Virtual machines are essentially creating a level of abstraction between the executing code and the executing machine. The Ethereum Virtual Machine (EVM) is what defines the rules for computing a new valid state from block to block. The EVM is a powerful, sandboxed virtual stack embedded within each full Ethereum node, responsible for executing contract bytecode. Contracts are typically written in higher level languages, like Solidity, then compiled to EVM bytecode.

This means that the machine code is completely isolated from the network, filesystem or any processes of the host computer. Every node in the Ethereum network runs an EVM instance which allows them to agree on executing the same instructions.

More into EVM:

Smart contract languages like Solidity cannot be executed by the EVM directly. Instead, they are compiled into low-level machine instructions (called opcodes). reference

  • Opcodes: Under the hood, the EVM uses a set of instructions (called opcodes) to execute specific tasks. At the time of writing, there are 140 unique opcodes. Together, these opcodes allow the EVM to be Turing-complete. This means the EVM is able to compute (almost) anything, given enough resources. Because opcodes are 1 byte, there can only be a maximum of 256 (1⁶²) opcodes
  • Bytecode: In order to efficiently store opcodes, they are encoded to bytecode. Every opcode is allocated a byte. Let’s take a look at the following bytecode: 0x6001600101
Bytecode, split up into bytes, decoded to opcodes (execution flow), and current stack
  • Costs of interacting with smart contracts: Since all contract executions are run by everyone running an Ethereum node, an attacker could try creating contracts including lots of computationally expensive operations to slow down the network. To prevent such attacks from happening, every opcode has its own base gas cost.
  • Deploying a smart contract: When deploying a smart contract, a regular transaction is created, without a to address. Additionally, some bytecode is added as input data. This bytecode acts as a constructor, which is needed to write initial variables to storage before copying the runtime bytecode to the contract’s code. During deployment, creation bytecode will only run once, while the runtime bytecode will run on every contract call.
Example Solidity contract, along with its bytecode needed to deploy
  • Decompiling bytecode: Several projects have created tools to attempt making bytecode more readable. Unfortunately, some parts of the original contract source, such as functions names or event names, are always lost due to optimization done by the compiler. Nonetheless, most function names can still be brute forced by comparing function signatures to large datasets containing popular function and event names. Contract calls usually require an “ABI” (Application Binary Interface), which is a piece of data documenting all functions and events, including their needed input and output. When calling a function on a contract, the function signature is determined by hashing the name of the function including its inputs (using keccak256), and truncating everything but the first 4 bytes.

& Finished!

References:

https://medium.com/mycrypto/the-ethereum-virtual-machine-how-does-it-work-9abac2b7c9e

--

--

M-Zohaib Nasir
M-Zohaib Nasir

No responses yet