"Blockchain Principles and Technology" study notes (5) - Ethereum transactions, consensus mechanisms and data structures

"Blockchain Principles and Technology" Study Notes Part Five

5. Ethereum transactions

In Ethereum, transactions carry functions such as account transfers, contract creation, and calling contracts.

5.1 Transaction Content

The data in the transaction is roughly divided into three parts: the basic transaction, the execution parameters of the transaction, and the signature of the transaction.

Basic transaction content

  • From: The address of the sender of the transaction, which is actually calculated from the <r, s, v> in the signature.
  • To: The address of the transaction recipient. It is set to 0x0000 when creating a contract, and it is the address of the contract when calling the contract.
  • Value: The amount of the transaction. 1Ether = 10^18 Wei.

Transaction execution parameters

  • Input Data: The data attached to the transaction, passing the code/constructor for creating the contract, calling the function and parameters of the contract.
  • Nonce: The Nonce of the transaction sender.
  • MaxFeePerGas: the maximum price the sender is willing to pay for the transaction
  • MaxPriorityFeePerGas: The maximum tip paid to miners
  • gasLimit: The maximum gas allowed to be consumed by the transaction, which solves the problem that the smart contract cannot be stopped.

transaction signature

  • hash: the hash value generated by the above fields
  • r, s, v: The hash of the transaction is digitally signed by the sender's private key to confirm the legitimacy of the transfer.

5.2 Transaction Fees

The transaction fee in Ethereum is calculated by the Gas mechanism, which mainly includes the following concepts:

  • Gas: the basic unit for computing resource consumption
  • GasLimit: The maximum Gas value allowed to be consumed
  • GasUsed: Gas value consumed after execution
  • GasPrice: The price of Ethereum per Gas

The transaction Gas is obtained by accumulating the basic transaction Gas plus the Gas value consumed during EVM operation. When GasUsed exceeds GasLimit, the transaction execution fails.
The GasUsed*GasPrice of the transaction is the handling fee paid by the user for the transaction.

5.3 Transaction cycle

insert image description here

1. initiate

  • User input from, to, value, data, gasPrice, etc.
  • User confirmation is sent to the Ethereum node
  • The Ethereum wallet software supplements the gasLimit and nonce for the user, uses the private key to obtain r, s, v, and finally serializes the transaction and sends it to the network.

2. broadcast

  • After the node receives/initiates the transaction, it verifies the transaction
  • The node verifies that it is a legal transaction and adds the transaction to the node's transaction pool
  • Nodes continue to broadcast transactions to adjacent nodes according to the P2P network broadcast strategy

3. Packaging and Execution

  • The full node packages the transaction and executes the transaction in order
  • After all the transactions that need to be packaged are executed, the information of the transaction, status, and receipt are packaged into the block.
  • After the accounting node obtains the legal block, it broadcasts to the adjacent nodes

4. Verification and execution

  • After receiving the broadcast block, the nodes that have not obtained the accounting right will verify the validity of the block and execute the transaction.

5.4 Execution Types of Transactions

According to the value of to, the execution of the transaction is divided into the following three types:

1. Create a contract transaction
in which to is empty. The transaction generates a contract address based on the from and nonce values, and executes the smart contract code in data. The EVM will store the code in the contract address.

2. Calling the contract transaction
to is the transaction of the contract account. The EVM gets the code in the to address and executes the code in the data.
Essentially. A call to a contract is a modification of the contract state.

3. Ordinary transfer transaction
to is an account controlled by a person (external account), and the transaction directly transfers Ethereum from from to to

6. The consensus mechanism of Ethereum

6.1 Solve Ethereum Fork: Ghost Protocol

Uncle block
refers to a block that is not in the main chain but is recorded by the main chain and meets the difficulty. When miners pack blocks, they can choose legal 0-2 uncle blocks independently.
It is stipulated in Ethereum that those with a common ancestor within 7 generations can be considered as uncle blocks.

Acceptance and Reward Rules for Uncle Blocks

  • For the blocks on the main chain, each block can accept up to 2 uncle blocks, and each time an uncle block is accepted, the main chain block reward will increase by 1/32.
  • The accepted uncle block should be the direct child block of the ancestor on the main chain, and the uncle block cannot be accepted repeatedly.
  • For the accepted uncle block, the block reward is 1-(accepted block height-uncle block height)/8
  • Transactions in uncle blocks do not need to be executed.

insert image description here

Uncle blocks can shrink and unify the main chain of the entire blockchain as much as possible, while maintaining the enthusiasm of miners.

6.2 New consensus mechanism: PoS

Proof-of-Stake: The number of coins you hold and the time you hold them determine the probability of you obtaining the right to bookkeeping this time. The more you hold, the greater the probability of getting bookkeeping.

Compared with PoW, it shortens the consensus time and saves energy, but it is easy to fork and centralize.

7. Ethereum mining difficulty adjustment

The formula for mining difficulty is:
H = 0 , D ( H ) = 0 D ( H ) = max ( D 0 , P ( H ) H d + x × ζ 2 ) + ϵ H=0,D(H)= 0 \\ D(H)=max(D_0,P(H)_{H_d}+x × \zeta_2)+\epsilonH=0,D(H)=0D(H)=max(D0,P(H)Hd+x×g2)+ϵ
whereP ( H ) H d P(H)_{H_d}P(H)Hdis the difficulty of the parent block, x × ζ 2 x×\zeta_2x×g2Used to adaptively adjust the block difficulty, ϵ \epsilonϵ represents the difficulty bomb set

