Fabric is a platform for providing distributed ledger solutions, and the underlying ledger data storage uses blockchain. Blockchain platforms can usually be divided into public chains, alliance chains and private chains. The typical representative of the public chain is an open blockchain network such as Bitcoin, and anyone can join this network. Alliance chain has access mechanism, so it can't access the network at will. A typical example of alliance chain is fabric.
Fabric performs better because it doesn't need to issue coins to motivate participants, nor does it need to dig mines to stop people from doing evil. In the optical fiber network, there are also many different types of nodes to form the network. Among them, Peer node carries books and smart contracts, which is the foundation of the whole blockchain network. This paper will analyze the structure and operation mode of Peer in detail.
In this article, it is assumed that readers have understood the concepts of blockchain and smart contract.
This paper is based on fabric 1.4 liter. ..
Blockchain network is a distributed network, and so is Fabric. Because Fabric is an alliance chain and needs access mechanism, the network structure will be much more complicated. This is a simplified structural network:
The meaning of each element is as follows:
For optical fiber networks, external users need to access the network through the client application, that is, A 1, A2 or A3 in the figure, and the client application needs to identify itself through the CA certificate to access the authorized part of the optical fiber network.
In the above network, * * * has four organizations, R 1, R2, R3 and R4. R4 is the creator of the whole optical fiber network, which is configured according to NC4.
In Fabric network, different organizations can form alliances, and the data between different alliances are isolated through channels. The data in the channel can only be accessed by organizations in the alliance, and each new channel can be regarded as a new chain. Unlike other blockchain networks, which usually have only one chain, Fabric can quickly build a new blockchain in the network through Channel.
The above R 1 is allied with R2 and traded on C 1. R2 also forms an alliance with R3 to trade in C2. R 1 and R2 are invisible to R3 when trading on C 1, while R2 and R3 are invisible to R 1 when trading on C2. The channel mechanism provides a good privacy protection capability.
Subscriber nodes are owned by the entire optical fiber network and are used to classify and package all transactions. Such as that O4 node in the above-mentioned network. This paper will not describe the Orderer node in detail, and this function can be understood as the mining process in the bitcoin network.
Peer nodes represent nodes in the network. Usually, a peer represents an organization. Peer is the foundation of the whole blockchain network and the carrier of smart contracts and books. Peer is also the focus of this article.
A peer node can carry multiple sets of account books and smart contracts, such as P2 node, which not only maintains the account books and smart contracts of C 1, but also maintains the account books and smart contracts of C2.
In order to better understand the role of peer nodes, we must first understand the overall transaction process of Fabric. The overall transaction flow chart is as follows:
Peer nodes can be divided into endorsement nodes and bookkeeping nodes according to their functions.
The client will submit a transaction request to the endorsement node, and the endorsement node will begin to simulate the execution of the transaction. After the simulation, the endorsement node will not update the account book data, but encrypt and sign the transaction and return it to the client.
After receiving this response, the client will submit the response to the Orderer node, which will sort these transactions, package them into blocks, and then distribute them to the bookkeeping node, which will verify the transactions and record them in the ledger.
The success of a transaction is specified according to the endorsement strategy, and each smart contract will specify an endorsement strategy.
Peer nodes represent all organizations in the alliance chain, and the blockchain network is also composed of peer nodes, which is also the carrier of account books and smart contracts.
Through the understanding of the above transaction process, we can know that peer nodes are the main participants. If users want to access ledger resources, they must interact with peer nodes. In a peer-to-peer node, multiple account books can be maintained at the same time, which belong to different channels. Each peer node will maintain a set of redundant account books to avoid a single point of failure.
Peer nodes can be divided into endorsers and principals according to their different roles in the transaction. Endorsers will simulate the transaction and principals will actually store the data in the account book.
The general ledger can be divided into two parts, one is the blockchain, and the other is the current state, also known as the world state.
Blockchain can only be added, and past data cannot be modified. The chain also contains two parts of information, one is the configuration information of the channel, and the other is the unmodified serialized record. Each block records the information of the previous block, and then links them together, as shown in the following figure:
The first block, called the genesis block, does not store any transaction information. Each block can be divided into block header, block data and block metadata. The block header stores the block number of the current block, the hash value of the current block and the hash value of the previous block, so that all blocks can be connected. Block data contains transaction data. Block metadata includes the time when the block was written, the writer and the signature.
The structure of each transaction is as follows. In the title, the name and version information of the chain code are included. Signature is the signature of the user who initiated the transaction. The proposal mainly has some parameters. Response is the result of intelligent contract execution. Endorsement is the result returned by the endorsement result.
The current status of the general ledger is maintained in WorldState, and the data is stored in the form of Key-Value, which can be quickly queried and modified. Every modification of WorldState will be recorded in the blockchain. The data in WorldState needs to rely on external storage, usually using LevelDB or CouchDB.
Blockchain and WorldState constitute a complete account book. The state of the world ensures flexible changes in business data, while the blockchain ensures that all modifications are traceable and cannot be tampered with.
After the transaction is completed and the data is written into the account book, it is necessary to synchronize these data to other peers. Gossip protocol is used in Fabric. Gossip is also channel-isolated, and only broadcasts and synchronizes the ledger data in peers within the channel.
Smart contracts need to be installed on peer-to-peer nodes, and smart contracts are the only way to access books. Smart contracts can be written in Go, Java and other languages.
After the smart contract is written, it needs to be packaged into chain codes, and each chain code can contain multiple smart contracts. ChainCode needs to be installed, and ChainCode needs to be installed on the peer node. After installation, ChainCode needs to be instantiated on the channel, and endorsement strategy needs to be specified when instantiating.
Smart contracts can be used to interact with account books after instantiation. The flow chart is as follows:
After the user writes and deploys the instantiated smart contract, he can submit a request to the smart contract through the client application, and the smart contract will acquire, place or delete the data in WorldState. Among them, the get operation directly reads the current state information of the transaction object from WorldState, and does not write information on the blockchain. However, put and delete operations will not only modify WorldState, but also write a transaction information on the blockchain, and the transaction information cannot be modified.
The information on the blockchain can be accessed through smart contracts or directly through the API in the client application.
Events are a way for client applications to interact with structured networks. The client application can subscribe to this event, and when the event occurs, the client application will receive a message.
There are two kinds of event sources, one is the event issued by smart contract, and the other is the event triggered by account book change. Users can get transaction information from the event, such as the height of the block.
This paper first introduces the overall network architecture of Fabric, and discusses the role of peer nodes in transaction processing by analyzing the transaction processing process of Fabric. Then, the account books and smart contracts maintained by peer nodes are analyzed in detail, and the process of maintaining account books and executing smart contracts by peer nodes is analyzed.
Text/Lei Jun
[1]/zh/technologies/ blockchain/series/OS-Academy-Hyperledger-Fabric/
[3]https://en.wikipedia.org/wiki/Gossip_protocol