This is an update. There were many mistakes in the last write-up. Let me update it this time!
Here are the references:
Try chain code writing and deployment testing_routiao's blog-CSDN blog
routiao's blog_CSDN blog - blockchain, blogger in the front-end field
test fabric-samples/test-network
1. Start the docker network
First we start the test example
cd fabric-samples/test-network
Then first remove all containers that were previously running
sudo ./network.sh down
then turn the network back on
sudo ./network.sh up
You can check to see if it is enabled with the command:
sudo docker ps -a
We can take a closer look at the results obtained after the above command:
2. Create a channel
Channel is a private layer for communication between members of a specific network. Only members who are invited to join the Channel can use it, and it is invisible to other members in the network. Each Channel has a separate blockchain ledger, which is invited Peers joining the Channel can store the Channel's ledger and then verify the Channel's transactions.
Run the following command to create a Channel named mychannel to connect Org1 and Org2.
sudo ./network.sh createChannel
When this appears, the channel has been created
Note: There may be many permission problems during the test. If permission denied appears, then you need to open the permission of the file, which will be described in detail in the next article.
Add a permission for the user to facilitate the operation of the entire fabric folder
sudo chmod -R 777 ~/go
At the same time, since it is also necessary to add opt permissions to the user
sudo chmod -R 777 /opt
To interact with the network:
After the test network is successfully started, the peer command on the command line can be used to interact with the network, which allows users to call deployed smart contracts, update channels or install and deploy new smart contracts.
First of all, you need to set it up. There are binary files such as peer in the bin directory, and several yaml files in the config directory. If necessary, you can use the following command to add peer, etc. to the environment variables:
First make sure you are in the test-network: Configure temporary variables: (If you do not configure here, you cannot use the peer command)
export PATH=${PWD}/../bin:$PATH
In addition, you need to configure the directory where the config is located.
export FABRIC_CFG_PATH=$PWD/../config/
We also need to set the go environment
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
In this way, the chaincode can be deployed. Note that sudo cannot be used here, otherwise it will cause problems in the environment.
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
If this happens, the chaincode deployment is successful.
We can also see in the file that if the chaincode is deployed, the system will create a jar package named basic.tar.gz under the test-network folder
3. Smart contract call
After the chain code is deployed, we can call the smart contract
Initialize the identity of the member as organization 1
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/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051
Initialize ledger
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":[]}'
After a successful run it will appear:
We then query for a list of assets that have been added to the channel ledger:
peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
View the effect:
It can be found that we have indeed queried the data, and carefully found that these data are actually written in the smart contract. We can find the smart contract called /asset-transfer-basic/chaincode-go/chaincode/smartcontract.go
Check out the method InitLedger to initialize the ledger is written like this
Then we will get this data after calling
[
{"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}
]
Next we use different methods in the smart contract.
4. Call different methods in the contract
These smart contracts are written in /asset-transfer-basic/chaincode-go/chaincode/smartcontract.go,
Let's open it up to see what he can do
4.1 Add assets
Look at the code in the go file:
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":"CreateAsset","Args":["asset7","pink","10","XZDD","16402"]}'
Implementation:
4.2 Delete assets
Smart contract:
implement:
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":"DeleteAsset","Args":["asset6"]}'
Implementation:
View information:
It can be found that the following record has disappeared
{"ID": "asset6", "color": "white", "size": 15, "owner": "Michel", "appraisedValue": 800}
4.3 Modify assets
Smart contract:
implement:
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":"UpdateAsset","Args":["asset7","RED","9","XZDD","521521"]}'
Implementation:
View information:
4.4 Transfer of assets
Smart contract:
implement:
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":["asset5","XZDD"]}'
Implementation:
View information:
You can check that the assets have been transferred to XZDD, and now XZDD is the owner.
4.5 View specific assets
Smart contract:
implement:
peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset7"]}'
Implementation:
5. Switch identity
In the past, organization 1 was used to verify code additions, deletions, modifications, and queries. Now we can open another terminal and use organization 2 to verify functions, only need to change the environment variables!
export PATH=${PWD}/../bin:$PATH
export CORE_PEER_TLS_ENABLED=true
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.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:9051
The specific operation will not be repeated here, you can try to open two bash windows, initialize to different identities, one window is used for asset change operations, and the other window is used for asset query operations to test the synchronization effect of the blockchain.