使用超级账本架构 SDK 开发应用

Hyperledger Fabric SDK 现已过时。但是,您可以安装和使用超级账本架构 SDK 开发适用于 Oracle Blockchain Platform 的应用。

Oracle Blockchain Platform 提供的 REST API 的创建充分考虑了灵活性;您可以调用事务处理、调用查询或查看事务处理状态。请参阅 REST API for Oracle Blockchain Platform

但是,这种灵活性意味着您可能希望将现有 API 端点包装在应用中以提供对象级控制。应用程序可以包含更细粒度的操作。

SDK 版本

提供了多个版本的超级账本架构 SDK。使用与您的实例所基于的超级账本架构版本匹配的 SDK 版本。

安装适用于 Node.js 的超级账本架构 SDK

有关如何将 Fabric SDK 用于 Node.js 的信息,请参见:适用于 Node.js 的超级账本 Fabric SDK 文档

Developer Tools(开发人员工具)选项卡上,打开 Application Development(应用程序开发)窗格。您可以使用此选项卡上的链接安装超级账本架构 Node.js SDK。

安装适用于 Java 的超级账本架构 SDK

有关如何使用适用于 Java 的 Fabric SDK 的信息,请参见:适用于 Java 的超级账本 Fabric SDK 文档

Developer Tools(开发人员工具)选项卡上,打开 Application Development(应用程序开发)窗格。

安装构建工具,如 Apache Maven。

构建应用程序

类似以下示例的结构化 Java 应用程序:

/Application
  /artifacts
    /cypto
      /orderer
        Contains the certificates required for the application to act on the orderer node
        In participant instances only contains TLS certificates
      /peer
        Contains the certificates required for the application to act on the peer node
    /src
      chaincode.go if installing and deploying chaincode to the blockchain
  /java
    pom.xml or other build configuration files
    /resources
      Any resources used by the Java code, including artifacts such as the endorsement policy yaml file and blockchain configuration properties
    /src
      Java source files

结构化 Node.js 应用程序,类似于以下示例:

/Application
  /artifacts
    /cypto
      /orderer
        Contains the certificates required for the application to act on the orderer node
        In participant instances only contains TLS certificates
      /peer
        Contains the certificates required for the application to act on the peer node
    /src
      chaincode.go if installing and deploying chaincode to the blockchain
  /node
    package.json file
    application.js
    /app
      Any javascript files called by the application
      /tools

运行应用产品

您现在就可以运行和测试应用程序了。除了应用程序返回的任何状态消息之外,您还可以在 Oracle Blockchain Platform 控制台中检查分类账以查看更改:

  1. 转到控制台中的 Channels(通道)选项卡,然后找到并单击运行链代码的通道的名称。
  2. 在渠道的分类账窗格中,查看链代码的分类账汇总。

更新超级账本架构 Go SDK,与 Oracle Blockchain Platform 协同工作

