Code Monkey home page Code Monkey logo

twonodes's Introduction

软件版本

Fabric 2.2 fabric-samples 3d2875c180c67d9f3b3a166bb15b9c73fb6c7091 fabric-samples 15275a0d4d46696c22e652a0674e36c570431a33

手动搭建hyperledger fabric v2.x 网络(一)

演示证书模板生成

cryptogen showtemplate > crypto-config.yaml

修改crypto-config.yaml文件中3处有关EnableNodeOUs的部分为true

sed -i 's/EnableNodeOUs: false/EnableNodeOUs: true/g' crypto-config.yaml

根据crypto-config.yaml生成相关证书

cryptogen generate --config=crypto-config.yaml

这期课程生成组织文件,会生成crypto-config目录。

手动搭建hyperledger fabric v2.x 网络(二)

创建通道所需的配置,需要选择Sep 16, 2020的提交

wget https://raw.githubusercontent.com/hyperledger/fabric-samples/3d2875c180c67d9f3b3a166bb15b9c73fb6c7091/test-network/configtx/configtx.yaml -O configtx.yaml

更改msp路径为项目具体的路径。 搜索所有包含../organizations文本的路径,替换为crypto-config当前路径。

sed -i 's/..\/organizations/crypto-config/g' configtx.yaml

修改Profiles中的部分,按照官方文档中的内容来。 所选提交无需修改Profiles部分。

使用官方工具,生成通道配置。

# 生成创世块文件
configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block -channelID fabric-channel
# 生成通道文件
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
# 生成组织1锚节点文件
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/OrglMSPanchors.tx -channelID mychannel -asOrg Org1MSP
# 生成组织2锚节点文件
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP

channel-artifacts中会生成4个文件。

手动搭建hyperledger fabric v2.x 网络(三)

创建peer与排序节点

wget https://raw.githubusercontent.com/hyperledger/fabric-samples/15275a0d4d46696c22e652a0674e36c570431a33/test-network/docker/docker-compose-test-net.yaml -O docker-compose.yaml

修改networks.test.name为:twonodes_test。 修改volumes中对应的宿主机路径。 修改组织1与组织2的网络名称和挂载路径 修改peer0.org2.example.com环境变量中的CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODEtwonodes_test。 把cli拆成cli1和cli2,分别表示两个组织的cli,环境变量部分,参考测试网络中的内容进行配置。 热心网友提供的docker-compose.yml文件

手动搭建hyperledger fabric v2.x 网络(四)完结

第三期中的docker-compose中还有一处需要修改,有关节点启动方式,详看视频。或者直接拿热心网友的文件过来用。 启动集群

mkdir -p chaincode/go/
docker-compose up -d
docker-compose ps

加入通道

进入到终端节点 使用cli1创建一个通道mychannel.block,并加入到该通道。

docker exec -it cli1 bash
peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/channel.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/msp/tlscacerts/tlsca.example.com-cert.pem
ls mychannel.block
peer channel join -b mychannel.block
exit

将创建好的通道复制到本地宿主机,将其传入cli2中,并加入到该通道。

docker cp cli1:/opt/gopath/src/github.com/hyperledger/fabric/peer/mychannel.block ./
docker cp ./mychannel.block cli2:/opt/gopath/src/github.com/hyperledger/fabric/peer/mychannel.block
docker exec -it cli2 bash
ls mychannel.block
peer channel join -b mychannel.block
exit

更新锚节点

docker exec -it cli1 bash
peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/OrglMSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
exit
docker exec -it cli2 bash
peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
exit

关于链码生命周期的操作

# 下载链码
wget https://raw.githubusercontent.com/hyperledger/fabric-samples/3d2875c180c67d9f3b3a166bb15b9c73fb6c7091/chaincode/sacc/sacc.go -O chaincode/go/sacc.go
# 编译链码
docker exec -it cli1 bash
cd /opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go
设置goproxy代理:go env -w GOPROXY=https://goproxy.cn,direct
go mod init
go mod tidy
go mod vendor
# 打包链码并分发链码
cd /opt/gopath/src/github.com/hyperledger/fabric/peer
peer lifecycle chaincode package sacc.tar.gz --path /opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go/ --label sacc_1
ls sacc.tar.gz
exit
docker cp cli1:/opt/gopath/src/github.com/hyperledger/fabric/peer/sacc.tar.gz ./
docker cp ./sacc.tar.gz cli2:/opt/gopath/src/github.com/hyperledger/fabric/peer/sacc.tar.gz

开启两个终端,分别进入cli1和cli2,执行以下命令。

# 安装链码
peer lifecycle chaincode install sacc.tar.gz

组织批准链码

# 查询已安装链码id
peer lifecycle chaincode queryinstalled
# 组织批准链码
peer lifecycle chaincode approveformyorg --channelID mychannel --name sacc --version 1.0 --init-required --package-id sacc_1:5970aafc8e84ea61640bc2d52edc760e691b18d51509a3eb9af12bd8de6de418 --sequence 1 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
# 检查是否已经批准
peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name sacc --version 1.0 --init-required --sequence 1 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --output json
# commit链码
peer lifecycle chaincode commit -o orderer.example.com:7050 --channelID mychannel --name sacc --version 1.0 --sequence 1 --init-required --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt

链码的整个生命周期完成 链码调用

peer chaincode invoke -o orderer.example.com:7050 --isInit --ordererTLSHostnameOverride orderer.example.com --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n sacc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["a","bb"]}'

链码查询

peer chaincode query -C mychannel -n sacc -c '{"Args":["query","a"]}'

链码测试键值对["a","cc"]

peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n sacc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["set","a","cc"]}'

清理实验环境

docker-compose down --volumes --remove-orphans
rm -rf crypto-config chaincode channel-artifacts sacc.tar.gz mychannel.block 

twonodes's People

Contributors

lyz05 avatar ab4ndon avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.