使用 CLI 部署链代码
创建链代码项目后,您可以在本地将其部署到自动生成的 Hyperledger Fabric 网络,也可以远程部署到 Oracle Blockchain Platform Cloud 或 Enterprise Edition。您还可以打包链代码项目,以手动部署到 Oracle Blockchain Platform。
将链代码部署到本地超级账本架构网络
创建链代码项目后,您可以将其部署到本地超级账本架构网络。在安装 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_id
和user_id
。 - 对于使用 ERC-1155 标准的任何令牌,参数为
orgId
和userId
。 - 对于使用 ERC-721 标准和 TypeScript 项目的非同质代币,参数为
orgId
和userId
。 - 对于使用 ERC-721 标准和 Go 项目的非同质代币,参数为
OrgId
和UserId
。
以下示例适用于不可替换的令牌。
adminList
数组示例:'[{"userId":"userid", "orgId":"OrgMSPId"}]'
adminList
数组示例:'[{"UserId":"userid", "OrgId":"OrgMSPId"}]'
adminList
数组示例:"[{\"userId\":\"userid\", \"orgId\":\"OrgMSPId\"}]"
adminList
数组示例:"[{\"UserId\":\"userid\", \"OrgId\":\"OrgMSPId\"}]"
对于本地超级账本架构网络,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 项目时,您可能会看到类似以下内容的错误:
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
- 部署故障
- 由于部署失败、部署损坏、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
- 需要重建环境
- 如果看到 channel not found 错误或类似以下文本的错误,请重建环境:
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
将链代码部署到远程 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>
输入具有 ADMIN
或 REST_CLIENT
角色的 Oracle Blockchain Platform 用户的 Oracle Identity Cloud Service 用户名和密码。有关用户和角色的详细信息,请参阅设置用户和应用程序角色。
要调用链代码,只需要 REST_CLIENT
角色。要部署或升级链代码,还必须为 IDCS 用户分配 ADMIN
角色。
- 已成功提取 Oracle Blockchain Platform 详细信息。
- 已成功提取对等节点列表。
- 已成功安装链代码项目。
- 链代码项目已成功批准并提交。
- 链代码已成功部署在每个对等节点和渠道中。
在具有多个组织的环境中,要通过参与者实例在同一通道上重新部署链代码,请使用控制台部署链代码。
升级链代码项目
升级链代码由 Blockchain App Builder 自动处理。对链代码进行更改后,再次调用 ochain deploy
命令,这将自动为您升级项目。再次运行 ochain deploy
命令时,请为 adminList
参数指定一个空数组,或使用 adminList
参数添加令牌管理员。如果您不是首次部署者,则必须为 adminList
参数提供空数组。
- 已成功提取 Oracle Blockchain Platform 详细信息。
- 已成功提取对等节点列表。
- 已进行检查以确定链代码项目是否已安装,如果已安装,则提取版本。
- 链代码版本已成功升级(例如,从版本 1.0 升级到 2.0)。
在具有多个组织的环境中,要升级链代码,请使用控制台并手动批准参与者的链代码。
将您的链代码项目打包到 Oracle Blockchain Platform
您可以将链代码项目打包为手动部署到 Oracle Blockchain Platform Cloud 或 Enterprise Edition。
用法:ochain package
package
命令创建仅包含构建和分发文件的归档文件。不包括链代码项目中的 binary
、libs
、node_modules
和 test
文件夹。此归档文件可以手动上载到 Oracle Blockchain Platform 进行部署。
由于更改了软件先决条件,当您为 TypeScript 链代码运行 ochain package
命令时,系统会提示您输入要为其创建程序包的 Oracle Blockchain Platform 实例的预配日期。在区块链应用构建器中创建的 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
此选项定义要打包的区块链应用程序构建器链代码项目的位置。如果未指定,则该位置默认为当前目录。
--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"