使用 CLI 部署您的鏈碼

建立鏈碼專案之後,您可以在本機將其部署到自動產生的 Hyperledger Fabric 網路,或者從遠端部署到您的 Oracle Blockchain Platform Cloud 或 Enterprise Edition。您也可以封裝鏈碼專案,以便手動部署至 Oracle Blockchain Platform。

將您的鏈碼部署至本機 Hyperledger Fabric 網路

建立鏈碼專案之後,您可以將其部署到本機 Hyperledger Fabric 網路。此單通道測試網路會在您安裝 Blockchain App Builder 時為您建立。

Blockchain App Builder ochain run 命令會啟動 Hyperledger Fabric 網路、其他服務,並為您安裝和部署鏈碼。

my-mac:GOProject myname$ ochain run -h
Usage: run [options] [...args]
Run chaincode project locally in debug mode.

Arguments :
[...args] (optional) Chaincode instantiate arguments. Arguments should be space separated.

Options:
-h, --help        output command usage information 
-D, --debug       enable debug logging
-P, --debug-port  (optional) specify debug process port
-b, --build       (optional) rebuild runtime if already exists
-p, --project     (optional) Path to Chaincode project to run. If not specified, it defaults to current directory.  

Examples :
$> ochain run   

權杖專案

對於記號鏈碼專案,您必須使用 ochain run 命令指定管理員清單:
ochain run <adminList array>
adminList 陣列是指定記號管理員的資訊陣列。第一次部署權杖鏈碼專案時,adminList 陣列是必要參數。如果再次部署專案,您可以傳送空白陣列給 adminList 參數,或使用 adminList 參數新增權杖管理員。其他不是第一次建置者的建置程式,必須為 adminList 參數提供空白陣列。有趣的記號和不可行記號的參數資訊不同:
  • 對於使用「記號分類架構」標準的有趣記號,參數為 org_iduser_id
  • 對於任何使用 ERC-1155 標準的記號,參數為 orgIduserId
  • 對於使用 ERC-721 標準和 TypeScript 專案的不可行記號,參數為 orgIduserId
  • 對於使用 ERC-721 標準和 Go 專案的不可行記號,參數為 OrgIdUserId

下列範例適用於不可行的記號。

範例:Mac OSX 和 Linux 上 TypeScript 的 adminList 陣列:
'[{"userId":"userid", "orgId":"OrgMSPId"}]'
Go on Mac OSX 和 Linux 的範例 adminList 陣列:
'[{"UserId":"userid", "OrgId":"OrgMSPId"}]'
範例 Microsoft Windows 上 TypeScript 的 adminList 陣列:
"[{\"userId\":\"userid\", \"orgId\":\"OrgMSPId\"}]"
Go on Microsoft Windows 的範例 adminList 陣列:
"[{\"UserId\":\"userid\", \"OrgId\":\"OrgMSPId\"}]"

對於本機 Hyperledger Fabric 網路,OrgMSPId 欄位會固定在值 Org1MSP 中。

如果要查看除錯日誌,可以將 --debug 選項傳送至命令。如果您指定 --debug 選項,請在 Windows 上使用「命令提示字元」而非 PowerShell。您可以透過將 --port 選項傳送至命令,在不同的連接埠上執行基本網路並將鏈碼部署到不同的連接埠。

驗證中

下列日誌顯示已順利安裝並部署鏈碼。

my-mac:TSProject myname$ ochain run
Recreating orderer.example.com ... done
Recreating ca.example.com ... done
Recreating peer0.org1.example.com ... done
[2020-09-23T18: 04:09.132] [INFO] default -
============ Started Install Chaincode ============
[2020-09-23T18:04:09.193] [INFO] default Chaincode TSProject:l not installed.
[2020-09-23T18:04:09.317] [INFO] default - Successfully sent install Proposal and received ProposalResponse
[2020-09-23T18:04:09.317] [INFO] default - Successfully installed chaincode TSProject
[2020-09-23T18:04:09.317] [INFO] default -
============ Finished Install Chaincode ============
[2020-09-23T18:04:09.317] [INFO] default - Successfully installed chaincode TSProject
[2020-09-23T18:04:09.318] [INFO] default -
============ started instantiate Chaincode ============
[2020-09-23T18:04:09.366] [INFO] default - Successfully sent Proposal and received ProposalResponse
[2020-09-23T18:04:11.434] [INFO] default - The chaincode instantiate transaction has been committed on peer localhost:7051
[2020-09-23T18:04:11.434] [INFO] default - The chaincode instantiate transaction was valid.
[2020-09-23T18:04:11.435] [INFO] default - Successfully sent transaction to the orderer.
[2020-09-23T18:04:11.435] [INFO] default - Successfully instantiated chaincode TSProject on channel mychannel
[2020-09-23T18:04:11.435] [INFO] default - 
============ Finished instantiate Chaincode ============
[2020-09-23T18:04:11.4351 INFO] default - Successfully instantiated chaincode TSProject on channel mychannel 
INFO (Runtime): Chaincode TSProject installed and ready:
INFO (RunCommand): Chaincode TSProject deployed

