开发环境介绍

仅以我自己的环境为例

  • UBUNTU 16.04 LTS虚拟机
  • docker
  • docker-compose
  • git

docker和docker-compose的安装

省略,大家自行搜索

golang安装

这里虽然网上也有很多,但是我在安装的过程中遇到很多坑,所以把过程写出来。

apt-get命令安装只能安装go1.6版本现在很多开源项目都用上了1.7以,所以需要手动安装更高版本。

下载地址:

https://www.golangtc.com/download

我下载的是go1.8.3.linux-amd64.tar.gz,拷贝到/usr/local目录下。

解压

sudo tar -zxf go1.8.3.linux-amd64.tar.gz

解压后,出现名为go的目录。

在/root/下新建gopath目录, 并且新增src, pkg以及bin三个子目录。

编辑/etc/profile文件,新增,

export GOPATH=$HOME/gopath
export GOROOT=/usr/local/go
export PATH=$GOROOT/bin:$GOPATH/bin:$PATH

source /etc/profile

这时在root用户下输入go env命令,查看go环境变量

root@pony-virtual-machine:~/gopath/src/github.com/hyperledger/fabric# go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/gopath"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
...

查看go的版本,

root@pony-virtual-machine:~/gopath/src/github.com/hyperledger/fabric# go version
go version go1.8.3 linux/amd64

测试下,在gopath目录下,新建hello.go文件,测试代码:

package main

import "fmt"

func main() {
    fmt.Printf("Hello World!\n")
}
root@pony-virtual-machine:~/gopath/src# go run hello.go 
Hello World!

fabric 环境搭建

后面所有的流程都在root目录下,我试过在/home/pony目录下执行下面的流程,会报如下错误:

Creating channel...
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_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_LOCALMSPID=Org1MSP
CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
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_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
CORE_PEER_ID=cli
CORE_LOGGING_LEVEL=DEBUG
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
2017-09-26 08:56:00.223 UTC [main] main -> ERRO 001 Cannot run peer because cannot init crypto, missing /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp folder
!!!!!!!!!!!!!!! Channel creation failed !!!!!!!!!!!!!!!!
================== ERROR !!! FAILED to execute End-2-End Scenario ==================

如果到root下还是报错,重启下机器试试

新建目录

root@pony-virtual-machine:~/gopath/src/github.com/hyperledger$ pwd
/root/gopath/src/github.com/hyperledger

切换到这个目录fabric源码

git clone https://github.com/hyperledger/fabric.git

由于Fabric一直在更新,所有我们并不需要最新最新的源码,需要切换到v1.0.0版本的源码即可:

git checkout v1.0.0

下载fabric docker镜像

root@pony-virtual-machine:~/gopath/src/github.com/hyperledger/fabric/examples/e2e_cli$ source download-dockerimages.sh -c x86_64-1.0.0 -f x86_64-1.0.0

docker images命令,可以看到多了一些docker镜像:

hyperledger/fabric-tools       latest              85d6d3ca0a30        3 months ago        1.32GB
hyperledger/fabric-tools       x86_64-1.0.0-rc1    85d6d3ca0a30        3 months ago        1.32GB
hyperledger/fabric-couchdb     latest              b37a08f8a0cb        3 months ago        1.48GB
hyperledger/fabric-couchdb     x86_64-1.0.0-rc1    b37a08f8a0cb        3 months ago        1.48GB
hyperledger/fabric-kafka       latest              dbb5796d915f        3 months ago        1.3GB
hyperledger/fabric-kafka       x86_64-1.0.0-rc1    dbb5796d915f        3 months ago        1.3GB
hyperledger/fabric-zookeeper   latest              d2efbb01a999        3 months ago        1.31GB
hyperledger/fabric-zookeeper   x86_64-1.0.0-rc1    d2efbb01a999        3 months ago        1.31GB
hyperledger/fabric-orderer     latest              5ec6333eae10        3 months ago        179MB
hyperledger/fabric-orderer     x86_64-1.0.0-rc1    5ec6333eae10        3 months ago        179MB
hyperledger/fabric-peer        latest              cf6bedc2935e        3 months ago        182MB
hyperledger/fabric-peer        x86_64-1.0.0-rc1    cf6bedc2935e        3 months ago        182MB
hyperledger/fabric-javaenv     latest              7cbe6aca3956        3 months ago        1.42GB
hyperledger/fabric-javaenv     x86_64-1.0.0-rc1    7cbe6aca3956        3 months ago        1.42GB
hyperledger/fabric-ca          latest              cea779a46670        3 months ago        238MB
hyperledger/fabric-ca          x86_64-1.0.0-rc1    cea779a46670        3 months ago        238MB
hyperledger/fabric-ccenv       latest              13ed472038d2        3 months ago        1.29GB
hyperledger/fabric-ccenv       x86_64-1.0.0-rc1    13ed472038d2        3 months ago        1.29GB

