一、环境准备
1. 虚拟机为 ubuntu 22.04
2. 将ubuntu 的软件镜像源更换为国内的镜像源(命令如下)
sudo vim /etc/apt/source.list (vim下载与使用自行百度)
将文件内容清空,然后复制下方代码粘贴,保存退出即可。
deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
# deb https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
3. 安装docker
参考CSDN博客:https://blog.csdn.net/justlpf/article/details/132982953
4. 安装golang
下载二进制包:All releases - The Go Programming Language 选择go1.20.5版本
解压安装包 :tar -C /usr/local -xzf go1.20.5.linux-amd64.tar.gz
设置环境变量:
编辑profile文件:vim ~/.profile
在文件的最后加上:
export GOROOT=/usr/local/go
export GOPATH=$HOME /test
export PATH=$PATH:$GOROOT/bin:$GOPATH
运行命令:source ~/.profile
验证是否完成安装:go version
5. 安装jq
sudo apt-get install jq
二、获取fabric与准备工作
1.使用git 获取:
2. 打开命令行,运行:./bootstrap.sh
3. 如果下载速度慢,或者无法下载,则运行
https://github.com/hyperledger/fabric/releases/download/v2.5.7/hyperledger-fabric-linux-amd64-2.5.7.tar.gz
4. 将下载好的文件保存到bootstrap.sh文件所在位置
5. 在bootstrap.sh文件所在位置打开命令行,运行以下命令:
tar -xzvf hyperledger-fabric-linux-amd64-2.5.7.tar.gz
tar -xzvf hyperledger-fabric-ca-linux-amd64-1.5.10.tar.gz
6. 使用记事本打开文件bootstrap.sh,找到文件中的download方法,如下:
将上面的内容修改为:
7. 运行命令:./bootstrap.sh,等待运行完成
8. 将bootstrap.sh文件所在目录的bin文件夹和config文件夹,复制到fabric-samples文件夹中,选择替换即可
三、运行
1. 打开fanric-samples目录下的test-network文件夹
2. 在此目录打开命令行
3. 删除先前运行的所有容器或工程
运行 ./network.sh down
4. 创建一个由两个对等节点和一个排序节点组成的Fabric网络
运行 ./network.sh up
成功将会有以下输出
5. 创建一个通道
运行 ./network.sh createChannel
成功将会有以下输出
如果你想自定义通道名,例如创建一个名为channe1的通道,则可以运行
./network.sh createChannel -c channel1
6. 打包智能合约
首先进入到fabric-samples/asset-transfer-basic/chaincode-go文件夹中,再运行命令:
go env -w GOPROXY=https://goproxy.cn,direct
GO111MODULE=on go mod vendor
cd ../../test-network
export PATH=${PWD}/../bin:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
peer version
peer lifecycle chaincode package basic.tar.gz --path ../asset-transfer-basic/chaincode-go/ --lang golang --label basic_1.0
运行以上命令之后,将会在test-network文件夹中创建一个名为basic.tar.gz的包
7. 在通道启动一个合约(链码)
./network.sh down
./network.sh up createChannel
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
运行成功如图:
8. 与网络交互
设置环境变量,操作节点Org1
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
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/[email protected]/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":[]}'
运行成功将会有以下输出:
-> INFO 001 Chaincode invoke successful. result: status:200
运行以下指令来获取添加到通道账本的资产列表
peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
查询结果如图:
当一个网络成员希望在账本上转一些或者改变一些资产,链码会被调用。使用以下的指令来通过调用 asset-transfer (basic) 链码改变账本上的资产所有者:
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":["asset6","Christopher"]}'
如果成功将有以下输出:
9. 操作org2,查询转帐结果
设置以下的环境变量来操作 Org2
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:9051
查询转账结果:
peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
结果如图:
标签:Fabric,--,一键,sh,export,https,com,example,分布式 From: https://blog.csdn.net/m0_58480257/article/details/140506522