目录
prerequisite
hyperledger安装
capiler部署
prerequisite
- 电脑系统:macos m1
- hyperledger fabric官网:Prerequisites — hyperledger-fabricdocs main documentation(有安装大致步骤但是会有些出入)
- 需要提前安装:
- go -> go1.21.1 darwin/arm64
- docker compose -> 2.22.0
- docker -> 24.0.6
- curl
- git -> 2.30.1
- homebrew
- jq
- nodejs -> 20.7.0
- npm -> 10.1.0
hyperledger部署
1. prerequisite各项安装
-
homebrew安装
-
git安装
brew install git
-
curl安装
brew install curl
-
docker和dockercompose安装
brew install docker
#无需安装dockercompose,安装docker就自行安装
-
go安装
# 安装golang
brew install go
# 配置环境变量
vi ~/.bash_profile
# 在最后一行写入
export PATH=/usr/local/Cellar/go@1.21/1.21.1/bin:$PATH
# 检查版本
go version
-
jq安装
brew install jq
以上步骤我们就完成了v2.4版本prerequisite需要的安装,接下来部署hyperledger fabric环境
2. hyperledger fabric安装
# 创建目录并进入
mkdir -p $HOME/go/src/github.com/<your_userid>
cd $HOME/go/src/github.com/<your_userid>
# 拉取fabric源码
git clone https://github.com/hyperledger/fabric.git
# 查看和切换分支
cd ./fabric
git branch -a
git checkout release-2.4
# 设置环境变量,将以下路径fabric-samples/bin环境变量中:
vim ~/.bash_profile
export PATH=$PATH:/Users/xiaomanong/Documents/tool/fabric/go/src/github.com/hyperledger/fabric-samples/bin
source ~/.bash_profile
#查看是否生效
fabric-ca-client version
- 启动和测试网络
#进入fabric-samples下的test-network
#启动网络
./network.sh up
#创建channel
# 1.默认为mychannel
./network.sh createChannel
# 2.输入自定义名称,可以创建多个不同名称通道
./network.sh createChannel -c channel1
./network.sh createChannel -c channel2
# 3.和建立网络创建通道一起
./network.sh up createChannel
成功创建通道:
# 在通道中启动一个链码
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
链码启动成功
# 与网络交互
# 把fabric-samples的bin文件下的二进制文件添加到CLI路径:
export PATH=${PWD}/../bin:$PATH
#将fabric-samples代码库中的FABRIC_CFG_PATH设置为指向其中的core.yaml文件:
export FABRIC_CFG_PATH=$PWD/../config/
#设置环境变量,允许用户作为org1操作peer的CLI:
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
#CORE_PEER_TLS_ROOTCERT_FILE和CORE_PEER_MSPCONFIGPATH环境变量指向Org1的organizations文件夹中的的加密材料。
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051
#运行如下命令,用一些资产来初始化账本:
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"InitLedger","Args":[]}'
这样子就是初始化成功
#查询账本
peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
#修改资产所有者
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"TransferAsset","Args":["asset2","yalujiangslipper"]}'
#执行完后再查询一下
peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
查询一下,asset2已经变为“yalujiangslipper”
#测试完毕,关!
./network.sh down
以上就是全部hyperledger fabric v2.4.9的部署,不装v2.5是因为2.5太新了无法和caliper配比。接下来就是在这个基础上安装caliper
caliper部署
我安装的是0.5.0版本来测试fabric
1. 环境部署
- nodejs和npm安装
#1、安装nodejs
brew install nodejs
#2、再执行下面命令来安装npm(npm是开发nodejs时所用的依赖库)
brew install npm
- 创建caliper工作区
#在与fabric-samples同级的目录下,创建caliper工作区,命名为caliper-workspace
mkdir caliper-workspace
#在caliper-workspace下建立三个文件夹networks,benchmarks和workload分别用于分别用于存放网络配置文件、基准测试配置文件、工作负载文件
cd caliper-workspace
mkdir networks benchmarks workload
-
构建networks文件
#进入networks文件夹,建立networkConfig.yaml文件
cd networks
touch networkConfig.yaml
#将以下文件(根据自己的路径),写入networkConfig.yaml文件
name: Calier test
version: "2.0.0"
caliper:
blockchain: fabric
channels:
- channelName: mychannel
contracts:
- id: basic
organizations:
- mspid: Org1MSP
identities:
certificates:
- name: 'User1@org1.example.com'
clientPrivateKey:
path: '/opt/fabric/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/keystore/priv_sk'
clientSignedCert:
path: '/opt/fabric/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/signcerts/User1@org1.example.com-cert.pem'
connectionProfile:
path: '/opt/fabric/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/connection-org1.yaml'
discover: true
-
构建workload模块
#进入workload文件夹,建立readAssets.js文件
cd workload
touch readAsset.js
#将以下内容写入readAssets.js文件
'use strict';
const { WorkloadModuleBase } = require('@hyperledger/caliper-core');
class MyWorkload extends WorkloadModuleBase {
constructor() {
super();
}
async initializeWorkloadModule(workerIndex, totalWorkers, roundIndex, roundArguments, sutAdapter, sutContext) {
await super.initializeWorkloadModule(workerIndex, totalWorkers, roundIndex, roundArguments, sutAdapter, sutContext);
for (let i=0; i<this.roundArguments.assets; i++) {
const assetID = `${this.workerIndex}_${i}`;
console.log(`Worker ${this.workerIndex}: Creating asset ${assetID}`);
const request = {
contractId: this.roundArguments.contractId,
contractFunction: 'CreateAsset',
invokerIdentity: 'User1',
contractArguments: [assetID,'blue','20','penguin','500'],
readOnly: false
};
await this.sutAdapter.sendRequests(request);
}
}
async submitTransaction() {
const randomId = Math.floor(Math.random()*this.roundArguments.assets);
const myArgs = {
contractId: this.roundArguments.contractId,
contractFunction: 'ReadAsset',
invokerIdentity: 'User1',
contractArguments: [`${this.workerIndex}_${randomId}`],
readOnly: true
};
await this.sutAdapter.sendRequests(myArgs);
}
async cleanupWorkloadModule() {
for (let i=0; i<this.roundArguments.assets; i++) {
const assetID = `${this.workerIndex}_${i}`;
console.log(`Worker ${this.workerIndex}: Deleting asset ${assetID}`);
const request = {
contractId: this.roundArguments.contractId,
contractFunction: 'DeleteAsset',
invokerIdentity: 'User1',
contractArguments: [assetID],
readOnly: false
};
await this.sutAdapter.sendRequests(request);
}
}
}
function createWorkloadModule() {
return new MyWorkload();
}
module.exports.createWorkloadModule = createWorkloadModule;
-
构建benchmark测试文件
#进入benchmarks文件夹,建立myAssetBenchmark.yaml文件
cd benchmarks
touch myAssetBenchmark.yaml
#将以下代码写入myAssetBenchmark.yaml文件
test:
# 基准测试的名称
name: basic-contract-benchmark
# 基准测试详细描述
description: test benchmark
workers:
# 指定用于执行工作负载的进程数
number: 2
# 描述每一个测试回合的设置
rounds:
# 测试标签,一般为测试链码的名称
- label: readAsset
description: Read asset benchmark
# caliper提交交易的时间
txDuration: 30
# 提交交易的速度控制
rateControl:
# 速度控制类型,有fixed-rate、fixed-load等,解释可参见https://hyperledger.github.io/caliper/v0.5.0/rate-controllers/
type: fixed-load
opts:
transactionLoad: 2
workload:
# 描述需要测试的工作负载文件路径
module: workload/readAsset.js
# 需要传递给工作负责文件的参数
arguments:
assets: 10
contractId: basic
2. 测试
#安装依赖
npm install --only=prod @hyperledger/caliper-cli@0.5.0
#绑定sdk
npx caliper bind --caliper-bind-sut fabric:2.4
#测试
npx caliper launch manager --caliper-workspace ./ --caliper-networkconfig networks/networkConfig.yaml --caliper-benchconfig benchmarks/myAssetBenchmark.yaml --caliper-flow-only-test
运行成功,caliper-benchmarks会出现report.html