在e2e_cli文件夹,这里提供了启动、关闭Fabric网络的自动化脚本。我们要启动Fabric网络,并自动运行Example02 ChainCode的测试,执行一个命令:

./network_setup.sh up

做了以下操作:

  • 编译生成Fabric公私钥、证书的程序,程序在目录:fabric/release/linux-amd64/bin

  • 基于configtx.yaml生成创世区块和通道相关信息,并保存在channel-artifacts文件夹。

  • 基于crypto-config.yaml生成公私钥和证书信息,并保存在crypto-config文件夹中。

  • 基于docker-compose-cli.yaml启动1Orderer+4Peer+1CLI的Fabric容器。

  • 在CLI启动的时候,会运行scripts/script.sh文件,这个脚本文件包含了创建Channel,加入Channel,安装Example02,运行Example02等功能。

最后运行完毕,我们可以看到这样的界面:

image

首先进入CLI容器,该容器为我们提供了和chaincode交互的命令环境,和我们用sdk操作是一样的道理。我们重新打开一个命令行窗口,输入:

docker exec -it cli bash

在容器里查询a账户的余额:

root@f3d3e70b2dbd:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
2017-09-26 10:34:23.957 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2017-09-26 10:34:23.957 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2017-09-26 10:34:23.957 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2017-09-26 10:34:23.957 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2017-09-26 10:34:23.957 UTC [msp/identity] Sign -> DEBU 005 Sign: plaintext: 0A91070A6708031A0C08AFDAA8CE0510...6D7963631A0A0A0571756572790A0161 
2017-09-26 10:34:23.957 UTC [msp/identity] Sign -> DEBU 006 Sign: digest: 2BC4334C2D16FA8B87C81CC5CEE8CBE0866478DB56BEDE5A8FB041485DA00AF6 
Query Result: 90
2017-09-26 10:34:23.985 UTC [main] main -> INFO 007 Exiting.....

余额是90。

命令的参数 -C, -n等是啥意思呢,哪里可以查询到他们的意思,还有就是都有哪些命令

CLI

我们试一试把a账户的余额再转20元给b账户,运行命令:

root@f3d3e70b2dbd:/opt/gopath/src/github.com/hyperledger/fabric/peer# 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":["invoke","a","b","20"]}'
2017-09-26 10:41:20.945 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2017-09-26 10:41:20.945 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2017-09-26 10:41:20.950 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2017-09-26 10:41:20.950 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2017-09-26 10:41:20.951 UTC [msp/identity] Sign -> DEBU 005 Sign: plaintext: 0A91070A6708031A0C08D0DDA8CE0510...696E766F6B650A01610A01620A023230 
2017-09-26 10:41:20.951 UTC [msp/identity] Sign -> DEBU 006 Sign: digest: E93D45C18677F2A46E72D3C088F0EF8B294D20E444FF9A6C9545B1F5C09C31AC 
2017-09-26 10:41:20.967 UTC [msp/identity] Sign -> DEBU 007 Sign: plaintext: 0A91070A6708031A0C08D0DDA8CE0510...1B158F2AEB7783FA6631D8BD80135795 
2017-09-26 10:41:20.967 UTC [msp/identity] Sign -> DEBU 008 Sign: digest: DF942C5FE8FE2484CF3E8391A01D4991884B12A08702D847F8435217C0782DA1 
2017-09-26 10:41:20.971 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> DEBU 009 ESCC invoke result: version:1 response:<status:200 message:"OK" > payload:"\n y0e\353\267_[Z\254\240\004G\3538\304)4x\345\324\031\215\244\306\266\302\260\364\353m\251\270\022Y\nE\022\024\n\004lscc\022\014\n\n\n\004mycc\022\002\010\003\022-\n\004mycc\022%\n\007\n\001a\022\002\010\004\n\007\n\001b\022\002\010\004\032\007\n\001a\032\00270\032\010\n\001b\032\003230\032\003\010\310\001\"\013\022\004mycc\032\0031.0" endorsement:<endorser:"\n\007Org1MSP\022\200\006-----BEGIN -----\nMIICGTCCAcCgAwIBAgIRAIsLKFecYFZbXqftZKxTIRwwCgYIKoZIzj0EAwIwczEL\nMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBG\ncmFuY2lzY28xGTAXBgNVBAoTEG9yZzEuZXhhbXBsZS5jb20xHDAaBgNVBAMTE2Nh\nLm9yZzEuZXhhbXBsZS5jb20wHhcNMTcwOTI2MTAxODIxWhcNMjcwOTI0MTAxODIx\nWjBbMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMN\nU2FuIEZyYW5jaXNjbzEfMB0GA1UEAxMWcGVlcjAub3JnMS5leGFtcGxlLmNvbTBZ\nMBMGByqGSM49AgEGCCqGSM49AwEHA0IABLus0hMQH/Htb2YdMSRmjG+fyuVokpIg\ng2be6roCHvktnl9bHt60qEIrRwOpbWQtRWGv16hkw+VMGTSrgtqSuq2jTTBLMA4G\nA1UdDwEB/wQEAwIHgDAMBgNVHRMBAf8EAjAAMCsGA1UdIwQkMCKAIFZuFGU237Wl\nXiBK//ppV06NH/pwobye5GUTINE1AUOLMAoGCCqGSM49BAMCA0cAMEQCIEB+HrH9\nn1SfuyOuQJpywztnNGzSRW92wqAy9b5ZB/71AiBj9lFGcaENqurZ5yo86epk9HDm\nOnYmXD7h7rJD/oEvYQ==\n-----END -----\n" signature:"0E\002!\000\271\204?G\017\227\237\270p\n\210\n\205\244\366{T\233\031\266\220a\010\256O12\267mk^\002\002 f3M\032\252\304r\"\023\204\276@\256x'\326\033\025\217*\353w\203\372f1\330\275\200\023W\225" > 
2017-09-26 10:41:20.972 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 00a Chaincode invoke successful. result: status:200 
2017-09-26 10:41:20.973 UTC [main] main -> INFO 00b Exiting.....

转账完毕, 我们试一试再查询一下a账户的余额,没问题的话,应该是只剩下70了,

root@f3d3e70b2dbd:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'2017-09-26 10:43:05.300 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2017-09-26 10:43:05.300 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2017-09-26 10:43:05.300 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2017-09-26 10:43:05.300 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2017-09-26 10:43:05.301 UTC [msp/identity] Sign -> DEBU 005 Sign: plaintext: 0A91070A6708031A0C08B9DEA8CE0510...6D7963631A0A0A0571756572790A0161 
2017-09-26 10:43:05.301 UTC [msp/identity] Sign -> DEBU 006 Sign: digest: 2EEC7E22151A2F97A46BED2E0AF5687C0CE02F2C6AE3535B819A8AECDF4A2B8E 
Query Result: 70
2017-09-26 10:43:05.312 UTC [main] main -> INFO 007 Exiting.....
root@f3d3e70b2dbd:/opt/gopath/src/github.com/hyperledger/fabric/peer# 

退出容器,用exit命令,然后关闭fabric。

./network_setup.sh down

参考

1.http://www.cnblogs.com/studyzy/p/7437157.html

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