Hyperledger Fabric测试网络的准备和基本使用

这篇具有很好参考价值的文章主要介绍了Hyperledger Fabric测试网络的准备和基本使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

相关安装

  1. npm、node、git、docker、docker-compose。docker保证一直运行

    service docker start
    service docker status
    

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

2.安装Java、maven环境

https://blog.csdn.net/qq_41829594/article/details/122408706

https://www.oracle.com/java/technologies/downloads/

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

https://maven.apache.org/download.cgi

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

tar -zxvf 

在/etc/profile配置环境变量,之后source文件使其生效,检查是否安装成功

# set java development
export JAVA_HOME=/etc/opt/java/jdk1.8.0   #Java解压路径
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

3.安装jq

apt install jq
jq --version

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

4.在fabric里下载.sh文件

curl -sSLO https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh && chmod +x install-fabric.sh

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

5.运行.sh文件,下载docker印象、Fabric示例和二进制文件

./install-fabric.sh docker samples binary
or
./install-fabric.sh d s b

启动测试网络

cd fabric-samples/test-network
./network.sh up

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

docker ps -a

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

创建channel

./network.sh createChannel

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

打包链码

下载别人写的Java合约源码

cd ~fabric-samples/chaincode/

git clone https://gitee.com/kernelHP/hyperledger-fabric-contract-java-demo.git

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

cd ../../test-network

export PATH=${PWD}/../bin:$PATH //将bin目录中二进制文件添加到CLI路径

export FABRIC_CFG_PATH=$PWD/../config/ //设置FABRIC_CFG_PATH为指向fabric-samples中的core.yaml文件

package创建链码包

peer lifecycle chaincode package hyperledger-fabric-contract-java-demo.tar.gz --path ../chaincode/hyperledger-fabric-contract-java-demo/ --lang java --label hyperledger-fabric-contract-java-demo_1 

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

安装链码包

链码打包后,我们需要在认可交易的每个peer节点上安装链码。我们将设置背书策略以要求来自Org1和Org2的背书,我们需要在两个组织的节点上安装链码:peer0.org1.example.com和peer0.org2.example.com

设置环境变量,以Org1的身份操作peer CLI

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/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

install节点安装链码

peer lifecycle chaincode install hyperledger-fabric-contract-java-demo.tar.gz

过程会非常慢,因为需要下载一些文件,可以另起一个终端查看进程

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

链码启动成功的截图,重复执行上条指令可发现链码成功安装

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

设置环境变量,以Org2的身份操作节点,重复步骤安装链码

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_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/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051
peer lifecycle chaincode install hyperledger-fabric-contract-java-demo.tar.gz

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

通过链码定义

安装链码包后,需要通过组织的链码定义。该定义包括链码管理的重要参数,如名称、版本和链码认可策略。如果组织已在节点上安装了链码,则可通过包ID 将节点安装的链码与通过的链码定义相关联

queryinstalled查询包ID,包ID用于将peer节点上安装的链码与通过的链码定义相关联,并允许组织使用链码来认可交易

peer lifecycle chaincode queryinstalled

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

将包ID设置为环境变量

export CC_PACKAGE_ID=hyperledger-fabric-contract-java-demo_1:54a21ac334812b0505cd548f1aed0bc0a7b603eef44c98a17ef05f004737b451

approveformyorgOrg2通过链码定义(当前的环境变量设置管理员为Org2)

peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

重新设置环境变量,使Org1为管理员

export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_ADDRESS=localhost:7051

Org1通过链码定义

peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

链码定义提交给通道

checkcommitreadiness检查通道成员是否已批准相同的链码定义,将生成一个JSON映射,显示通道成员是否批准

peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 1.0 --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --output json

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

commit将链码定义提交到通道

peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 1.0 --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --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

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

querycommitted确认链码定义已提交给通道,若成功则放回链码定义的顺序和版本

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

调用链码

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 hyperledger-fabric-contract-java-demo --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":"createCat","Args":["cat-0" , "tom" ,  "3" , "蓝色" , "大懒猫"]}'

peer chaincode query -C mychannel -n hyperledger-fabric-contract-java-demo -c '{"Args":["queryCat" , "cat-0"]}'

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 hyperledger-fabric-contract-java-demo --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":"updateCat","Args":["cat-0" , "tom" ,  "3" , "白色" , "超级大懒猫"]}'

peer chaincode query -C mychannel -n hyperledger-fabric-contract-java-demo -c '{"Args":["queryCat" , "cat-0"]}'

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 hyperledger-fabric-contract-java-demo --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":"deleteCat","Args":["cat-0"]}'