疑難排解

在區域網路上執行鏈碼專案時,可能會發生下列問題。

遺漏 Go 權限
在區域網路安裝 Go chaincode 專案時,您可能會看到類似以下的錯誤:
My-Mac:GoProj myname$ ochain run
Starting ca.example.com    ... done
Starting orderer.example.com ... done
Starting peer0.orgl.example.com ... done
INFO (Runtime): 2020/06/22 22:57:09 build started

INFO (Runtime): Building ....

INFO (Runtime): go build runtime/cgo: copying /Users/myname/Library/Caches/go-build/f8/.….….d: open /usr/local/go/pkg/darwin_amd64/runtine/ 
cgo.a: permission denied

ERROR (Runtime): go build runtine/cgo: copying /Users/myname/Library/Caches/go-build/f8/.….….d: open /usr/local/go/pkg/darwin_amd64/runtime/
cgo.a: permission denied

INFO (Runtime): An error occurred while building: exit status 1

Stopping peer0.orgl.exmple.com ... done 
Stopping ca.example.com	... done
Stopping orderer.example.con ... done
這是因為遺漏 Go 的權限所致。此錯誤僅在 Mac OS 中出現。此為已知問題:
解決方案:變更您 $GOROOT 的權限,然後再試一次 ochain run
sudo chmod -R 777 /usr/local/go
建置失敗
由於部署失敗、部署損毀、Docker 對等容器已滿,或本機網路中已終止 Docker 對等,您可能會看到類似以下的錯誤:
============ Started instantiate Chaincode ============
[2028-19-01T19:25:lO.372] [ERROR] default - Error instantiating Chaincode GollGl on channel mychannel, detailed 
error: Error: error starting container: error starting container: Failed to generate platform-specific docker 
build: Failed to pull hyperledger/fabric-ccenv:latest : API error (404): manifest for hyperledger/ 
fabric-ccenv:latest not found: manifest unknown: manifest unknown [2020-19-01T19:25:10.372] (INFO) default -
============ Finished instantiate Chaincode ============
[2020-19-01119:25:10.372] [ERROR] default - Error: Error instantiating Chaincode Goll01 on channel mychannel, 
detailed error: Error: error starting container: error starting container: Failed to generate platfom-specific 
docker build: Failed to pull hyperledger/fabric-ccenv: latest : API error (404): manifest for hyperledger/ 
fabric-ccenv:lalest not found: manifest unknown: manifest unknown exited: signal: terminated 
INFO: exited: signal: terminated

