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

应用程序使用软件开发工具包 (SDK) 访问允许查询和更新分类账的 API。您可以安装并使用超级账本架构 SDK 来开发 Oracle Blockchain Platform 应用。

Oracle Blockchain Platform 提供的 REST API 的创建具有最大的灵活性;您可以调用事务处理、调用查询或查看事务处理的状态。请参阅 REST API for Oracle Blockchain Platform

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

SDK 版本

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

安装 Node.js 的 Hyperledger Fabric SDK

有关如何使用 Fabric SDK for Node.js 的信息,请访问: Hyperledger Fabric SDK for Node.js 文档

开发人员工具选项卡上,打开应用程序开发窗格。
  • 使用此选项卡上的链接可以安装超级账本架构 Node.js SDK。
  • (Hyperledger Fabric v1.4.7) 如果之前安装了 Hyperledger Fabric v1.4.7),则必须按照 Update the Hyperledger Fabric v1.4.7 SDK to Work with Oracle Blockchain Platform 中的说明对其进行修改,才能使用 Oracle Blockchain Platform

安装适用于 Java 的 Hyperledger Fabric SDK

有关如何使用适用于 Java 的 Fabric SDK 的信息,请参见: Hyperledger Fabric SDK for Java 文档

开发人员工具选项卡上,打开应用程序开发窗格。
  • 使用此选项卡上的链接可以安装超级账本架构 Java SDK。
  • (Hyperledger Fabric v2.x) If you've previously installed it you must modify it to work with Oracle Blockchain Platform following the instructions in Update the Hyperledger Fabric v2.x SDKs to Work with Oracle Blockchain Platform.
  • (Hyperledger Fabric v1.4.7) 如果之前安装了 Hyperledger Fabric v1.4.7),则必须按照 Update the Hyperledger Fabric v1.4.7 SDK to Work with Oracle Blockchain Platform 中的说明对其进行修改,才能使用 Oracle Blockchain Platform

安装构建工具,例如 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. 在渠道的分类帐窗格中,查看链代码的分类帐汇总。

更新 Hyperledger Fabric v2.x SDK 以使用 Oracle Blockchain Platform

OCI 基础设施组件与随 Hyperledger Fabric v2.x 提供的 Java SDK 不兼容。请按照本主题中的步骤更正此问题。

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

更新 SDK 有两种方法:

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

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

手动生成软件包

对于 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. 运行项目。

更新 Hyperledger Fabric v1.4.7 SDK 以使用 Oracle Blockchain Platform

OCI 基础设施组件与 Hyperledger Fabric v1.4.7 随附的 Node.js 和 Java SDK 之间存在不兼容性。请按照本主题中的步骤更正此问题。

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

更新 SDK 有两种方法:

  • 使用 Oracle 脚本下载并安装 Node.js SDK 或 Java SDK,该 SDK 在安装时会为代码打补丁。
  • 手动,如本主题所述。

要使用脚本,请在控制台的开发人员工具选项卡上,选择应用程序开发窗格。下载 Node.js SDK 和 Java SDK 的链接具有内置的更新,这些更新将在安装时为代码打补丁。

  • Fabric Java SDK:我们创建了一个更新的 grpc-netty-1.23.0.jar 文件,该文件是需要修改的 Java SDK 引用的模块。
  • Fabric Node.js SDK:我们创建了 npm_bcs_client.sh 脚本来替换用户下载和安装 Node.js Fabric 客户机软件包时将执行的标准 Fabric npm install 操作。该脚本运行相同的 npm 命令,但它也修补了所需的组件并重建了它。

手动更新 Fabric Node.js SDK

执行以下操作以重建 grpc-node 模块,将对等节点和排序节点与 grpcs 客户机(通过 TLS)连接。
  1. 在不执行 grpc 模块生成脚本的情况下安装 fabric-client
    npm install --ignore-scripts fabric-client
  2. 在 Windows 上,需要显式禁用 ALPN

    • 通过更改来更新 node_modules/grpc/binding.gyp
      '_WIN32_WINNT=0x0600'
      to
      '_WIN32_WINNT=0x0600','TSI_OPENSSL_ALPN_SUPPORT=0'
    • 由于 https://github.com/nodejs/node/issues/4932 中概述的问题,要在 Windows 上构建 grpc-node,必须先删除 <node_root_dir>/include/node/openssl/。运行以下命令查找 <node_root_dir>
      node-gyp configure
  3. 重建 grpc

    npm rebuild --unsafe-perm --build-from-source
您现在可以安装所需的任何其他模块并运行项目。

手动更新 Fabric Java SDK

对于 fabric-sdk-java,请执行以下步骤来重建 grpc-netty 软件包,以便将对等节点和排序节点与 grpcs 客户机(通过 tls)连接。grpc-nettygrpc-java 的子项目。

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

    git clone https://github.com/grpc/grpc-java.git
  3. 找到 fabric-sdk-java 使用的 grpc 版本,然后检出代码。

    不同版本的 fabric-sdk-java 可能使用不同版本的 grpc。检查 pom.xml 以了解 fabric-sdk-java 使用的 grpc 版本。例如,fabric-sdk-java 1.4.11 使用 grpc-java 1.23.0,如其 pom.xml 中所示:https://github.com/hyperledger/fabric-sdk-java/blob/v1.4.11/pom.xml

    grpc-java 目录中,检出 fabric-sdk-java 使用的 grpc 版本:
    git checkout -b v1.23.0
  4. 更改代码以避免服务器端出现 alpn 错误。
    • 更改 grpc-java_root/netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java 的目标代码
    • 在函数 userEventTriggered0 中,更改:
      if (NEXT_PROTOCOL_VERSIONS.contains(handler.applicationProtocol())) {
      to
      if (handler.applicationProtocol() == null || NEXT_PROTOCOL_VERSIONS.contains(handler.applicationProtocol())) {
      代码将类似于:
          @Override
          protected void userEventTriggered0(ChannelHandlerContext ctx, Object evt) throws Exception {
            ...
                if (handler.applicationProtocol() == null || NEXT_PROTOCOL_VERSIONS.contains(handler.applicationProtocol())) {
                  // Successfully negotiated the protocol.
                  logSslEngineDetails(Level.FINER, ctx, "TLS negotiation succeeded.", null);
            ...
          }
  5. 构建项目以生成目标打补丁的程序包。使用 gradle 构建 grpc-java 项目。或者,您只能在 grpc netty 目录 gradle build 中重建 grpc-netty 子项目。

    构建完成后,可以在目录 grpc-java\netty\build\libs\grpc-netty-1.23.0.jar 中获取目标打补丁的 jar 软件包。

  6. 将打补丁的程序包添加到 Maven 本地资料档案库中。

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

  7. 运行项目。