The main content of this article is as follows

  • Starting a Peer Node
    • Prepare the material
    • The configuration file
    • Start the program
  • Creating an Application Channel
    • Generate the transaction file that creates the Channel
    • Generate the creation block file that applies the Channel
  • Example Add a peer to a Channel
  • Set anchor node
    • Generate a transaction file that modifies the anchor node configuration
    • Initiate a transaction request to modify the anchor configuration
  • conclusion

All right, enough about the theory. At this point, I assume you are familiar with how Fabric works.

Let’s get back to our practice!

Starting a Peer Node

Prepare the material

  1. According to the previous plan, each organization prepares two Linux services:

    organization Network IP Network port Access to the domain name Outside the network port
    M 172.16.0.10 7051 peer0.m.com 7051
    M 172.16.0.11 8051 peer1.m.com 8051
    Rate 192.168.10.10 7051 peer0.rate.com 7051
    Rate 192.168.10.11 8051 peer1.rate.com 8051
    DB 10.1.1.10 7051 peer0.db.com 7051
    DB 10.1.1.11 8051 peer1.db.com 8051
  2. Copy the MSP and genesis block files to the corresponding server:

    “MSP files” source file: organizations/peerOrganizations/m.com/peers/peerN.m.com target path: / etc/hyperledger/fabric/MSP note: each node of the source file is different

    “The TLS file” source file: organizations/peerOrganizations/m.com/peers/peerN.m.com/tls target path: / etc/hyperledger/fabric/TLS note: each node of the source file is different

  3. Peer executable program file Refer to the Operation Guide.

The configuration file

As with the Orderer node, the configuration file is required to start the peer node. The default is to read core.yaml in the current directory or in the FABRIC_CFG_PATH directory.

As with the Orderer node, we configure it with environment variables, using peer1.m.com as an example:

CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock

Container engine socket address (container configuration, but not applicable to K8S management software)



CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=${COMPOSE_PROJECT_NAME}_mcorp

# Network to which chain code nodes are connected (configured when using containers)



CORE_PEER_PROFILE_ENABLED=true  

# Whether to enable the Go Profile, the production environment should be closed



FABRIC_LOGGING_SPEC=INFO

# Output log level



CORE_PEER_TLS_ENABLED=true

# Whether to enable TLS



CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt

# TLS certificate of this node



CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key

# TLS private key of this node



CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt

# TLS CA root certificate



CORE_PEER_GOSSIP_USELEADERELECTION=false

# Whether to elect the leader node



CORE_PEER_GOSSIP_ORGLEADER=true

# Whether this node is used as the leader node (both true and CORE_PEER_GOSSIP_USELEADERELECTION cannot be true)



CORE_CHAINCODE_EXECUTETIMEOUT=300s  

Timeout for invoking chain code. If the chain image is deleted, leave enough time to rebuild the image



CORE_PEER_ID=peer1.m.com

# Unique ID of this object



CORE_PEER_ADDRESS=peer1.m.com:7051

# this node access address



CORE_PEER_LISTENADDRESS = 0.0.0.0:7051

# program listening address



CORE_PEER_CHAINCODEADDRESS=peer1.m.com:7052

# link code service access address



CORE_PEER_CHAINCODELISTENADDRESS = 0.0.0.0:7052

# link code service listens for address



CORE_PEER_GOSSIP_BOOTSTRAP=peer0.m.com:7051

# GOSSIP protocol seed address, specified as another node in the organization or this node



CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.m.com:7051

If this parameter is not set, other organizations cannot directly access this node



CORE_PEER_LOCALMSPID=McorpMSP

# organization unique MSP ID, which must be consistent with channel Settings



CORE_LEDGER_STATE_STATEDATABASE=CouchDB

Goleveldb is the default state database type



CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb0:5984

State database address and port



CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=

State database user name



CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=

State database password

Copy the code

Start the program

Run the following command on the server:

peer node start
Copy the code

Then, observe whether the output log information has an error.

At this point, all nodes are started. But for a network to really work, you need a Channel.

Creating an Application Channel

For Fabric, adding, deleting, or modifying channels is also done through transactions. This is a system configuration transaction.

No matter what the transaction, the processing logic is consistent, except in the packaging transaction, system configuration transaction and ordinary transaction slightly different.

System configuration transactions and normal transactions are not packaged in one block at the same time. That is, a block will contain only normal transactions, or only system configuration transactions.

This actually makes sense, the system configuration is stored in the system blockchain, and the system blockchain is stored in the Orderer. Normal transaction blocks are sent to peers. System configuration transactions do not.

