使用 Visual Studio Code 部署链代码

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

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

创建链代码项目后,您可以在本地 Hyperledger Fabric 基本网络中对其进行测试。

当您为 Visual Studio Code 安装 Blockchain App Builder 扩展时,它会自动创建一个包含单个通道的 Hyperledger Fabric 网络。这在环境窗格中列为 Local Environment。您无法删除或修改此环境;您只能向其部署链代码,并在链代码停止正常工作时重新构建它。

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

Blockchain App Builder 链代码部署将启动 Hyperledger Fabric 基本网络和其他服务,并为您安装和部署链代码。

  1. 在 Chaincode Details 窗格中,选择 Deploy(部署)。
  2. 在部署向导中:
    • 确保选择了正确的链代码名称。
    • 选择目标环境。对于本地部署,请选择本地环境。
    • 选择要部署到的渠道。安装扩展时,默认情况下会创建一个名为 "mychannel" 的通道,该通道可用于测试。
    • (可选)输入所需的任何初始参数。
    • 对于令牌项目,首次部署时必须输入令牌管理员列表作为参数。该列表是一个 {user_id, org_id} 信息数组,用于指定令牌管理员。对于本地 Hyperledger Fabric 网络,请将值 Org1MSP 用于 org_id 字段。对于 NFT 链代码,adminList 参数的关键字为 userId,TypeScript 的关键字为 orgId,Go 的关键字为 UserId 和 OrgId。首次部署后,您可以为 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. 在 Environments Details 向导中:
    • 输入远程环境的名称。
    • 输入说明。
    • 在 REST 代理 URL 中,输入远程 Oracle Blockchain Platform 实例的 REST 代理 URL。
    • 输入具有 ADMIN 或 REST_CLIENT 角色的 Oracle Blockchain Platform 用户的 Oracle Identity Cloud Service 用户名和密码。要调用链代码,仅需要 REST_CLIENT 角色。要部署或升级链代码,还必须向 Identity Cloud Service 用户分配 ADMIN 角色。有关用户和角色的更多信息,请参见 Administering Oracle Blockchain Platform 中的 Set Up Users and Application Roles 。
  3. 单击 Create(创建)保存环境。用户名和密码将临时保存在本地 Visual Studio Code 会话中。如果关闭 Visual Studio Code,然后启动新的 Visual Studio Code 会话,则必须再次输入用户名和密码。

部署链代码

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

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

升级 Chaincode 项目

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

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

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

将您的 Chaincode 项目打包,以手动部署到 Oracle Blockchain Platform

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

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

  1. 在 Chaincodes 窗格中选择链代码项目。
  2. 右键单击并选择 Package 。
  3. 选择要将软件包保存到的位置,然后单击 Select Output Folder(选择输出文件夹)。

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

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