阅读
编辑
历史
通知
分享
Ethereum Virtual Machine (EVM)
Ethereum Virtual Machine(EVM) is a decentralized computation engine that executes smart contracts on the Ethereum network. EVM ensures that code within smart contracts runs exactly as intended by enforcing rules and maintaining consistency across the network. [1][2]
Overview
A virtual machine (VM), as the name indicates, is related to the virtualization of the actual computer. The EVM blockchain is designed to be the infrastructure for smart contracts based on Ethereum. Initially, it was built to avoid the denial-of-service attacks that pose a threat to the crypto space. [3]
Ethereum Virtual Machine (EVM) was designed by Gavin Wood during his work on Ethereum. Written in C++ and using the LLVM Project compiler, EVM is a special state machine that operates continuously and whose immutable operations determine the state of each block in the Ethereum blockchain. [1]
The Ethereum Virtual Machine (EVM) is a fundamental component of the Ethereum network. EVM is the core feature that makes Ethereum more than a distributed ledger like Bitcoin. Forming the backbone of the Ethereum Blockchain, EVM provides developers with a run-time environment to create smart contracts and DApps (Decentralized Applications) in languages like Solidity, Vyper, Python, and Yul, among others.[1][4]
EVM has access to all nodes in the network, handles smart contracts execution, and handles all transactions on the Ethereum blockchain without any major downtime reported.[1]
Elements of EVM
Opcodes
EVM opcodes assist EVM in executing specific tasks of smart contracts or transactions. Aiming for efficiency, EVM converts its bytecode to opcode, where each opcode is assigned one byte in bytecode. Each opcode operation requires a certain amount of gas to be used by the EVM.[4]
Smart Contracts
Every time a smart contract gets executed, the EVM computes the new state of the Ethereum network, updating the account balances, data storage of smart contracts, and transactions on the account and contract levels. All these functions are what lives inside of the called state. EVM is the environment where these smart contracts could live and interact with each other, where EVM combines the resources of thousands of CPUs connected to the Ethereum network. EVM cannot directly execute Solidity, so the code is compiled to machine instructions called opcodes, and gas is needed to use opcodes.[4][8]
Gas
The main objective of gas is to act as the fee for computing the operations of a smart contract done by each node in the Ethereum network. The computation fees prevent an attacker from deploying many complex contracts that will require long computation times to halt the network. These fees make this type of DDoS (Distributed Denial-of-Service) attack too expensive to be executed.
Gas also serves as compensation for validators responsible for guaranteeing that the information in the transactions is valid. When network activity is high, people will raise their gas limit to get their transactions executed and validated. This way, validators will choose from the mempool the transactions with higher gas limits.[4]
How EVM works
Virtual machines are essentially creating a level of abstraction between the executing code and the executing machine. This layer is needed to improve the portability of software, as well as to make sure applications are separated from each other, and separated from their host.
Smart contract languages like Solidity cannot be executed by the EVM directly. Instead, they are compiled into low-level machine instructions called opcodes. These opcodes allow the EVM to be Turing-complete. Because opcodes are 1 byte, there can only be a maximum of 256 (16²) opcodes.
To efficiently store opcodes, they are encoded to bytecode. Every opcode is allocated a byte. During execution, the bytecode is split up into its bytes (1 byte equals 2 hexadecimal characters).
EVM uses a 256-bit register stack from which the most recent 16 items can be accessed or manipulated at once. In total, the stack can only hold 1024 items. Because of these limitations, complicated opcodes instead use contract memory to retrieve or pass data. However, memory is not persistent. When contract execution finishes, the memory contents will not be saved. Thus, while stack can be compared to function arguments, memory can be compared to declaring variables. In order to store data indefinitely and make it accessible for future contract executions, one can use storage.
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.
At the end of this bytecode, a Swarm hash of a metadata file created by Solidity gets appended. Swarm is a distributed storage platform and content distribution service, or, more simply stated: a decentralized file storage. The metadata file contains various information about the contract, such as the compiler version or the contract’s functions. This is an experimental feature, and not many contracts have publicly uploaded their metadata to the Swarm network.[1][3][5][6]
Use Cases
Smart Contracts
The EVM executes smart contracts, enabling decentralized applications (DApps) to run autonomously without intermediaries.[4][7]
Decentralized Finance (DeFi)
EVM powers DeFi applications, facilitating financial services like lending, borrowing, and trading without traditional banks.[4][7]
Tokenization
Creation and management of digital tokens, including ERC-20 tokens, which are widely used for various blockchain projects.[4]
Cross-Chain Compatibility
EVM-compatible chains enable interoperability between different blockchain networks.[4][7]
DAOs
A decentralized autonomous organization (DAO) governs the EVM. A DAO is a community project without a central authority, giving the members total control of the project. Besides being autonomous, DAOs are transparent. Smart contracts stipulate the guidelines and implement policies based on the coded instructions. Since DAO activities are accessible, verifiable, and open to public audit, the participants can learn how the protocol runs. [7]
Virtual Machines for Other Blockchains
Other chains, aside from Ethereum, also offer their virtual machines. Some examples are the Algorand Virtual Machine (AVM), NEO Virtual Machine (NeoVM), and Rootstock Virtual Machine (RSK), which are built on top of the Bitcoin network and allow developers to write smart contract code for decentralized applications on Ethereum. [4]
Benefits and Drawbacks of EVM
Benefits
Developers can execute code without worrying about its impact on the rest of the network or the possibility of it playing truant with data or personal files hosted on any of the node computers. Additionally, they can run complex smart contracts on different computing environments with distributed consensus. This ensures that the failure of a single node does not have any negative impact on the running of the DApp or smart contract, since the EVM code remains the same across all nodes.
The sanctity of the outcome that EVM makes is well-suited to the sustainable expansion of the DApps and smart contract Ethereum ecosystem. Add to this the library of standard codes available for developers to choose from, an increasing number of EVM-compatible layer-2 blockchains, and a large number of potential EVM use cases, and it is easy to see why the EVM is the preferred platform for Web3 development.[1]
Drawbacks
The most important of downside that needs to be considered is the high transaction fees or gas costs associated with running a smart contract on the Ethereum network. Paid in ETH, these fees vary depending on the complexity of the contract and the network congestion at the time of execution, making it imperative for developers and entrepreneurs to price their services accordingly.
Additionally, since Solidity is the most preferred language for coding on the EVM, it does imply that developers need to have adequate experience with it and possess a modicum of technical expertise to create efficient smart contracts by using it. If developers choose to code using other languages, they need to be careful in resolving any inherent repetitions in the code since the EVM will proceed to compile them anyway. While upgrading smart contracts is possible at a later stage, it comes with security risks associated with creating an intermediary smart contract that references the address of the original smart contract.[1]
Ethereum Virtual Machine (EVM)
参考文献
[1]
Cointelegraph - What is an Ethereum Virtual Machine (EVM) and how does it work?
May 15, 2024
[2]
[3]
Medium - Understanding the Ethereum Virtual Machine: A Comprehensive Guide with Diagrams
May 15, 2024
[4]
[5]
[6]
[7]
Coingecko - What is the Ethereum Virtual Machine (EVM), and How it Works
May 15, 2024
[8]