Ethereum (iii)

Source: Internet
Author: User

Overview

Ethereum virtual Machine (EVM) is the operating environment of the smart contract in Ethereum. Not only is it encapsulated in a sandbox, it is actually completely isolated, meaning that code running within EVM does not have access to the network, file system, or other processes. Even smart contracts have limited contact with other smart contracts.

Account

There are two types of accounts in Ethereum, which share the same address space. External accounts, which are controlled by the public-private key pair (human). Contract account, which is controlled by the code stored in the account.

The address of the external account is determined by the public key, and the address of the contract account is determined when the contract is created (this address is calculated from the address of the contract creator and the number of transactions issued at that address, and the number of transactions issued by the address is also referred to as "nonce")

The contract account stores the code, and the external account does not, except that the two types of accounts are the same for EVM.

Each account has a key-value form of persistent storage. Where the length of key and value is 256bit, the name is storage.

In addition, each account has an ethereum balance (in "Wei"), which can be changed by sending it a transaction with an Ethernet currency.

Transaction

A transaction is a message that is sent from one account to another (probably the same account or 0 account, see below). Transactions can contain binary data (payload) and etheric currency.

If the target account contains code, the code executes and payload is the input data.

If the target account is a 0 account (the account address is 0), the transaction will create a new contract. As mentioned above, this contract address is not a 0 address, but is calculated by the address of the contract creator and the number of transactions (known as Nonce) issued by the address. The payload that created the contract transaction is executed as an EVM bytecode. The executed output is permanently stored as the contract code. This means that in order to create a contract, you do not need to send a real contract code to the contract, but rather send the code that returns the real code.

Gas

Each transaction on the Ethereum will be charged a certain amount of gas,gas to limit the amount of work required to execute the exchange and to pay for the execution. When EVM executes a trade, gas is gradually consumed by specific rules.

Gas price, the ether currency, is set by the creator of the transaction, and the transaction fee required to send the account is paid in advance = gas prices * Gas amount. If there is gas remaining at the end of execution, the gas will be returned to the sending account.

No matter where it is executed, once gas is exhausted (such as a negative value), a Out-of-gas exception is triggered. All state modifications made by the current call frame will be rolled back.

Storage, main memory and stacks

Each account has a piece of persistent memory area called storage. The length of the form Key-value,key and value is 256 bits. In the contract, the storage of the account cannot be traversed. Compared to the other two, the stored read operation is relatively expensive and modifies storage more. A contract can only read and write to its own storage.

The second memory area is called main storage. Each time the contract executes a message, there is a new, erased main memory. Main memory can be addressed in byte granularity, but the read-write granularity is 32 bytes (256 bits). The overhead of manipulating main memory becomes larger as it grows (the square level).

EVM is not a register-based, but a stack-based virtual machine. So all calculations are performed in a region called a stack. The stack has a maximum of 1024 elements, 256 bits per element. Access to the stack is limited to its top, by allowing one of the top 16 elements to be copied to the top of the stack, or the top element of the swap stack and one of the 16 elements below. All other operations can only take the top two (or one, or more, depending on the specific action) element and press the result on top of the stack. Of course, the elements on the stack can be placed in storage or main memory. However, it is not possible to access only the element of the specified depth on the stack, and before that, all elements above the specified depth must be removed from the stack.

Instruction Set

EVM's instruction set is deliberately kept to a minimum size to avoid possible error implementations that could lead to consensus issues. All instructions are for 256-bit operation of this basic data type. With the usual arithmetic, bit, logic and comparison operations. can also be achieved conditional and unconditional jump. In addition, the contract can access the relevant properties of the current chunk, such as its number and timestamp.

Message invocation

Contracts can be invoked by means of a message to invoke other contracts or to send an etheric currency to a non-contract account. Message invocation and trading are very similar, they all have a source, a target, data payload, ether currency, gas and return data. In fact each transaction can be thought of as a top-level message call, which in turn produces more message calls.

A contract can determine the distribution of the remaining gas. For example, how much gas is used for internal message invocation, or how much gas is expected to be retained. If an Out-of-gas exception (or other exception) occurs when an internal message is called, the contract is notified and an error code is pressed on the stack. This is only the gas exhaustion of the internal message call. In solidity, in this case, the contract initiating the call triggers a manual exception by default. This exception will print out the call stack.

As I said before, the called contract (the same as the contract that initiates the call) will have a new main memory and be able to access the payload of the call. The call payload is stored in a separate area known as Calldata. After the call execution finishes, the returned data is stored in the caller's pre-allocated chunk of memory.

The number of call tiers is limited to 1024, so for more complex operations, we should use loops instead of recursion.

Code calls and libraries

There is a special type of message call, called Callcode. It is almost exactly the same as a message invocation, except that the code that loads the destination address will run in the contract context in which the call originated.

This means that a contract can dynamically load code from another address at run time. Store, both the current address and the balance point to the contract that originated the call, and only the code is fetched from the called address.

This allows the solidity to implement "libraries". Reusable library code can be applied to the storage of a contract and can be used to implement complex data structures.

Log

At the block level, data can be stored in a special, indexable structure. This feature is called a log, and solidity uses it to implement events. The log data cannot be accessed after the contract is created, but the data can be accessed efficiently from outside the blockchain. Because part of the log data is stored in the Bron filter (Bloom filter), we can search the log efficiently and securely, so those network nodes (light clients) that do not download the entire blockchain can also find these logs.

Create

Contracts can even create other contracts with a special directive (not a simple call to a 0 address). The call to create a contract differs from a normal message invocation in that the result of the execution of the payload data is treated as code, and the caller/creator gets the address of the new contract on the stack.

Self-destruct

The contract code is removed from the blockchain only if the contract on an address is performing a self-destruct operation. The remaining etheric currency on the contract address is sent to the specified target, and then its storage and code is removed.

Note that even if the code for a contract does not contain self-destruct instructions, you can still perform this operation through code calls (Callcode).

Original: https://solidity.readthedocs.org/en/latest/introduction-to-smart-contracts.html

Translator: @rink1969

Proofreading:@Jan

Ethereum (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.