You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Caliper测试Fabric链码报无有效节点响应错误

Hyperledger Fabric TypeScript链码Caliper基准测试链码启动失败问题

操作背景

对指定开源仓库内TypeScript编写的Hyperledger供应链智能合约开展性能基准测试,参考Hyperledger Caliper v0.5.0对接已有Fabric网络的官方教程操作,运行环境为WSL2后端的Docker Desktop + WSL2。

执行步骤

  • 克隆目标项目仓库后,在同级目录创建caliper-workspace工作目录
  • 进入项目的Fabric网络脚本目录,执行自带脚本启动Fabric网络、创建名为mychannel的通道、部署TypeScript版本的supplychain链码,执行命令如下:
./network.sh up createChannel -ca -s couchdb
./network.sh deployCC -ccn supplychain -ccp ../../chaincode -ccl typescript
  • 在caliper-workspace目录下新建networks、benchmarks、workload三个子目录,分别存放Caliper运行所需的三个配置文件:networks/networkConfig.yaml、benchmarks/myAssetBenchmark.yaml、workload/readAsset.js,文件内容分别如下:

myAssetBenchmark.yaml

test:
name: basic-contract-benchmark
description: test benchmark
workers:
  number: 2
rounds:
  - label: readAsset
    description: Read asset benchmark
    txDuration: 30
    rateControl:
      type: fixed-load
      opts:
        transactionLoad: 2
    workload:
      module: workload/readAsset.js
      arguments:
        assets: 10
        contractId: supplychain

networkConfig.yaml

name: Caliper test
version: "2.0.0"

caliper:
  blockchain: fabric

channels:
  - channelName: mychannel
    contracts:
    - id: supplychain

organizations:
  - mspid: Org1MSP
    identities:
      certificates:
      - name: 'User1'
        clientPrivateKey:
          path: '../hyperledger-supply-chain/network/fabric-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/keystore/46d82eb2dbaed33f941f9a39394bba7b5167d09d116ba563de1675f22b083d3e_sk'
        clientSignedCert:
          path: '../hyperledger-supply-chain/network/fabric-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/signcerts/cert.pem'
    connectionProfile:
      path: '../hyperledger-supply-chain/network/fabric-network/organizations/peerOrganizations/org1.example.com/connection-org1.yaml'
      discover: true

readAsset.js

'use strict';

const { WorkloadModuleBase } = require('@hyperledger/caliper-core');

class MyWorkload extends WorkloadModuleBase {
    constructor() {
        super();
    }

    async initializeWorkloadModule(workerIndex, totalWorkers, roundIndex, roundArguments, sutAdapter, sutContext) {
        await super.initializeWorkloadModule(workerIndex, totalWorkers, roundIndex, roundArguments, sutAdapter, sutContext);

        for (let i=0; i<this.roundArguments.assets; i++) {
            const id = `${this.workerIndex}_${i}`;
            console.log(`Worker ${this.workerIndex}: Creating asset ${id}`);
            const request = {
                contractId: this.roundArguments.contractId,
                contractFunction: 'createProduct',
                invokerIdentity: 'User1',
                contractArguments: [id, '1234567890', 50, 'Fruit Juices', [],
                '2022-06-24T18:25:43.511Z', JSON.stringify({}),'Apple Juice', 'Etobicoke, ON, Canada',
                '2021-06-24T18:25:43.511Z','$9.00', 200, 'ml'],
                readOnly: false
            };

            await this.sutAdapter.sendRequests(request);
        }
    }

    async submitTransaction() {
        const randomId = Math.floor(Math.random()*this.roundArguments.assets);
        const myArgs = {
            contractId: this.roundArguments.contractId,
            contractFunction: 'readProduct',
            invokerIdentity: 'User1',
            contractArguments: [`${this.workerIndex}_${randomId}`],
            readOnly: true
        };

        await this.sutAdapter.sendRequests(myArgs);
    }

    async cleanupWorkloadModule() {
        for (let i=0; i<this.roundArguments.assets; i++) {
            const assetID = `${this.workerIndex}_${i}`;
            console.log(`Worker ${this.workerIndex}: Deleting asset ${assetID}`);
            const request = {
                contractId: this.roundArguments.contractId,
                contractFunction: null,
                invokerIdentity: 'User1',
                contractArguments: [id],
                readOnly: false
            };

            await this.sutAdapter.sendRequests(request);
        }
    }
}

function createWorkloadModule() {
    return new MyWorkload();
}

module.exports.createWorkloadModule = createWorkloadModule;
  • 在caliper-workspace目录下安装Caliper依赖,绑定Fabric 2.2版本适配层,执行命令如下:
