You are on page 1of 8

Lab Hyperledger No.

03
Exploring test-network with Hyperledger Fabric

Họ và tên Sinh viên: Lương Hoàng Nhật Hiếu


Mã Sinh viên: 123200060 Nhóm: 20.89

1. Using the Fabric test network...............................................................................1


2. Prepare...................................................................................................................1
3. Bring up the test network.....................................................................................3
4. Interacting with the network................................................................................3
5. Bring down the network........................................................................................6
6. What’s happening behind the scenes?.................................................................7
7. REFERENCE.........................................................................................................7

1. Using the Fabric test network


The test network is provided for learning about Fabric by running nodes on your local
machine. Developers can use the network to test their smart contracts and applications.
The network is meant to be used only as a tool for education and testing and not as a
model for how to set up a network. In general, modifications to the scripts are
discouraged and could break the network. It is based on a limited configuration that
should not be used as a template for deploying a production network:
 It includes two peer organizations and an ordering organization.
 For simplicity, a single node Raft ordering service is configured.
 To reduce complexity, a TLS Certificate Authority (CA) is not deployed. All
certificates are issued by the root CAs.
 The sample network deploys a Fabric network with Docker Compose. Because the
nodes are isolated within a Docker Compose network, the test network is not
configured to connect to other running Fabric nodes.
The test network has been successfully verified with Docker Desktop version 2.5.0.1
Check now your chaincode containers are up and running by typing
hyperledger@vmhyper:~$ docker ps -a

2. Prepare
 Cách 1: Sử dụng công cụ MobaXterm để truy cập từ xa đến máy ảo theo địa chỉ IP:
wandertour.ddns.net, port 25, username: hyperledger, pass: 123456
C:\Users\ABC>ssh -p 25 hyperledger@wandertour.ddns.net

KhoaCNTT-Trường ĐHBK, ĐHĐN


 Cách 2: Tải về máy ảo và chạy, sử dụng lệnh ssh để truy cập từ xa đến máy ảo theo địa
chỉ IP: 192.168.1.15, port 25, username: hyperledger, pass: 123456
 Tải máy ảo VMHyper2023 tại LINK06 và giải nén file tải về tại thư mục gốc ổ đĩa.

KhoaCNTT-Trường ĐHBK, ĐHĐN


 Cài đặt và chạy VMware Workstation. Từ cửa sổ VMWare, chọn menu File/Open, mở
file *.vmx trong thư mục VMHyper2023 và khởi động máy ảo.

3. Bring up the test network


 NOTE: Do not execute commands 13 when performing remote access on
itfdut.ddns.net server because the test-network has already been started (script 1a)
1) hyperledger@vmhyper:~/fabric-samples/test-network$ ./network.sh down
2) hyperledger@vmhyper:~/fabric-samples/test-network$ ./network.sh up createChannel
3) hyperledger@vmhyper:~/fabric-samples/test-network$ ./network.sh deployCC -ccn
basic -ccp ../asset-transfer-basic/chaincode-go -ccl go

The subcommand will install the asset-transfer (basic) chaincode


deployCC

on peer0.org1.example.com and peer0.org2.example.com and then deploy the chaincode on the


channel specified using the channel flag (or mychannel if no channel is specified).
If you are deploying a chaincode for the first time, the script will install the chaincode
dependencies. You can use the language flag, -ccl , to install the Go, typescript or
javascript versions of the chaincode. You can find the asset-transfer (basic) chaincode in
the asset-transfer-basic folder of the fabric-samples directory. This folder contains sample
chaincode that are provided as examples and used by tutorials to highlight Fabric
features.

4. Interacting with the network


hyperledger@vmhyper:~$ clear
hyperledger@vmhyper~ $ ls $HOME/fabric-samples/
hyperledger@vmhyper~ $ cd $HOME/fabric-samples/test-network

# Environment variables for Org1


hyperledger@vmhyper:~/fabric-samples/test-network$
export PATH=${PWD}/../bin:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/
org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export
CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/
users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051

