Hyperledger Fabric Basics
Hyperledger Fabric is one of the open-source projects under Hyperledger, the umbrella project hosted by the Linux Foundation. It aims to provide a platform for building modular blockchain solutions.
Overview
1. Common Characteristics of Blockchains
1. Distributed ledger: All nodes jointly maintain fully consistent ledger data. Blocks are linked in order, each one storing the hash of the previous block, which forms a chain data structure and keeps the data tamper-proof.
2. Consensus protocol: Since v1.0, Fabric uses a consensus mechanism made up of transaction endorsement, transaction ordering, and transaction validation. However, the current Kafka orderer cluster only provides crash fault tolerance (CFT), not Byzantine fault tolerance (PBFT).
3. Smart contract: In Fabric, the ledger is accessed and controlled through chaincode, which automatically executes the business logic defined by the participants.
4. Cryptography: Building on PKI, Fabric introduces the MSP module to manage the identities of network members. Members use asymmetric cryptography to authenticate each other and encrypt data.
2. Advantages over Other Blockchain Platforms
- Hyperledger Fabric is a strong implementation of a consortium blockchain and is better suited to enterprise blockchain scenarios. It has complete permission control, so members must pass identity verification before they can join the network. It also balances data sharing with privacy protection, which makes it more secure.
- It uses a modular design, so the consensus mechanism, cryptographic algorithms, and other components are pluggable.
- It uses container technology: every node and every chaincode runs in a Docker container. Containers are isolated from each other but can still communicate.
Basic Concepts
1. Ledger: the blockchain and the state database maintained by a peer.
2. World state: the latest key-value pairs after a series of transactions.
3. Channel: a private subnetwork. The peers in a channel jointly maintain a ledger, which keeps data isolated and confidential. Each channel corresponds to one ledger, maintained by the peers that have joined that channel. A peer can join multiple channels and maintain multiple ledgers.
5. Org: Organization, an entity that manages a set of members. A channel can contain multiple organizations.
6. Chaincode: a program that runs inside a peer and provides business logic interfaces for querying or updating the ledger.
7. Endorse: endorsement, the process in which a peer executes a transaction, signs the result, and returns a response.
8. Ordering Service: the service that orders transactions, packs them into blocks, and broadcasts the blocks to the nodes in the network.
9. PKI: Public Key Infrastructure, a standards-based set of technologies and specifications that uses public-key cryptography to provide a secure foundation for e-commerce.
10. MSP: Membership Service Provider, a membership management service built on PKI. It generates certificates for network members and manages their identities.
Node Architecture
Fabric nodes have different identities and serve different functions. The diagram below shows the nodes in the network.

1. Peers
1. Committing peer: Every peer is a committer. It validates the transactions in the blocks it receives from the ordering service and maintains a copy of the ledger.
2. Endorsing peer: Some peers execute transactions and sign the results to endorse them, acting as endorsing peers. Endorsement is a dynamic role: a peer is an endorsing peer only when an application sends it an endorsement request. The rest of the time it is an ordinary committing peer that only validates transactions and commits them to the ledger.
3. Leader peer: the peer that communicates with the orderer. It fetches the latest blocks from the ordering service and syncs them within its organization.
2. Orderers
- The ordering service receives transactions that carry endorsement signatures, orders them, packages them into blocks, and broadcasts the blocks to peers. This ensures that peers on the same chain receive the same messages in the same logical order.
- The ordering service can process transactions for multiple chains and keeps data isolated across channels, so only peers on the same chain can access that chain's data.
- The ordering service can use either a centralized (Solo) or a distributed (Kafka) protocol. A Kafka cluster provides crash fault tolerance (CFT).
3. CA Nodes
- The CA node is Fabric's Certificate Authority.
- The CA node accepts registration requests from clients and returns an enrollment secret that the user logs in with to obtain an identity certificate.
- Every operation on the blockchain network verifies the user's identity.
4. Client Nodes
- A client node represents the entity operated by an end user. It must connect to a peer or to an orderer to communicate with the blockchain network.
- The client submits transaction proposals to endorsing peers. Once it has collected enough endorsements, it broadcasts the transaction to the ordering service, which orders it and generates a block.
Data Storage
Data storage in a Fabric blockchain consists mainly of one file store (block data) and three databases, structured as shown below.
1. Block Data
- Block data is stored as binary files. Each ledger's data is stored in its own directory on the peer's file system.
- Block storage is handled by the block file manager (blockfileMgr), which decides which file in which directory each block goes into.
- Files created by the block file manager are named with the prefix "blockfile_" followed by a 6-digit suffix, for example blockfile_000000. The default block file size is 64MB. Once the current file exceeds that size, blocks are written to the next file.
2. Block Index
- Fabric provides several kinds of indexes so the required block data can be found quickly. The index database is updated every time a block is committed. The index keys are:
- Block Number
- Block Hash
- Tx ID
- The index value is a File Location Pointer, structured as follows:
- fileSuffixNum: the number of the file that contains the data
- offset: the offset within the file
- bytesLength: the number of bytes the content occupies
3. State Database
- The State Database records the results of transaction execution. The latest state represents the latest values of all keys on the channel, which is why it is also called the "world state".
- Transactions and queries that invoke chaincode are executed against the current state database.
- The state database supports queries on a single key, on multiple keys, and on a key range. With CouchDB, it also supports complex conditional queries.
4. History Data
History Database: records the history of each piece of state data. Each history entry is represented as a 4-tuple:
- namespace: identifies the chaincodeID
- writeKey: the key being written
- blockNo: the number of the block that contains the write
- tranNo: the number of the transaction within that block that performs the write
Transaction Flow
Below is Fabric's standard transaction flow. Every operation that updates ledger data goes through this flow.

1. Send the transaction proposal
The client sends a transaction proposal (Proposal) to the endorsing peers. The proposal contains the parameters the transaction needs.
2. Simulate the transaction proposal
The endorsing peer invokes chaincode to simulate the transaction proposal (Proposal). These executions do not update the ledger.
Each execution produces a set of data read from and written to the state data, called the read-write set (RWsets). The read-write set is the main content recorded in a transaction.
3. Return the proposal response
The endorsing peer endorses the read-write set by signing it (Endorse), generates a proposal response (Proposal response), and returns it to the application.
4. Order the transaction
The application builds a transaction from the proposal responses it has received and sends it to the ordering service. The ordering service packs a batch of transactions into a block and distributes it to the committing peers.
5. Validate and commit the transaction
Each peer validates every transaction in the block, including checking the endorsement policy and checking for version conflicts (to prevent double spending). Transactions that fail validation are marked as invalid (Invalid).
Ledger update: the peer applies the read-write sets to the state database and commits the block to the blockchain.
6. Notify the client of the result
Each committing peer notifies the application whether the transaction succeeded, and the transaction is complete.
大佬,想问问关于Fabric的数据存储格式有详细的文档么?
文档可能没有,对于 区块或状态数据库的结构 ,可以直接在fabric源码中打印出来,或者在app层调用查询接口获得。
我之前在 balance-transfer应用中查询过相关结构,可以参考https://blog.csdn.net/zhayujie5200/article/details/80456301
好的,多谢。
这个是你要的吗?https://haojunsheng.github.io/2020/01/fabric-data-strcture/
很赞
多谢