使用 Hyperledger Fabric SDK 開發應用程式

應用程式使用軟體開發套件 (SDK) 來存取允許對分類帳進行查詢和更新的 API。您可以安裝並使用 Hyperledger Fabric SDK 來開發適用於 Oracle Blockchain Platform 的應用程式。

Oracle Blockchain Platform 所提供的 REST API 已建立最大彈性;您可以呼叫交易、呼叫查詢或檢視交易的狀態。請參閱 Oracle Blockchain Platform 的 REST API

不過,這項彈性代表您可能會想要包裝應用程式中現有的 API 端點,以提供物件層次的控制。應用程式可以包含更多微點作業。

SDK 版本

有多個版本的 Hyperledger Fabric SDK 可供使用。使用符合您執行處理所依據之 Hyperledger Fabric 版本的 SDK 版本。

安裝 Node.js 版的 Hyperledger Fabric SDK

如需如何使用 Fabric SDK for Node.js 的資訊,請參閱:Hyperledger Fabric SDK for Node.js 文件

開發人員工具頁籤中,開啟應用程式開發窗格。您可以使用此頁籤上的連結來安裝 Hyperledger Fabric Node.js SDK。

安裝 Java 適用的 Hyperledger Fabric SDK

有關如何使用 Fabric SDK for Java 的資訊,請參閱:Hyperledger Fabric SDK for Java 文件

開發人員工具頁籤中,開啟應用程式開發窗格。

安裝組建工具,例如 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. 移至主控台的通道頁籤,然後尋找並按一下執行鏈碼的通道名稱。
  2. 在管道的分類帳窗格中,檢視鏈碼的分類帳摘要。

更新 Hyperledger Fabric v2.x SDK 以與 Oracle Blockchain Platform 搭配運作

OCI 基礎架構元件與 Hyperledger Fabric v2.x 隨附的 Java SDK 之間不相容。請依照本主題中的步驟來更正此問題。

更新 Hyperledger Fabric 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. 建置專案以產生目標修正的套裝程式。使用漸層建立 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. 執行專案。