超级账本Hyperledger Fabric的使用

本文详细介绍了超级账本Hyperledger Fabric的使用,包括Fabric的概念、部署示例、Fabric CA的使用、智能合约的开发与管理、Fabric SDKs、编译过程以及常见问题汇总。内容涵盖从创建创世区块到合约实例化、调用的全过程。
摘要由CSDN通过智能技术生成

原文地址:超级账本Hyperledger Fabric的使用

 

说明

网易云课堂:HyperLedger Fabric手动部署教程的视频讲解

超级账本HyperLedger Fabric手动部署教程的文字实录(公开)

Hyperledger是Linux基金会在2015年发起的项目,目的是推进区块链技术的开发应用。Hyperledger收录了多个区块链项目。

区块链技术平台:

Sawtooth
Iroha
Fabric 
Burrow
Indy

区块链工具:

Cello
Composer
Explorer
Quilt

Farbric

Fabric是一个支持智能合约(smart contracts)的分布式账本(ledger)系统。

Fabric是私有的,只有通过MSP(Membership Service Provider)认证的成员才可以进入到系统,参与合约的缔造与执行。

Fabric是插件式设计,账本数据有多种存放方式,共识机制(consensus mechanisms)可以切换,支持不同类型的MSP。

Fabric开发了channel功能,一个channel对应一个账本,只有加入channel的成员可见,可以防止竞争对手知晓交易的细节。

账本由两部分组成:全局状态(word state)和交易日志(transaction log)。 全局状态中记录的是当前状态,交易日志中记录了所有的交易记录,全局状态是这些交易记录的执行结果。

智能合约(Smart Contracts)用chaincode编写,由区块链外部的应用调用执行,chaincode通常是用来更新账本的。

Farbric的chaincode目前(2018-02-23 15:08:54)只支持Go语言,以后会支持Java和其它的语言。

达成共识的过程中,交易需要严格按照发生的顺序记录到账本中,Farbric提供了多种共识机制(SOLO、Kafka、SBFT…),建立交易网络的时候根据实际需要选用共识机制。

概念

Fabric的模型主要由以下几个概念组成:

Assets:           交易的资产
ChainCode:        描述交易逻辑的代码 
Ledger Features:  账本功能
Privacy through Channels: channel的私密性,可以对全网开发,也可以只对部分成员开放
                          包含交易逻辑的ChainCode可以只部署在特定用户端,实现部分公开的效果
                          还可以在ChainCode中对数据进行加密
Security & Membership Services: 参与交易的用户都经过认证的可信用户
Consensus:  交易从发起到被提交到账本的过程中的检验

部署示例

下面使用的fabric提供的一个部署示例,这个例子中会创建一个由4个peer组成的fabric网络。

部署要求:

docker版本不低于17.06.2
docker-compose版本不低于1.14.0
go版本1.9.x
如果用node.js开发应用,node.js版本不低于8.9.x

下载示例:

git clone https://github.com/hyperledger/fabric-samples.git
cd fabric-samples/first-network/

下载fabric的命令文件,-s指定要安装的版本:

mkdir -p /opt/fabric/1.0.6 
cd /opt/fabric/1.0.6 
curl -sSL https://goo.gl/6wtTN5 | bash -s 1.0.6   #这个网址需要翻墙访问
export PATH=$PATH:/opt/fabric/1.0.6/bin

执行结束后,在/opt/fabr /1.0.6/bin目录中可以看到以下文件:

configtxgen             //用于生成配置文件,存放在channel-artifacts目录中
configtxlator
cryptogen               //用于为网络的参与者生成证书,存放在crypto-config目录中
get-byfn.sh
get-docker-images.sh
orderer
peer

上面的命令执行时还会下载9个镜像,这9个镜像构成了fabric系统。

hyperledger/fabric-tools    
hyperledger/fabric-orderer  
hyperledger/fabric-peer     
hyperledger/fabric-javaenv  
hyperledger/fabric-ca       
hyperledger/fabric-ccenv    
hyperledger/fabric-zookeeper
hyperledger/fabric-kafka    
hyperledger/fabric-couchdb  

到fabric-samples/first-network/目录中执行byfn.sh,byfn.sh脚本可以用来创建一个由4个peer(分属2个组织)组成的网络

$./byfn.sh -h
Usage:
  byfn.sh -m up|down|restart|generate [-c <channel name>] [-t <timeout>] [-d <delay>] [-f <docker-compose-file>] [-s <dbtype>] [-i <imagetag>]
  byfn.sh -h|--help (print this message)
    -m <mode> - one of 'up', 'down', 'restart' or 'generate'
      - 'up' - bring up the network with docker-compose up
      - 'down' - clear the network with docker-compose down
      - 'restart' - restart the network
      - 'generate' - generate required certificates and genesis block
    -c <channel name> - channel name to use (defaults to "mychannel")
    -t <timeout> - CLI timeout duration in microseconds (defaults to 10000)
    -d <delay> - delay duration in seconds (defaults to 3)
    -f <docker-compose-file> - specify which docker-compose file use (defaults to docker-compose-cli.yaml)
    -s <dbtype> - the database backend to use: goleveldb (default) or couchdb
    -i <imagetag> - pass the image tag to launch the network using the tag: 1.0.1, 1.0.2, 1.0.3, 1.0.4 (defaults to latest)

