Hyperledger Fabric 学习联盟链 2 -- fabric 尝鲜

淡淡的烟草味﹌ 2023-06-15 11:05 74阅读 0赞

请先看这篇 Hyperledger Fabric 实战联盟链 1 — Mac下Hyperledger Fabric(超级账本)环境搭建

https://www.jianshu.com/p/e108cf655c0f

1.准备工作

上一章拉取fabric 和 fabric-samples 之后,目录结构如下:

appledeMacBook-Air:hyperledger apple$ ll

total 0

drwxr-xr-x 4 apple staff 128 11 20 14:38 ./

drwxr-xr-x 5 apple staff 160 11 19 16:55 ../

drwxr-xr-x 46 apple staff 1472 11 20 14:47 fabric/

drwxr-xr-x 15 apple staff 480 11 19 20:16 fabric-samples/

进入fabric 目录,并将relese 版本切换到 1.0

appledeMacBook-Air:hyperledger apple$ cd fabric

appledeMacBook-Air:fabric apple$ git checkout release-1.0

正在检出文件: 100% (5064/5064), 完成.

分支 ‘release-1.0’ 设置为跟踪来自 ‘origin’ 的远程分支 ‘release-1.0’。

切换到一个新分支 ‘release-1.0’

接下来安装两个工具 configtxgen 和 cryptogen

appledeMacBook-Air:fabric apple$ cd common/configtx/tool/configtxgen/

appledeMacBook-Air:configtxgen apple$ ll

total 48

drwxr-xr-x 5 apple staff 160 11 20 14:59 ./

drwxr-xr-x 5 apple staff 160 11 20 14:59 ../

-rw-r—r— 1 apple staff 14341 11 20 14:59 main.go

-rw-r—r— 1 apple staff 4531 11 20 14:59 main_test.go

drwxr-xr-x 4 apple staff 128 11 20 14:59 metadata/

appledeMacBook-Air:configtxgen apple$ go install —tags=nopkcs11

appledeMacBook-Air:cryptogen apple$ pwd

/Users/apple/go/src/github.com/hyperledger/fabric/common/tools/cryptogen

appledeMacBook-Air:cryptogen apple$ go install —tags=nopkcs11

appledeMacBook-Air:cryptogen apple$

这两个工具都被安装到了 $GOPATH的bin 目录下,可以查看一下,cd $GOPATH/bin

appledeMacBook-Air:cryptogen apple$ cd /Users/apple/go/bin/

appledeMacBook-Air:bin apple$ ll

total 77584

drwxr-xr-x 6 apple staff 192 11 20 15:07 ./

drwxr-xr-x 5 apple staff 160 10 4 20:35 ../

-rwxr-xr-x 1 apple staff 15860660 11 20 15:03 configtxgen*

-rwxr-xr-x 1 apple staff 8037116 11 20 15:07 cryptogen*

-rwxr-xr-x 1 apple staff 5147152 10 4 20:36 goimports*

-rwxr-xr-x 1 apple staff 9876156 10 7 21:15 gopm*

appledeMacBook-Air:bin apple$

2.搭建第一条网络

进入fabric-samples ,切换分支

appledeMacBook-Air:fabric-samples apple$ pwd

/Users/apple/go/src/github.com/hyperledger/fabric-samples

appledeMacBook-Air:fabric-samples apple$ git checkout release-1.0

已经位于 ‘release-1.0’

您的分支与上游分支 ‘origin/release-1.0’ 一致。

进入 first-network

appledeMacBook-Air:fabric-samples apple$ cd first-network/

appledeMacBook-Air:first-network apple$ ll

total 112

drwxr-xr-x 15 apple staff 480 11 20 14:10 ./

drwxr-xr-x 15 apple staff 480 11 19 20:16 ../

-rw-r—r— 1 apple staff 42 11 19 20:07 .env //环境变量

-rw-r—r— 1 apple staff 335 11 19 20:07 README.md

drwxr-xr-x 4 apple staff 128 11 19 20:16 base/ //公共服务

-rwxr-xr-x 1 apple staff 15108 11 19 20:16 byfn.sh* //启动脚本

drwxr-xr-x 7 apple staff 224 11 19 20:19 channel-artifacts/

-rw-r—r— 1 apple staff 5013 11 19 20:16 configtx.yaml //可以根据configtxgen生成配置文件,放入到channel-artifacts/目录

drwxr-xr-x 4 apple staff 128 11 20 14:10 crypto-config/

-rw-r—r— 1 apple staff 3858 11 19 20:16 crypto-config.yaml //可以根据cryptogen生成配置文件,放入到crypto-config/ 目录

-rw-r—r— 1 apple staff 3015 11 19 20:16 docker-compose-cli.yaml //四个compose 文件用于驱动网络

-rw-r—r— 1 apple staff 4604 11 19 20:16 docker-compose-couch.yaml

-rw-r—r— 1 apple staff 2883 11 19 20:07 docker-compose-e2e-template.yaml

-rw-r—r— 1 apple staff 3091 11 20 14:10 docker-compose-e2e.yaml

