Hyperledger是Linux 基金會主導的一個開源的區塊鏈(BlockChain)項目. 本文介紹了一個簡單的Hyperledger智能合約的開發過程.node
本文使用Docker做爲Hyperledger智能合約的本地開發環境.git
從Docker官網http://www.docker.com/
下載其安裝包,並安裝Docker.github
啓動Docker QuickStarter Terminal並運行以下命令在Docker中安裝Hyperledger Fabric.docker
docker pull hyperledger/fabric-peer:latest docker pull hyperledger/fabric-membersrvc:latest
在工做目錄下建立以下的docker-compose.yml
json
membersrvc: image: hyperledger/fabric-membersrvc ports: - "7054:7054" command: membersrvc vp0: image: hyperledger/fabric-peer ports: - "7050:7050" - "7051:7051" - "7053:7053" environment: - CORE_PEER_ADDRESSAUTODETECT=true - CORE_VM_ENDPOINT=unix:///var/run/docker.sock - CORE_LOGGING_LEVEL=DEBUG - CORE_PEER_ID=vp0 - CORE_PEER_PKI_ECA_PADDR=membersrvc:7054 - CORE_PEER_PKI_TCA_PADDR=membersrvc:7054 - CORE_PEER_PKI_TLSCA_PADDR=membersrvc:7054 - CORE_SECURITY_ENABLED=true - CORE_SECURITY_ENROLLID=test_vp0 - CORE_SECURITY_ENROLLSECRET=MwYpmSRjupbT links: - membersrvc command: sh -c "sleep 5; peer node start --peer-chaincodedev"
而後在Docker QuickStarter Terminal中運行如下命令以啓動Hyperledger Fabricbash
docker-compose up
安裝並運行SSH客戶端,例如putty或mRemoteNG, 使用如下信息鏈接Docker區塊鏈
host : 192.168.99.100 user name : docker password : tcuser
運行以下命令以確認Hyperledger Fabric的進程ui
docker ps
運行以下命令以進入Hyperledger Fabric環境3d
docker exec -it hyperledger_vp0_1 bash
運行以下命令從git下載Hyperledger Fabric源代碼unix
mkdir -p $GOPATH/src/github.com/hyperledger cd $GOPATH/src/github.com/hyperledger git clone http://gerrit.hyperledger.org/r/fabric
運行以下命令建立HelloWorld目錄
mkdir -p $GOPATH/src/github.com/HelloWorld/ cd $GOPATH/src/github.com/HelloWorld/
建立以下HelloWorld.go文件
package main import ( "errors" "fmt" "strconv" "github.com/hyperledger/fabric/core/chaincode/shim" ) type HelloWorldChaincode struct { } func (t *HelloWorldChaincode) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) { fmt.Printf("HelloWorld - Init called with function %s!\n", function) return nil, nil } func (t *HelloWorldChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) { fmt.Printf("HelloWorld - Invoke called with function %s!\n", function) return nil, nil } func (t *HelloWorldChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) { fmt.Printf("HelloWorld - Query called with function %s!\n", function) message := "Hello World" return []byte(message), nil; } func main() { err := shim.Start(new(HelloWorldChaincode)) if err != nil { fmt.Printf("Error starting Hello World chaincode: %s", err) } }
使用以下命令編譯代碼
go build ./
使用以下命令運行HelloWorld
export CORE_CHAINCODE_ID_NAME=mycc export CORE_PEER_ADDRESS=0.0.0.0:7051 ./HelloWorld &
向Hyperledger服務接口http://192.168.99.100:7050/registrar
發送以下REST請求,使用內置的jim用戶登陸系統
{ "enrollId": "jim", "enrollSecret": "6avZQLwcUe9b" }
向Hyperledger服務接口http://192.168.99.100:7050/chaincode
發送以下初始化HelloWorld的REST請求
{ "jsonrpc": "2.0", "method": "deploy", "params": { "type": 1, "chaincodeID":{ "name": "mycc" }, "ctorMsg": { "function":"init", "args":[] }, "secureContext": "jim" }, "id": 1 }
調用HelloWorld的REST請求
{ "jsonrpc": "2.0", "method": "invoke", "params": { "type": 1, "chaincodeID":{ "name":"mycc" }, "ctorMsg": { "function":"invoke", "args":[] }, "secureContext": "jim" }, "id": 3 }
以及查詢HelloWorld的REST請求
{ "jsonrpc": "2.0", "method": "query", "params": { "type": 1, "chaincodeID":{ "name":"mycc" }, "ctorMsg": { "function":"query", "args":[] }, "secureContext": "jim" }, "id": 5 }
本文介紹了一個簡單的Hyperledger智能合約在本地Docker環境下的開發過程.