Typically, one would first generate the required certificates and
genesis block, then bring up the network. e.g.:

    byfn.sh -m generate -c mychannel
    byfn.sh -m up -c mychannel -s couchdb
    byfn.sh -m up -c mychannel -s couchdb -i 1.0.6
    byfn.sh -m down -c mychannel

Taking all defaults:
    byfn.sh -m generate
    byfn.sh -m up
    byfn.sh -m down

创建网络:

./byfn.sh -m generate      #准备证书文件等
./byfn.sh -m up            #启动网络

执行完成后,会启动8个容器,其中6个用first-network/docker-compose-cli.yaml启动的容器:

orderer.example.com         # 用于形成共识 
peer0.org1.example.com      # 成员org1.example.com的第一个peer
peer1.org1.example.com      # 成员org1.example.com的第二个peer
peer0.org2.example.com      # 成员org2.example.com的第一个peer
peer1.org2.example.com      # 成员org2.example.com的第二个peer
cli                         # 命令行工具,启动后sleep一段时间后退出

另外三个是运行智能合约的容器:

dev-peer1.org2.example.com-mycc-1.0
dev-peer0.org1.example.com-mycc-1.0
dev-peer0.org2.example.com-mycc-1.0

一个容器是在创建合约后,实例化时创建的,两个是指定peer进行查询、交易时,需要智能合约的时候创建的。

另外有一个名为cli的容器会在启动10秒钟后自动退出:

docker start cli

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
CORE_PEER_LOCALMSPID="Org1MSP"
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

fabric ca

fabric ca用来管理fabric的用户,即用户证书的签署和撤销等。

启动fabric ca server启动后,可以直接用fabirc-ca-client创建、撤销证书。

fabric-ca-server init -b admin:adminpw

详情参考hyperleader fabric-ca

编译:

git clone https://github.com/hyperledger/fabric-ca.git
cd fabric-ca
make

启动fabric-ca-server:

$ fabric-ca-server init -b  admin:pass
$ fabric-ca-server start -b  admin:pass
2018/04/02 10:09:58 [INFO] Configuration file location: /opt/app/fabric/ca/fabric-ca-server-config.yaml
2018/04/02 10:09:58 [INFO] Starting server in home directory: /opt/app/fabric/ca
2018/04/02 10:09:58 [INFO] Server Version: 1.1.1-snapshot-d536f5a
2018/04/02 10:09:58 [INFO] Server Levels: &{Identity:1 Affiliation:1 Certificate:1}
2018/04/02 10:09:58 [INFO] The CA key and certificate already exist
2018/04/02 10:09:58 [INFO] The key is stored by BCCSP provider 'SW'
2018/04/02 10:09:58 [INFO] The certificate is at: /opt/app/fabric/ca/ca-cert.pem
2018/04/02 10:09:58 [INFO] Initialized sqlite3 database at /opt/app/fabric/ca/fabric-ca-server.db
2018/04/02 10:09:58 [INFO] Home directory for default CA: /opt/app/fabric/ca
2018/04/02 10:09:58 [INFO] Listening on http://0.0.0.0:7054

注册用户admin,密码为pass:

export FABRIC_CA_CLIENT_HOME=/opt/app/fabric-ca/clients/admin
mkdir -p $FABRIC_CA_CLIENT_HOME
fabric-ca-client enroll -u http://admin:pass@localhost:7054

用户注册后会在$FABRIC_CA_CLIENT_HOME目录中,生成该用户的msp文件。

登记一个新的管理员admin2:

$ export FABRIC_CA_CLIENT_HOME=/opt/app/fabric-ca/clients/admin
$ fabric-ca-client register --id.name admin2 --id.affiliation org1.department1 --id.attrs 'hf.Revoker=true,admin=true:ecert'
2018/04/02 10:24:10 [INFO] Configuration file location: /opt/app/fabric-ca/clients/admin/fabric-ca-client-config.yaml
2018/04/02 10:24:11 [INFO] 127.0.0.1:37738 POST /register 201 0 "OK"
Password: yRPoQHgvRMfv

admin2的密码为yRPoQHgvRMfv,用该密码注册admin2:

export FABRIC_CA_CLIENT_HOME=/opt/app/fabric-ca/clients/admin2
mkdir -p $FABRIC_CA_CLIENT_HOME
fabric-ca-client enroll -u http://admin2:yRPoQHgvRMfv@localhost:7054

登记peer节点:

$ export FABRIC_CA_CLIENT_HOME=/opt/app/fabric-ca/clients/ad
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值