本篇博客主要是向讀者介紹 fabric 在部署時的一些細節,還有做者本身學習過程當中的心得。git
generateArtifacts.sh 腳本里面主要執行了三個函數github
generateCerts # 這個是初始化組織證書和密鑰的操做
replacePrivateKey # 這個函數是生成一個docker-compose 腳本,沒有啥用
generateChannelArtifacts # 這個是定義組織機構和初始化創世塊的操做
給chaincode_example02.go chaincode 增長一個test 的功能,打開對應的文件,將Invoke 函數進行修改,修改後的內容docker
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response { fmt.Println("ex02 Invoke") function, args := stub.GetFunctionAndParameters() if function == "invoke" { // Make payment of X units from A to B return t.invoke(stub, args) } else if function == "delete" { // Deletes an entity from its state return t.delete(stub, args) } else if function == "query" { // the old "Query" is now implemtned in invoke return t.query(stub, args) } else if function == "test" { return t.test (stub, args) } return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"delete\" \"query\"") }
增長了一個test 的方法,test 函數代碼shell
func (t *SimpleChaincode) test (stub shim.ChaincodeStubInterface, args []string) pb.Response { var V string var Vval, Rval int var err error if len(args) != 2 { return shim.Error("Incorrect number of arguments. Expecting 2") } V = args[0] Rval, err = strconv.Atoi(args[1]) if err != nil { return shim.Error("Expecting integer value for asset holding") } if Rval == 123 { Vvalbytes, err := stub.GetState(V) if err != nil { return shim.Error("Failed to get state") } if Vvalbytes == nil { return shim.Error("Entity not found") } Vval, _ = strconv.Atoi(string(Vvalbytes)) Vval = Vval + 10 // Write the state back to the ledger err = stub.PutState(V, []byte(strconv.Itoa(Vval))) if err != nil { return shim.Error(err.Error()) } } return shim.Success(nil) }
讀者給peer 從新安裝chaincode 後,能夠在cli 中執行如下命令,實現給對應的用戶增長10 塊錢 bash
peer chaincode invoke -o orderer.example.com:7050 --tls true --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 -c '{"Args":["test","a","123"]}'
例如:在cli 容器上查詢a 帳戶的餘額函數
docker exec -it cli peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
用戶也能夠利用 docker-compose 來執行該方法,可是須要先準備docker-compose 的cli配置文件,用戶能夠模仿 docker-compose-cli.yaml 文件進行編寫,例如做者編寫了一個名字叫「docker-compose-test.yaml」的配置文件。學習
這種方式有一個好處,就是執行的宿主機器,不須要知道以前是否已經啓動了相關的docker 服務,由於執行docker-compose 命令時 ,會臨時啓動相應的docker 服務,執行完對應的shell 命令後,又會自動將其刪除,真正的作到即用即起的效果。測試
# Copyright IBM Corp. All Rights Reserved. # # SPDX-License-Identifier: Apache-2.0 # version: '2' services: cli: container_name: cli image: hyperledger/fabric-tools tty: true environment: - GOPATH=/opt/gopath - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock - CORE_LOGGING_LEVEL=DEBUG - CORE_PEER_ID=cli - CORE_PEER_ADDRESS=peer0.org1.example.com:7051 - CORE_PEER_LOCALMSPID=Org1MSP - CORE_PEER_TLS_ENABLED=true - CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt - CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key - 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 working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer # command: /bin/bash -c './scripts/script.sh ${CHANNEL_NAME}; sleep $TIMEOUT' volumes: - /var/run/:/host/var/run/ - ../chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ - ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/ - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
仍是以查詢a帳號餘額爲例spa
docker-compose -f docker-compose-test.yaml run --rm --no-deps cli peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'