peer chaincode query -C mychannel -n hyperledger-fabric-contract-java-demo -c '{"Args":["queryCat" , "cat-0"]}'

无猫查询

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

创建一只猫

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

再进行查询

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

关闭网络

./network.sh down

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

遇到的问题

1.docker保持启动状态

https://blog.csdn.net/ACkingdom/article/details/125747583

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

2.忘起测试网络了

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链

3.Java版本过高,推荐1.8

Hyperledger Fabric测试网络的准备和基本使用,fabric,java,智能合约,区块链文章来源地址https://www.toymoban.com/news/detail-559320.html

到了这里,关于Hyperledger Fabric测试网络的准备和基本使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Hyperledger Fabric网络快速启动

    目录 1、网络服务配置 2、关联的docker-compose-base.yaml 各Peer节点容器设置如下信息。 3、被关联的Peer-base.yaml 4、启动网络 2、完成通道的创建 2.1将节点加入应用通道  更新锚节点  2.为什么要创建节点并将其加入应用通道中?         由于要启动多个网络节点,Hyperledger Fa

    2024年02月12日
    浏览(31)
  • 搭建 Hyperledger Fabric区块链网络

    这里以下载最新版为例,得益于官网文档;网址为:教程 或者使用该命令下载指定版本  wget https://studygolang.com/dl/golang/go1.19.linux-amd64.tar.gz 之后解压到指定文件,一般为 /usr/local/下 ,需要配置环境变量

    2024年01月20日
    浏览(50)
  • Fabric 超级账本学习【5】Fabric2.4网络环境下——搭建Hyperledger Fabric区块链浏览器

    博主最近在搭建Hyperledger Fabric区块链浏览器过程中也学习了很多博主的搭建流程,踩了很多雷,踩了很多 坑,现将成功搭建好的Hyperledger Fabric区块链浏览器详细流程分享如下,帮助大家避雷闭坑 Hyperledger Explorer 是一个简单,强大,易于使用,维护良好的开源实用程序,可浏

    2023年04月23日
    浏览(36)
  • Hyperledger Fabric 应用实战(2)--网络节点设置

    网络名称:rentnet 联盟组织:orderer排序组织+三个成员组织supervisor、rentalcrop、 agency 通道:rentsign 账本数据库: couchdb 物理节点 组织 容器节点 supervisor supervisor.freerent.cn peer0.supervisor.freerent.cn peer1.supervisor.freerent.cn peer2.supervisor.freerent.cn couchdb0.supervisor.freerent.cn couchdb1.supervisor

    2024年02月12日
    浏览(27)
  • Hyperledger Fabric 网络环境的一点理解

    Hyperledger Fabric 开发链码,一般都是测试网络开发,然后部署到生产网络。 下面介绍测试网络、生产网络的一点理解。 使用cryptogen等工具建立测试网络,开发环境使用。 这里以https://github.com/hyperledger/fabric-samples 2022.2.12的代码为例进行说明。 目录: fabric-samples/test-network/orga

    2024年02月15日
    浏览(24)
  • Hyperledger Fabric Docker 方式多机部署生产网络

    3 个 orderer 节点;组织 org1 , org1 下有两个 peer 节点, peer0 和 peer1; 组织 org2 , org2 下有两个 peer 节点, peer0 和 peer1; 因为我只有 3 台虚拟机资源所以没法实现完全的多机部署,资源使用规划如下: 节点 宿主机 IP hosts 端口 cli 192.168.0.105 N/A N/A orderer0 192.168.0.105 orderer0.example.com

    2024年01月23日
    浏览(62)
  • Hyperledger Fabric的使用及开发

    Hyperledger Fabric是Linux基金会发起的一种跨行业的区块链技术,目前在多家大型公司有着应用,这里就不多做HF本身的介绍了,有兴趣可关注其官网。 开始前需要一定的准备工作,安装各类中间件: 其中安装docker compose的时候如出现以下问题,可如此处理 问题: command \\\'gcc\\\' fai

    2024年02月12日
    浏览(26)
  • Hyperledger Fabric 智能合约开发及 fabric-sdk-go/fabric-gateway 使用示例

    在上个实验 Hyperledger Fabric 多组织多排序节点部署在多个主机上 中,我们已经实现了多组织多排序节点部署在多个主机上,但到目前为止,我们所有的实验都只是研究了联盟链的网络配置方法(尽管这确实是重难点),而没有考虑具体的应用开发。本文将在前面实验的基础上

    2024年01月20日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包