使用 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 網路、其他服務,並為您安裝和部署鏈碼。

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   

安裝 Blockchain App Builder 時所建立的測試網路會執行 Hyperledger Fabric v2.5.7 和 Hyperledger Fabric CA v1.5.9。

權杖專案

對於權杖鏈碼專案,您必須使用 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

以下為不可行記號的範例。

範例 macOS 和 Linux 上 TypeScript 的 adminList 陣列:
'[{"userId":"userid", "orgId":"OrgMSPId"}]'
Go macOS 和 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 選項傳送至命令。在 Windows 上,如果您指定 --debug 選項,請使用命令提示字元而不是 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 專案時,可能會看到類似以下文字的錯誤:
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 指令搭配使用:

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) Restproxy Url of the remote OBP instance.
    For example,
    OBP Cloud Services: https://test-xyz-abc.blockchain.ocp.oraclecloud.com:7443/restproxy
    OBP Enterprise Edition: https://restproxy.test-xyz-abc.blockchain.platform.com:443/restproxy

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

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

若要呼叫鏈碼,只需要 REST_CLIENT 角色。若要部署或升級鏈碼,也必須為 Identity Cloud Service 使用者指定 ADMIN 角色。

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

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

升級 Chaincode 專案

區塊鏈 App 產生器會自動處理升級鏈碼。變更鏈碼後,請再次呼叫 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 會根據您的選擇,對鏈碼基礎架構進行相應的封裝。

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"