Hyperledger Fabric 网络体验 - 网络启动过程概览

进入fabric-samples/test-network目录,执行指令:

./network.sh up -i 2.5

执行完指令能看到fabric已经启动。
在这里插入图片描述

作为第一次Fabric网络体验,网络启动主要包含三个操作,分别是生成配置文件、启动网络和操作网络。

  1. 配置文件
  • 使用cryptogen tool生成证书文件(给组织颁发证书)
  • 生成Orderer节点的创世区块genesis.block(创世区块)
  • 生成通道配置交易文件 channel.tx(通道是fabric独有的机制,相当于是一个子链)
  • 为组织1和组织2创建锚节点(锚节点是一个负责对外通讯的节点)
along@DESKTOP-CKPKB5K:~/fabric/fabric-samples/test-network$ ./network.sh up -i 2.5
Starting nodes with CLI timeout of '5' tries and CLI delay of '3' seconds and using database 'leveldb' with crypto from 'cryptogen'
LOCAL_VERSION=v2.5.4
DOCKER_IMAGE_VERSION=v2.5.9
Local fabric binaries and docker images are out of  sync. This may cause problems.
/home/along/fabric/fabric-samples/test-network/../bin/cryptogen
Generating certificates using cryptogen tool
Creating Org1 Identities
+ cryptogen generate --config=./organizations/cryptogen/crypto-config-org1.yaml --output=organizations
org1.example.com
+ res=0
Creating Org2 Identities
+ cryptogen generate --config=./organizations/cryptogen/crypto-config-org2.yaml --output=organizations
org2.example.com
+ res=0
Creating Orderer Org Identities
+ cryptogen generate --config=./organizations/cryptogen/crypto-config-orderer.yaml --output=organizations
+ res=0
Generating CCP files for Org1 and Org2
/home/along/fabric/fabric-samples/test-network/../bin/configtxgen
Generating Orderer Genesis block
+ configtxgen -profile TwoOrgsOrdererGenesis -channelID system-channel -outputBlock ./system-genesis-block/genesis.block
2024-07-25 17:08:05.879 CST 0001 INFO [common.tools.configtxgen] main -> Loading configuration
2024-07-25 17:08:05.885 CST 0002 INFO [common.tools.configtxgen.localconfig] completeInitialization -> orderer type: etcdraft
2024-07-25 17:08:05.885 CST 0003 INFO [common.tools.configtxgen.localconfig] completeInitialization -> Orderer.EtcdRaft.Options unset, setting to tick_interval:"500ms" election_tick:10 heartbeat_tick:1 max_inflight_blocks:5 snapshot_interval_size:16777216 
2024-07-25 17:08:05.885 CST 0004 INFO [common.tools.configtxgen.localconfig] Load -> Loaded configuration: /home/along/fabric/fabric-samples/test-network/configtx/configtx.yaml
2024-07-25 17:08:05.888 CST 0005 INFO [common.tools.configtxgen] doOutputBlock -> Generating genesis block
2024-07-25 17:08:05.888 CST 0006 INFO [common.tools.configtxgen] doOutputBlock -> Creating system channel genesis block
2024-07-25 17:08:05.889 CST 0007 INFO [common.tools.configtxgen] doOutputBlock -> Writing genesis block
+ res=0
Creating volume "docker_orderer.example.com" with default driver
Creating volume "docker_peer0.org1.example.com" with default driver
Creating volume "docker_peer0.org2.example.com" with default driver
  1. 启动网络
  • 启动了4peer节点
  • 启动了一个orderer节点
  • 启动了一个管理工具
