歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> PERL編程 >> Hyperledger智能合約Hello World示例程序

Hyperledger智能合約Hello World示例程序

日期:2017/3/1 9:10:22   编辑:PERL編程

簡介

Hyperledger是Linux 基金會主導的一個開源的區塊鏈(BlockChain)項目. 本文介紹了一個簡單的Hyperledger智能合約的開發過程.

開發環境

本文使用Docker作為Hyperledger智能合約的本地開發環境.

第一步

從Docker官網http://www.docker.com/下載其安裝包,並安裝Docker.

第二步

啟動Docker QuickStarter Terminal並運行如下命令在Docker中安裝Hyperledger Fabric.

docker pull hyperledger/fabric-peer:latest
docker pull hyperledger/fabric-membersrvc:latest

第三步

在工作目錄下創建如下的docker-compose.yml

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 Fabric

docker-compose up

第四步

安裝並運行SSH客戶端,例如putty或mRemoteNG, 使用以下信息連接Docker

host : 192.168.99.100
user name : docker
password : tcuser

第五步

運行如下命令以確認Hyperledger Fabric的進程

docker ps

運行如下命令以進入Hyperledger Fabric環境

docker exec -it hyperledger_vp0_1 bash

第六步

運行如下命令從git下載Hyperledger Fabric源代碼

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環境下的開發過程.

Copyright © Linux教程網 All Rights Reserved