The basic part has a lower bound, the minimum value is D 0 = 131072 D_0=131072D0=131072

7.1 Adaptive Difficulty Adjustment

x = ⌊ P ( H ) H d 2048 ⌋ ,   ζ 2 = m a x ( y − ⌊ H s − P ( H ) H s 9 ⌋ , − 99 ) x= \left\lfloor \frac{P(H)_{H_d}}{2048} \right\rfloor, \ \zeta_2=max \left( y-\left\lfloor \frac{H_s-P(H)_{H_s}}{9} \right\rfloor, -99 \right) x=2048P(H)Hd, g2=max(y9HsP(H)Hs,99 )
x is the adjusted unit,ζ 2 \zeta_2g2is the adjusted coefficient
If the parent block contains uncle, y is 2, otherwise it is 1
H s H_sHsis the timestamp of this block, P ( H ) H s P(H)_{H_s}P(H)Hsis the timestamp of the parent block, and stipulates that H s > P ( H ) H s H_s > P(H)_{H_s}Hs>P(H)Hs

7.2 Difficulty Bomb

ϵ = ⌊ 2 ⌊ H i ′ / 100 , 000 ⌋ − 2 ⌋ ,   H i ′ = m a x ( H i − 30 , 000 , 000 ) \epsilon = \left\lfloor 2^{\lfloor H'_i / 100,000 \rfloor -2} \right\rfloor, \ H'_i=max(H_i-30,000,000) ϵ=2Hi/100,0002, Hi=max(Hi30,000,000 )
ϵ \epsilonϵ is an exponential function of 2. Doubles every 100,000 blocks

The purpose of setting up the difficulty bomb is to reduce the risk of fork when migrating to the PoS protocol, and to guide miners to have the willingness to migrate to PoS.

8. Data structure and storage

8.1 Blocks and Uncles

The block header of an Ethereum block includes:

  • The state root that records the state of Ethereum
  • Hash values ​​of transaction list, receipt list, parent block and uncle block
  • Block number, difficulty, miner address, timestamp, Nonce value
  • Gas upper limit, sum of transaction gas, proof of work summary
  • variable length field

The contents of the Ethereum block body:

  • TX list consisting of transactions
  • List of receipts with trade execution information
  • A list of uncle blocks used to improve the Ethereum consensus process

insert image description here

The state of the world
The summary of the state of all accounts in Ethereum, that is, the state root of the block header, is realized through a special tree-like hash data structure.

Receipt corresponds to the data structure of the transaction, representing information such as
some intermediate state writing of transaction execution and transaction execution results .
The contents of the receipt include:

  • Execution logs output by Ethereum's smart contracts to the EVM
  • Gas for smart contract execution
  • The state root of Ethereum after a single transaction is executed
  • If the transaction creates a contract, the address of the newly created contract is written in the receipt

8.2 Merkle Patricia Trie

Due to the huge number of potential accounts in Ethereum, Merkle Tree alone cannot store data, and the modification cost is high, so Ethereum uses a hexadecimal compressed prefix tree as an index from address to account, and then uses Merkle Tree to link each layer of nodes Combine and calculate the hash value of the nodes, and finally get the root hash value.

Compressed prefix tree
Ordinary dictionary tree (Trie) uses each letter as a node of the tree, which causes the height of the tree to be too high and wastes storage space; compressed prefix tree (Patricia Trie) forms the common prefix part into a subtree, step by step The downward division improves the search efficiency.

insert image description here

MPT
In MPT, "word" corresponds to the hash value of the account, and "pointer" corresponds to the hash pointer. The hash value of the leaf node record (prefix + account hash value), and the hash value of the parent node is the hash value of (prefix + child branch).

The following figure is an example:
H1=H(""+ hash value of account 1), H3=H(d+ hash value of account 3)
H5=H(bc+H1+H2+""+""+...+" ")
H6=H(b+H4+...+""+H3+"+"")
H_root=H(""+...+""+H5+...+""+H6+"")

insert image description here

State tree
The state tree of Ethereum records the state of each account. When the state changes, it will only affect the state of the corresponding branch, and the block stores the state root of the corresponding state tree.

Contract storage tree
The contract account storage tree also uses MPT to record the mapping from storage address to storage value, and the hash value of the mapping table is called storage root.
Since a unit in the contract storage space is exactly 256 bits, it can be directly used as a leaf node

Transaction Tree and Receipt Tree
These two trees construct the MPT through the serial number of the transaction/receipt in the block, and the receipt and transaction information correspond one-to-one.

8.3 Bloom Filter

Bloom filters can retrieve whether a value is in a set, and Ethereum is used to index and lookup the log of receipts.
It is highly efficient when a certain error rate is tolerated.

Principle: Use multiple hash functions to map key values ​​into a bitmap. For a key value, if there is no mark bit in the bitmap under the same hash function mapping, the key value must not be in the set.

insert image description here

Guess you like

Origin blog.csdn.net/weixin_60482947/article/details/129326057
Recommended