您必须更新超级账本架构 Go SDK 中的 NormalizeURL 函数,才能将其与基于 Kubernetes 的 Oracle Blockchain Platform 版本结合使用。

  1. 在超级账本架构 Go SDK 中打开 client.go 文件进行编辑:vendor/github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/lib/client.go
  2. 按以下代码中所示更新 NormalizeURL 函数。
    编辑前使用 NormalizeURL 函数:
    // NormalizeURL normalizes a URL (from cfssl)
    func NormalizeURL(addr string) (*url.URL, error) {
    	addr = strings.TrimSpace(addr)
    	u, err := url.Parse(addr)
    	if err != nil {
    		return nil, err
    	}
    	if u.Opaque != "" {
    		u.Host = net.JoinHostPort(u.Scheme, u.Opaque)
    		u.Opaque = ""
    	} else if u.Path != "" && !strings.Contains(u.Path, ":") {
    		u.Host = net.JoinHostPort(u.Path, "")
    		u.Path = ""
    	} else if u.Scheme == "" {
    		u.Host = u.Path
    		u.Path = ""
    	}
    	if u.Scheme != "https" {
    		u.Scheme = "http"
    	}
    .
    .
    .
    编辑后的 NormalizeURL 函数:
    // NormalizeURL normalizes a URL (from cfssl)
    func NormalizeURL(addr string) (*url.URL, error) {
    	addr = strings.TrimSpace(addr)
    	u, err := url.Parse(addr)
    	if err != nil {
    		return nil, err
    	}
    	if u.Opaque != "" {
    		u.Host = net.JoinHostPort(u.Scheme, u.Opaque)
    		u.Opaque = ""
    	} else if u.Host != "" && !strings.Contains(u.Host, ":") {
    		u.Host = net.JoinHostPort(u.Host, "")
    		u.Path = ""
    	} else if u.Scheme == "" {
    		u.Host = u.Path
    		u.Path = ""
    	}
    .
    .
    .
  3. 保存 client.go 文件。

将超级账本架构 Java SDK 更新为与 Oracle Blockchain Platform 协同工作

OCI 基础设施组件与随 Hyperledger Fabric v2.x 提供的 Java SDK 之间存在不兼容性。您必须更新 SDK 才能将其与 Oracle Blockchain Platform 一起使用。

更新超级账本架构 SDK 的方法

更新 SDK 有两种方法:

  • 从控制台下载修改后的软件包。我们创建了一个更新的 grpc-netty-shaded-1.38.0.jar 文件,该文件是需要修改的 Java SDK 引用的模块。
  • 手动构建软件包,如以下步骤中所述。

要下载 grpc-netty-shaded-1.38.0.jar 文件,请单击控制台的 Developer Tools(开发人员工具)选项卡,然后选择 Application Development(应用程序开发)窗格。

手动构建软件包

对于 fabric-sdk-java 项目,完成以下步骤以重建 grpc-netty-shaded 软件包,以将对等节点和排序节点与 grpcs 客户机(通过 TLS)连接。grpc-netty-shaded 软件包是 grpc-java 的子项目。

  1. 安装项目相关性:
    mvn install
  2. 下载 grpc-java 源代码:

    git clone https://github.com/grpc/grpc-java.git
  3. 查找 fabric-sdk-java 项目使用的 grpc 版本并查看代码。
    grpc-java 目录中,检查 fabric-sdk-java 项目使用的 grpc 版本:
    cd grpc-java && git checkout v1.38.0
  4. 更改代码以避免服务器端出现 alpn 错误。创建包含以下内容的 grpc-java-patch 修补程序文件:
    diff --git a/netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java b/netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java
    index 19d3e01b7..ebc4786a3 100644
    — a/netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java
    +++ b/netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java
    @@ -611,7 +611,8 @@ final class ProtocolNegotiators {
    SslHandshakeCompletionEvent handshakeEvent = (SslHandshakeCompletionEvent) evt;
    if (handshakeEvent.isSuccess()) {
    SslHandler handler = ctx.pipeline().get(SslHandler.class);
    
        if (sslContext.applicationProtocolNegotiator().protocols()
        + if (handler.applicationProtocol() == null
        + || sslContext.applicationProtocolNegotiator().protocols()
        .contains(handler.applicationProtocol())) {
        // Successfully negotiated the protocol.
        logSslEngineDetails(Level.FINER, ctx, "TLS negotiation succeeded.", null);
  5. 应用补丁程序:

    git apply grpc-java.patch
  6. 构建项目以生成目标打补丁包。使用 gradle 构建 grpc-java-shaded 项目:

    cd netty/shaded
    gradle build -PskipAndroid=true -PskipCodegen=true

    构建完成后,目标修补的 .jar 软件包位于 grpc-java/netty/build/libs/grpc-netty-shaded-1.38.0.jar

  7. 将打补丁的软件包添加到 Maven 本地系统信息库中。

    通过以下两种方式之一将原始 grpc-netty-shaded .jar 软件包替换为已修补的软件包:
    • 使用 Maven 按本地文件安装软件包:
      mvn install:install-file -Dfile=grpc-netty-shaded-build/grpc-netty-shaded-1.38.0.jar -DgroupId=io.grpc -DartifactId=grpc-netty-shaded -Dversion=1.38.0 -Dpackaging=jar
      必须将目标 groupidartifactidversion 与要替换的软件包保持相同。
    • 手动替换您的软件包。转到本地 Maven 系统信息库,找到目标软件包所在的目录,然后将软件包替换为已打补丁的软件包。

  8. 运行项目。