1. The introduction of the Fabric

Hyperledger Fabric, also known as Hyperledger, was initiated by IBM and later became one of the blockchain projects in The Linux Foundation Hyperledger.

Fabric is a platform that provides a distributed ledger solution, and the underlying ledger data store uses blockchain. Blockchain platforms can generally be divided into public chains, federated chains and private chains. Public chains are typically represented by public blockchain networks such as Bitcoin, in which anyone can join. An alliance chain has an access mechanism and cannot join the network at will. A typical example of an alliance chain is Fabric.

Fabric does not need to issue coins to incentivise participants, nor does it need to mine to prevent evil, so Fabric has better performance. In a Fabric network, there are many different types of nodes to form the network. Peer nodes, which host ledgers and smart contracts, are the foundation of the entire blockchain network. In this paper, the structure and operation mode of Peer will be analyzed in detail.

In this article, it is assumed that the reader already understands blockchain, smart contracts, etc.

This article is based on Fabric1.4 LTS.

2. Network structure

A blockchain network is a distributed network, and so is a Fabric. As a Fabric is an alliance chain, it requires an access mechanism, so the network structure is much more complex. Here is a simplified Fabric network:

The meanings of each element are as follows:

  • A: Indicates the client node, that is, the user’s application

  • CA: indicates the CA certificate node

  • P: indicates the peer node

  • O: indicates the Orderer node

  • L: Accounts

  • C: indicates a Channel

  • CC: indicates Channel configuration

  • R: indicates the organization

  • S: Indicates a smart contract

  • NC: indicates the Fabric network configuration

For a Fabric network, external users access the network through client applications (A1, A2, or A3 in the figure). The client applications authenticate themselves with CA certificates so that they can access the authorized parts of the Fabric network.

In the above network, there are four organizations, R1, R2, R3, and R4. Where R4 is the creator of the entire Fabric network, which is configured according to NC4.

In a Fabric network, different organizations can form federations. Data between federations is isolated through channels. Data in a Channel can only be accessed by organizations in the federation, and each new Channel can be considered a new chain. Unlike other blockchain networks, where there is usually only one chain, Fabric can quickly build a new blockchain across the network through channels.

Above R1 and R2 form an alliance and trade on C1. R2 also formed another alliance with R3, trading on C2. R1 and R2 are invisible to R3 when they trade on C1, and R2 and R3 are invisible to R1 when they trade on C2. The Channel mechanism provides good privacy protection.

The Orderer node is common to the entire Fabric and is used to sort and package all transactions. For example, the O4 node in the network above. The Orderer node is not covered in detail in this article, but can be thought of as a mining process in the Bitcoin network.

Peer nodes represent nodes in the network, and usually a Peer represents an organization. Peer is the foundation of the entire blockchain network, and the carrier of smart contracts and ledbooks. Peer is also the focus of this paper.

A Peer node can host multiple sets of books and smart contracts. For example, P2 maintains both the books and smart contracts of C1 and C2.

3. Transaction process

To better understand the role of Peer nodes, take a look at the overall transaction flow of the Fabric. The overall transaction flow chart is as follows:

Peer nodes can be divided into endorsement nodes and billing nodes according to their functions.

The client will submit the transaction request to the endorsement node, and the endorsement node starts to simulate the execution of the transaction. After the simulation execution, the endorsement node will not update the ledger data, but encrypt and sign the transaction, and then return it to the client.

The client receives the response and submits it to the Orderer node. The Orderer node sorts the transactions, packages them into blocks, and distributes them to the billing node, which verifies the transactions, and then records the transactions in the ledger.

The success of a deal is defined by an endorsement strategy, which is assigned to every smart contract.

4. Peer node

Peer nodes represent various organizations in the alliance chain, and the blockchain network is also composed of Peer nodes, which are also carriers of ledgers and smart contracts.

From the understanding of the above transaction process, we can know that the Peer node is the main participant. If users want to access ledger resources, they must interact with the peer node. In a Peer node, it is possible to maintain multiple ledgers at the same time, which belong to different channels. Each Peer maintains a set of redundant ledgers, thus avoiding a single point of failure.

Peer nodes can be divided into Endorser nodes and Committer nodes according to their different roles in transactions. Endorsement nodes simulate the execution of transactions, and only then can accounting nodes truly store data in the ledger.

books

The ledger can be divided into two parts, one is blockchain and the other is Current State, also known as World State.

Past data can only be appended to the blockchain, and cannot be modified. The blockchain also contains two parts of information, one is the configuration information of the channel, and the other is an unmodifiable, serialized record. Each block records the information of the previous block, which is then linked to a chain, as shown below:

The first block, called the Genesis Block, does not store transaction information. Each block can be divided into block headers, 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 to connect all the blocks together. Block data contains transaction data. Block metadata includes the time of block writing, writer and signature.

The structure of each transaction is as follows. In the Header, the ChainCode name and version information are included. Signature is the Signature of the user who initiated the transaction. There are mainly some parameters in the Proposal. In Response is the result of smart contract execution. The Endorsements value is the result returned by the endorsement result.

The current state of the 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 to WorldState will be recorded in the blockchain. Data in WorldState depends on external storage, usually using LevelDB or CouchDB.

Blockchain and WorldState constitute a complete ledger. WorldState guarantees flexible changes of business data, while blockchain guarantees that all modifications are traceable and immutable.

After the transaction is completed, the data has been written to the ledger and needs to be synchronized to other peers. The Fabric uses the Gossip protocol. Gossip is also Channel isolated and only broadcasts and synchronizes ledger data among peers in a Channel.

Intelligent contract

The smart contract needs to be installed on the Peer node and is the only way to access the ledger. Smart contracts can be written in languages such as Go and Java.

Once written, smart contracts need to be packaged into a ChainCode, and each ChainCode can contain multiple smart contracts. ChainCode needs to be installed. ChainCode needs to be installed on the Peer node. Once installed, ChainCode needs to be instantiated on the Channel, specifying an endorsement policy when instantiated.

Once instantiated, smart contracts can be used to interact with ledgers. The flow chart is as follows:

After the user has written and deployed the instantiated smart contract, requests can be submitted to the smart contract through the client application, and the smart contract performs get, PUT, or DELETE on the data in the WorldState. The GET operation directly reads the current status information of the transaction object from WorldState, without writing information to the blockchain. However, put and DELETE operations will not only modify WorldState, but also write a transaction information to the blockchain, and the transaction information cannot be modified.

Information on the blockchain can be accessed through smart contracts or directly through apis in client applications.

An Event is a way for a client application to interact with a Fabric network. A client application can subscribe to an Event. When an Event occurs, the client application receives a message.

There are two types of Event sources, one is the Event emitted by smart contract, the other is the Event triggered by ledger change. Users can obtain transaction information from Event, such as block height and other information.

5. Summary

In this paper, the overall network architecture of Fabric is first introduced, the role of peer nodes in transactions is discussed through the analysis of Fabric transaction flow, and then the ledger and smart contract maintained by peer nodes are analyzed in detail. The maintenance ledger of peer node and the process of executing smart contract of peer node are analyzed.

The text/Rayjun

REF

[1] hyperledger – fabric. Readthedocs. IO/zh_CN/relea…

[2] developer.ibm.com/zh/technolo…

[3] en.wikipedia.org/wiki/Gossip…