ERROR: Error in Chaincode deployment
這是因為對等容器無法再次正常啟動。
解決方案:再次嘗試使用 ochain run 指令,但使用 -b 選項。此選項會為您重新建立執行時期。
ochain run -b
需要重新建立環境
如果您看到找不到通道錯誤或與下列文字類似的錯誤,請重新建立環境:
Starting ca.example.com ... 
Starting orderer.example.com ... 
Starting orderer.example.com ... error
ERROR: for orderer.example.com  
Cannot start service orderer.example.com: 
error while creating mount source path '/host_mnt/c/Users/opc/.vscode/extensions/oracle.oracle-blockchain-1.4.0/node_modules/@oracle/ochain-cli/runtime/network/basic-network/config': mkdir /host_mnt/c/Users/opc/.vscode/extensions/oracle.oracle-blockchain-1.4.0: operation not permitted
Starting ca.example.com... error
ERROR: for ca.example.com  
Cannot start service ca.example.com: error while creating mount source path '/host_mnt/c/Users/opc/.vscode/extensions/oracle.oracle-blockchain-1.4.0/node_modules/@oracle/ochain-cli/runtime/network/basic-network/crypto-config/peerOrganizations/org1.example.com/ca': mkdir /host_mnt/c/Users/opc/.vscode/extensions/oracle.oracle-blockchain-1.4.0: operation not permitted
ERROR: for orderer.example.com  
Cannot start service orderer.example.com: error while creating mount source path '/host_mnt/c/Users/opc/.vscode/extensions/oracle.oracle-blockchain-1.4.0/node_modules/@oracle/ochain-cli/runtime/network/basic-network/config': mkdir /host_mnt/c/Users/opc/.vscode/extensions/oracle.oracle-blockchain-1.4.0: operation not permitted
ERROR: for ca.example.com  
Cannot start service ca.example.com: error while creating mount source path '/host_mnt/c/Users/opc/.vscode/extensions/oracle.oracle-blockchain-1.4.0/node_modules/@oracle/ochain-cli/runtime/network/basic-network/crypto-config/peerOrganizations/org1.example.com/ca': mkdir /host_mnt/c/Users/opc/.vscode/extensions/oracle.oracle-blockchain-1.4.0: operation not permitted
Encountered errors while bringing up the project.
ERROR: Starting ca.example.com ... 
Starting orderer.example.com ... 
Starting orderer.example.com ... error
ERROR: for orderer.example.com  
Cannot start service orderer.example.com: error while creating mount source path '/host_mnt/c/Users/opc/.vscode/extensions/oracle.oracle-blockchain-1.4.0/node_modules/@oracle/ochain-cli/runtime/network/basic-network/config': mkdir /host_mnt/c/Users/opc/.vscode/extensions/oracle.oracle-blockchain-1.4.0: operation not permitted
Starting ca.example.com ... error
ERROR: for ca.example.com  
Cannot start service ca.example.com: error while creating mount source path '/host_mnt/c/Users/opc/.vscode/extensions/oracle.oracle-blockchain-1.4.0/node_modules/@oracle/ochain-cli/runtime/network/basic-network/crypto-config/peerOrganizations/org1.example.com/ca': mkdir /host_mnt/c/Users/opc/.vscode/extensions/oracle.oracle-blockchain-1.4.0: operation not permitted
ERROR: for orderer.example.com  
Cannot start service orderer.example.com: error while creating mount source path '/host_mnt/c/Users/opc/.vscode/extensions/oracle.oracle-blockchain-1.4.0/node_modules/@oracle/ochain-cli/runtime/network/basic-network/config': mkdir /host_mnt/c/Users/opc/.vscode/extensions/oracle.oracle-blockchain-1.4.0: operation not permitted
ERROR: for ca.example.com  
Cannot start service ca.example.com: error while creating mount source path '/host_mnt/c/Users/opc/.vscode/extensions/oracle.oracle-blockchain-1.4.0/node_modules/@oracle/ochain-cli/runtime/network/basic-network/crypto-config/peerOrganizations/org1.example.com/ca': mkdir /host_mnt/c/Users/opc/.vscode/extensions/oracle.oracle-blockchain-1.4.0: operation not permitted
Encountered errors while bringing up the project.
ERROR: Error in Chaincode deployment
若要重建本機環境,請執行下列指令:
ochain run -b

將您的鏈碼部署至遠端 Oracle Blockchain Platform 網路

在本機網路上部署並測試鏈碼專案以確保其如設計般運作之後,就可以將其部署到 Oracle Blockchain Platform。

部署您的鏈碼

用法:ochain deploy [options] [...args]

下列引數和選項可與 ochain deploy 指令搭配使用:

my-mac:TSProject myname$ ochain deploy -h 
Usage: deploy [options] [...args]
Deploy chaincode project to Oracle Blockchain Platform

Arguments:
     [...args]    (optional) Chaincode instantiate arguments. Arguments should be space separated.
 
Options :
    -h, --help                      output command usage information
    -D, --debug                     enable debug logging
    -P, --project <project>         (optional) Path to Chaincode project to deploy. If not specified, it defaults to current directory.
    -c, --channel <channel>         (optional) Blockchain Channel to deploy chaincode to. If not specified, it defaults to the 'default' channel.
    -u, --username <username>       (required) A user name that has install chaincode privileges. Contact your administrator for more details.
    -v, --userversion <userversion> (optional) A user-specified chaincode version.
                             If a version isn't specified, for a new chaincode it will start at v1 and then increment to v2, v3, and so on.
                             For an existing chaincode, v1.a will increment to v1.a1, v1 will increment to v2, and v1.0 will increment to v1.1.
    -s --sign <password>            (required) Password to authenticate user.
    -r --url <url>                  (required) URL of the remote OBP instance. Example: https://<blockchain-instance-url>:7443/

