Take a look at the samples two department network three automation scripts have done what to boot block chain network execution Scriptsscriptsh scripts four themselves perform Channelchaincode related operations start the network into the CLI container create channel add to channel install C Haincode initialization chaincode Chaincode Query method Chaincode Invoke method Chaincode other methods
A. Download Samples
Note: build the basic environment first, then follow the steps below.
git clone https://github.com/hyperledger/fabric-samples.git
CD Fabric-samples
When the download is complete, there will be the following files, this time we use First-network to deploy the network:
two. Deployment Network
CD first-network
./byfn.sh-m up
If you see the output below, the network startup is successful. It automatically performs the following actions: 1. Create a block Chain network (4 peer+1 order) 2. Create channel to add peer to the Channel 3. Deploy/Execute chaincode.
.......
========= all good, byfn execution completed ===========
_____ _ _ ____
| ____| | | | _ \
| _| | \| | | | | |
| |___ | |\ | | |_| |
| _____| |_| \_| |____/
three. What are the automation scripts doing?
BYFN.SH is a packaged automated deployment script in the demo, and we'll take a step-by-step analysis of what it does. 1. Start Block Chain network
./byfn.sh-m up will eventually perform networkup:
function Networkup () {
if [!-D "Crypto-config"]; then
generatecerts #
1 replaceprivatekey #2
generatechannelartifacts # 3
fi
channel_name= $CHANNEL _name timeout= $CLI _timeout docker-compose -F $COMPOSE _file up-d 2>&1 #4
if [$-ne 0]; then
echo "ERROR!!!! Unable to start network "
Docker logs-f CLI
exit 1
fi
docker logs-f CLI
}
What it does: use Cryptogen to generate a certificate/secret key for each node and organization in the network based on the Crypto-config.yaml file. Here, a template file is used to replace the private key in the template with the private key generated above. Use Configtxgen to generate the required data for the channel according to the Configtx.yaml file. Using Docker-compose to run the services defined in Docker-compose-cli.yaml, each service is run in a separate Docker container.
at this point the network is started , through the Docker PS View the current network:
Fa5e433e52d3 hyperledger/fabric-tools "/bin/bash-c ' ..." Minutes ago up minutes CLI 5f767fa25e10 Hyperle Dger/fabric-orderer "Orderer" minutes ago up to minutes 0.0.0.0:7050->7050/tcp orderer.example.com 2d4c09ceed5f Hyperledger/fabric-peer "Peer node start "Minutes ago up minutes 0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp Peer1.org1.examp le.com 6d865dbcc698 Hyperledger/fabric-peer "Peer node start" minutes ago up minute S 0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp peer0.org2.example.com e1f2ebe3f527 hyperledger/fa Bric-peer "Peer node start" Minutes ago up to minutes 0.0.0.0:7051->7051/tcp, 0.0.0. 0:7053->7053/tcp peer0.org1.example.com B74AED9E23F2 Hyperledger/fabric-peer "Peer node start" Minutes ago up minutes 0.0.0.0:10051-&
GT;7051/TCP, 0.0.0.0:10053->7053/tcp peer1.org2.example.com
2. Execute/scripts/script.sh script
Where the CLI service is finally started, the/scripts/script.sh script is automatically executed after the startup is successful (command:/bin/bash-c './scripts/script.sh ${channel_name} is configured in the CLI service) ; Sleep $TIMEOUT ').
The main actions of the script are as follows:
# 1. Create channel echo "Creating Channel ..." CreateChannel # 2. Add all peer to channel "having a" ll peers join the channel ... "Joinchannel # 3. Set anchor points peer Echo ' updating anchor peers for ORG1 in each organization updateanchorpeers 0 echo "Updating anchor peers for org2 ..." Updateanchorpeers 2 # # 4. Install Peer0 echo on peer2 chaincode node installing On Org1/peer0 ... "Installchaincode 0 echo" Install chaincode on Org2/peer2 ... "Installchaincode 2 # 5. Initialize Chaincode echo "instantiating Chaincode on Org2/peer2 ..." on Peer2 node Instantiatechaincode 2 # 6. Execute chaincode query operation on PEER0 node echo "Querying Chaincode on Org1/peer0 ..." Chaincodequery 0 100 # 7. Peer0 on Chaincode nodes E operation echo "Sending invoke transaction on Org1/peer0 ..." Chaincodeinvoke 0 # 8. Install Chaincode echo "Installing Chaincode on Org2/peer3 ..." on Peer3 Installchaincode 3 # 9. Perform a query operation on the PEER3 to see if the result is the "echo" Quer Ying Chaincode on Org2/peer3 ... "Chaincodequery 3
Four. Perform Channel/chaincode related Operations yourself
What if you want to perform channel/chaincode related operations yourself? Execute./byfn.sh-m down to shut down the network, and then disable the automatic execution of the/scripts/script.sh script.
Start Network
./byfn.sh-m Up
When finished, Docker PS View network in another command panel (two organizations, two peer per organization):
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 91203e17dff2 hyperledger/fabric-tools "/bin/bash" Notoginseng seconds ago Up seconds CLI 50eed8db37e5 Hyperledger/fabric-peer "Peer node star T "seconds ago up seconds 0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp peer1.org2.example.c Om fccc67250e79 hyperledger/fabric-peer "peer node start" seconds ago up seconds 0.0.0.0:80 51->7051/tcp, 0.0.0.0:8053->7053/tcp peer1.org1.example.com 04ba77fe9c07 hyperledger/fabric-peer "PE ER node start "seconds ago up seconds 0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp peer0.or g2.example.com e2a14716ff5b hyperledger/fabric-orderer "orderer" seconds ago up seconds 0.0.0.0:7050->7050/TCP orderer.example.com b88215b96968 Hyperledger/fabric-peer "Peer node start" 4 6 seconds ago up seconds 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp peer0.org1.example.com
Enter CLI container
Docker exec-it CLI Bash
Execution results:
Set the ORG1 PEER0 environment variable in the CLI and copy it directly to the command panel:
Core_peer_localmspid= "Org1msp"
core_peer_tls_rootcert_file=/opt/gopath/src/github.com/hyperledger/fabric/ PEER/CRYPTO/PEERORGANIZATIONS/ORG1.EXAMPLE.COM/PEERS/PEER0.ORG1.EXAMPLE.COM/TLS/CA.CRT
CORE_PEER_ mspconfigpath=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerorganizations/org1.example.com/ Users/admin@org1.example.com/msp
core_peer_address=peer0.org1.example.com:7051
Execution results:
Create channel
Create the channel on the Peer0 node of ORG1:
Execution results:
Add to Channel
Add 4 peer to the channel because the environment variables for each peer node are different, so configure the correct environment variable first, then execute the join command.
To set ORG1-PEER0 environment variables:
Core_peer_localmspid= "Org1msp"
core_peer_tls_rootcert_file=/opt/gopath/src/github.com/hyperledger/fabric/ PEER/CRYPTO/PEERORGANIZATIONS/ORG1.EXAMPLE.COM/PEERS/PEER0.ORG1.EXAMPLE.COM/TLS/CA.CRT
CORE_PEER_ mspconfigpath=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerorganizations/org1.example.com/ Users/admin@org1.example.com/msp
core_peer_address=peer0.org1.example.com:7051
Add to Channel:
Peer Channel Join-b Mychannel.block
Execution results:
The same is true for other nodes, and other node environment variables are as follows:
Org1-peer1:
Core_peer_localmspid= "Org1msp"
core_peer_tls_rootcert_file=/opt/gopath/src/github.com/hyperledger/fabric/ PEER/CRYPTO/PEERORGANIZATIONS/ORG1.EXAMPLE.COM/PEERS/PEER0.ORG1.EXAMPLE.COM/TLS/CA.CRT
CORE_PEER_ mspconfigpath=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerorganizations/org1.example.com/ Users/admin@org1.example.com/msp
core_peer_address=peer1.org1.example.com:7051
ORG2-PEER0:
Core_peer_localmspid= "Org2msp"
core_peer_tls_rootcert_file=/opt/gopath/src/github.com/hyperledger/fabric/ PEER/CRYPTO/PEERORGANIZATIONS/ORG2.EXAMPLE.COM/PEERS/PEER0.ORG2.EXAMPLE.COM/TLS/CA.CRT
CORE_PEER_ mspconfigpath=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerorganizations/org2.example.com/ Users/admin@org2.example.com/msp
core_peer_address=peer0.org2.example.com:7051
Org2-peer1:
Core_peer_localmspid= "Org2msp"
core_peer_tls_rootcert_file=/opt/gopath/src/github.com/hyperledger/fabric/ PEER/CRYPTO/PEERORGANIZATIONS/ORG2.EXAMPLE.COM/PEERS/PEER0.ORG2.EXAMPLE.COM/TLS/CA.CRT
CORE_PEER_ mspconfigpath=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerorganizations/org2.example.com/ Users/admin@org2.example.com/msp
core_peer_address=peer1.org2.example.com:7051
Install Chaincode
We use the Chaincode in the demo to specify the Chaincode name/version/path
Peer Chaincode install-n mycc-v 1.0-p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
Results:
The Chaincode file is mapped like this:
Initialize Chaincode
Peer Chaincode instantiate-o orderer.example.com:7050--tls $CORE _peer_tls_enabled--cafile/opt/gopath/src/ github.com/hyperledger/fabric/peer/crypto/ordererorganizations/example.com/orderers/orderer.example.com/msp/ Tlscacerts/tlsca.example.com-cert.pem-c mychannel-n mycc-v 1.0-c ' {Args ': ["Init", "a", "M", "B", "A"]} ' P ' or G1msp.member ', ' Org2msp.member ') "
Results:
Note the following:-C ' {"Args": ["Init", "a", "M", "B", "200"]} '
This will trigger the Chaincode init method and deposit a 100/200 yuan to the A account/b account respectively. -P "OR (' org1msp.member ', ' Org2msp.member ')"
This involves the endorsement strategy (endorsement), which is specified by the-p argument. This means that the transaction must be done through the support (endorsement) of a member of the ORG1 or org2, with more grammatical details to look at here.
After initialization, there will be one more container running Chaincode, container name rule: dev-node-chaincode name-chaincode version
Chaincode initialization is required only once, but Chaincode needs to be installed on all peer within channel. At present we only install Chaincode on the Org2-peer1 node, and switch to the other corresponding nodes according to the previous environment variables to perform the Chaincode installation. Query method of Chaincode
Check the balance of a:
Query results:
the method of Chaincode's Invoke
b transfer to a 50 yuan:
Peer Chaincode Invoke-o orderer.example.com:7050
Execution results:
Check the balance again:
Chaincode Other methods
From the beginning of v1.0.0, we write the Chaincode as long as the implementation of the Chaincodestubinterface interface can:
Type Chaincode Interface {
///Initialize or update Chaincode will perform this operation
Init (stub chaincodestubinterface) PB. Response
//define Curd method
Invoke (stub chaincodestubinterface) PB. The Response
//old version has a Query method that is now integrated into the invoke inside
}
Let's see how the Chaincode_example02.go is implemented:
func (t *simplechaincode) Init (stub shim. Chaincodestubinterface) PB. Response {fmt. Println ("ex02 Init") _, Args: = Stub. Getfunctionandparameters () ... return shim. Success (NIL)}//defines three methods Func (T *simplechaincode) Invoke (stub shim. Chaincodestubinterface) PB. Response {fmt. Println ("ex02 Invoke") function, args: = Stub. Getfunctionandparameters () if function = = "Invoke" {//a-transfer->b x Yuan return t.invoke (stub, args)