使用 Visual Studio Code 部署链代码

创建链代码项目后,您可以在本地将其部署到自动生成的 Hyperledger Fabric 网络,也可以远程部署到 Oracle Blockchain Platform Cloud 或 Enterprise Edition。您还可以打包链代码项目,以手动部署到 Oracle Blockchain Platform。

将链代码部署到本地超级账本架构网络

创建链代码项目后,您可以在本地超级账本架构基本网络中对其进行测试。

为 VS Code 安装 Blockchain App Builder 扩展时,它会自动创建一个具有单个通道的 Hyperledger Fabric 网络。这将在环境窗格中列为 Local Environment。您无法删除或修改此环境;您只需将链代码部署到它并重建它(如果它停止正常工作)。

安装 Blockchain App Builder 时创建的测试网络运行 Hyperledger Fabric v2.5.7 和 Hyperledger Fabric CA v1.5.9。

区块链应用程序构建器链代码部署将启动 Hyperledger Fabric 基本网络和其他服务,并为您安装和部署链代码。

  1. Chaincode Details 窗格中,选择 Deploy(部署)
  2. 在部署向导中:
    • 确保选择了正确的链代码名称。
    • 选择目标环境 - 对于本地部署,请选择本地环境
    • 选择要部署到的渠道。缺省情况下,使用扩展的安装创建名为 "mychannel" 的通道,可用于测试。
    • (可选)输入可能需要的任何初始参数。
    • 对于令牌项目,首次部署时,必须输入令牌管理员列表作为参数。该列表是 {user_id, org_id} 信息数组,用于指定令牌管理员。对于本地 Hyperledger Fabric 网络,使用 org_id 字段的值 Org1MSP。对于 NFT 链代码,adminList 参数的关键字为 userIdorgId(对于 TypeScript)和 UserId(对于 Go),OrgId(对于 Go)。首次部署后,可以为 adminList 参数提供空数组,也可以使用 adminList 参数添加令牌管理员。非首次部署者的其他部署者必须为 adminList 参数提供空数组。为此,请在部署窗格中打开 Init 参数列表,然后单击 adminList 参数旁边的减号(–)按钮,这将选择一个空数组。
  3. 单击部署

当链代码完成部署后, Output(输出)控制台将声明它已成功安装并在给定通道上部署。

疑难解答

在本地网络上运行链代码项目时,您可能会遇到以下问题。

缺少 Go 权限
在本地网络中安装 Go 链代码项目时,您可能会在 Output 控制台中看到类似以下内容的错误:
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
这是因为缺少 Go 权限。此错误仅在 Mac OS 中出现。这是已知问题:
解决方法:更改 $GOROOT 的权限,然后重新尝试部署:
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
这是因为对等容器无法再次正确启动。
解决方法:通过在环境窗格中选择本地环境,右键单击并选择重建本地环境来重建运行时。尝试重新部署。
需要重建环境
您可能会看到类似的错误:
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

创建与 Oracle Blockchain Platform 实例的连接配置

您必须启动并运行 Oracle Blockchain Platform 实例才能完成此步骤。

  1. 在 Visual Studio Code 环境窗格中,单击创建环境图标。
  2. 环境详细信息向导上:
    • 输入远程环境的名称。
    • 输入说明。
    • REST 代理 URL 中,输入远程 Oracle Blockchain Platform 实例的 REST 代理 URL。
    • 输入具有 ADMINREST_CLIENT 角色的 Oracle Blockchain Platform 用户的 Oracle Identity Cloud Service 用户名和密码。要调用链代码,只需要 REST_CLIENT 角色。要部署或升级链代码,还必须为 IDCS 用户分配 ADMIN 角色。有关用户和角色的详细信息,请参阅《管理 Oracle Blockchain Platform Enterprise Edition 》中的设置用户和应用程序角色
  3. 单击创建可保存环境。用户名和密码将临时保存在本地 Visual Studio Code 会话中。如果关闭 Visual Studio Code,然后启动新的 Visual Studio Code 会话,则必须再次输入用户名和密码。

部署链代码

  1. Chaincodes 窗格中选择要部署的链代码项目。
  2. Chaincode Details 窗格中,选择 Deploy(部署)
  3. 在部署向导中,应预先填充链代码项目的名称。
    • 选择您的目标环境 - 对于远程部署,请选择您之前设置的 Oracle Blockchain Platform 环境。
    • 输入要部署到的渠道的名称。
    • (可选)设置任何必需的初始参数。
  4. 单击部署
将链代码成功部署到远程 Oracle Oracle Blockchain Platform 后,控制台日志将显示发生了以下事件:
  • 已成功提取 Oracle Blockchain Platform 详细信息。
  • 已成功提取对等节点列表。
  • 已成功安装链代码项目。
  • 链代码项目已成功批准并提交。
  • 链代码已成功部署在每个对等节点和渠道中。

在具有多个组织的环境中,要通过参与者实例在同一通道上重新部署链代码,请使用控制台部署链代码。

升级链代码项目

升级链代码由 Blockchain App Builder 自动处理。对链代码进行更改后,请重新部署,这将自动为您升级项目。运行升级过程时,可以为 adminList 参数传递空数组,也可以使用 adminList 参数添加令牌管理员。如果您不是首次部署者,则必须为 adminList 参数提供空数组。要传递 adminList 参数的空数组,请在部署窗格中打开 Init 参数列表,然后单击 adminList 参数旁边的减号(–)按钮,这将选择空数组。

如果升级成功,则日志将显示发生了以下事件:
  • 已成功提取 Oracle Blockchain Platform 详细信息。
  • 已成功提取对等节点列表。
  • 已进行检查以确定链代码项目是否已安装,如果已安装,则提取版本。
  • 链代码版本已成功升级(例如,从版本 1.0 升级到 2.0)。

在具有多个组织的环境中,要升级链代码,请使用控制台并手动批准参与者的链代码。

将您的链代码项目打包到 Oracle Blockchain Platform

您可以将链代码项目打包为手动部署到 Oracle Blockchain Platform Cloud 或 Enterprise Edition。

Package 函数创建仅包含构建和分发文件的归档文件。不包括链代码项目中的 binarylibsnode_modulestest 文件夹。此归档文件可以手动上载到 Oracle Blockchain Platform 进行部署。

  1. Chaincodes 窗格中选择您的链代码项目。
  2. 右键单击,选择程序包
  3. 选择要将程序包保存到的位置,然后单击选择输出文件夹

由于更改了软件先决条件,当您为 TypeScript 链代码运行 Package 函数时,系统会提示您输入要为其创建程序包的 Oracle Blockchain Platform 实例的预配日期。在区块链应用构建器中创建的 TypeScript 链代码与以前版本的 Oracle Blockchain Platform 不兼容,不更改底层基础设施。Blockchain App Builder 根据您的选择相应地打包链代码基础设施。

当命令成功完成时,将返回软件包的位置。