使用 Visual Studio Code 部署链代码

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

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

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

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

区块链应用构建器链代码部署可启动超级账本架构基本网络、其他服务,并为您安装和部署链代码。

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

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

疑难解答

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

缺少 Go 权限
在本地网络中安装 Go 链代码项目时,您可能会在输出控制台中看到类似于以下内容的错误:
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 Network

在本地网络上部署并测试链代码项目以确保其按设计工作后,您可以将其部署到 Oracle Blockchain Platform。

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

您必须启动并运行区块链平台实例才能完成此步骤。

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

部署您的链代码

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

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

升级 Chaincode 项目

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

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

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

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

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

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

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

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

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