along@DESKTOP-CKPKB5K:~/fabric/fabric-samples/test-network$ ./network.sh up -i 2.5
Starting nodes with CLI timeout of '5' tries and CLI delay of '3' seconds and using database 'leveldb' 
LOCAL_VERSION=v2.5.4
DOCKER_IMAGE_VERSION=v2.5.9
Local fabric binaries and docker images are out of  sync. This may cause problems.
Creating orderer.example.com    ... done
Creating peer0.org2.example.com ... done
Creating peer0.org1.example.com ... done
Creating cli                    ... done
CONTAINER ID   IMAGE                               COMMAND             CREATED         STATUS                  PORTS                                                                                            NAMES
d3a4ef5a7683   hyperledger/fabric-tools:latest     "/bin/bash"         1 second ago    Up Less than a second                                                                                                    cli
ffcbad39bc93   hyperledger/fabric-peer:latest      "peer node start"   4 seconds ago   Up 2 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
4e971c5229f7   hyperledger/fabric-peer:latest      "peer node start"   4 seconds ago   Up 2 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
105098e02bfb   hyperledger/fabric-orderer:latest   "orderer"           4 seconds ago   Up 2 seconds            0.0.0.0:7050->7050/tcp, :::7050->7050/tcp, 0.0.0.0:9443->9443/tcp, :::9443->9443/tcp             orderer.example.com
root@ubuntu:/home/uu/go/src/github.com/hyperledger/fabric-samples/asset-transfer-basic/chaincode-go# go mod vendor github.com/hyperledger/fabric-samples/asset-transfer-basic/chaincode-go imports github.com/hyperledger/fabric-contract-api-go/v2/contractapi: no required module provides package github.com/hyperledger/fabric-contract-api-go/v2/contractapi; to add it: go get github.com/hyperledger/fabric-contract-api-go/v2/contractapi github.com/hyperledger/fabric-samples/asset-transfer-basic/chaincode-go/chaincode/mocks imports github.com/hyperledger/fabric-chaincode-go/v2/pkg/cid: no required module provides package github.com/hyperledger/fabric-chaincode-go/v2/pkg/cid; to add it: go get github.com/hyperledger/fabric-chaincode-go/v2/pkg/cid github.com/hyperledger/fabric-samples/asset-transfer-basic/chaincode-go/chaincode/mocks imports github.com/hyperledger/fabric-chaincode-go/v2/shim: no required module provides package github.com/hyperledger/fabric-chaincode-go/v2/shim; to add it: go get github.com/hyperledger/fabric-chaincode-go/v2/shim github.com/hyperledger/fabric-samples/asset-transfer-basic/chaincode-go/chaincode/mocks imports github.com/hyperledger/fabric-protos-go-apiv2/ledger/queryresult: no required module provides package github.com/hyperledger/fabric-protos-go-apiv2/ledger/queryresult; to add it: go get github.com/hyperledger/fabric-protos-go-apiv2/ledger/queryresult github.com/hyperledger/fabric-samples/asset-transfer-basic/chaincode-go/chaincode/mocks imports github.com/hyperledger/fabric-protos-go-apiv2/peer: no required module provides package github.com/hyperledger/fabric-protos-go-apiv2/peer; to add it: go get github.com/hyperledger/fabric-protos-go-apiv2/peer github.com/hyperledger/fabric-samples/asset-transfer-basic/chaincode-go/chaincode/mocks imports google.golang.org/protobuf/types/known/timestamppb: no required module provides package google.golang.org/protobuf/types/known/timestamppb; to add it: go get google.golang.org/protobuf/types/known/timestamppb 怎么办
06-12
### 解决 Hyperledger Fabric Chaincode-go 项目中 go mod vendor 报错问题 在 Hyperledger Fabric 的 Go 语言链码开发过程中,使用 `go mod vendor` 命令时可能会遇到依赖包缺失的问题。以下是针对提示缺少 `fabric-contract-api-go`、`fabric-chaincode-go`、`fabric-protos-go-apiv2` 和 `protobuf` 等依赖包的解决方案[^1]。 #### 问题分析 当执行 `go mod vendor` 时,如果出现依赖包缺失的报错,通常是因为以下原因: 1. **Go 模块代理配置不正确**:某些依赖包可能无法通过默认的 Go 模块代理下载。 2. **Hyperledger Fabric 的依赖包未正确托管**:部分 Hyperledger Fabric 的依赖包(如 `fabric-contract-api-go`)并未托管在官方 Go 模块仓库中,需要手动添加或克隆。 3. **版本兼容性问题**:使用的 Hyperledger Fabric SDK 或链码版本与依赖包版本不匹配。 #### 解决方案 以下是具体的解决步骤: 1. **确保 Go 模块代理配置正确** 在 Linux 或 macOS 系统中,可以通过设置环境变量启用国内的 Go 模块代理,以避免因网络问题导致的依赖包下载失败: ```bash go env -w GOPROXY=https://goproxy.cn,direct ``` 这一配置可以加速依赖包的下载,并减少因网络问题导致的错误[^4]。 2. **手动添加缺失的依赖包** 对于 Hyperledger Fabric 的特定依赖包(如 `fabric-contract-api-go` 和 `fabric-chaincode-go`),可以直接通过 `go get` 命令安装。例如: ```bash go get github.com/hyperledger/fabric-contract-api-go/contractapi go get github.com/hyperledger/fabric-chaincode-go/shim ``` 如果 `go get` 命令仍然无法正常工作,则需要手动克隆这些依赖包到项目的 `vendor` 目录下。例如: ```bash git clone https://github.com/hyperledger/fabric-contract-api-go.git $GOPATH/src/github.com/hyperledger/fabric-contract-api-go git clone https://github.com/hyperledger/fabric-chaincode-go.git $GOPATH/src/github.com/hyperledger/fabric-chaincode-go ``` 3. **处理 `fabric-protos-go-apiv2` 依赖** `fabric-protos-go-apiv2` 是 Hyperledger Fabric 的内部协议缓冲区定义文件生成的 Go 代码。如果项目中缺失该依赖,可以通过以下方式解决: - 确保已安装 `protoc` 编译器以及相关插件。 - 手动克隆 `fabric-protos-go-apiv2` 到项目目录中: ```bash git clone https://github.com/hyperledger/fabric-protos-go-apiv2.git $GOPATH/src/github.com/hyperledger/fabric-protos-go-apiv2 ``` 4. **确保 Protobuf 版本正确** 链码开发中可能需要特定版本的 Protobuf 工具和库。建议安装最新稳定版的 Protobuf 并验证其版本: ```bash protoc --version ``` 如果未安装或版本过旧,可以从 [Protobuf 官方网站](https://developers.google.com/protocol-buffers) 下载并安装。 5. **重新运行 `go mod vendor`** 在完成上述步骤后,再次运行 `go mod vendor` 命令,确保所有依赖包均已正确加载到 `vendor` 目录中: ```bash go mod tidy go mod vendor ``` #### 示例代码 以下是一个简单的链码示例,展示了如何导入必要的依赖包: ```go package main import ( "github.com/hyperledger/fabric-contract-api-go/contractapi" "fmt" ) type SmartContract struct { contractapi.Contract } func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) error { return nil } func main() { chaincode, err := contractapi.NewChaincode(new(SmartContract)) if err != nil { fmt.Println(err) return } if err := chaincode.Start(); err != nil { fmt.Println(fmt.Errorf("error starting smart contract: %v", err)) } } ``` ### 注意事项 - 如果仍然遇到依赖包缺失的问题,可以尝试清理现有的 `go.mod` 和 `go.sum` 文件后重新初始化模块: ```bash rm -rf go.mod go.sum vendor/ go mod init <module-name> go mod tidy go mod vendor ``` - 确保使用的 Hyperledger Fabric 版本与链码开发工具链版本一致,以避免版本不兼容带来的问题[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冲上云霄的Jayden

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值