The content of this article is extracted and summarized from the second chapter of “Detailed Explanation and Actual combat of Ethereum technology”, which is generally able to glimpse the whole picture of Ethereum.

1. Ethereum’s overall architecture

Ethereum’s overall architecture consists of three layers: bottom service, core layer and top application, as shown in the figure:




Ethereum Architecture


Second, the block

A block can be defined as a data structure that records the results of transactions and states that occur over a period of time, and is a consensus on the current state of the ledger.

Block is mainly composed of block header, transaction list and tertiary block header:

Block head

The block header contains: And I have a Prev Hash for the parent block, a Uncles Hash, a stateRoot, a Transaction Root Hash, and a Receipt Root Hash ), time stamp (Times tamp), random number (Nonce), etc

One major change in the block data structure on the Ethereum blockchain compared to Bitcoin is the preservation of three Merkle roots, namely the state tree, transaction tree, and receipt tree.

Transaction list

A trade list is a series of trades selected by miners from a pool of trades in the revenue block.

Uncle block

And isolated blocks that aren’t on the main chain and are picked up by blocks on the main chain via the Uncles field are called uncle blocks.





Ethereum block structure

Third, account

The account is indexed by the address derived from the public key, the last 20 bytes of which are taken

There are two types of accounts in the Ethereum system, external accounts and contract accounts. The external account stores the state of the Ethereum balance, while the contract account has the state of the smart contract and its variables in addition to the balance.

External accounts

An external Account (EOA), controlled by a private key, is an Account that is actually controlled by the user. Store the ethereum balance state

Account of the contract

A contract account is an account that contains a contract code. Contract accounts are controlled not directly by private key files, but by contract codes. The address of the contract account is calculated from the address of the contract’s creator at the time the contract was created and the transactions originating from that address.

In contrast to external accounts, contract accounts have the status of smart contracts and their variables in addition to the balance

Private and public keys

Each external account in Ethereum is defined by a pair of keys, a private key and a public key.

There are three common types of private keys:

1 ) Private key

A Private key is a randomly generated 256-bit binary number. Users can even randomly generate a private key with pen and paper by writing a random 256-bit string containing only “0” or “1”. The 256-bit binary number is the initial state of the private key.

2 )Keystore & Password

In the official Ethereum wallet, the private and public keys will be stored in an encrypted JSON file in the Key Store subdirectory. This JSON file is Keys Tore, so the user needs to back up Keys Tore and the corresponding Password (the Password set when creating the wallet).

3) the Memonic code

Memonic code is proposed by BIP 39 scheme, the purpose is to randomly generate 12 ~ 24 easy to remember words, the word sequence through PBKDF2 and hMAC-SHA512 function to create a random seed, the seed through biP-0032 proposal to generate a deterministic wallet

Data structure and storage

Blocks, transactions and other data are ultimately stored in the Level DB database. The Level DB database is a key-value database. The key is generally related to hash, and the value is the RLP encoding of the stored content.

Data organization form

Ethereum uses MPT trees (Merkle Patricia Trie) as a form of data organization to organize and manage users’ account status, transaction information and other important data. MPT is an encrypted and authenticated data structure that combines the best of Merkle and Trie tree (prefix tree) data types.

Merkle tree

A Merkle tree is a tree data structure that can be binary or multi-binary. It consists of a set of leaves, a set of intermediate nodes, and a root node. The bottommost leaf node contains the underlying data, each intermediate node is a hash of its children, and the root node is a hash of its children, representing the root of the Merkle tree.

Merkle trees are created to allow blocks of data to be transmitted piecemeal; A node can download the block header from one node and the rest of the tree associated with it from another source, and still be sure that all the data is correct.

Merkle trees can be used to store all key-value pairs

Merkle trees have the following properties:

1. Each data set corresponds to a unique legal root hash value.

2. It is easy to update, add, or remove tree nodes and generate new root hash values.

3. There is no way to modify any part of the tree without changing the root hash value, so if the root hash value is included in the signed document or valid block, the tree is guaranteed to be correct.

4. Anyone can provide only one branch to a particular node and prove through cryptography that the node with the corresponding content is indeed in the tree.





Merkle tree

Trie tree

Key represents a path from the root to the corresponding value. Starting from the root node, each character in the key (front to back) represents the child nodes through which the value is located from the root node. Value is stored in leaf nodes and is the end node of each path.

MPT tree

Combined with Merkle and Trie, and made the following improvements:

1. To ensure the encryption security of the tree, each node is referenced by its hash value

2. For non-leaf nodes stored in the Leve IDB database, key represents the SHA3 hash value of the NODE’s RLP code, and value is the RLP code of the node.

3. Many node types are introduced to improve efficiency:

3.1 Empty node: Simply represents empty, which is an empty string in the code

3.2 Leaf node: A list of key-value pairs, where key is a special hexadecimal code and value is an RLP code.

