Ethereum Basics
Ethereum Basics
No Loops Allowed
○ This prevents denial-of-service (DoS) attacks where a transaction could get stuck
in an infinite computation, clogging the network.
Value Blindness
○ Bitcoin scripts operate at a transaction level but do not have direct access to input
or output values.
○ Imagine you want to create a script that allows a transaction to be valid only if the
output amount is exactly 1 BTC. In Bitcoin Script, this is not possible because the
script cannot check or enforce the transaction amount.
Lack of State
Blockchain Blindness
Bitcoin Script cannot check previous transaction details beyond the UTXOs being spent.
Example: You cannot write a script that says, “Only allow spending if this address has
received BTC 10 times before.”
You cannot create a Bitcoin contract that releases BTC if the price of gold crosses
$2,000.
● States: The global state of the Ethereum blockchain, which includes account
balances, smart contract code, and storage.
● Transitions: Changes to the state caused by transactions (e.g., sending ETH, calling
a smart contract).
● Inputs: Transactions submitted by users.
● Rules: The Ethereum Virtual Machine (EVM) executes transactions and smart
contracts according to predefined rules, ensuring deterministic state transitions.
● Unbounded refers to the fact that Ethereum's state is not limited in size. The
state can grow as more accounts, contracts, and data are added to the
blockchain.
● However, it is practically bounded because the growth of the state is
constrained by real-world factors like storage costs, gas fees, and block size
limits. These factors prevent the state from growing infinitely in practice
Platform Independence:
○ Code is compiled into an intermediate format (e.g., bytecode) that the VM can
execute, making it portable across different platforms.
Memory Management:
○ The VM handles memory allocation and garbage collection, reducing the risk of
memory leaks and other common errors.
Standardized Environment
○ The EVM provides a standardized runtime environment with its own stack,
memory, and storage systems.
○ This ensures that smart contracts behave the same way, regardless of the
underlying platform.
Deterministic Execution:
○ The EVM is designed to produce the same output for the same input and initial
state, no matter where it is executed.
○ This determinism is crucial for maintaining consensus across the decentralized
network.
Summarizing!
It uses a blockchain to synchronize and store the system’s state changes, along
with a cryptocurrency called ether to meter and constrain execution resource costs.
○ Account-based blockchain
○ Transactions == state transaction function
While providing high availability, auditability, transparency, and neutrality, it also reduces
or eliminates censorship and reduces certain counterparty risks.
Layers in Ethereum
DApps represent a broader perspective than smart contracts. A DApp is, at the very least,
a smart contract and a web user interface. More broadly, a DApp is a web application that
is built on top of open, decentralized, peer-to-peer infrastructure services.
For example, if Alice wants to send Bob 3 tokens, her account balance is reduced
by 3, while Bob's balance increases by the same amount. This direct balance
management simplifies the handling and tracking of funds as users don’t have to
juggle multiple outputs. Because of this simplicity, the Account Model is better
suited for smart contracts and programmability as it allows for more straightforward
state management.
Comparing UTXO and Account Models
Both the UTXO and Account models facilitate transaction tracking within blockchain
systems, but they operate fundamentally differently. The UTXO model’s discrete
units make tracing fund flows challenging, but not impossible. In contrast, the
Account Model simplifies balance management by maintaining a single balance per
account, but is inherently more open due to identifiable accounts.
The benefits of UTXOs are:
1. Higher degree of privacy: if a user uses a new address for each transaction
that they receive then it will often be difficult to link accounts to each other. This
applies greatly to currency, but less to arbitrary dapps, as arbitrary dapps often
necessarily involve keeping track of complex bundled state of users and there
may not exist such an easy user state partitioning scheme as in currency.
2. Potential scalability paradigms: UTXOs are more theoretically compatible
with certain kinds of scalability paradigms, as we can rely on only the owner of
some coins maintaining a Merkle proof of ownership, and even if everyone
including the owner decides to forget that data then only the owner is harmed.
In an account paradigm, everyone losing the portion of a Merkle tree
corresponding to an account would make it impossible to process messages
that affect that account at all in any way, including sending to it. However,
non-UTXO-dependent scalability paradigms do exist.
The benefits of accounts are:
1. Large space savings: for example, if an account has 5 UTXO, then switching from a UTXO model to an
account model would reduce the space requirements from (20 + 32 + 8) * 5 = 300 bytes (20 for the address,
32 for the txid and 8 for the value) to 20 + 8 + 2 = 30 bytes (20 for the address, 8 for the value, 2 for a
nonce(see below)). In reality savings are not nearly this massive because accounts need to be stored in a
Patricia tree (see below) but they are nevertheless large. Additionally, transactions can be smaller (eg. 100
bytes in Ethereum vs. 200-250 bytes in Bitcoin) because every transaction need only make one reference
and one signature and produces one output.
2. Simplicity: easier to code and understand, especially once more complex scripts become involved.
Although it is possible to shoehorn arbitrary decentralized applications into a UTXO paradigm, essentially
by giving scripts the ability to restrict what kinds of UTXO a given UTXO can be spent to, and requiring
spends to include Merkle tree proofs of change-of-application-state-root that scripts evaluate, such a
paradigm is much more complicated and ugly than just using accounts.
3. Constant light client reference: light clients can at any point access all data related to an account by
scanning down the state tree in a specific direction. In a UTXO paradigm, the references change with each
transaction, a particularly burdensome problem for long-running dapps that try to use the above mentioned
state-root-in-UTXO propagation mechanism.
From the developers
“We have decided that, particularly because we are dealing with dapps containing arbitrary state
and code, the benefits of accounts massively outweigh the alternatives. Additionally, in the
spirit of the We Have No Features principle, we note that if people really do care about privacy
then mixers and coinjoin can be built via signed-data-packet protocols inside of contracts.”
One weakness of the account paradigm is that in order to prevent replay attacks, every transaction
must have a "nonce", such that the account keeps track of the nonces used and only accepts a
transaction if its nonce is 1 after the last nonce used. This means that even no-longer-used accounts
can never be pruned from the account state.
“Imagine buying an asset for 10 ETH - you pay the seller but want assurance they won't resend the
same transaction to drain your account. This is where nonces come in.
A nonce is a number that can only be used once in cryptographic communication. In Ethereum,
nonces are per-account transaction sequence numbers.
When you send that first 10 ETH, your account's nonce increments to 1. The transaction also
includes this nonce value. For any subsequent transactions, the nonce must increment (2, 3, etc)
properly, or nodes will reject it.
This prevents the seller from merely re-broadcasting the original nonce 1 transaction. The network
will see that the nonce is no longer valid for your account and drop the fraudulent transaction.”
As such, nonces uniquely sequence transactions from each account, ensuring external ownership
and thwarting replay attacks. By systematically incorporating nonces, Ethereum guarantees
transaction integrity across the decentralized network.
“An account's balance can be spent in multiple transactions, but each transaction must have
a unique nonce to ensure it is processed correctly.”
● Alice sends 10 ETH to Bob with nonce 0.
● The network processes the transaction and updates Alice’s nonce to 1.
● If Alice tries to send another 10 ETH to Bob with the same nonce 0, the network will
preventing double-spending.
Ethereum Wallet
We will use the term "wallet" to mean a software application that helps you manage
your Ethereum account.
In short, an Ethereum wallet is your gateway to the Ethereum system.
It holds your keys and can create and broadcast transactions on your behalf.
Ethereum Wallet
https://github.com/ethereumbook/ethereumbook/bl
ob/develop/02intro.asciidoc#metamask_password
Which networks does
MetaMask support? https://github.com/ethereumbook/ethereumbook/bl
MetaMask supports Ethereum (including all
ob/develop/02intro.asciidoc#metamask_password
ERC-20 tokens) and both layer one and two
EVM-compatible blockchains, including:
● Why Deprecation?
Seed phrases as we know them today were codified for usage in Bitcoin, according to a standard
referred to as Bitcoin Improvement Proposal 39, or BIP-39. In simple terms, a series of words are
selected with a high level of randomness from a specific list of words. In MetaMask and many
other Ethereum-compatible technologies, there are 12 words in a seed phrase
Creating an HD Wallet from the Seed
HD wallets are created from a single root seed, which is a 128-, 256-, or 512-bit
random number. Most commonly, this seed is generated from a mnemonic as
detailed in the previous section.
Every key in the HD wallet is deterministically derived from this root seed, which
makes it possible to recreate the entire HD wallet from that seed in any compatible
HD wallet. This makes it easy to export, back up, restore, and import HD wallets
containing thousands or even millions of keys by transferring just the mnemonic
from which the root seed is derived.
Ethereum Address
Ethereum addresses are unique identifiers that are derived from public keys or contracts using the Keccak-256 one-way hash
function.
If Public key K:
K = 6e145ccef1033dea239875dd00dfb4fee6e3348b84985c92f103444683bae07b83b5c38e5e...
SHA-256(K) = 2a5bc342ed616b5ba5732269001d3f1ef827552ae1114027bd3ecf1f086ba0f9
Then we keep only the last 20 bytes (least significant bytes), which is our Ethereum address:
001d3f1ef827552ae1114027bd3ecf1f086ba0f9
● Slot 0: 42
● Slot 1: 100
● Data Integrity:
○ The StorageRoot ensures that contract storage cannot be tampered with. Any change to
storage modifies the root hash.
○ Ethereum nodes can verify storage values without downloading the entire state (using
Merkle proofs).
● Efficiency:
○ Instead of storing all storage data in the account, Ethereum only keeps the root hash,
saving space.
○ Full storage data is maintained by execution clients (e.g., Geth, Erigon).
● State Proofs:
○ Light clients can verify specific storage values by checking Merkle proofs against the
StorageRoot.
● Every Ethereum account (whether an Externally Owned Account (EOA) or a
Contract Account) has a CodeHash field in its state.
● For Contract Accounts, this is the Keccak-256 hash of the EVM (Ethereum
Virtual Machine) bytecode stored on the blockchain.
● For EOAs (user-controlled accounts), CodeHash is always keccak256("") (the
hash of an empty string), because EOAs have no code.
● When a contract is deployed, its runtime bytecode is stored on-chain, and its CodeHash is
computed and saved in the account state.
● When executing a transaction, Ethereum nodes can verify a contract’s integrity by:
○ Fetching the bytecode from the network.
○ Computing its Keccak-256 hash.
○ Comparing it with the stored CodeHash to ensure no tampering occurred.
● Security: Ensures that contract code hasn’t been altered (since changing the code would
change the hash).
● Efficiency: Allows lightweight verification without storing full bytecode in the state trie.
● Differentiation: Helps distinguish between EOAs (no code) and contracts (with code).
Two types of Ethereum Account
In addition to ether, transactions can contain data indicating which specific function in the
contract to run and what parameters to pass to that function. In this way, transactions can
call functions within contracts.
Note that because a contract account does not have a private key, it cannot initiate a
transaction.
Only EOAs can initiate transactions, but contracts can react to transactions by calling other
contracts, building complex execution paths. One typical use of this is an EOA sending a request
transaction to a multisignature smart contract wallet to send some ETH on to another address. A
typical DApp programming pattern is to have Contract A calling Contract B in order to
maintain a shared state across users of Contract A.
Example
The Solidity compiler comes as a standalone executable, as part of various frameworks, and bundled in
Integrated Development Environments (IDEs). To keep things simple, we will use one of the more popular IDEs,
called Remix.
Use your Chrome browser (with the MetaMask wallet you installed earlier) to navigate to the Remix IDE at
https://remix.ethereum.org.
Example
Example
Example
Example
Fee Discussion (EIP - 1559)
Instead of a singular Gas Price, you now have to pay attention to three separate values:
● The Base Fee, which is determined by the network itself and is subsequently burned.
● A Max Priority Fee, which is optional, determined by the user, and is paid directly to miners.
● The Max Fee Per Gas, which is the absolute maximum you are willing to pay per unit of gas to get
your transaction included in a block. For brevity and clarity, we will refer to this as the Max Fee.
The Base Fee is determined by the Ethereum network rather than being set by end-users looking to transact or miners
seeking to validate transactions. The Base Fee targets 50% full blocks and is based upon the contents of the most recent
confirmed block. Depending on how full that new block is, the Base Fee is automatically increased or decreased.
For example:
● If the last block was exactly 50% full (15 M), the Base Fee will remain unchanged.
● If the last block was 100% full, the Base Fee will increase by the maximum 12.5% for the next block.
● If the last block was more than 50% full but less than 100% full, the Base Fee will increase by less than 12.5%.
● If the last block was 0% full – that is, empty – the Base Fee will decrease the maximum 12.5% for the next block.
● If the last block was more than 0% full but less than 50% full, the Base Fee will decrease by less than 12.5%.
Fee Discussion
The Base Fee is algorithmically adjusted block-by-block based on how full the previous
block was. This mechanism, introduced in EIP-1559, aims to:
1. Keep block sizes stable (targeting ~15M GAS units per block, max hard cap 30 M
Gas unit, so 15 M means half full).
2. Automatically adjust fees to match network demand.
3. Prevent fee spikes during congestion (unlike the old auction system - First Price
Auction).
Fee Discussion
● Transactions in the same block share the same base fee, but across
different blocks, the base fee differs because:
○ Network activity fluctuates (e.g., NFT mints, DeFi liquidations cause
spikes).
○ The base fee adjusts every approx 15 seconds (avg. block time).
Problem with previous fee mechanism
● Users had to guess the right gas price to get their transactions included.
● During congestion (e.g., ICO craze, DeFi boom), fees spiked exponentially due to bidding wars.
● Example: Fees could jump from 50 Gwei to 500 Gwei in minutes, forcing users to overpay.
● Wasted ETH: Users often overpaid (e.g., bidding 200 Gwei when 100 Gwei would have sufficed).
● Bribe Miners: Users had to bribe miners (via higher gas prices) for priority.
● Delayed Transactions: If you underbid, your TX could sit unconfirmed for hours.
● No Refunds: Even if blocks had extra space, excess fees went to miners (not users).
● No block size flexibility: Blocks had a fixed gas limit (e.g., 15M gas), leading to:
a. Backlogs: Thousands of stuck transactions during peak demand.
b. Gas Price Wars: Users constantly resubmitted TXs with higher fees.
● All fees went to miners, contributing to ETH inflation. No mechanism to remove ETH from circulation (unlike
EIP-1559’s base fee burn)
Problem with previous fee mechanism
● Users set their own Max Fee per transaction (it's a user-defined parameter).
● Validators (block proposers) then select transactions based on these fees.
● The network auto-calculates the Base Fee using a deterministic formula.
For 'typical' transactions that are submitted under normal, not-congested network conditions, the Max Priority Fee
will need to be close to 2.0 GWEI. But, for transactions where order or inclusion in the next block is important, or when
the network is highly congested, a higher Max Priority Fee may be necessary to prioritize your transaction.
A somewhat subtle nuance to the Max Priority Fee is that it represents the maximum tip you are willing to pay to
a miner. However, if the Base Fee plus the Max Priority Fee exceeds the Max Fee, the Max Priority Fee
will be reduced in order to maintain the upper bound of the Max Fee. This means the actual tip may need to be
smaller than your Max Priority Fee and, under such circumstances, your transaction may become less attractive
to miners.
The Max Priority Fee — also often referred to as the miner tip — is an 'optional' additional fee that is paid directly
to miners in order to incentivize them to include your transaction in a block. While the Max Priority Fee is
technically optional, at the moment most network participants estimate that transactions generally require a minimum
2.0 GWEI tip to be candidates for inclusion. With that said, specific mining pools may choose to set alternative
minimums for inclusion.
For 'typical' transactions that are submitted under normal, not-congested network conditions, the Max Priority Fee
will need to be close to 2.0 GWEI. But, for transactions where order or inclusion in the next block is important, or when
the network is highly congested, a higher Max Priority Fee may be necessary to prioritize your transaction.
A somewhat subtle nuance to the Max Priority Fee is that it represents the maximum tip you are willing to pay to
a miner. However, if the Base Fee plus the Max Priority Fee exceeds the Max Fee, the Max Priority Fee
will be reduced in order to maintain the upper bound of the Max Fee. This means the actual tip may need to be
smaller than your Max Priority Fee and, under such circumstances, your transaction may become less attractive
to miners.
The base fee is burned (permanently removed from circulation) as part of Ethereum’s EIP-1559 upgrade for three key
reasons:
1. Economic Sustainability
○ Burning the base fee reduces ETH supply over time, making Ethereum more deflationary.
○ This counterbalances ETH issuance (new ETH created as block rewards) and can increase scarcity.
2. Fairer Fee Market
○ Before EIP-1559, miners/validators earned all transaction fees, leading to volatile gas wars.
○ Burning the base fee removes their incentive to artificially congest the network for higher profits.
3. Security & Predictability
○ Since the base fee is algorithmically set (not controlled by miners), burning it ensures no single party
benefits from manipulating fees
● Prevents Manipulation: If miners earned the base fee, they could spam the network
to drive up fees.
● Better ETH Economics: Burning ETH reduces supply, potentially increasing its
value long-term.
● Fairer for Users: Users pay only what’s necessary (base fee + tip), not inflated
auction prices.
● Since EIP-1559 (August 2021), over 4 million ETH (~$10B+) has been burned.
● Burning offsets ~60-80% of new ETH issuance, making Ethereum net deflationary
during high activity.
● If burning > issuance: ETH supply shrinks (deflationary).
● If burning < issuance: ETH supply grows (inflationary).
Example
Example
0x665E796Bb3Aa5136ccdDA419C044979B251C87Ca
Example
Example
Internal Transaction also called as Message
GAS
Gas is the fuel of Ethereum. Gas is not ether—it’s a separate virtual currency with its own exchange rate
against ether. Ethereum uses gas to control the amount of resources that a transaction can use, since it
will be processed on thousands of computers around the world.
Gas is separate from ether in order to protect the system from the volatility that might arise along with rapid
changes in the value of ether, and also as a way to manage the important and sensitive ratios between the
costs of the various resources that gas pays for (namely, computation, memory, and storage).
The gasPrice field in a transaction allows the transaction originator to set the price they are willing to pay in
exchange for gas. The price is measured in wei per gas unit.
The nonce is one of the most important and least understood components of a
transaction.
nonce: A scalar value equal to the number of transactions sent from this address or, in
the case of accounts with associated code, the number of contract-creations made by
this account.
Strictly speaking, the nonce is an attribute of the originating address; that is, it only has
meaning in the context of the sending address. However, the nonce is not stored
explicitly as part of an account’s state on the blockchain. Instead, it is calculated
dynamically, by counting the number of confirmed transactions that have
originated from an address.
Gaps in Nonces, Duplicate Nonces, and Confirmation
If you then transmit a transaction with the missing nonce 1, both transactions
(nonces 1 and 2) will be processed and included (if valid, of course). Once you fill
the gap, the network can mine the out-of-sequence transaction that it held in the
mempool.
Message contains
- Sender
- Recipient
- Amount of ether
- Start gas
An important consequence of the message mechanism is the "first class citizen" property
of Ethereum -
The idea that contracts have equivalent powers to external accounts, including the
ability to send message and create other contracts.
This allows contracts to simultaneously serve many different roles: for example, one
might have a member of a decentralized organization (a contract) be an escrow account
(another contract) between an paranoid individual employing quantum-proof Lamport
signatures (a third contract) and a co-signing entity which itself uses an account with five
keys for security (a fourth contract). The strength of the Ethereum platform is that the
decentralized organization and the escrow contract do not need to care
about what kind of account each party to the contract is
● The "strength" mentioned is that contracts don’t need to know or care
whether the other parties they interact with are EOAs or other contracts.
This abstraction simplifies design and enables composability (like Lego pieces
snapping together).
● For example, the DAO and escrow don’t need special logic to handle whether
a user is a person, a multisig, or another contract—they just send/receive
messages.
Transaction Propagation
The Ethereum network uses a "flood routing" protocol. Each Ethereum client acts as a node in a peer-to-peer (P2P)
network, which (ideally) forms a mesh network. No network node is special: they all act as equal peers.
Transaction propagation starts with the originating Ethereum node creating (or receiving from offline) a signed
transaction. The transaction is validated and then transmitted to all the other Ethereum nodes that are directly connected
to the originating node. On average, each Ethereum node maintains connections to at least 13 other nodes, called its
neighbors.
Each neighbor node validates the transaction as soon as they receive it. If they agree that it is valid, they store a copy
and propagate it to all their neighbors (except the one it came from). As a result, the transaction ripples outwards from
the originating node, flooding across the network, until all nodes in the network have a copy of the transaction. Nodes
can filter the messages they propagate, but the default is to propagate all valid transaction messages they receive.
Within just a few seconds, an Ethereum transaction propagates to all the Ethereum nodes around the globe. To be able
to track the origins of transactions, or interfere with propagation, an attacker would have to control a significant
percentage of all nodes.
Verifier’s dilemma
Expressive transaction scripts in emerging cryptocurrencies can require significant
computational work to verify.
Miners are incentivized to verify all scripted transactions for the “common good” of the cryptocurrency so to
speak.
However, verifying scripts consumes computation resources and therefore delays
honest miners in the race to mine the next block.
○ Hardware Costs: Running a full node requires high RAM/CPU (state storage is ~TB-scale).
○ Opportunity Cost: Time spent verifying = less time proposing blocks.
Valid transactions will eventually be included in a block of transactions and, thus, recorded in the Ethereum blockchain. Once mined
into a block, transactions also modify the state of the Ethereum singleton, either by modifying the balance of an account (in the
case of a simple payment) or by invoking contracts that change their internal state. These changes are recorded alongside the
transaction, in the form of a transaction receipt, which may also include events.
Lifecycle of Transaction
Gas Fees: Base: 0.00337008 Gwei |Max: 0.005174646 Gwei |Max Priority: 0.001 Gwei
Savings Your Buffer = Max Fee - Max Priority Savings per gas:
0.005174646 - 0.001 = 0.004174646 Gwei 0.004174646 -
0.00337008 =
Base Fee was lower than buffer: 0.000804566 Gwei
0.00337008 (Base) < 0.004174646 (Buffer)
Total Savings:
3,870,875 gas ×
0.000804566 Gwei =
0.000003114 ETH
1. Max Priority Fee is a cap on your tip, not the tip itself
2. Actual tip is the smaller of:
○ Your Max Priority Fee
○ What's left after base fee (Max Fee - Base Fee)
3. Savings only occur when:
○ You have room between Max Fee and Max Priority Fee (Max Fee - Max Priority > 0)
○ AND the base fee is lower than this buffer
1. You set Max Fee > Max Priority Fee (creating a buffer for base fee changes)
2. The actual Base Fee ends up lower than your buffer (Max Fee - Max Priority Fee)
Interpret