实验条件与说明
- 本人的Mac Catalina
- Docker Desktop 3.6.0
- HyperLedger Fabric 2.5.4
前言
下载Hyperledger Fabric Docker映像和示例后,可以使用Fabric示例存储库中提供的脚本部署测试网络。
测试网络是通过在本地机器上运行节点来学习Fabric的。开发人员可以使用网络来测试他们的智能合约和应用程序。该网络仅用于教育和测试工具,而不是如何建立网络的模型。
一般来说,不鼓励修改脚本,这可能会破坏网络。它基于一个有限的配置,不应用作部署生产网络的模板:
- 它包括两个同行组织和一个订购组织。
- 为了简单起见,配置了一个单节点Raft订购服务。
- 为了降低复杂性,不部署TLS证书颁发机构(CA)。所有证书都由根CA颁发。
- 示例网络使用Docker Compose部署了一个Fabric网络。因为节点在Docker Compose网络中是隔离的,所以测试网络没有配置为连接到其他正在运行的Fabric节点。
要了解如何在生产中使用Fabric,请参阅部署生产网络。
注意:这些说明已经过验证,可以针对最新稳定的Fabric Docker映像和所提供的tar文件中的预编译安装实用程序工作。如果使用当前主分支中的图像或工具运行这些命令,则可能会遇到错误。
[重要说明]要继续往下阅读的前提是,您已经成功地在您的机器上安装了上述工具与Fabric示例系列。
启动测试网络¶
您可以在Fabric示例存储库(fabric-samples)的测试网络(test-network)目录中找到用于启动网络的脚本。使用以下命令导航到测试网络目录:
cd fabric-samples/test-network
在该目录中,您可以找到一个带注释的脚本network.sh,该脚本使用本地机器上的Docker映像来建立Fabric网络。你可以运行命令:
./network.sh -h
来打印脚本帮助信息:
Usage:
network.sh <Mode> [Flags]
Modes:
up - Bring up Fabric orderer and peer nodes. No channel is created
up createChannel - Bring up fabric network with one channel
createChannel - Create and join a channel after the network is created
deployCC - Deploy a chaincode to a channel (defaults to asset-transfer-basic)
down - Bring down the network
Flags:
Used with network.sh up, network.sh createChannel:
-ca <use CAs> - Use Certificate Authorities to generate network crypto material
-c <channel name> - Name of channel to create (defaults to "mychannel")
-s <dbtype> - Peer state database to deploy: goleveldb (default) or couchdb
-r <max retry> - CLI times out after certain number of attempts (defaults to 5)
-d <delay> - CLI delays for a certain number of seconds (defaults to 3)
-i <imagetag> - Docker image tag of Fabric to deploy (defaults to "latest")
-cai <ca_imagetag> - Docker image tag of Fabric CA to deploy (defaults to "latest")
-verbose - Verbose mode
Used with network.sh deployCC
-c <channel name> - Name of channel to deploy chaincode to
-ccn <name> - Chaincode name.
-ccl <language> - Programming language of the chaincode to deploy: go (default), java, javascript, typescript
-ccv <version> - Chaincode version. 1.0 (default), v2, version3.x, etc
-ccs <sequence> - Chaincode definition sequence. Must be an integer, 1 (default), 2, 3, etc
-ccp <path> - File path to the chaincode.
-ccep <policy> - (Optional) Chaincode endorsement policy using signature policy syntax. The default policy requires an endorsement from Org1 and Org2
-cccg <collection-config> - (Optional) File path to private data collections configuration file
-cci <fcn name> - (Optional) Name of chaincode initialization function. When a function is provided, the execution of init will be requested and the function will be invoked.
-h - Print this message
Possible Mode and flag combinations
up -ca -r -d -s -i -cai -verbose
up createChannel -ca -c -r -d -s -i -cai -verbose
createChannel -c -r -d -verbose
deployCC -ccn -ccl -ccv -ccs -ccp -cci -r -d -verbose
Examples:
network.sh up createChannel -ca -c mychannel -s couchdb -i 2.0.0
network.sh createChannel -c channelName
network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-javascript/ -ccl javascript
network.sh deployCC -ccn mychaincode -ccp ./user/mychaincode -ccv 1 -ccl javascript
从目录test-network下运行如下命令:
./network.sh down
能够从以前的运行中删除任何容器或工件。
然后,您可以通过发出以下命令来打开网络。如果尝试从另一个目录运行脚本,您将遇到问题:
./network.sh up
此命令创建由两个对等节点(一个订购节点)组成的Fabric网络。运行时不会创建任何频道/network.sh,尽管我们将在未来的一步中实现这一目标。如果命令成功完成,您将看到正在创建的节点的日志(在我的机器上):
Using docker and docker-compose
Starting nodes with CLI timeout of '5' tries and CLI delay of '3' seconds and using database 'leveldb'
Unable to find image 'hyperledger/fabric-tools:latest' locally
latest: Pulling from hyperledger/fabric-tools
01085d60b3a6: Pulling fs layer
367f9186226c: Pulling fs layer
b47f6a4ae6bc: Pulling fs layer
b5933c87679e: Pulling fs layer
77b2bdbf772c: Pulling fs layer
b2d9526a33e2: Pulling fs layer
b5933c87679e: Waiting
77b2bdbf772c: Waiting
b2d9526a33e2: Waiting
01085d60b3a6: Verifying Checksum
01085d60b3a6: Download complete
b5933c87679e: Verifying Checksum
b5933c87679e: Download complete
01085d60b3a6: Pull complete
77b2bdbf772c: Verifying Checksum
77b2bdbf772c: Download complete
367f9186226c: Verifying Checksum
367f9186226c: Download complete
367f9186226c: Pull complete
b47f6a4ae6bc: Verifying Checksum
b47f6a4ae6bc: Download complete
b47f6a4ae6bc: Pull complete
b2d9526a33e2: Verifying Checksum
b2d9526a33e2: Download complete
b5933c87679e: Pull complete
77b2bdbf772c: Pull complete
b2d9526a33e2: Pull complete
Digest: sha256:b3aea8173802186244663334b4196c415cb267d77ea8c617fdd30652c5a732c4
Status: Downloaded newer image for hyperledger/fabric-tools:latest
LOCAL_VERSION=v2.5.3
DOCKER_IMAGE_VERSION=v2.5.4
Local fabric binaries and docker images are out of sync. This may cause problems.
Creating network "fabric_test" with the default driver
Creating volume "compose_orderer.example.com" with default driver
Creating volume "compose_peer0.org1.example.com" with default driver
Creating volume "compose_peer0.org2.example.com" with default driver
Pulling peer0.org2.example.com (hyperledger/fabric-peer:latest)...
latest: Pulling from hyperledger/fabric-peer
01085d60b3a6: Already exists
5920bb1ab585: Pull complete
0c13247db338: Pull complete
f1ebf2febfff: Pull complete
7ba246813ff2: Pull complete
20e090879749: Pull complete
Digest: sha256:c2e735a3cb8250c47ed3589294b3f0c078004ec001b949710f334736651e106d
Status: Downloaded newer image for hyperledger/fabric-peer:latest
Pulling orderer.example.com (hyperledger/fabric-orderer:latest)...
latest: Pulling from hyperledger/fabric-orderer
01085d60b3a6: Already exists
0ec39628d119: Pull complete
c9ef912f2449: Pull complete
5f267c8c968e: Pull complete
54a738441be6: Pull complete
d6c2bf9dde2f: Pull complete
Digest: sha256:1a7144705b435062f4be2886de98d0408165cf51326ec721c3f58b40bf8bf139
Status: Downloaded newer image for hyperledger/fabric-orderer:latest
Creating peer0.org2.example.com ... done
Creating orderer.example.com ... done
Creating peer0.org1.example.com ... done
Creating cli ... done
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f62a959bc72 hyperledger/fabric-tools:latest "/bin/bash" 13 seconds ago Up 10 seconds cli
39576870f68f hyperledger/fabric-peer:latest "peer node start" 19 seconds ago Up 13 seconds 0.0.0.0:7051->7051/tcp, :::7051->7051/tcp, 0.0.0.0:9444->9444/tcp, :::9444->9444/tcp peer0.org1.example.com
c2e5e9caeec2 hyperledger/fabric-peer:latest "peer node start" 19 seconds ago Up 13 seconds 0.0.0.0:9051->9051/tcp, :::9051->9051/tcp, 7051/tcp, 0.0.0.0:9445->9445/tcp, :::9445->9445/tcp peer0.org2.example.com
9915b136d096 hyperledger/fabric-orderer:latest "orderer" 19 seconds ago Up 13 seconds 0.0.0.0:7050->7050/tcp, :::7050->7050/tcp, 0.0.0.0:7053->7053/tcp, :::7053->7053/tcp, 0.0.0.0:9443->9443/tcp, :::9443->9443/tcp orderer.example.com
74a3f32737e9 hello-world "/hello" 3 days ago Exited (0) 3 days ago romantic_tu
dd06dcd7b609 hello-world "/hello" 2 years ago Exited (0) 2 years ago recursing_yalow
注意:上述命令运行成功的前提是您已经启动Docker Desktop!
测试网络的组件¶
部署成功测试网络后,您可能需要一些时间来检查其组件。运行以下命令以列出计算机上运行的所有Docker容器。您应该看到network.sh脚本创建的三个节点:
docker ps -a
与Fabric网络交互的每个节点和用户都需要属于一个组织才能参与网络。测试网络包括两个对等组织,Org1和Org2。它还包括一个维护网络订购服务的单一排序(Ordering)组织。
Peer节点是任何结构网络的基本组件。Peers存储区块链账本,并在交易提交到账本之前验证交易。Peers运行包含用于管理区块链账本上资产的业务逻辑的智能合约。
网络中的每个Peers都需要属于一个组织。在测试网络中,每个组织各运行一个Peers,即peer0.org.example.com和peer0.org2.example.com。
每个Fabric网络还包括排序(Ordering)服务。虽然Peers验证交易并将交易块添加到区块链账本中,但他们不会决定交易的顺序,也不会将其包含到新的块中。在分布式网络上,Peers可能彼此相距很远,并且对事务何时创建没有共同的看法。就交易顺序达成共识是一个成本高昂的过程,会给Peers带来开销。
排序(Ordering)服务允许Peers专注于验证交易并将其提交到分类账。排序节点收到客户认可的交易后,对交易顺序达成共识,然后将其添加到块中。然后,这些块被分发到Peers节点,Peers节点将这些块添加到区块链账本中。
示例网络使用由Peers组织运营的单节点Raft订购服务。您可以看到机器上运行的排序节点为orderer.example.com。虽然测试网络只使用单节点排序服务,但生产网络将有多个排序节点,由一个或多个排序方组织运营。不同的排序节点将使用Raft一致性算法来就网络上的事务顺序达成一致。
创建通道(待续。。。。。。)
引用
https://hyperledger-fabric.readthedocs.io/en/latest/test_network.html
https://blog.csdn.net/u012421101/article/details/130306042
https://hyperledger-fabric.readthedocs.io/en/latest/test_network.html#troubleshooting