drwxr-xr-x 3 apple staff 96 11 19 20:16 scripts/ //测试脚本目录

appledeMacBook-Air:first-network apple$

查看启动脚本提供的方法

appledeMacBook-Air:first-network apple$ ./byfn.sh -h

Usage:

byfn.sh -m up|down|restart|generate [-c ] [-t ] [-d ] [-f ] [-s ] [-i ]

byfn.sh -h|—help (print this message)

  1. -m <mode> - one of 'up', 'down', 'restart' or 'generate'
  2. - 'up' - bring up the network with docker-compose up //启动网络
  3. - 'down' - clear the network with docker-compose down //关闭网络
  4. - 'restart' - restart the network //重启网络
  5. - 'generate' - generate required certificates and genesis block //根据之前的两个工具生成证书和创世区块
  6. -c <channel name> - channel name to use (defaults to "mychannel”) //通道名字 默认是mychannel
  7. -t <timeout> - CLI timeout duration in microseconds (defaults to 10000) //客户端超时时间,默认10秒
  8. -d <delay> - delay duration in seconds (defaults to 3)
  9. -f <docker-compose-file> - specify which docker-compose file use (defaults to docker-compose-cli.yaml)
  10. -s <dbtype> - the database backend to use: goleveldb (default) or couchdb //数据库选择
  11. -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.:

  1. byfn.sh -m generate -c mychannel //生成节点证书,以及创世区块的配置
  2. byfn.sh -m up -c mychannel -s couchdb
  3. byfn.sh -m up -c mychannel -s couchdb -i 1.0.6
  4. byfn.sh -m down -c mychannel

Taking all defaults:

  1. byfn.sh -m generate
  2. byfn.sh -m up
  3. byfn.sh -m down

生成节点证书,以及创世区块的配置

appledeMacBook-Air:first-network apple$ ./byfn.sh -m generate -c imocc

Generating certs and genesis block for with channel ‘imocc’ and CLI timeout of ‘10’

Continue (y/n)? y

proceeding …

/Users/apple/go/bin/cryptogen

##########################################################

##### Generate certificates using cryptogen tool ######### //首先生成参与主体的证书

##########################################################

org1.example.com

org2.example.com

/Users/apple/go/bin/configtxgen

##########################################################

######### Generating Orderer Genesis block ############## //生成创世区块

##########################################################

2018-11-20 15:56:57.035 CST [common/configtx/tool] main -> INFO 001 Loading configuration

2018-11-20 15:56:57.080 CST [common/configtx/tool] doOutputBlock -> INFO 002 Generating genesis block

2018-11-20 15:56:57.083 CST [common/configtx/tool] doOutputBlock -> INFO 003 Writing genesis block

#################################################################

### Generating channel configuration transaction ‘channel.tx’ ### //生成通道配置文件

#################################################################

2018-11-20 15:56:57.150 CST [common/configtx/tool] main -> INFO 001 Loading configuration

2018-11-20 15:56:57.158 CST [common/configtx/tool] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx

2018-11-20 15:56:57.158 CST [common/configtx/tool] doOutputChannelCreateTx -> INFO 003 Writing new channel tx

#################################################################

####### Generating anchor peer update for Org1MSP ########## //生成两个锚节点

#################################################################

2018-11-20 15:56:57.226 CST [common/configtx/tool] main -> INFO 001 Loading configuration

2018-11-20 15:56:57.235 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update

2018-11-20 15:56:57.236 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update

#################################################################

####### Generating anchor peer update for Org2MSP ##########

#################################################################

2018-11-20 15:56:57.304 CST [common/configtx/tool] main -> INFO 001 Loading configuration

2018-11-20 15:56:57.312 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update

2018-11-20 15:56:57.312 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update

appledeMacBook-Air:first-network apple$

检查环境是否正常

appledeMacBook-Air:first-network apple$ docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

d22ea915929d hyperledger/fabric-tools:x86_64-1.0.0 “/bin/bash -c ‘./scri” 18 hours ago Exited (0) About an hour ago cli

02a37a28971e hyperledger/fabric-peer:x86_64-1.0.0 “peer node start” 18 hours ago Exited (1) About an hour ago peer1.org2.example.com

f0a3b7fb36f7 hyperledger/fabric-orderer:x86_64-1.0.0 “orderer” 18 hours ago Exited (1) About an hour ago orderer.example.com

5fd58b2a5e85 hyperledger/fabric-peer:x86_64-1.0.0 “peer node start” 18 hours ago Exited (1) About an hour ago peer1.org1.example.com

ea0542ae9e05 hyperledger/fabric-peer:x86_64-1.0.0 “peer node start” 18 hours ago Exited (1) About an hour ago peer0.org2.example.com

2d1b752868cf hyperledger/fabric-peer:x86_64-1.0.0 “peer node start” 18 hours ago Exited (1) About an hour ago peer0.org1.example.com

appledeMacBook-Air:first-network apple$

启动网络,注意这里报错了,目前没找到原因

appledeMacBook-Air:first-network apple$ ./byfn.sh -m generate -c imocc -i 1.0.0

Generating certs and genesis block for with channel ‘imocc’ and CLI timeout of ‘10’

Continue (y/n)? y

proceeding …

/Users/apple/go/bin/cryptogen

##########################################################

Generate certificates using cryptogen tool

##########################################################

org1.example.com

org2.example.com

/Users/apple/go/bin/configtxgen

##########################################################

######### Generating Orderer Genesis block ##############

##########################################################

2018-11-20 16:26:24.599 CST [common/configtx/tool] main -> INFO 001 Loading configuration

2018-11-20 16:26:24.644 CST [common/configtx/tool] doOutputBlock -> INFO 002 Generating genesis block

2018-11-20 16:26:24.646 CST [common/configtx/tool] doOutputBlock -> INFO 003 Writing genesis block

#################################################################

Generating channel configuration transaction ‘channel.tx’

#################################################################

2018-11-20 16:26:24.696 CST [common/configtx/tool] main -> INFO 001 Loading configuration

2018-11-20 16:26:24.704 CST [common/configtx/tool] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx

2018-11-20 16:26:24.705 CST [common/configtx/tool] doOutputChannelCreateTx -> INFO 003 Writing new channel tx

#################################################################

####### Generating anchor peer update for Org1MSP ##########

#################################################################

2018-11-20 16:26:24.769 CST [common/configtx/tool] main -> INFO 001 Loading configuration

2018-11-20 16:26:24.778 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update

2018-11-20 16:26:24.779 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update

#################################################################

####### Generating anchor peer update for Org2MSP ##########

#################################################################

2018-11-20 16:26:24.869 CST [common/configtx/tool] main -> INFO 001 Loading configuration

2018-11-20 16:26:24.880 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update

2018-11-20 16:26:24.880 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update

appledeMacBook-Air:first-network apple$ ./byfn.sh -m up -c imocc -i 1.0.0

Starting with channel ‘imocc’ and CLI timeout of ‘10’

Continue (y/n)? y

proceeding …

Recreating peer1.org1.example.com … done

Recreating peer1.org2.example.com … done

Recreating orderer.example.com … done

Recreating peer0.org2.example.com … done

Recreating peer0.org1.example.com … done

Recreating cli … done

/ | | | / \ | _ \ | |

_ \ | | / _ \ | |) | | |

) | | | / ___ \ | _ < | |