Examples:
$> ochain deploy -u <username> -s <password> -r <url of the remote OBP instance> -c <name of the channel>

輸入具備 ADMINREST_CLIENT 角色之 Oracle Blockchain Platform 使用者的 Oracle Identity Cloud Service 使用者名稱和密碼。如需有關使用者和角色的詳細資訊,請參閱設定使用者和應用程式角色

若要呼叫鏈碼,只需要 REST_CLIENT 角色。若要部署或升級鏈碼,也必須將 IDCS 使用者指派為 ADMIN 角色。

順利將鏈碼部署至遠端 Oracle Blockchain Platform 之後,日誌將顯示發生下列事件:
  • 已順利擷取 Oracle Blockchain Platform 詳細資訊。
  • 端點列表已成功抓取 。
  • 已順利安裝鏈碼專案。
  • 已順利核准並確認鏈碼專案。
  • 已順利在各個對等和通道上部署鏈碼。

在具有多個組織的環境中,若要透過參與者執行處理在相同通道上重新部署鏈碼,請使用主控台來部署鏈碼。

升級鏈碼專案

升級鏈碼是由 Blockchain App Builder 自動處理。變更鏈碼後,請再次呼叫 ochain deploy 指令,該指令會自動為您升級專案。當您再次執行 ochain deploy 命令時,請為 adminList 參數指定空白陣列,或使用 adminList 參數新增記號管理員。如果您不是第一次建置程式,則必須為 adminList 參數提供空白陣列。

如果升級成功,記錄會顯示發生下列事件:
  • 已順利擷取 Oracle Blockchain Platform 詳細資訊。
  • 端點列表已成功抓取 。
  • 已進行檢查以判斷鏈碼專案是否已安裝,如果已安裝,則會擷取版本。
  • 已順利升級鏈碼版本 (例如,從版本 1.0 升級至版本 2.0)。

在具有多個組織的環境中,若要升級鏈碼,請使用主控台並手動核准參與者的鏈碼。

將您的 Chaincode 專案封裝為 Oracle Blockchain Platform 進行手動部署

您可以封裝鏈碼專案,以便手動部署至 Oracle Blockchain Platform Cloud 或 Enterprise Edition。

用法:ochain package

package 指令會建立僅包含建置和分送檔案的歸檔檔案。不包含鏈碼專案中的 binarylibsnode_modulestest 資料夾。此封存檔案可手動上傳至 Oracle Blockchain Platform 進行部署。

由於軟體先決條件的變更,當您執行 TypeScript 鏈碼的 ochain package 命令時,系統會提示您輸入要為其建立套裝程式的 Oracle Blockchain Platform 執行處理佈建日期。在 Blockchain App Builder 中建立的 TypeScript 鏈碼與舊版的 Oracle Blockchain Platform 不相容,不會變更基礎基礎架構。Blockchain App Builder 會根據您的選擇將鏈碼基礎架構相應地封裝在一起。

my-mac:~ myname$ ochain package -h 
Usage: package [options]
Package and archive an Ochain chaincode project 
Options :
    -h, --help             output command usage information
    -D, --debug            enable debug logging
    -p, --project <path>   Path to the Ochain chaincode project to be packaged. If not specified, it defaults to current directory.
    -o, --out <path>       Path to the generated chaincode archive file. If not specified, it defaults to current directory.
About:
This CLI command packages and archives an existing chaincode project 
Examples:
$> ochain package --project <Path to the Ochain chaicode project> —out <Path to the generated chaincode archive file>

當指令順利完成時,就會傳回套裝軟體的位置。

這個指令使用兩個可選引數:
  • --project

    此選項定義要封裝的區塊鏈 App 產生器鏈碼專案位置。如果未指定,位置預設為目前的目錄。

  • --out

    此選項可用來提供所產生歸檔檔案的輸出路徑。如果沒有指定,就會預設為目前的目錄。

範例:
ochain package -p /Blockchain/DevTools/bp1/CC -o /Blockchain/DevTools/bp1/output

"Your chaincode project has been packaged at /Blockchain/DevTools/bp1/output/CC.zip"