简介
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环境下的开发过程.