|/ || // _\ || _\ |_|

Build your first network (BYFN) end-to-end test

Channel name : imocc

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

2018-11-20 06:18:26.933 UTC [main] main -> ERRO 001 Cannot run peer because error when setting up MSP from directory /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp: err Could not initialize BCCSP Factories [Failed initializing PKCS11.BCCSP %!s()

[Could not initialize BCCSP PKCS11 [Failed to initialize software key store: An invalid KeyStore path provided. Path cannot be an empty string.]]]

!!! Channel creation failed !!!

========= ERROR !!! FAILED to execute End-2-End Scenario ===========

停掉网络 删除容器 以及容器里的链码

./byfn.sh -m down -c imocc -i

验证看是否有容器在运行,已经没有容器在运行了

appledeMacBook-Air:first-network apple$ docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

docker 里面也删除了链码镜像

appledeMacBook-Air:first-network apple$ docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

hyperledger/fabric-baseimage x86_64-0.4.7 390ac2e95bc7 7 months ago 1.414 GB

hyperledger/fabric-baseos x86_64-0.4.7 c0e784934c4e 7 months ago 152 MB

hyperledger/fabric-tools latest 0403fd1c72c7 16 months ago 1.32 GB

hyperledger/fabric-tools x86_64-1.0.0 0403fd1c72c7 16 months ago 1.32 GB

hyperledger/fabric-couchdb x86_64-1.0.0 2fbdbf3ab945 16 months ago 1.481 GB

hyperledger/fabric-kafka x86_64-1.0.0 dbd3f94de4b5 16 months ago 1.297 GB

hyperledger/fabric-zookeeper x86_64-1.0.0 e545dbf1c6af 16 months ago 1.312 GB

hyperledger/fabric-orderer latest e317ca5638ba 16 months ago 179 MB

hyperledger/fabric-orderer x86_64-1.0.0 e317ca5638ba 16 months ago 179 MB

hyperledger/fabric-peer latest 6830dcd7b9b5 16 months ago 181.9 MB

hyperledger/fabric-peer x86_64-1.0.0 6830dcd7b9b5 16 months ago 181.9 MB

hyperledger/fabric-javaenv x86_64-1.0.0 8948126f0935 16 months ago 1.424 GB

hyperledger/fabric-ccenv x86_64-1.0.0 7182c260a5ca 16 months ago 1.292 GB

hyperledger/fabric-ca x86_64-1.0.0 a15c59ecda5b 16 months ago 237.9 MB

发表评论

表情:
评论列表 (有 0 条评论,74人围观)

还没有评论,来说两句吧...

相关阅读