KhoaCNTT-Trường ĐHBK, ĐHĐN


After you bring up the test network, you can use the peer CLI to interact with your
network. The peer CLI allows you to invoke deployed smart contracts, update channels,
or install and deploy new smart contracts from the CLI.
Make sure that you are operating from the test-network directory.
The CORE_PEER_TLS_ROOTCERT_FILE and CORE_PEER_MSPCONFIGPATH environment variables point
to the Org1 crypto material in the organizations folder.
hyperledger@vmhyper:~/fabric-samples/test-network$ peer channel list
2023-09-03 14:23:28.240 UTC 0001 INFO [channelCmd] InitCmdFactory ->
Endorser and orderer connections initialized
Channels peers has joined:
Mychannel

 Run the following command to initialize the ledger with assets:

dutchain@vmhyper:~/fabric-samples/test-network$ peer chaincode invoke -o


localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "$
{PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com
/msp/tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n basic --peerAddresses
localhost:7051 --tlsRootCertFiles
"${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example
.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles
"${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example
.com/tls/ca.crt" -c '{"function":"InitLedger","Args":[]}'
2023-09-04 16:59:06.615 +07 0001 INFO [chaincodeCmd] chaincodeInvokeOrQuery
-> Chaincode invoke successful. result: status:200

If successful, you should see output similar to the following example:


-> INFO 001 Chaincode invoke successful. result: status:200

You can now query the ledger from your CLI. Run the following command to get the
list of assets that were added to your channel ledger:
dutchain@vmhyper:~/fabric-samples/test-network$ peer chaincode query -C mychannel -n
basic -c '{"Args":["GetAllAssets"]}'

If successful, you should see the following output:


[
{"ID": "asset1", "color": "blue", "size": 5, "owner": "Tomoko", "appraisedValue": 300},
{"ID": "asset2", "color": "red", "size": 5, "owner": "Brad", "appraisedValue": 400},
{"ID": "asset3", "color": "green", "size": 10, "owner": "Jin Soo", "appraisedValue": 500},
{"ID": "asset4", "color": "yellow", "size": 10, "owner": "Max", "appraisedValue": 600},
{"ID": "asset5", "color": "black", "size": 15, "owner": "Adriana", "appraisedValue": 700},
{"ID": "asset6", "color": "white", "size": 15, "owner": "Michel", "appraisedValue": 800}

KhoaCNTT-Trường ĐHBK, ĐHĐN


]

Chaincodes are invoked when a network member wants to transfer or change an asset
on the ledger. Use the following command to change the owner of an asset on the ledger
by invoking the asset-transfer (basic) chaincode:

dutchain@vmhyper:~/fabric-samples/test-network$ peer chaincode invoke -o


localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "$
{PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com
/msp/tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n basic --peerAddresses
localhost:7051 --tlsRootCertFiles "$
{PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.c
om/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "$
{PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.c
om/tls/ca.crt" -c '{"function":"TransferAsset","Args":["asset6","Christopher"]}'

If the command is successful, you should see the following response:


2019-12-04 17:38:21.048 EST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode
invoke successful. result: status:200

dutchain@vmhyper:~/fabric-samples/test-network$ peer chaincode invoke -o


localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "$
{PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com
/msp/tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n basic --peerAddresses
localhost:7051 --tlsRootCertFiles "$
{PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.c
om/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "$
{PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.c
om/tls/ca.crt" -c '{"function":"TransferAsset","Args":["asset6","HoVaTen"]}'

Because the endorsement policy for the asset-transfer (basic) chaincode requires the
transaction to be signed by Org1 and Org2, the chaincode invoke command needs to
target both peer0.org1.example.com and peer0.org2.example.com using the --peerAddresses flag.

KhoaCNTT-Trường ĐHBK, ĐHĐN


Because TLS is enabled for the network, the command also needs to reference the TLS
certificate for each peer using the --tlsRootCertFiles flag.
After we invoke the chaincode, we can use another query to see how the invoke
changed the assets on the blockchain ledger. Since we already queried the Org1 peer, we
can take this opportunity to query the chaincode running on the Org2 peer. Set the
following environment variables to operate as Org2:

# Environment variables for Org2


hyperledger@vmhyper:~/fabric-samples/test-network$
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/
org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export
CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.c
om/users/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051

You can now query the asset-transfer (basic) chaincode running


on peer0.org2.example.com :

hyperledger@vmhyper:~/fabric-samples/test-network$ peer chaincode query -C


mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'

The result will show that "asset6" was transferred to Christopher:

{"ID":"asset6","color":"white","size":15,"owner":"Christopher","appraisedValue":800}

You can use the following command to examine the MSP folder of the Org1 admin
user:
hyperledger@vmhyper:~/fabric-samples/test-network$ tree
organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/

The command will reveal the MSP folder structure and configuration file:

organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/
└── msp
├── IssuerPublicKey
├── IssuerRevocationPublicKey
├── cacerts
│ └── localhost-7054-ca-org1.pem
├── config.yaml
├── keystore
│ └── 58e81e6f1ee8930df46841bf88c22a08ae53c1332319854608539ee78ed2fd65_sk
├── signcerts
│ └── cert.pem
└── user

KhoaCNTT-Trường ĐHBK, ĐHĐN


You can find the certificate of the admin user in the signcerts folder and the private
key in the keystore folder.
Both cryptogen and the Fabric CAs generate the cryptographic material for each
organization in the organizations folder. You can find the commands that are used to set
up the network in the registerEnroll.sh script in the organizations/fabric-ca directory.

5. Bring down the network


When you are finished using the test network, you can bring down the network with
the following command./network.sh down. The command will stop and remove the node
and chaincode containers, delete the organization crypto material, and remove the
chaincode images from your Docker Registry. The command also removes the channel
artifacts and docker volumes from previous runs, allowing you to
run ./network.sh up again if you encountered any problems.

6. What’s happening behind the scenes?


If you are interested in learning more about the sample network, you can investigate
the files and scripts in the test-network directory. The steps below provide a guided tour of
what happens when you issue the command of ./network.sh up .
 ./network.sh creates the certificates and keys for two peer organizations and the
orderer organization. By default, the script uses the cryptogen tool using the
configuration files located in the organizations/cryptogen folder. If you use the -
ca flag to create Certificate Authorities, the script uses Fabric CA server
configuration files and registerEnroll.sh script located in
the organizations/fabric-ca folder. Both cryptogen and the Fabric CAs create the
crypto material and MSP folders for all three organizations in
the organizations folder.
 Once the organization crypto material has been generated, the network.sh can
bring up the nodes of the network. The script uses the docker-compose-test-
net.yaml file in the docker folder to create the peer and orderer nodes.
The docker folder also contains the docker-compose-e2e.yaml file that brings up the
nodes of the network alongside three Fabric CAs. This file is meant to be used
to run end-to-end tests by the Fabric SDK. Refer to the Node SDK repo for
details on running these tests.
 If you use the createChannel subcommand, ./network.sh runs
the createChannel.sh script in the scripts folder to create a channel using the
supplied channel name. The script uses the configtxgen tool to create the channel
genesis block based on the TwoOrgsApplicationGenesis channel profile in
the configtx/configtx.yaml file. After creating the channel, the script uses the peer

KhoaCNTT-Trường ĐHBK, ĐHĐN


cli to join peer0.org1.example.com and peer0.org2.example.com to the channel, and
make both of the peers anchor peers.
 If you issue the deployCC command, ./network.sh runs the deployCC.sh script to
install the asset-transfer (basic) chaincode on both peers and then define then
chaincode on the channel. Once the chaincode definition is committed to the
channel, the peer cli initializes the chaincode using the Init and invokes the
chaincode to put initial data on the ledger.

7. REFERENCE
[1]. https://hyperledger-fabric.readthedocs.io/en/latest/test_network.html

-----------------------------------------------

KhoaCNTT-Trường ĐHBK, ĐHĐN

You might also like