Lab03 Exploring Test-Network With Hyperledger Fabric

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Lab Blockchain Hyperledger

Exploring test-network with Hyperledger Fabric

Họ và tên Sinh viên:


Mã Sinh viên: Nhóm:

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


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

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
ubuntu@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ách 2: Sử dụng lệnh trong cửa sổ lệnh Windows:
C:\Users\ABC>ssh -p 15 ubuntu@wandertour.ddns.net

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


 Cách 3: Tải về máy ảo VMHyper 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 22, username: ubuntu, 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.
 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) ubuntu@vmhyper:~/fabric-samples/test-network$ ./network.sh down
2) ubuntu@vmhyper:~/fabric-samples/test-network$ ./network.sh up createChannel
3) ubuntu@vmhyper:~/fabric-samples/test-network$ ./network.sh deployCC -ccn basic -
ccp ../asset-transfer-basic/chaincode-go -ccl go

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


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).

4. Interacting with the network


ubuntu@vmhyper:~$ clear
ubuntu@vmhyper~ $ ls $HOME/fabric-samples/

ubuntu@vmhyper~ $ cd $HOME/fabric-samples/test-network

# Environment variables for Org1


ubuntu@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

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.
KhoaCNTT-Trường ĐHBK, ĐHĐN
The CORE_PEER_TLS_ROOTCERT_FILE and CORE_PEER_MSPCONFIGPATH environment variables
point to the Org1 crypto material in the organizations folder.
ubuntu@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

 View the paths:


ubuntu@vmhyper:~/fabric-samples/test-network$ ls $CORE_PEER_TLS_ROOTCERT_FILE
ubuntu@vmhyper:~/fabric-samples/test-network$ ls $CORE_PEER_MSPCONFIGPATH
ubuntu@vmhyper:~/fabric-samples/test-network$ echo
{PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/
msp/tlscacerts/
ubuntu@vmhyper:~/fabric-samples/test-network$ echo
${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.co
m/tls/

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

ubuntu@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.co
m/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles
"${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.co
m/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:
ubuntu@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},

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


{"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}
]

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:

ubuntu@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":"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

ubuntu@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":"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

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


target both peer0.org1.example.com and peer0.org2.example.com using the --
peerAddresses flag. 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


ubuntu@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.co
m/users/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051

Query the asset-transfer (basic) chaincode running on peer0.org2.example.com :


ubuntu@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":80
0}

Examine the MSP folder of the Org1 admin user:


ubuntu@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. REFERENCE
[1]. https://hyperledger-fabric.readthedocs.io/en/latest/test_network.html

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

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

You might also like