"Blockchain Principles and Technology" study notes (4) - the basic structure, account model and smart contract of Ethereum

"Blockchain Principles and Technology" Study Notes Part Four

3. Ethereum

1. Introduction to Ethereum

Ethereum has expanded on the basis of Bitcoin's original performance and application scenarios. It is the first blockchain system that supports smart contracts, allowing the application scenarios of blockchain to extend from a single encrypted digital currency transaction to a flexible and diverse one.
custom app design.

1.1 Stages of Ethereum Development

  • Frontier (frontier) stage: only for developer testing
  • Homestead phase: Provides a graphical interface, prompting safety and stability
  • Metripolis (metropolis) stage: optimized in terms of smart contract execution performance, Ethereum economic policy, etc.
  • Serenity stage: The consensus mechanism changes from PoW to PoS, adopting more advanced technologies.

1.2 Comparison between Ethereum and Bitcoin

In terms of technology
, Ethereum provides support for smart contracts, and adopts an account model in transactions, so that the status of the account can be saved in the account in real time. In addition, the gas fee (Gas) setting allows Ethereum to limit the execution of contract instructions, reducing the risk of being attacked.

In terms of performance
, in terms of reward mechanism, Ethereum has added rewards for uncle blocks and reduced the block generation time to about 15s;

In the consensus mechanism, Ethereum adopts the Ethash variant algorithm of PoW, which requires high memory, which limits the advantages of powerful mining machines. Later, Ethereum used PoS, so that currency holders can also have a certain decision-making power.

In terms of community
, the Ethereum community is more active than the Bitcoin community.

2. The basic structure and principles of Ethereum

2.1 Basic concepts

Status: All accounts, balances, smart contract codes, smart contract status, etc. are collectively referred to as status.
Smart contract: the code that acts on the state transition
Ethereum virtual machine: the virtual machine that executes the state transition code
Blockchain data: including the data of the Ethereum data chain, and state data, receipt data, etc.
Node: the node that saves the blockchain data .

2.2 State transition

It is known that the state of block N before execution is S, and the state after execution is S'. S' is obtained from S through the state transition of transactions in N, and the transition process is executed by EVM.

Since the transformation rules are fixed, as long as S is determined, the resulting S' must be determined.

If a fork occurs in the Ethereum block, we can quickly roll back to the state before the fork through the state root of the block header.

2.3 Basic Architecture

  • Each node independently maintains blockchain data
  • When a new transaction is generated, the nodes independently read information from the blockchain data and execute it.
  • EVM writes the execution result back to the blockchain data
  • Nodes reach a consensus through a consensus mechanism to maintain a consistent state of the entire network.

insert image description here

3. Account model and transfer

3.1 Account Model

Ethereum's records of user accounts are determined by all Ethereum nodes and the entire system to determine and record user behavior, which is decentralized.

The public key and private key of the account are calculated by the elliptic curve, and the KECCAK256 hash algorithm is used for the public key, and the last 40 bits are taken as the address of the Ethereum account.

The account structure of Ethereum records the balance of the address (Balance), the smallest unit is Wei, and the counter of the number of transactions initiated by the address (Nonce), which can verify the legality of the transaction and the order in which the account initiates the transaction.

Only when the Nonce of the transaction corresponds to the Nonce of the account, the transaction is legal.

The account model of Ethereum is for participants to have stable identities, so as to support smart contracts, and smart contracts can be used to realize some financial derivatives, which is beneficial for participants to invest.

4. Smart contract

Smart contracts use pre-specified codes to manage and change state variables stored on Ethereum, and rely on the blockchain system to achieve consensus on execution among participants.

4.1 Contract account and data storage

The account representing the contract is called the contract account. In addition to recording the balance of the account and the cumulative number of transactions, the computer code of the contract is saved in the field of "contract machine code" in the form of machine code, and the state (variable) of the contract is saved In a storage mapping table, the hash value of the storage table is stored in the "contract storage" field of the account.

insert image description here

There are two ways to generate the contract address:

  • Calculated by the contract creator's address and Nonce;
  • Calculated by the contract creator address, the specified initialization value and the hash value of the contract code

When a contract creates a contract, the Nonce value of the contract account that created the contract needs to be changed.

4.2 Driving smart contracts

4.2.1 Call contract

  • The recipient of the transaction is the address where the smart contract is located
  • The data field of the transaction specifies the function to call and the parameters of the function. The hash value of the function name is used as the index to call the procedure function, and the serialized parameters are attached after the hash value.

4.2.2 Create a contract

  • Because there is no contract account address, the recipient address in the transaction is 0
  • The code and initialization code of the data storage contract

4.2.3 Downtime problem and Gas
Each operation of the Ethereum smart contract specifies the value of Gas that needs to be consumed, and requires the initiator of the transaction to pay the Gas amount in advance. If the Gas amount is exhausted when running the contract code, Then the program terminates, which solves the problem that the smart contract never stops.

4.2.4 Ethereum Virtual Machine
Ethereum runs smart contracts using a unified virtual machine EVM, which is a 256-bit stack virtual machine, and all instructions operate on the top of the stack.

Guess you like

Origin blog.csdn.net/weixin_60482947/article/details/129325978