Hyperledger Fabric 2.x 自定义智能合约

知是行的主意,行是知的功夫。这篇文章主要讲述Hyperledger Fabric 2.x 自定义智能合约相关的知识,希望能为你提供帮助。

一、说明为了持续地进行信息的更新,以及对账本进行管理(写入交易,进行查询等),区块链网络引入了智能合约来实现对账本的访问和控制;智能合约在 Fabric 中称之为 链码,是区块链应用的业务逻辑。
本文分享如何使用 java 语言开发智能合约,以及合约的安装与使用。
 
二、环境准备1、部署好 Fabric 的测试网络,按照上一篇文章《Hyperledger Fabric 2.x 环境搭建》的内容执行第1至5步

  • 启动好两个 peer 节点和一个 orderer 节点
  • 创建好 mychannel 通道

2、在环境变量中配置好执行命令(bin)、配置(config)与MSP文件夹的路径:
执行 vim /etc/profile 添加以下内容:
export FABRIC_PATH=/opt/gopath/src/github.com/hyperledger/fabric-samples
export FABRIC_CFG_PATH=$FABRIC_PATH/config/
export MSP_PATH=$FABRIC_PATH/test-network/organizations
export CORE_PEER_TLS_ENABLED=true
export PATH=$FABRIC_PATH/bin:$PATH

FABRIC_PATH路径按实际进行修改。
【Hyperledger Fabric 2.x 自定义智能合约】


三、下载合约代码gitee:https://gitee.com/zlt2000_admin/my-fabric-chaincode-java
github:https://github.com/zlt2000/my-fabric-chaincode-java


四、代码解析在 Fabric 2.x 版本后的合约编写方式与旧版本略有不同,需要实现 ContractInterface 接口,下面是官方的一段说明:
All chaincode implementations must extend the abstract class ChaincodeBase. It is possible to implement chaincode by extending ChaincodeBase directly however new projects should implement org.hyperledger.fabric.contract.ContractInterface and use the contract programming model instead.
4.1. pom.xml文件
配置远程仓库
< repositories>
< repository>
< id> central< /id>
< url> http://maven.aliyun.com/nexus/content/groups/public/< /url>
< releases>
< enabled> true< /enabled>
< /releases>
< snapshots>
< enabled> false< /enabled>
< /snapshots>
< /repository>
< repository>
< id> jitpack.io< /id>
< url> https://www.jitpack.io< /url>
< /repository>
< repository>
< id> artifactory< /id>
< url> https://hyperledger.jfrog.io/hyperledger/fabric-maven< /url>
< /repository>
< /repositories>

 
依赖合约sdk
< dependency>
< groupId> org.hyperledger.fabric-chaincode-java< /groupId>
< artifactId> fabric-chaincode-shim< /artifactId

    推荐阅读