1. Fabric application development model
Fabric development includes client application and smart contract (chaincode) development.
After the chaincode is developed, it is deployed to the Peer node of the blockchain network. The ledger is operated through the chaincode. When calling a transaction, it is actually calling a function method in the chaincode, which implements business logic and performs get, put, and delete operations on the ledger.
The client application provides a user interface, calls the fabric SDK, and completes blockchain operations and transactions.
This training focuses on the development of client applications
2. Introduction to Fabric SDK interface
Fabric provides SDK for application calls. The SDK includes interfaces such as transaction processing, member management, blockchain query and event processing.
The Fabric SDK has the following characteristics: Many underlying interface calls are returned asynchronously, and the gRPC protocol is used to transmit data in both directions.
gRPC combined with Protocol Buffer can reduce the amount of transmitted data and improve network transmission performance
Supported languages are Go, Java, JavaScript, and Python. In this configuration, we take Java language development as an example
Use the same interface as internal modules, reducing port openness and security risks
The entire Fabric network, except fabric-ca adopts HTTP protocol, others are GRPC protocol
Fabric SDK interface module
Fabric SDK defines two types of module interfaces; one accesses the fabric-ca interface, and the other accesses the fabric network interface, where fabric-ca is optional and can be replaced by other mature third-party CA systems
The modules provided by Fabric SDK are as follows:
HFClient module : application entry, providing channel management, chain code management, data storage, and cryptography-related functions. Each HFC instance corresponds to a blockchain network, including accounting nodes, sorting nodes, etc. If the application layer needs to access multiple networks, multiple instances of HFC can be createdConfig module : When initializing HFC, configuration information needs to be obtained offline. The Config module includes trusted root certificates, sorting service node certificates and IP addresses, bookkeeping node certificates and IP addresses, etc. After the configuration module is read, it is passed to HFC
Channel module : corresponding to the channel instance, the channel needs to be initialized after the order and peer are configured, and a request to obtain the configuration block is sent to the order service node during initialization
Peer module : The Peer node is the node where the HFC module sends endorsement requests and transaction queries. Peer instance contains information such as node name, address, role, registration certificate, etc.
Order module : The Order node is the node where the HFC module sends transaction ordering. The Order instance contains the address information of the sorting service node, and defines the interface for sending atomic broadcast requests and obtaining blocks.
User module : represents the entity that has completed the registration certificate and signature key. The registration certificate must be a CA trusted by the blockchain network issued
There is a difference between User Identity and Peer Identity. In the SDK, user identity can access secret key information and sign. The node identity cannot access the secret key, but can only verify the signature
3. Fabric application development process
The general steps of the Fabric application development process are as follows:
1. Create a channel on the sorting node
2. User registration and registration
3. Peer node joins the channel
4. Install and instantiate the chain code through the Peer node
5. Initiate a transaction and generate a block
Initiate a transaction as shown in the figure below:
> 4. Fabric application development process
If the application layer directly deals with the Fabric SDK, the process is not simple, and there will be a lot of redundant codes. Generally, a Framework will be packaged for the blockchain between the application layer and the Fabric SDK, simplifying the application layer and blocks. The difficulty of chain docking, for the application layer, there is no need to care about the underlying logic and process of the blockchain
Fabric source code introduction:
Fabric-CA source code introduction:
api: CA interface related code implementation
cmd/fabric-ca-client: CA client related code implementation
cmd/fabric-ca-server: Realization of functions implemented by CA server-related codes
lib/metadata:: database operation code implementation
Five, Fabric application development considerations
Understand the operation of Fabric and the transaction process before starting application development, otherwise you will be at a loss
Special attention needs to be paid to the configuration options of Fabric, including encryption, timeout, etc.
Almost every SDK has more or less bugs. Some bugs are mostly caused by the SDK itself, and some are problems with Fabric. After encountering these problems, we can judge which step went wrong according to the transaction process. , and then you can modify the log level on each node to get error messages. Relocate to a certain line of code in the SDK for debugging
It is necessary to pay attention to the matching of the SDK version and the Fabric platform version, as different versions may be incompatible
It is recommended that beginners start learning from the E2E examples that come with Fabric, which will get twice the result with half the effort