Fabric framework learning-official test and basic concepts

3. Basic demonstration and introduction of Fabric

After installing the environment, start to try to start a small demo to experience the whole process

3.1 Case introduction

step1 Generate a certificate

# 1. 先进入之前下载的环境中,找到first-network文件夹进入
# 2. 运行其中的脚本./byfn.sh
./byfn.sh generate   # generate参数会生成一些证书文件

Parameters of this script:

Generate a crypto-config directory, which stores a series of certificate files

step2

# 2. 运行Fabric每个节点需要使用的镜像,并向每个节点容器中安装链码,安装完了之后再做一个链码的调用
./byfn.sh up  	# 启动
./byfn.sh down  # 关闭

Example of successful startup:

Tested content:

Inquire:

Transfer, check again:

Case process analysis

I just saw the end of the run, but I don’t know what I did. The following is a rough introduction to the whole process.

Some notes:

  • Each organization has multiple node members
  • Each peer node runs in an independent docker container, which can be regarded as an independent virtual machine
  • Each peer node has a database (see below for specific functions)
  • Nodes between groups need to communicate, and communication needs to create channels
  • cli is the client node, the client node sends the request (query, transaction) to the peer node for processing
  • If you want each peer node to have the ability to process transactions, you need to install the chain code for each node
  • The role of the certificate: each node will generate a corresponding ssl certificate to ensure security

Process:

  1. Create channel
  2. The communication node is added to the channel
  3. Prepare the prepared chain code and install it on each peer node
  4. Initialization (you don't need to do it for each node, you only need to do it once)
  5. The client initiates a transaction request -> transfer
  6. After the transaction is successful, the data is sent to the ordering node order
  7. The sorting node will pack the data
  8. The packaged data is written into the block

And these nodes can be configured through yaml files

3.2 Fabric logical architecture

What we need to learn to use is the above four parts:

  • Member management

    • Sign Up

      • The consortium chain or private chain developed by Fabric is not open to everyone

      • Successful registration of an account is not a user name and password

      • Use certificate as a sign of identity authentication

    • Identity protection

    • Transaction audit

    • Content confidential

      • There can be multiple blockchains in Fabric, distinguished by channels (channels can be understood as QQ groups)
      • And a peer node can be added to multiple channels
      • Each channel is independent of each other, and the node can only know the data in the channel when it enters the channel (each QQ group is independent of each other, only after entering the group can you see the group message) => content confidentiality
  • Ledger management

    • Blockchain
      • Saved all transaction records
      • If each node joins multiple channels, then there are multiple blockchains that need to be saved
    • World state
      • Used to view the latest status of the data
      • Data is stored in the database of the current node
        • Each node has its own database: levelDB, couchdb can also be used
        • Data is stored as key-value pairs in the database
      • To view the historical state, you need to observe in the blockchain
  • Transaction management

    • Deployment transaction
      • The deployment is the chain code, which is to install the chain code-chaincode to the node
    • Call transaction
      • invoke call
  • Smart contract

    • A piece of code that handles the business logic agreed by the network members
    • Realize the separation of chain code and ledger (separation of logic and data)

3.3 Basic concepts

3.3.1 Organization

Organization refers to a social entity that has a clear goal-oriented and well-designed structure and a consciously coordinated activity system, while maintaining close contact with the external environment.

What is in an organization in Fabric:

  • User (multiple)
  • Data processing node -> peer (may also be multiple)
    • put writes the data to the blockchain
    • get data query

A small example:

For example, this is the model of milk traceability chain

The dairy farms, processing plants, and distributors in the picture are all organizations. Many users (cattle farms, processing plants, etc.) are responsible for providing information. There are nodes (peers) that record information to record information.

Three organizations jointly maintain the same blockchain data

When there is a user who needs to view information (tracing the source), query on this chain.


Instructions for peer nodes:

  • There may be multiple peer nodes (data processing nodes) in each organization, and each peer under the same organization stores the same blockchain data
  • The more peer nodes in an organization, the more redundant the data and the safer the data

3.3.2 Node

There are many nodes in Fabric, among which there are many types:

  • client

    Transaction management (cli, node sdk, java sdk)

    • For the above example, if Niuchang A needs to submit data, then it needs to use a client terminal, which is responsible for data submission and data query.
    • There are many ways to implement the terminal:
      • cli: Operate through the Linux command line, and use shell commands to submit and query data
      • Node sdk is nodejs: implement a client with nodejs api for users to access through the browser
      • java: Implement a client through java
      • go: the same can be done
  • peer

    Store and synchronize ledger data

    • The user submits the data through the client tool, and the data is written to a peer node
    • The data synchronization between peer nodes is done by the Fabric framework and does not require us to implement
  • orderer

    Sort and distribute transactions

    • Transaction data needs to be packaged first, and then written into the block (similar to the work of miners in the blockchain)
    • Why sort?
      • Solve the double spend problem
      • Every transaction initiated will be sorted by the orderer, and a special algorithm will be used to solve the double-spending problem

3.3.3 Channel

A channel is a communication mechanism provided by a consensus service (ordering), which connects peers and orderers to form a confidential communication link (virtual), which achieves the requirement of business isolation ; the channel is also connected to the ledger (ledger). ) State (worldstate) is closely related

The concept of the channel can be understood as a QQ group, and only the nodes in the channel can achieve data synchronization

Focus map! ! ! !

  • consensus Service: orderer node
  • Three different colored lines represent three different channels
  • A peer node can join multiple channels
  • When a peer is added to several channels, there are several generated blockchains, and the corresponding data can be obtained from the channel

3.3.4 Transaction Process

Simplified process:

  1. To complete a transaction, the transaction must have an endorsement strategy (specified when installing the chaincode)

  2. Suppose a certain strategy is:

    • Members in organization A must agree
    • Members in organization B must also agree
  3. Application/SDK: Acting as a client

    • Write data, query data
  4. The client initiates a proposal (what I want to do) to the peer node, Endorse: endorsement

    • The proposal will be sent to peer nodes in two organizations A and B
  5. The peer node will preview the transaction and get the result of the execution

  6. The peer node will send the result to the client, Respond (Get Endorsed)

    • The client will judge the returned result:
      • If the simulated transaction fails, the entire transaction process is terminated
      • Continue on all success
  7. The client submits the transaction to the ordering node, Broadcast

  8. Ordering nodes perform transaction packaging, generally a certain number of transactions are packaged and then packaged together

  9. The orderer node will send the packaged data to the peer, and the peer node will write the data into the block. Blocks

  10. Update the state of the world

    • The sending of packaged data is not real-time
    • There is a setting file, in the file configuration

Endorsement strategy:

  • To complete a transaction, the completion process of the transaction is endorsement
  • To complete a transaction, the transaction must have an endorsement strategy (specified when installing the chaincode)
  • The endorsement strategy can be understood as the test guidance rules before the actual data is on the blockchain

3.3.5 Ledger

3.3.6 SMEs

Membership Service Provider (MSP) is a component of a management framework that provides virtual member operations

Who will have MSP:

  • Every node will have MSP
  • Every user has MSP

MSP can be regarded as an account, which contains the authentication certificate of the node

Tips

1. ./byfn.sh generate出错 Failed to generate orderer genesis block

This error will result in the failure to create the genesis block, and the order node container will not start, ./byfn.sh upand an error will be reported:

Error: failed to create deliver client: orderer client failed to connect to orderer.example.com:7050: failed to create new connection: context deadline exceeded

Guess you like

Origin blog.csdn.net/weixin_43988498/article/details/108960468