Skip to content

Eddy3129/solidity-gitbook

Repository files navigation

description Basic introduction on Solidity Smart Contracts
icon user-robot-xmarks

Smart Contracts & Solidity

What is a Smart Contract?

Smart contracts are computer programs stored on the blockchain. They produce a deterministic output for every input and are considered immutable (unless implementing UUPS or Proxy patterns for upgradability).

The properties of a smart contract includes:

  • Immutability: Code cannot be changed once deployed, which necessitates deploying a new instance.
  • Deterministic: For every same input, every node running it will produce the same output.
  • Atomic: Either transaction completes or not executed at all when an error occurs. If an error occurs, the EVM halts execution of that specific transaction and reverts all state changes to the pre-transaction state, so the network can continue to operate without interruption.
  • Single-threaded: Every transaction is handled sequentially, and operate in a single-threaded manner.

What is the Ethereum Virtual Machine (EVM)?

A virtual machine is software that acts as an abstraction layer between code and the machine. It allows for portable execution, meaning it produces the same output across different physical machines, just like the Java Virtual Machine (JVM).

The EVM is the runtime environment for executing smart contracts. It executes programs that have been compiled into EVM Opcodes, which behave similarly to assembly instructions. For every function call, the opcodes are processed on the EVM stack, while accessing data from storage (persistent on-chain state variables) or memory (temporary data during function calls).

The EVM architecture, from https://www.saxenism.com/blog/evm-deep-dive

This architecture is crucial for enabling nodes running on entirely different operating systems to maintain the same functionalities and execution consistency across the entire Ethereum network.

When you call a function, you are technically sending a message (transaction) to the Ethereum network. The EVM uses specific fields in this message to route your request.

FieldDescription
FieldDescription
toThe Destination. The address of the smart contract you want to interact with.
valueThe Payment. The amount of ETH (in wei) to send along with the call. (Optional, 0 by default).
dataThe Instructions (Calldata). A hexadecimal string containing the Function Selector (first 4 bytes) and Arguments (encoded parameters).

Once the transaction reaches the contract, the EVM follows these steps:

  1. Dispatch: The EVM reads the first 4 bytes of the data (the Function Selector). It compares this against the contract's functions.
    • Match Found: Jumps to the specific code for that function.
    • No Match: Triggers the fallback() function (if it exists) or reverts the transaction immediately.
  2. Execution: The EVM spins up a fresh Stack and Memory for this specific call.
    • Stack/Memory: Temporary workspaces for calculations. Removed after execution.
    • Storage: The persistent database of the contract where state vars are stored.
  3. Outcome:
    • Success: The transaction finishes, update blockchain state, and any return values are sent back.
    • Revert: If an error occurs (e.g., require fails), the EVM halts. All changes to Storage are undone. Gas used up to that point is not refunded.

EVM Opcodes, Storage, and Memory

Opcodes are the lowest level of instructions the EVM executes. Each opcode is represented as a single byte like 0x01. Each opcode has a specific gas cost based on its complexity.

OpcodeMnemonicDescriptionGas Cost
0x01ADDAdds the top two items on the stack.3
0x35CALLDATALOADReads input data from the transaction.3
0x51MLOADReads a word from volatile Memory.3+
0x54SLOADReads from the global database.100 (Warm) - 2100 (Cold)
0x55SSTOREWrites a word to persistent Storage. 2900 - 20000+

When a function executes an operation like 1 + 1, the EVM performs a specific sequence of stack operations. It first uses PUSH1 to place each value onto the Stack, which is the only place where computation occurs. The ADD opcode then "pops" (removes) the top two items, calculates the sum, and pushes the result back onto the stack. This result remains temporary until it is explicitly moved elsewhere—either saved to volatile Memory using MSTORE (costing only ~3 gas) or written to persistent Storage using SSTORE.

The distinction in cost between Memory and Storage is driven by persistence and global consensus. Memory acts as temporary RAM, reading and writing here is cheap (~3 gas) because it requires no disk access. Storage, however, is the permanent "hard drive" of the blockchain. Writing to it (SSTORE) is gas expensive because every node in the network must update the global state trie. This is why developers often "cache" storage variables in memory for calculations before writing them back.

About

A GitBook where I jot down my learnings on Solidity and the EVM.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors