Introduction to Ethereum Virtual Machine EVM, detailed explanation of smart contracts

Ethereum as an example: What is a smart contract? How to deploy, call and execute smart contracts? How does a smart contract work? Where do smart contracts exist? How to distinguish whether a smart contract is being called? What is the relationship between the world state database, EVM, and smart contracts?

What is a smart contract

Refers to "a set of commitments specified in digital form, including an agreement on which the parties to a contract can execute those commitments." As early as 1994, American computer scientist Nick Szabo proposed the concept of smart contracts.

A smart contract is a computer program that automatically executes contracts based on blockchain technology. They are a set of rules and logic written in code that run on the blockchain. Smart contracts define actions that should be performed when specific conditions are met, without the intervention of an intermediary party.

Ethereum is one of the most well-known blockchain platforms that supports smart contracts. In Ethereum, smart contracts are written in a programming language called Solidity. These contract codes are deployed on the Ethereum blockchain and verified and executed by nodes throughout the network. The execution results and status of smart contracts are stored on the blockchain, ensuring transparency and non-tamperability.

Smart contracts can perform a variety of tasks, including but not limited to transferring digital currencies, managing digital identities, performing voting and investing, etc. Due to their self-executing and transparent nature, smart contracts provide more security while removing the need to trust third parties.

How to deploy and execute smart contracts

Take Ethereum as an example. Smart contracts can be deployed and called in the Ethereum Virtual Machine EVM.
On Ethereum, contract execution is triggered by sending a transaction to the contract address. The contract address is generated from the address of the contract creator and the nonce of the creator's account, so it uniquely identifies a contract on the blockchain.

The process of executing the contract is as follows:

  1. Creating a contract: First, someone (the creator) deploys the contract by sending a special transaction (called a contract creation transaction). This transaction contains the contract's bytecode (that is, the compiled code of the contract) and other necessary parameters. In this transaction, the creator sends ether (ETH) to the contract address as the initial funding for the contract.

  2. Contract creation transaction: This contract creation transaction is broadcast to the entire Ethereum network and included in a block by miners.

  3. Blockchain confirmation: Miners create new blocks by executing the contract bytecode in the transaction. This is how contracts are created in the Ethereum network.

  4. Triggering the contract: Once the contract is created, anyone can call the contract by sending a transaction to the contract address. This transaction contains the data for calling the contract, that is, the contract function to be executed and its parameters.

  5. Contract execution: When this transaction is packaged by miners and written to the blockchain, each node in the Ethereum network will execute the bytecode of the contract, simulating the contract's Implementation process. This ensures that all nodes achieve the same result.

In this way, the execution of the contract is triggered by sending a transaction to the contract address and including the data to be executed in the transaction. The nodes of the Ethereum network perform corresponding operations according to the logic of the contract based on the data in the transaction.

The principles of smart contracts

Smart contract and blockchain framework diagram
The above figure shows the structure of a smart contract. It generally has two attributes: "value" and "status". If-Then and What-If statements are used in the code to preset the corresponding trigger scenarios and response rules for the contract terms. The smart contract is jointly agreed upon by multiple parties and signed by each, then submitted with the user-initiated transaction (transaction, Txn), propagated through the P2P network, verified by miners, and stored in a specific block of the blockchain. After the user obtains the returned contract address, contract interface and other information, he or she can invoke the contract by initiating a transaction. "Miners" are motivated by the system's preset incentive mechanism and will contribute their own computing power to verify transactions.

After receiving the contract creation or calling transaction, the "miner" creates the contract or executes the contract code in the local sandbox execution environment (such as the Ethereum Virtual Machine). The contract code is based on trusted external data sources [also known as Oracle Machines]. ] and world state check information to automatically determine whether the current scene meets the contract trigger conditions to strictly execute the response rules and update the world state. After the transaction is verified to be valid, it is packaged into a new data block. After the new block is certified by the consensus algorithm, it is linked to the main blockchain chain, and all updates take effect.

Ethereum Virtual Machine EVM

EVM is a computing engine that facilitates the deployment and operation of smart contracts. Without the EVM, it would be impossible to execute software programs on the Ethereum protocol. Therefore, the EVM is a key part of Ethereum's core architecture.

virtual machine

A virtual machine is a program that simulates the behavior of a physical computer. A virtual machine has its storage and processing units and runs as a process on your computer. It's a bit like one computer running on top of another.

A virtual machine is similar to regular Windows or MacOS software. The difference here is that virtual machines are designed to perform more advanced functions. Additionally, unlike a regular operating system, a virtual machine does not have access to other parts of the computer, such as storage or bandwidth.

If you've ever played Android games on your PC using an emulator, then virtual machines may be familiar. While emulators and virtual machines are very different, they both come close to the hardware's ability to execute code in a "sandbox" environment.

Turing completeness

British mathematician Alan Turing invented the first Turing machine, a forerunner of today's computers. Given enough time and resources, a Turing machine can handle any computation, no matter how complex.

Now, let's combine these ideas and define the Ethereum Virtual Machine:

The Ethereum Virtual Machine (EVM) is a giant virtual machine that allows code to be deployed and executed. You simply install the necessary client software to access the EVM and use it to execute programs on Ethereum. Essentially, the EVM acts as a "world computer", executing software operations in a decentralized environment.

Since there is no centralized control, EVM is maintained by several individuals/companies who provide computing power to the system in exchange for rewards. Therefore, it helps in creating censorship-resistant applications that cannot be shut down unilaterally by any party.

EVM is Turing complete as it can be used to perform calculations of various complexity. This is the difference between Ethereum and Bitcoin, because Bitcoin is Turing incomplete, limiting its functionality.

Bitcoin's primary function is a "distributed ledger," which specifies the rules for the transfer of value. In addition to handling value transfers, Ethereum (via the EVM) also supports the deployment of smart contracts. Therefore, Ethereum is described as a "distributed state machine".

"State" refers to information about a system at any point in time. In Ethereum, state refers to the addresses, account balances, and smart contract code that exist at a specific moment. Each transaction causes a change in the state of Ethereum (a state transition), which is reflected throughout the network.

Where do smart contracts exist?

In Ethereum, the code of a contract is not always stored in the Ethereum Virtual Machine (EVM). Instead, the code of the contract is written to the blockchain when the contract is created and stored in the contract account on the blockchain.

When you deploy a contract, a special transaction is created that contains the contract's bytecode (i.e., the binary representation of the contract). This transaction triggers a block on the blockchain, which contains the creation of the contract and stores the contract's bytecode in the contract account.

The contract address is calculated from the contract creator's address and the nonce of the creator's account. This ensures that the contract address is unique within the Ethereum network.

When you send a transaction to a contract address, the Ethereum node checks whether the address matches the contract address. If there is a match, the Ethereum node will execute the contract code stored in the contract account and provide transaction data for contract execution.

Contract account

In Ethereum, contract accounts are stored differently from ordinary accounts. Contract accounts are not associated with private keys and addresses like ordinary accounts, but are associated with contract code and storage space.

The storage location of the contract account is in the world state database of Ethereum. Ethereum uses a database-like structure to store the status of the entire network, including account balances, contract codes, contract storage data, etc. Each state in this database corresponds to a block, so the entire network state of Ethereum will be updated with the generation of each block.

Specifically, the data of the contract account is stored under the address of the account. The contract address is calculated from the sender address of the transaction that created the contract and the nonce value of the sender account. This address uniquely identifies a contract account and stores the contract’s code and data in Ethereum’s world state database.

It should be noted that although the address of the contract account is calculated from the creator and nonce, the private key of the account does not exist directly. The private key is used to sign transactions and control the account, while the contract account does not have a private key. Its execution is triggered by the transaction and relies on the logic of the contract code.

world state database