3.3 Extension node: A list of key-value pairs, but the value here is a hash value of other nodes that can be linked to other nodes.

3.4 Branch Node: A list of 17 lengths. The key in MPT is encoded in a special hexadecimal representation, plus the last value. The first 16 elements correspond to the 16 possible hexadecimal characters in the key. If a key-value pair terminates at the branch node, the last element represents a value, that is, the branch node can either be the termination of the search path, It can also be an intermediate node in a path.

4. Special hexadecimal prefix encoding (HP) for encoding keys




State tree root

The three trees

As shown in figure




Ethereum block header structure

1. State of the tree

The state tree contains a key-value mapping, where the key is the account address and the value is the account content, mainly {nonce, balance,codeHash, storageRoot}. Nonce is the ordinal number of account transactions, balance is the account balance, codeHash is the hash value of the code, and storageRoot is the root node of another tree. The state tree represents the entire state after accessing the block.

Ethereum is an account-based blockchain application platform, where the state of an account is not stored directly in each block, but in the form of “status data” stored in Ethereum nodes.

2. The tree

Each block has a separate transaction tree.

The order of transactions in a block is largely determined by the “miners”, and this data is unknown until the block is dug up. Miners, however, tend to rank trades by their GasPrice and Nonce. First, the transactions in the transaction list are divided into various sending accounts, and the transactions of each account are sorted according to the nonce of these transactions. After sorting transactions for each account, the transaction with the highest price is selected by comparing the first transaction for each account, using a heap

In the key-value pairs contained in the transaction tree, each key is the number of the transaction and the value is the content of the transaction

3. The receipt

Each block has its own receipt tree, which does not need to be updated and represents the corresponding receipt for each transaction.

The transaction receipt is an RLP-encoded data structure: [medState, Gas_ Used, logbloom,logs]. Medstate is the state of the root after transaction processing. Gas_used is the amount of Gas used after transaction processing; Logs are a list of elements of the table [ADDRESS, [Topicl, topic2…], data], which is called by the opcode LOGO during the execution of the transaction… LOG4 is generated (including primary and subcalls), address is the contract address to generate the log, topicn is up to four 32 byte values, and data is an array of any byte size; Logbloom is a Bloom filter consisting of the address and topic of all logs in a transaction.

The database supports one-to-one Level DB

LeveI DB is a very efficient key-value pair database implemented by Goog. The key values are binary and can currently support billions of data volumes with very high performance.

Ethereum has three Leve IDB databases: BlockDB, StateDB, and ExtrasDB.

BlockDB holds the main content of a block, including the bulk and transaction. StateDB stores account status data; ExtrasDB stores receipt information and other ancillary information.

5. Consensus mechanism

Consensus mechanism is an algorithm for blockchain transactions to reach distributed consensus. Due to the high or low network latency under the point-to-point network, the order of transactions received by each node may be different. Therefore, the blockchain system needs to design a mechanism for the nodes to achieve consensus on the order of transactions occurring in the same time, which is the consensus mechanism.

Pow

PoW is the result of your work to prove that you have done the work

Features of hash functions:

1. Collision-free, that is, there is no case where the input values are different but the hash values are the same after hash transformation.

2. Occult, that is, given a hash value, it is computationally impossible to reverse and derive the input value.

3. There is no better way to get a hash value to fall within a particular range than by brute force.

POW algorithm principle: the node constantly changes the random number to search for the appropriate hash value, when the node first calculates the appropriate hash value, its packaged block, if verified by other consensus nodes, will be added to the blockchain.

Ethash (Ethereum’s special POW algorithm)

In order to solve the mining centralization problem, a PoW algorithm that can resist ASIC and be quickly verified by light client is specially designed

Algorithm flow:

1. For each block, a seed can be computed by scanning the block header, which is only relevant to the current block.

2. Using the seed can generate a 16MB pseudo-random cache, which the light client will store.

3. Regenerate a 1GB data set based on the cache, called DAG. Each element in the dataset depends on only a few elements in the cache, which means that as long as there is a cache, you can quickly calculate the element in the DAG at a given location. Miners store datasets that grow linearly over time.

4. Mining can be summarized as a process in which “miners” randomly select elements from DAG and hash them. DAG can also be understood as a complete search space.

5. The verifier only needs to spend a small amount of memory to store the cache, because the verifier can calculate the desired elements in the DAG based on the cache, and then verify that the hash of these specified elements is less than a certain hash value, that is, verify that the “miner” is working properly.

Ethash algorithm is characterized by the efficiency of mining has nothing to do with CPU, but is positively correlated with memory size and bandwidth, in order to remove the advantages of dedicated hardware and resist ASIC.

POS

PoS, which distributes benefits based on the amount and duration of digital currency currently held by network participants, is a proof of ownership of the currency

Consensus algorithm types:

