本文在基于fabricv2.5.9基础上,通过运行network.sh(网络部署脚本),解析其部署流程。 ./network.sh up: 启动网络。 总结:这个命令用于启动一个Hyperledger Fabric网络,创建一个通道,并使用证书颁发机构(CA)来管理证书 流程解析 1.部署fabric-ca 1.1 编写docker-compsoe文件,配置ca信息文件目录参考:fabric-samples/test-network/compose/compose-ca.yaml 启动docker容器,基于每个组织至少一个ca的原则,为orderer、org1、org2分别部署一个ca。 注册ca admin(ca管理员) fabric-ca-client enroll: 表示注册(enroll)一个新身份 msp目录下存储的文件如下: 登记peer0 fabric-ca-client register --caname ca-org1 --id.name peer0 --id.secret peer0pw --id.type peer --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/org1/ca-cert.pemfabric-ca-client register: 这是Fabric CA客户端的注册命令,用于在CA中注册新的身份。注册过程只创建身份记录,而不生成证书。 登记 user fabric-ca-client register --caname ca-org1 --id.name user1 --id.secret user1pw --id.type client --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/org1/ca-cert.pem登记 org admin(组织管理员) fabric-ca-client register --caname ca-org1 --id.name org1admin --id.secret org1adminpw --id.type admin --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/org1/ca-cert.pem== ca admin(ca管理员)和org admin(组织管理员) == 注册peer0,生成peer0 msp证书 fabric-ca-client enroll -u https://peer0:peer0pw@localhost:7054 --caname ca-org1 -M /home/savia/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/org1/ca-cert.pem生成peer0生成tls证书 fabric-ca-client enroll -u https://peer0:peer0pw@localhost:7054 --caname ca-org1 -M /home/savia/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls --enrollment.profile tls --csr.hosts peer0.org1.example.com --csr.hosts localhost --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/org1/ca-cert.pemfabric-ca-client enroll: 这是注册(enroll)命令,用于从CA获取证书。 msp文件与tls文件 tls证书 : 用于加密节点之间的网络通信,以确保数据传输的机密性和完整性。TLS证书还包括用于验证对等节点和客户端的身份。 MSP证书和TLS证书有不同的用途。MSP证书用于身份验证和授权,而TLS证书用于加密通信和保护数据的机密性。因此,需要分别生成这两种证书。 注册user,生成user msp文件 fabric-ca-client enroll -u https://user1:user1pw@localhost:7054 --caname ca-org1 -M /home/savia/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/org1/ca-cert.pem对于用户来说,TLS证书通常不需要,因为用户的主要作用是在网络上提交交易和查询链码,而不是直接与其他节点进行加密通信。故未生成注册tls证书 注册org admin ,生成org admin msp文件 fabric-ca-client enroll -u https://org1admin:org1adminpw@localhost:7054 --caname ca-org1 -M /home/savia/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/org1/ca-cert.pem组织管理员主要负责网络的配置和管理,而TLS证书的主要作用是保护节点间的通信。由于组织管理员的工作主要是在管理层面,而不直接涉及节点间的通信加密,因此通常不需要TLS证书。网络中已经通过节点端的TLS证书确保了通信的安全性,故未生成tls证书 生成和配置Org2的节点和用户的身份信息注册ca管理员 fabric-ca-client enroll -u https://admin:adminpw@localhost:8054 --caname ca-org2 --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/org2/ca-cert.pem登记peer0 fabric-ca-client register --caname ca-org2 --id.name peer0 --id.secret peer0pw --id.type peer --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/org2/ca-cert.pem登记user fabric-ca-client register --caname ca-org2 --id.name user1 --id.secret user1pw --id.type client --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/org2/ca-cert.pem登记org admin fabric-ca-client register --caname ca-org2 --id.name org2admin --id.secret org2adminpw --id.type admin --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/org2/ca-cert.pem注册peer0,生成peer0 msp文件 fabric-ca-client enroll -u https://peer0:peer0pw@localhost:8054 --caname ca-org2 -M /home/savia/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/org2/ca-cert.pem生成peer0 tls 文件 fabric-ca-client enroll -u https://peer0:peer0pw@localhost:8054 --caname ca-org2 -M /home/savia/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls --enrollment.profile tls --csr.hosts peer0.org2.example.com --csr.hosts localhost --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/org2/ca-cert.pem生成user msp 文件 fabric-ca-client enroll -u https://user1:user1pw@localhost:8054 --caname ca-org2 -M /home/savia/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/users/User1@org2.example.com/msp --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/org2/ca-cert.pem生成org admin msp 文件 fabric-ca-client enroll -u https://org2admin:org2adminpw@localhost:8054 --caname ca-org2 -M /home/savia/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/org2/ca-cert.pem 生成和配置Orderer Org的节点的身份信息注册ca管理员 fabric-ca-client enroll -u https://admin:adminpw@localhost:9054 --caname ca-orderer --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/ordererOrg/ca-cert.pem登记orderer 节点 fabric-ca-client register --caname ca-orderer --id.name orderer --id.secret ordererpw --id.type orderer --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/ordererOrg/ca-cert.pem登记组织管理员 fabric-ca-client register --caname ca-orderer --id.name ordererAdmin --id.secret ordererAdminpw --id.type admin --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/ordererOrg/ca-cert.pem注册orderer节点,生成msp文件 fabric-ca-client enroll -u https://orderer:ordererpw@localhost:9054 --caname ca-orderer -M /home/savia/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/ordererOrg/ca-cert.pem生成orderer tls文件 fabric-ca-client enroll -u https://orderer:ordererpw@localhost:9054 --caname ca-orderer -M /home/savia/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/tls --enrollment.profile tls --csr.hosts orderer.example.com --csr.hosts localhost --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/ordererOrg/ca-cert.pem注册组织管理员,生成msp生成证书 fabric-ca-client enroll -u https://ordererAdmin:ordererAdminpw@localhost:9054 --caname ca-orderer -M /home/savia/fabric-samples/test-network/organizations/ordererOrganizations/example.com/users/Admin@example.com/msp --tls.certfiles /home/savia/fabric-samples/test-network/organizations/fabric-ca/ordererOrg/ca-cert.pem 2.为网络中的组织(Org1,Org2)生成CCP文件CCP文件是一个JSON或YAML格式的文件,用于配置客户端应用程序,使其能够连接到Hyperledger Fabric网络中的不同组织和节点。如: 组织和节点信息 证书和密钥路径(用于身份验证和授权的证书和密钥路径,以确保客户端能够安全地与网络中的节点进行通信) 通道信息 通过在客户端应用程序中加载CCP文件,并结合Fabric Gateway API,你可以安全地连接到区块链网络并执行各种操作。 注意:CCP文件在Hyperledger Fabric网络的部署阶段并不直接使用,它主要用于客户端应用程序来连接和操作已经部署好的网络。客户端配置和操作则依赖于CCP文件提供的网络连接信息 本例中通过ccp-generate.sh生成ccp文件,参考文件目录:fabric-samples/test-network/ccp-generate.sh 文件参考目录:fabric-samples/test-network/compose/compose-test-net.yaml 启动docker容器,整体网络结构如下: 文件参考目录:fabric-samples/test-network/configtx/configtx.yaml configtx.yaml 文件定义了通道配置文件和创世块配置所需的各种参数和选项。configtxgen 工具通过读取 configtx.yaml 文件中的配置,生成包含这些配置的创世块。 注意:默认情况下,configtxgen 工具会在当前工作目录下查找名为 configtx.yaml 的文件。如果 configtx.yaml 文件位于不同的目录,或者希望指定特定的文件路径,需要使用 FABRIC_CFG_PATH 环境变量来指向包含该文件的目录。 4.1.2生成通道的创世块(genesis block) configtxgen -profile ChannelUsingRaft -outputBlock ./channel-artifacts/mychannel.block -channelID mychannel configtxgen: Hyperledger Fabric工具,用于生成通道配置交易和创世块。 -profile ChannelUsingRaft: -profile 参数指定了生成创世块所使用的配置文件的配置名称。 ChannelUsingRaft 指定使用 configtx.yaml 配置文件中的ChannelUsingRaft配置 -outputBlock ./channel-artifacts/mychannel.block: -outputBlock 参数指定了输出创世块的文件路径。 ./channel-artifacts/mychannel.block 表示将生成的创世块保存到 channel-artifacts 目录下的 mychannel.block 文件中。 -channelID mychannel: -channelID 参数指定了通道的ID,这个ID将被用于识别通道。 mychannel 是通道的名称。 4.2排序节点加入通道本例中使用orderer.sh脚本执行 使用 osnadmin 命令行工具来让排序节点加入指定的通道。 osnadmin channel join: 命令用于将排序节点加入到指定的通道。 --channelID ${channel_name}: 指定通道ID,即通道的名称。 --config-block ./channel-artifacts/${channel_name}.block: 提供通道配置块的路径,这是创建通道时生成的文件。 -o localhost:7053: 指定排序节点的地址和端口,这里使用了 localhost 和端口 7053。 --ca-file “$ORDERER_CA”: 指定排序节点的CA证书文件路径。这个证书用于验证排序节点的TLS连接。 --client-cert “$ORDERER_ADMIN_TLS_SIGN_CERT”: 指定客户端的TLS证书,用于身份验证。 --client-key “$ORDERER_ADMIN_TLS_PRIVATE_KEY”: 指定客户端的TLS私钥。 成功后将输出: 至此通道已经激活,可供peer加入。 5.将peer加入到通道 5.1将org1 peer加入到通道中 Using organization 1 执行命令,将peer加入到通道中: peer channel join -b ./channel-artifacts/mychannel.block 5.2将org2 peer加入到通道中Using organization 2(同上) peer channel join -b ./channel-artifacts/mychannel.block 5.3 配置锚节点组织将peer加入到通道后,应选择至少一名peer称为锚节点。锚节点是利用私有数据和服务发现等共所必须的。 5.3.1配置Org1锚节点Using organization 1 获取通道配置 peer channel fetch config /home/savia/fabric-samples/test-network/channel-artifacts/config_block.pb -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com -c mychannel --tls --cafile /home/savia/fabric-samples/test-network/organizations/ordererOrganizations/example.com/tlsca/tlsca.example.com-cert.pem从排序服务中获取了当前通道 mychannel 的最新配置区块,并将其保存为 config_block.pb 文件。 解码配置块 configtxlator proto_decode --input /home/savia/fabric-samples/test-network/channel-artifacts/config_block.pb --type common.Block --output /home/savia/fabric-samples/test-network/channel-artifacts/config_block.json jq '.data.data[0].payload.data.config' /home/savia/fabric-samples/test-network/channel-artifacts/config_block.json使用 configtxlator 解码配置块,将配置块从 protobuf 格式转换为 JSON 格式,以便于编辑和查看。 在通道中生成Org2的锚节点更新交易 生成锚节点更新交易 jq '.channel_group.groups.Application.groups.Org1MSP.values += {"AnchorPeers":{"mod_policy": "Admins","value":{"anchor_peers": [{"host": "peer0.org1.example.com","port": 7051}]},"version": "0"}}' /home/savia/fabric-samples/test-network/channel-artifacts/Org1MSPconfig.json编辑 Org1MSPconfig.json 文件,将 AnchorPeers 信息添加到 Org1MSP 组。此步骤指定了 peer0.org1.example.com 为 Org1 的锚节点,端口为 7051。 生成更新交易 configtxlator proto_encode --input /home/savia/fabric-samples/test-network/channel-artifacts/Org1MSPconfig.json --type common.Config --output /home/savia/fabric-samples/test-network/channel-artifacts/original_config.pb configtxlator proto_encode --input /home/savia/fabric-samples/test-network/channel-artifacts/Org1MSPmodified_config.json --type common.Config --output /home/savia/fabric-samples/test-network/channel-artifacts/modified_config.pb configtxlator compute_update --channel_id mychannel --original /home/savia/fabric-samples/test-network/channel-artifacts/original_config.pb --updated /home/savia/fabric-samples/test-network/channel-artifacts/modified_config.pb --output /home/savia/fabric-samples/test-network/channel-artifacts/config_update.pb configtxlator proto_decode --input /home/savia/fabric-samples/test-network/channel-artifacts/config_update.pb --type common.ConfigUpdate --output /home/savia/fabric-samples/test-network/channel-artifacts/config_update.json jq .使用 configtxlator 将修改后的配置(包括锚节点信息)编码为 protobuf 格式,并生成更新交易文件 Org1MSPanchors.tx。 提交更新 configtxlator proto_encode --input /home/savia/fabric-samples/test-network/channel-artifacts/config_update_in_envelope.json --type common.Envelope --output /home/savia/fabric-samples/test-network/channel-artifacts/Org1MSPanchors.tx提交锚节点更新交易到通道 mychannel。这个步骤将更新应用到通道配置中,设置 Org1 的锚节点。 Using organization 2 获取通道配置 peer channel fetch config /home/savia/fabric-samples/test-network/channel-artifacts/config_block.pb -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com -c mychannel --tls --cafile /home/savia/fabric-samples/test-network/organizations/ordererOrganizations/example.com/tlsca/tlsca.example.com-cert.pem解码配置块 configtxlator proto_decode --input /home/savia/fabric-samples/test-network/channel-artifacts/config_block.pb --type common.Block --output /home/savia/fabric-samples/test-network/channel-artifacts/config_block.json jq '.data.data[0].payload.data.config' /home/savia/fabric-samples/test-network/channel-artifacts/config_block.json在通道中生成Org2的锚节点更新交易 jq '.channel_group.groups.Application.groups.Org2MSP.values += {"AnchorPeers":{"mod_policy": "Admins","value":{"anchor_peers": [{"host": "peer0.org2.example.com","port": 9051}]},"version": "0"}}' /home/savia/fabric-samples/test-network/channel-artifacts/Org2MSPconfig.json configtxlator proto_encode --input /home/savia/fabric-samples/test-network/channel-artifacts/Org2MSPconfig.json --type common.Config --output /home/savia/fabric-samples/test-network/channel-artifacts/original_config.pb configtxlator proto_encode --input /home/savia/fabric-samples/test-network/channel-artifacts/Org2MSPmodified_config.json --type common.Config --output /home/savia/fabric-samples/test-network/channel-artifacts/modified_config.pb configtxlator compute_update --channel_id mychannel --original /home/savia/fabric-samples/test-network/channel-artifacts/original_config.pb --updated /home/savia/fabric-samples/test-network/channel-artifacts/modified_config.pb --output /home/savia/fabric-samples/test-network/channel-artifacts/config_update.pb configtxlator proto_decode --input /home/savia/fabric-samples/test-network/channel-artifacts/config_update.pb --type common.ConfigUpdate --output /home/savia/fabric-samples/test-network/channel-artifacts/config_update.json jq . configtxlator proto_encode --input /home/savia/fabric-samples/test-network/channel-artifacts/config_update_in_envelope.json --type common.Envelope --output /home/savia/fabric-samples/test-network/channel-artifacts/Org2MSPanchors.tx至此,一个基础的区块链网络部署完成,可根据自身需求,基于fabric-sample提供的应用程序示例,部署相应的链码测试。 (责任编辑:) |