In Ethereum, the world state database is a persistent, global state storage system used to track and manage the status information of accounts and contracts on the entire Ethereum blockchain network. Its functions mainly include:

  1. Storage account information: The world state database stores the current balance, contract code, contract storage data and other information of each account.

  2. Provides global status: By recording the status of each account, the world state database provides a global blockchain status that reflects the current situation of each account.

  3. Support smart contracts: Smart contracts are self-executing codes on Ethereum. The status and data of the contract are stored in the world state database. This allows the contract to change state as transactions and blocks are executed.

  4. Storing blockchain history: The world state database also stores the historical state of the blockchain, and each block corresponds to a state. This allows you to review historical status retrospectively and verify the validity of transactions.

Principle:
The principle of the world state database involves the design of Merkle trees and data structures. Each block contains a state root, which is the root node of the Merkle tree and contains the hash values ​​of all account states. When a new block is generated, the world state database is updated, and the new state is calculated and stored to form a new state tree. This ensures the immutability and integrity of the data.

By adopting the Merkle tree structure, Ethereum implements an efficient state change and verification mechanism. When you need to verify the status of an account, you only need to check the corresponding Merkle proof instead of traversing the entire database. This design improves performance and scalability while maintaining global consistency.

In Ethereum, each node has its own local world state database. This is because Ethereum is a distributed blockchain network, and each node needs to independently maintain and update its own state database. Each node receives and verifies transactions through the blockchain network, executes smart contracts, and then updates the local world state.

Although each node has its own local state database, due to the consensus mechanism of the blockchain, eventually each node should reach the same consensus state. This is the core concept of distributed consensus: although each node may start from a different starting point, through the consensus algorithm, eventually they will reach the same state, the same world state. This ensures consistency across the entire network.

How can I tell if I am calling a smart contract?

In Ethereum, transactions contain a field called to, which specifies the recipient address of the transaction. If the to field is null or not set, then the transaction will be considered a contract creation transaction.

When you send a transaction to a contract address, the Ethereum network finds the corresponding contract through this address and executes the function in the contract corresponding to the transaction data. This is achieved through the input data in the transaction (called input data).

The address of the contract is generated by the creator of the contract and the nonce of the creator's account, so it is unique on the blockchain. When you send a transaction to a contract address, the Ethereum network knows that the address is a contract address and therefore executes the contract code corresponding to the transaction data.

In short, Ethereum knows that you are calling a contract rather than a simple transaction, mainly through the to field in the transaction and the input data of the transaction. If the to field specifies a contract address, the Ethereum network will execute the function in the contract corresponding to the input data.

The relationship between world state database, EVM, and smart contracts

In Ethereum, there is a close relationship between the world state database (World State), EVM (Ethereum Virtual Machine) and smart contracts. The following is the relationship between them:

  1. EVM and smart contracts:

    • Execution environment: EVM is the runtime environment of Ethereum and is responsible for executing the bytecode of smart contracts.
    • Smart contract code: Smart contracts are program codes written by developers in languages ​​such as Solidity and Vyper. These codes are compiled into EVM executable bytecode.
  2. EVM and world state database:

    • State maintenance: EVM maintains the state of smart contracts by reading and updating the world state database.
    • State reading and writing: When executing a smart contract, EVM can read the current state in the state world database and update these states based on the execution results of the smart contract.
  3. World state database and smart contracts:

    • State storage: The world state database is the global database of Ethereum, which stores the current status of all accounts (including user accounts and smart contract accounts).
    • Smart contract status: The status of the smart contract, including variable values ​​and other information, is stored in the world state database. Each smart contract has a unique address corresponding to an entry in the world state database.

In summary, smart contracts are executed through EVM and read and write the world state database during the execution process. EVM serves as a virtual machine to execute the bytecode of smart contracts, while the world state database is responsible for storing the current status of smart contracts and accounts. The collaborative work of these three constitutes the execution and state management mechanism of smart contracts on Ethereum.

Guess you like

Origin blog.csdn.net/ydl1128/article/details/134916882