首页 > 解决方案 > 如何在 IBM Cloud Functions 操作中使用 Hyperledger Fabric 节点 SDK 包?

问题描述

我正在尝试创建一个使用 Hyperledger Fabric 节点 SDK 包的 IBM Cloud Functions 区块链 node.js 操作,但在操作中需要 fabric-network 包时遇到了问题。

由于它是第 3 方包,看来我需要将操作作为压缩文件上传,但是当我这样做时,我看到:

"error": "Initialization has failed due to: Error: Failed to load gRPC binary module because it was not installed for the current system\nExpected directory: node-v57-linux-x64-glibc\nFound: [node-v57-darwin-x64-unknown]\nThis problem can often be fixed by running \"npm rebuild\" on the current system"

我想创建一个 javascript 操作,如下所示:

'use strict'

const { X509WalletMixin, Gateway } = require('fabric-network')

async function main(params) {
  return { message: 'success' }
}

处理这样的 3rd 方包的正确方法是什么?

标签: hyperledger-fabricopenwhiskibm-cloud-functions

解决方案


具有本机依赖项的 Node.js 模块需要针对与无服务器运行时相同的平台架构进行编译。如果您node_modules从本地开发机器捆绑目录,它可能不匹配。

有两种方法可以使用具有本机依赖项的库...

  1. npm install从平台映像在 Docker 容器内运行。
  2. 使用预安装的库构建自定义运行时映像。

第一种方法最简单,但只能在包含所有源文件和库的 zip 文件小于操作大小限制 (48MB) 时使用。

在运行npm install时容器内运行

  • 运行以下命令将本地目录绑定到运行时容器并运行npm install.
docker run -it -v $PWD:/nodejsAction openwhisk/action-nodejs-v10 "npm install"

这将留下一个node_modules文件夹,其中包含为正确运行时编译的本机依赖项。

  • 压缩动作源文件,包括node_modules目录。
zip -r action.zip *
  • 使用动作存档创建新动作。
ibmcloud wsk action create my-action --kind nodejs:10 action.zip

构建自定义运行时映像

  • Dockerfile使用构建期间运行的npm install命令创建一个。
FROM openwhisk/action-nodejs-v10

RUN npm install fabric-network
  • 构建镜像并将其推送到 Docker Hub。
$ docker build -t <USERNAME>/custom-runtime .
$ docker push <USERNAME>/custom-runtime
  • 使用自定义运行时映像创建新操作。
ibmcloud wsk action create my-action --docker <USERNAME>/custom-runtime action.zip

确保node_modules包含在action.zip不包含相同的库文件。


推荐阅读