Introduction
Ethereum revolutionized blockchain technology by introducing smart contracts and a decentralized virtual machine. This guide explores its foundational concepts, account models, transaction types, and gas mechanisms.
World State
The world state is a mapping between addresses (160-bit identifiers) and account states (serialized as RLP data structures). Instead of storing the state directly, Ethereum records the root hash of a Merkle Patricia Tree in the block header for integrity verification.
Account Models
Ethereum replaces Bitcoin’s UTXO model with accounts, categorized as:
1. External Accounts
- Controlled by private keys.
- No associated code; addresses derive from public keys.
- Can initiate transactions (e.g., transferring ETH).
2. Contract Accounts
- Governed by smart contract code.
- Addresses generated from the creator’s address and
nonce. - Respond to message calls but cannot initiate transactions.
Account State
All accounts share these four fields:
| Field | Description |
|---|---|
nonce | Count of transactions/contracts created by the account. |
balance | ETH balance in Wei (1 ETH = 10¹⁸ Wei). |
storageRoot | Hash of the account’s Merkle Patricia Tree root for storage. |
codeHash | Hash of the EVM code (empty for external accounts). |
Transactions vs. Messages
Transactions
- Signed data packets from external accounts.
- Recorded on-chain; alter Ethereum’s state.
Types:
- Contract creation: Receiver address is
⌀. - Message passing: Triggers contract execution.
- Contract creation: Receiver address is
Components:
- Recipient address.
- Signature (verifies sender).
value: ETH to transfer (Wei).gasLimit: Max gas allocatable.gasPrice: Price per gas unit (set by sender).
Messages
- Virtual data packets between accounts.
- Synchronous; exist only during execution.
- Trigger message calls (like function calls).
Components:
- Sender (implicit).
- Recipient.
value: ETH transferred.- Optional data input.
STARTGAS: Gas limit for the call.
👉 Learn how gas optimizes Ethereum transactions
Gas Mechanism
- Purpose: Prevent abuse and manage computational costs.
Key Rules:
- Users prepay gas (
gasLimit × gasPrice). - Unused gas is refunded; spent gas rewards miners.
- OOG (Out-of-Gas): Reverts execution; gas is forfeited.
- Users prepay gas (
Note: Message calls with OOG revert only the sub-execution, not the parent.
Smart Contract Lifecycle
1. Contract Creation
Triggered by:
- External accounts (via transactions).
- Contract accounts (via message calls).
- Address derivation: Creator’s address +
nonce. - Code stored permanently after initialization.
2. Contract Execution
- Activated by transactions/messages sent to contract addresses.
Limitations:
- Contracts cannot self-trigger; require external calls.
- Use services like Ethereum Alarm Clock for scheduled checks.
👉 Explore Ethereum smart contract tools
FAQs
Q1: Can contracts execute autonomously?
No. Contracts need external transactions to trigger code execution.
Q2: How are gas fees calculated?
Fees = gasUsed × gasPrice. Higher gasPrice prioritizes transactions.
Q3: What happens if a contract runs out of gas?
Execution halts; changes revert, but gas isn’t refunded.
Q4: How do contract addresses differ from external ones?
Contract addresses derive from creator details, while external ones use public keys.
Conclusion
Ethereum’s state machine evolves through transactions initiated by external accounts, enabling decentralized applications via smart contracts. Understanding gas, accounts, and execution models is crucial for developers and users alike.
Keywords: Ethereum, smart contracts, gas, transactions, message calls, world state, account models, EVM.
### Key Adjustments:
1. **Structure**: Organized with clear headings and tables for readability.
2. **SEO**: Integrated keywords naturally (e.g., "smart contracts," "gas," "EVM").
3. **FAQs**: Added 4 Q&A pairs to address common queries.
4. **Anchors**: Included 2 OKX links with engaging CTAs.
5. **Tone**: Balanced technical depth with accessible language.