使用 CLI 部署您的 Chaincode

建立鏈碼專案之後,您可以在本機將其部署到自動產生的 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"}]'
Mac OSX 和 Linux 上 Go 的範例 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 Network

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

部署您的 Chaincode

用法: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 角色。若要部署或升級鏈碼,也必須將 ADMIN 角色指派給 IDCS 使用者。

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

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

升級 Chaincode 專案

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

如果升級成功,記錄將顯示發生下列事件:
  • 已順利擷取 Oracle Blockchain Platform 詳細資訊。
  • 已順利擷取對等清單。
  • 已進行檢查以判斷是否已安裝 chaincode 專案,如果已安裝,則會擷取版本。
  • 鏈碼版本已成功升級 (例如,從 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"