Creating an application channel consists of the following two steps:

  1. Generate the transaction file that creates the Channel
  2. Generate the creation block file that applies the Channel

Generate the transaction file that creates the Channel

Find the configtx.yaml file we used to generate the Creation block.

Remember there are two Profiles Profiles in it? I’ve already used one, and I’m going to use the other one here.


We’ve gone through each of the configuration items, so we won’t bother here.

Pay attention to the point is that the organization must be included in the Consortium in the Application.Org anizations specified in the league.

The Consortium was formed when the Creation block was created. If you forgot, check out the config Consortiums section.

After the configuration file is ready, run the following command:

configtxgen -profile ForCreateChannel -outputCreateChannelTx ./channel-artifacts/bondnet.tx -channelID bondnet



# -profile Specifies which section of Profiles in the profile to use

# -outputCreatechanneltx Specifies the name of the file to generate the transaction information

# -channelid Specifies the channel name

Copy the code

Upon completion, the file./channel-artifacts/bondnet.tx is generated. This contains the transaction request to create a Channel and the associated read-write set information, which is used to make a request to orderer to create a Channel.

Generate the creation block file that applies the Channel

Remember the trading process?

To generate a block, you need a trade request, and you need a client to initiate the trade request.

The client software can be user-written. If not, a peer can be used for system maintenance operations such as creating a Channel.

We can select any organization in a Channel and use its administrator and peer nodes to complete the creation block file operation of the application Channel.

However, the general practice is to find a host or container that can run the peer as the client and run it by setting environment variables.

In addition, a user is required to operate the client and create a Channel as an administrator. Therefore, the user must be an administrator.

Assume that the client host name is client-host.

The operation steps are as follows:

  1. Copy organizations/peerOrganizations/m.com/users/[email protected] to client – host: / TMP directory.

  2. Copy organizations/ordererOrganizations/mid.org/orderers/order0.mid.org/tls/ca.crt to client-host:/tmp/order0.mid.org/tls/ Directory.

  3. Copy channel-artifacts/bondnet.tx to the client-host:/ TMP directory.

  4. Make a new shell connection.

  5. Configure the following environment variables:

export CORE_PEER_LOCALMSPID=McorpMSP

# Use M company's MSP



export CORE_PEER_TLS_ENABLED=true

# enable TLS



export CORE_PEER_MSPCONFIGPATH=/tmp/[email protected]/msp

# specify the MSP directory for admin

Copy the code
  1. Execute command:
peer channel create -o order0.mid.org:7050 -c bondnet -f /tmp/bondnet.tx --outputBlock /tmp/bondnet.block --tls true --cafile /tmp/order0.mid.org/tls/ca.crt

# - o: specify the order service address, if the IP address, you can use - ordererTLSHostnameOverride order certificates specified set in the domain name.

# -c: Specifies the channel name.

# -f: specifies the transaction file path to create Channel.

# -- TLS: whether to enable TLS, if Order enabled TLS, set to true.

Cafile: specifies the root CA of the ORDER certificate.

# --outputBlock: specifies the path to generate the block file.

Copy the code

/ TMP /bondnet. Block is the genesis block file of the blockchain corresponding to bondnet Channel.

NOTE The above is only the default case. If relevant policies and ACLs change, you should adjust them according to the new policy or ACL.

Example Add a peer to a Channel

This step is to add the peer node associated with the new Channel to the Channel.

If you have a peer that does not want to participate in the Channel, you can skip the join operation.

This can be done by selecting a peer node in each organization or by client-host in the previous section.

Here, I execute it on the peer0.m.com node, and the steps are as follows:

  1. Copy the bondnet.block file generated in the previous section to the/TMP/directory of the peer node.

  2. Will also require an administrator to operate, copy organizations/peerOrganizations/m.com/users/[email protected] to peer0.m.com: / TMP directory.

  3. Make a new shell connection.

  4. Configure the following environment variables:

export CORE_PEER_MSPCONFIGPATH=/tmp/[email protected]/msp

Copy the code
  1. Run the following command to add peer0.m.com to the channel:
peer channel join -b /tmp/bondnet.block

Copy the code
  1. If there are other peers to be added, set the following environment variables to switch the connected peer:
export CORE_PEER_ADDRESS=peer1.m.com:8051

Copy the code
  1. Then run the following command to add peer1.m.com to the channel:
peer channel join -b /tmp/bondnet.block

Copy the code