Chain-based PoS and BFT (Byzantine Fault Tolerant) style PoS.

In the PoS based on chain, the algorithm in each time slot in pseudo random selection from the verifier sets a verifier (for example, Settings for each cycle l0s, each cycle is a time slot), gives the rights of the validation is to create a new block, but the largest number of validation is to ensure that the block to block (pointing to a block is usually the last piece of the longest chain). Thus, over time, most of the blocks converge to a chain.

In BFT style PoS, verifiers are assigned relative rights, giving them the right to propose blocks and vote on the proposed blocks to determine which is the new block, and to select a new block to join the blockchain in each round. In each round, each verifier “votes” for a particular block, and finally all online and honest verifiers “discuss” whether the given block can be added to the blockchain, and their opinions cannot be changed.

Six, trading

Ethereum transactions mainly refer to the signature packet of a message sent by an external account to another account on the blockchain, which mainly contains the sender’s signature, the recipient’s address, and the amount of Ethereum transferred by the sender to the recipient.

Transactions are an important part of ethereum’s overall architecture. They connect ethereum accounts and act as a transfer of value.

Trading content

From: address of the transaction sender, mandatory;

To: Address of the recipient of the transaction. If empty, this is a transaction to create a smart contract.

Value: The number of Ether that the sender wants to transfer to the receiver;

Data (also written input) : Existing data field, if present, indicating that the transaction is a create or invoke smart contract transaction;

Gas Limit (also called Gas, StartGas) : Indicates the maximum amount of Gas allowed to be consumed for this transaction;

GasPrice: represents the Gas price that the sender is willing to pay to the miner;

Nonce: a flag used to distinguish between different transactions made by the same user;

Hash: The hash value (hash value) generated from the above information as the ID of the transaction;

R, S, v: three parts of the transaction signature, which are generated by the sender’s private key signing the transaction hash.

Transaction costs

In order to prevent users from sending too many meaningless transactions in the block chain public chain and wasting computing resources of miners, the sender of transactions is required to pay a certain price for each transaction, namely transaction cost.

Because there are only transfers in Bitcoin, the computational overhead for each transaction is roughly the same, so the sender of each transaction pays a relatively fixed fee in the form of Bitcoin. However, ethereum introduced smart contracts, and the calculation of transaction consumption related to the creation and invocation of smart contracts is greatly different. Therefore, relatively complex Gas and Gas Price are introduced to Price transaction fees.

1.Gas

Gas is the basic unit used to measure the amount of computing resources consumed by a transaction. The more computationally complex an Ethereum node needs to perform a transaction, the more Gas the transaction consumes.

2.Gas Price

Gas Price is the processing fee (Ether) for a unit of Gas. The miners’ union ranks the transactions received by Gas Price or Gas * Gas Price from largest to smallest in order to determine which transaction will be included in the block first. In order to get miners to accept a deal as soon as possible, the transaction sender can increase the Gas Price of the deal to incentivize miners when there is a spike in transaction volume on the Ethereum public chain

3.Gas Limit

There are two:

For a single transaction, the Gas Limit (sometimes referred to as StartGas) represents the maximum amount of Gas that the transaction sender is willing to pay to execute the transaction, which the sender needs to set when sending the transaction. Protects users from bad codes that cost them too much in transaction fees. GasPrice * Gas Limit indicates the maximum amount a user is willing to pay for a transaction.

For blocks, the Gas Limit is the maximum amount of Gas that can be contained in a single block, and it is up to the miners to determine its size. To prevent miners from consuming so much of their resources that the blocks dug up cannot form the longest trading chain. However, miners cannot arbitrarily change the Gas Limit of a block. According to the Ethereum protocol, the current block’s Gas Limit can only fluctuate up or down 1/1024 based on the previous block’s Gas Limit.


Transaction type

1. Transfer transactions

web3.eth.sendTransaction({

from :”Oxb60e8dd6lc5d32be8058bb8eb970870f07233155″,

to:”Oxd46e8dd67c5d32be8058bb8eb970870f07244567″,

value: 10000000000000000

});

2. Create deals for smart contracts

web3.eth.sendTransaction({

from:”Oxb60e8dd6lc5d32be8058bb8eb970870f07233155″,

data :”contract binary code”

});

3. Execute transactions on smart contracts

web3.eth.sendTransaction({

from:”Oxb60e8dd6lc5d32be8058bb8eb970870f07233155″,

to:”Oxb4259e5d9bc67a0f2ce3ed372ffc5lbe46c33c4d”,

data :”hash of the invoked method signature and encoded parameters”

});

Transaction execution process






















7. Data coding and compression

RLP (Recursive Length Prefix) is an encoding algorithm used to encode arbitrary binary data with nested structure. RLP is the main method of Ethereum data serialization.


conclusion

In addition to EVM and block synchronization protocol, other basic coverage, as for some details of the best through the analysis of ethereum source code to clarify.