Editor's Note: This article comes fromEthereum enthusiasts (ID: Ethfans)Editor's Note: This article comes from
Ethereum enthusiasts (ID: Ethfans)
, Author: Lucas Saldanha, translation & proofreading: An Zi Clint & A Jian, reprinted by Odaily with authorization.
I hope that you in front of the screen can understand what a Merkle tree is and its role in Ethereum after reading it; understand the concepts of "world state" and "account state"; The structure of the block.
(Disclaimer: This article is based on the Yellow Paper of the Byzantine e94ebda version on June 5, 2018)
secondary title
Merkle tree
Before discussing the main data objects of Ethereum, I would like to give you a brief introduction to what a Merkle tree is and the attributes that make it work.
Merkle-Patricia trees have many interesting properties, and if you want to learn more about their use in Ethereum, I recommend reading this article.
In a Merkle tree, leaf nodes hold the hash of block data, while non-leaf nodes hold the hashes of their child nodes.
image description
-Schematic diagram of Merkle tree (including nodes and the relationship between them)-
Any change in the data pointed to by the Merkle tree will cause changes in the node hash. Since the hash value stored in each parent node depends on the data contained in the child node, the change of the data in the child node will cause the change of the parent node hash. And such an impact is a chain reaction, from the leaf node to the root node. Therefore, changes to the data pointed to by the leaf nodes will cause changes in the hash stored by the root node. From the above structural features, we can derive two important attributes:
When judging whether the data pointed to by two Merkle trees is exactly the same, we don't need to compare each leaf node, but only need to compare the hash stored in the root node.
The important thing about the first attribute is that we can use only the hash value of the root node to mark the data pointed to by the entire tree at a certain moment. This means that the block can be marked only by saving the hash value of the root node (without storing all the data in the blockchain), and the data cannot be tampered.
So far we have clarified the role of the root node hash in the Merkle tree, and let's introduce the main objects in Ethereum.
secondary title
The world state is a mapping from addresses (accounts) to account states. Although the state of the world is not stored on the blockchain, in the description of the Yellow Paper, the state of the world is also saved by a tree (this tree is also called a state database or a state tree). The world state can be viewed as a global state that is continuously updated as transactions are executed. Ethereum is like a decentralized computer, and the state of the world is the hard drive of this computer.
image description
-World state tree and account storage-
secondary title
Account Status
There are two types of accounts in Ethereum: Externally Owned Accounts (EOA) and contract accounts. The accounts we use to send and receive Ethereum to each other and deploy smart contracts are EOA accounts, and the accounts automatically generated when deploying smart contracts are contract accounts. Each smart contract has its own unique Ethereum account.
nonce
The account status reflects various information of an Ethereum account. For example, it stores the balance information of the current account's Ethereum, the number of transactions sent by the current account...Each account has an account status.
balance
Let's take a look at what is included in the account status:
storageRoot
The number of transactions sent from this address (if it is currently an EOA account) or contract creation operations generated by this account (don't care what the contract creation operation is for now).
codeHash
The amount of ether (measured in Wei) owned by this account.
The hash value of the root node of the account storage tree (we will introduce what account storage is later).
For contract accounts, it is this account that stores the hash value of the EVM code. For EOA accounts, leave this blank.
The immutability of codeHash makes it impossible to repair and update a smart contract with vulnerabilities if it is deployed. Correspondingly, only one new contract can be deployed (and the vulnerable version will always exist on the blockchain). This is why it is necessary to use Truffle for smart contract development and deployment, and follow the requirements of best practices when programming with Solidity.
-Account status and account storage tree-
trade
secondary title
trade
Transactions drive the transition from the current state to the next state. There are three kinds of transactions in Ethereum:
A transaction that transfers value between EOAs (for example, changing the balance size of the sender and receiver).
Send a message to call a contract's transaction (for example, to set a value in a contract by sending a message call to trigger a setter method).
A transaction for deploying a contract (thus creating a contract account).
nonce
(From a technical point of view, the first two transactions are the same... They are all transactions that change the account status through message calls, except that one is an EOA account and the other is a contract account. Here, the transactions are divided into three types It is for the convenience of readers to understand.)
gasPrice
A transaction consists of the following parts:
gasLimit
The number of transactions issued by this account (proofreading note: it can be roughly understood as "this is the first transaction of this account").
to
The fee (measured in Wei) paid per unit of gas when performing this transaction, performing calculations.
The maximum amount of gas that can be used when executing this transaction.
If this transaction is for sending ether, here is the EOA address to receive ether.
value
If this transaction is used to send a message to a contract (for example, call a method in a smart contract), here is the address of the contract.
If this transaction is used to create a contract, the value here is empty.
If this transaction is sending or receiving ether, here is the amount of tokens in Wei sent to the receiving account.
v, r, s
If this transaction is used to send a message call to a contract, here is the amount of Wei paid to the smart contract that received this message.
dataIf this transaction is used to create a contract, here is the amount of ether in Wei stored in the account when the contract was initialized.
The value used in the cryptographic signature of the transaction, which can be used to determine the sender of the transaction.
init(Only used for value transfer and sending message calls to smart contracts)
The input data attached to the message call (for example, if you want to execute the setter method in the smart contract, the data area should include the identifier of the setter method and the parameter value you want to set).
(only for contract creation)
As you might expect, all transactions in a block are also stored in a Merkle tree. And the hash value of the root node of this tree is saved by the block header! Let's analyze the block structure of Ethereum.
block
secondary title
block
The block header is the part of the blockchain in Ethereum. It saves the hash value of the previous block (also called the parent block), and forms a cryptographically endorsed chain through the connection of block headers.
The block body contains a series of transactions recorded in this block, as well as a list of uncle block (ommer) block headers. If you want to learn more about uncle blocks, I recommend reading this article.
image description
parentHash
-Abstract schematic diagram of Ethereum block-
ommersHash
Let's introduce what parts the block header includes.
beneficiary
The block header hash of the previous block. Each block contains the hash of previous blocks, all the way back to the genesis block on the chain. This is also the structural design of maintaining data that will not be tampered with (any tampering with the previous block will affect the hash value of all subsequent blocks).
stateRoot
The hash value of the uncle block header and part of the block body.
transactionsRoot
The Ethereum account that earns income for mining this block.
receiptsRoot
The hash of the root node of the world state tree (after all transactions have been executed).
logsBloom
The hash value of the root node of the transaction tree. This tree contains all transactions of the block body.
difficulty
Whenever a transaction is executed, Ethereum generates a transaction receipt corresponding to the result. Here is the hash of the root node of the transaction receipt tree.
number
Bloom filter, which is used to determine whether a certain log has been generated by a transaction in a certain block (if you are interested in this aspect, you can refer to this answer on Stack Overflow). This avoids storing log information in chunks (saving a lot of space).
gasLimit
The difficulty value of this block. This is a measure of the difficulty of mining the current block (details and calculations of this concept are not covered here).
gasUsed
The total number of preamble blocks. This indicates the height of the blockchain (i.e. how many blocks are on the blockchain). The number of the genesis block is 0.
timestamp
Every transaction costs gas. The gas limit indicates the total amount of gas that can be used by all transactions recorded in this block. This is a means of limiting the number of transactions within a block.
extraData
The total amount of gas actually consumed by each transaction in the block.
mixHash
Unix timestamp when the block was created. Bear in mind that due to the decentralized nature of the Ethereum network, we cannot trust this value, especially when writing smart contracts that involve time-related business logic.
nonce
A variable-length byte array that can input anything. When miners create blocks, they can add anything in this area.
Oops... My mouth is sour when I talk about it... I suggest you take it easy and absorb it slowly! But I want to emphasize again that reading this article should not aim to remember every noun and its function (you can find these on Google). The original intention of my writing is to introduce all aspects of Ethereum objects in a simple way (at least simpler than the Yellow Paper), so as to help novices understand what those professional terms represent. Just think of this article as "Learning Ethereum Objects the Stupid Way"! 🙂
in conclusion
secondary title
in conclusion
Let's briefly recap what we learned! In general, Ethereum has four prefix trees:
The world state tree includes a mapping from addresses to account states. The hash value of the root node of the world state tree is saved by the block (in the stateRoot field), which indicates the current state when the block is created. There is only one world state tree in the entire network.
The account storage tree stores data information related to a certain smart contract. The hash of the root node of the account storage tree (in the storageRoot field) is saved by the account state. Each account has an account storage tree.
The transaction tree contains all transaction information in a block. The hash of the root node of the transaction tree is saved by the block header (in the transactionsRoot field). Each block has a transaction tree.
The transaction receipt tree contains receipt information for all transactions in a block. Also the block header (in the receiptsRoot area) holds the hash value of the root node of the transaction receipt tree; each block has a corresponding transaction receipt tree.
The objects we are discussing today are:
State of the world: the hard drive of the distributed computer Ethereum. It is a mapping from addresses to account states.
Account Status: Stores the status information of each Ethereum account. The account state also holds the storageRoot of the account state tree, which contains the account's storage data.
Block: Contains a link to the previous block (parentHash) and holds transactions that when executed result in a new state in the system. The block also saves stateRoot, transactionRoot, receiptsRoot, the root node hash of the world state tree, the transaction tree and the corresponding transaction receipt tree.
I want to use a picture to represent the various conceptual information mentioned in the article.
image description
