0Pricing
Web3 & DApp Development Fundamentals · 课时

EVM 与燃料机制

理解以太坊虚拟机(EVM)作为智能合约运行环境的作用,以及用于支付交易费用的燃料概念。

EVM 与燃料机制 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 3 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web3 & DApp Development Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web3 & DApp Development Fundamentals 课程共包含 3 节课。

本课时的部分内容尚未翻译,以英文显示。

The Brain of Ethereum

Ethereum isn't just a ledger; it's a decentralized computer! This "computer" is called the Ethereum Virtual Machine (EVM).

Think of the EVM as a global, single-instance computer that runs all smart contracts and updates the state of the blockchain.

Every node in the Ethereum network runs the EVM, ensuring everyone agrees on the exact state of the network.

A Deterministic State Machine

The EVM is a deterministic state machine. This means that given the same starting state and the same transaction, it will always produce the same output state.

It processes transactions, executes smart contract code, and updates the blockchain's state (account balances, contract storage, etc.) accordingly.

This determinism is crucial for a decentralized network, as it allows all participants to independently verify the blockchain's integrity.

EVM Opcodes: Machine Language

Smart contracts, written in languages like Solidity, are compiled into EVM bytecode. This bytecode consists of low-level instructions called opcodes.

Each opcode performs a specific task, such as adding two numbers, pushing data onto the stack, or storing data in memory.

The EVM executes these opcodes sequentially, processing the logic defined by the smart contract.

Why Do We Need Gas?

Running computations on a global, decentralized computer isn't free. This is where Gas comes in.

Gas is a unit of computational effort required to perform operations on the Ethereum network.

It's used to:

  • Prevent infinite loops in smart contracts.
  • Compensate miners/validators for their computational resources.
  • Prioritize transactions based on the fee offered.

Gas Limit: Your Transaction's Budget

When you send a transaction, you specify a Gas Limit. This is the maximum amount of gas you're willing to spend on that transaction.

It acts as a safety mechanism. If the transaction requires more gas than the limit, it will revert (fail), but you still pay for the gas used up to that point.

A standard Ether transfer typically costs 21,000 gas units. Smart contract interactions can cost much more depending on their complexity.

Gas Price: Cost Per Unit

While Gas Limit is the quantity, Gas Price is the cost per unit of gas. It's typically denominated in Gwei.

1 Gwei = 0.000000001 Ether. So, 1 Ether = 1,000,000,000 Gwei.

You can set a higher Gas Price to incentivize miners/validators to include your transaction in a block sooner, especially during network congestion.

Calculating Transaction Fees

The total transaction fee you pay is calculated as:

Gas Units Used * Gas Price

For example, if a transaction uses 21,000 gas units and the gas price is 50 Gwei:

  • Fee = 21,000 * 50 Gwei = 1,050,000 Gwei
  • Which is 0.00105 Ether

Remember, you only pay for the gas actually used, up to your Gas Limit.

The 'Out of Gas' Error

If your transaction runs out of gas before completing its execution, it will result in an "Out of Gas" error.

When this happens:

  • All state changes made by the transaction are reverted.
  • The transaction fails and is not recorded on the blockchain (except as a failed transaction).
  • Crucially, you still pay the transaction fee for all the gas consumed up to the point of failure.

Gas Refunds: A Small Reward

Ethereum offers gas refunds for certain operations that reduce the blockchain's state size.

For instance, if a smart contract deletes data from storage, a portion of the gas spent on that deletion is refunded.

This incentivizes developers to write efficient contracts that don't unnecessarily bloat the blockchain's state.

Quick Check: EVM & Gas

Test your understanding of the Ethereum Virtual Machine and Gas mechanics.

EVM & Gas: The Essentials

In this lesson, we explored the core mechanics of the Ethereum network:

  • The Ethereum Virtual Machine (EVM) is the global, deterministic computer executing smart contracts.
  • Gas is the unit of computational effort required for transactions.
  • Gas Limit sets the maximum gas a transaction can consume.
  • Gas Price (in Gwei) determines the cost per unit of gas.
  • Total transaction fees are Gas Used * Gas Price.
  • Running "out of gas" reverts the transaction but still consumes the spent gas.

Understanding these concepts is vital for developing efficient and cost-effective DApps!

常见问题解答

「EVM 与燃料机制」课时是免费的吗?

是的 — 「EVM 与燃料机制」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web3 & DApp Development Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Web3 & DApp Development Fundamentals 课程共包含 3 节课。

「EVM 与燃料机制」这节课中我会学到什么?

理解以太坊虚拟机(EVM)作为智能合约运行环境的作用,以及用于支付交易费用的燃料概念。 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Web3 & DApp Development Fundamentals 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 3 节。

「EVM 与燃料机制」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?

能。每节 Web3 & DApp Development Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 以太坊区块链
  2. 什么是智能合约
  3. EVM 与燃料机制
← 返回 Web3 & DApp Development Fundamentals