npm install --only=prod @hyperledger/caliper-cli@0.5.0
npx caliper bind --caliper-bind-sut fabric:2.2
  • 在caliper-workspace目录执行以下命令启动性能基准测试:
npx caliper launch manager --caliper-workspace ./ --caliper-networkconfig networks/networkConfig.yaml --caliper-benchconfig benchmarks/myAssetBenchmark.yaml --caliper-flow-only-test

报错现象

测试执行过程中交易提交失败,两个peer节点均返回500错误,交易模拟阶段失败,核心报错如下:

节点返回链码启动失败,链码注册失败,对应链码容器以状态码0退出,涉及节点为peer0.org1.example.com:7051、peer0.org2.example.com:9051

error: [Transaction]: Error: No valid responses from any peers. Errors:
peer=peer0.org2.example.com:9051, status=500, message=error in simulation: failed to execute transaction b6cc9ab28537a5539aa0876a823a679a952dee1232540e8efbe1ad01e02ebfbb: could not launch chaincode supplychain_1.0:a38d65123f915106ad4d0f8893b0880ed2979ddcc84106b9267f5ee31ddd4: chaincode registration failed: container exited with 0
peer=peer0.org1.example.com:7051, status=500, message=error in simulation: failed to execute transaction b6cc9ab28537a5539aa0876a823a679a952dee1232540e8efbe1ad01e02ebfbb: could not launch chaincode supplychain_1.0:a38d65123f915106ad4d0f8893b0880ed2979ddcc84106b9267f5ee31ddd4: chaincode registration failed: container exited with 0

createProduct交易提交失败,错误原因与上述一致,链码容器启动异常退出

error [caliper] [connectors/v2/FabricGateway]   Failed to perform submit transaction [createProduct] using arguments [1_0,1234567890,50,Fruit Juices,,2022-06-24T18:25:43.511Z,{},Apple Juice,Etobicoke, ON, Canada,2021-06-24T18:25:43.511Z,$9.00,200,ml],  with error: Error: No valid responses from any peers. Errors:
    peer=peer0.org2.example.com:9051, status=500, message=error in simulation: failed to execute transaction 5837e5ab8b41016290a3d204f4ffde53bad977e917eb1ae53784623959d4e359: could not launch chaincode supplychain_1.0:a38d65123f915106ad4d0f8893b0880ed2979ddcc84106b9267f5ee31ddd4: chaincode registration failed: container exited with 0
    peer=peer0.org1.example.com:7051, status=500, message=error in simulation: failed to execute transaction 5837e5ab8b41016290a3d204f4ffde53bad977e917eb1ae53784623959d4e359: could not launch chaincode supplychain_1.0:a38d65123f915106ad4d0f8893b0880ed2979ddcc84106b9267f5ee31ddd4: chaincode registration failed: container exited with 0

排查修复步骤

按优先级从高到低排查:

  • 先确认目录结构与配置路径正确:启动命令指定的三个配置文件必须放在对应子目录下,不要直接放在工作区根目录。networkConfig.yaml里硬编码的私钥文件名是固定哈希值,每次重启Fabric网络后keystore下的私钥文件名会重新生成,必须替换成当前网络实际生成的私钥路径,不要直接照搬示例里的文件名。
  • 定位链码容器退出的具体原因:报错仅提示链码容器退出码0,没有具体错误信息,直接执行docker ps -a找到状态为Exited的supplychain对应链码容器,复制容器ID后执行docker logs <容器ID>就能看到链码启动时的完整输出。WSL2环境下优先检查Docker文件挂载权限,这类问题占WSL2环境Fabric链码启动失败的70%以上,通常是容器没有权限读取链码运行时文件导致启动后直接退出。
  • 脱离Caliper验证链码可用性:先进入Fabric网络的cli容器,手动调用一次createProduct接口,确认链码本身能正常安装、提交、调用。TypeScript链码部署时要确认链码目录下依赖安装完成、npm run build编译成功,生成的dist目录下入口文件存在,排除部署脚本执行异常导致的链码未正确编译/提交问题。
  • 修正工作负载脚本的代码错误:提供的readAsset.js存在两个明显bug,一是cleanupWorkloadModule循环里引用了未定义的id变量,实际定义的变量名是assetID;二是删除资产时contractFunction设为了null,必须替换成链码实际存在的删除资产方法名,否则就算链码正常启动,清理阶段也会报错。
  • 确认版本匹配关系:Caliper v0.5.0绑定fabric:2.2适配层只能对接Fabric 2.2.x版本网络,如果项目脚本启动的是2.4及以上版本的Fabric网络,需要重新执行caliper bind命令绑定对应版本的Fabric适配层,版本不匹配会出现链码生命周期调用、网关连接异常。

内容的提问来源于stack exchange,提问作者CristianJTC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 11:57:13