Similarly, to add a peer to a Channel, change CORE_PEER_ADDRESS to the service address of that peer.

Other organizations do the same.

Finally, don’t forget to clean up the files that you copied to/TMP. The user’s private key cannot be leaked.

If operating on client-host, environment variables need to be set several more, for example:

export CORE_PEER_LOCALMSPID=McorpMSP

export CORE_PEER_TLS_ENABLED=true

export CORE_PEER_MSPCONFIGPATH=./organizations/peerOrganizations/m.com/users/[email protected]/msp

export CORE_PEER_TLS_ROOTCERT_FILE=./organizations/peerOrganizations/m.com/peers/peer0.m.com/tls/ca.crt 

# ca. CRT is used to verify the certificate from the peer when joining the channel

export CORE_PEER_ADDRESS=peer0.m.com:7051

Copy the code

Set anchor node

Setting up anchor nodes is essentially a modification of the application Channel configuration.

Generate a transaction file that modifies the anchor node configuration

The anchor configuration is still in the configtx.yaml file


Run the following command to generate a transaction file that changes the anchor node configuration:

configtxgen -profile ForCreateChannel -outputAnchorPeersUpdate ./channel-artifacts/McorpMSPanchors.tx -channelID bondnet  -asOrg McorpMSP

# -profile Specifies which section of Profiles in the profile to use

# -outputanchorPeersupdate specifies the name of the file that generated the configuration anchor node transaction

# -channelid Specifies the channel name

# -asorg Each organization needs to be configured separately. This parameter specifies the MSP name (not the MSP ID) of the organization.

Copy the code

Do the same for other organizations after changing -asorg.

Initiate a transaction request to modify the anchor configuration

The following uses the anchor node of the M.com organization as an example. Do the same for other organizations.

On the client-host, perform the following steps:

  1. Copy organizations/peerOrganizations/m.com/users/[email protected] to client – host: / TMP directory.

  2. Copy organizations/ordererOrganizations/mid.org/orderers/order0.mid.org/tls/ca.crt to client-host:/tmp/order0.mid.org/tls/ Directory.

  3. Copy the channel – artifacts/McorpMSPanchors. The tx to client – host: / TMP directory.

  4. Make a new shell connection.

  5. Configure the following environment variables:

export CORE_PEER_LOCALMSPID=McorpMSP

# Use M company's MSP



export CORE_PEER_TLS_ENABLED=true

# enable TLS



export CORE_PEER_MSPCONFIGPATH=/tmp/[email protected]/msp

# specify the MSP directory for admin



export CORE_PEER_ADDRESS=peer1.m.com:7051

# Specify the peer node to connect to

Copy the code
  1. Execute command:
peer channel update -o order0.mid.org:7050 -c bondnet -f /tmp/McorpMSPanchors.tx --tls true --cafile /tmp/order0.mid.org/tls/ca.crt

# - o: specify the order service address, if the IP address, you can use - ordererTLSHostnameOverride order certificates specified set in the domain name.

# -c: Specifies the channel name.

# -f: specifies the path to change the anchor node's transaction file.

# -- TLS: whether to enable TLS, if Order enabled TLS, set to true.

Cafile: specifies the root CA of the ORDER certificate.

Copy the code

Q: Who plays the role of client and endorsement node in creating and modifying channel transactions? What chain code is called?

A: The peer program that initiates the request acts as the client, and the peer node specified by CORE_PEER_ADDRESS acts as the endorsement node, invoking the system chain code.

conclusion

So far, our basic network is all set up. In the next article, we will begin writing and using Fabric applications in earnest.

I don’t know how you feel after watching the basic network building process?

I have two main feelings:

  1. Fabric breaks up the pieces of a blockchain system into individual modules, although this is unfriendly for beginners and the learning curve is too steep. But when you look at other blockchain systems, they seem to be just a recombination of modules or processes in the Fabric (of course, if you look deeper, the systems are quite different). Isn’t the process of learning and exploring interesting in itself?

  2. A sense of control. Compared to playing with the public chain, private chains and affiliate chains have more autonomy and don’t have to follow many rules set by the public chain. The modularity of Fabric allows programmers to feel that “the world is at their fingertips.” Perhaps the stereotype of programmers is that they are silent, dull and difficult to communicate. That’s because you have no idea what kind of world we live in.

You may not yet feel the modularity of Fabric. Don’t worry, there are still a lot of new concepts to be understood!

I am a2Stream programmer, we next time again cheap!

This article is formatted using MDNICE