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

使用Hyperledger Caliper对接已部署以太坊合约的基准测试问题

解决Hyperledger Caliper与以太坊已部署合约交互问题

要实现Caliper与已部署合约的交互,需从配置和代码两方面调整,具体步骤如下:

一、修改网络配置文件

在contracts下的目标合约节点中,添加已部署合约的address字段,让Caliper直接关联到已存在的合约实例,无需重新部署。修改后的配置如下:

{
    "caliper": {
        "blockchain": "ethereum"
    },
    "ethereum": {
        "url": "ws://127.0.0.1:8545",
        "contractDeployerAddress": "0x618.......d9",
        "contractDeployerAddressPassword": "",
        "contractDeployerAddressPrivateKey": "0xe8....d936",
        "fromAddress": "0x6188....64d9",
        "fromAddressPassword": "",
        "transactionConfirmationBlocks": 12,
        "contracts": {
            "Storage": {
                "path": "Path/to/contract/Storage.json",
                "address": "0x【你的已部署合约地址】", // 新增字段,替换为实际合约地址
                "gas": {
                    "query": 100000,
                    "transfer": 70000
                }
            }
        }
    }
}

二、调整基准测试配置文件

  1. 统一ethereum.url与网络配置中的地址(当前两个配置文件url不一致,一个8545一个7545,需保持相同)
  2. 如果测试的是写操作(比如示例中的store函数),将rounds[0].readOnly设为false,避免Caliper按只读请求处理。修改后的配置如下:
caliper:
  blockchain: ethereum
  ethereum:
    url: ws://127.0.0.1:8545 # 与网络配置统一
    from: "0x618......C64d9"
    accounts:
      - privateKey: "0xe8.....d936"
        balance: "1000000000000000000000"  # Initial balance in Wei

test:
  name: basic-contract-benchmark
  description: A test benchmark
  workers:
    type: local
    number: 1
  rounds:
    - label: callFunction
      description: Call function benchmark
      txNumber: 1
      rateControl:
        type: "fixed-rate"
        opts:
          tps: 1
      workload:
        module: path/to/sample2.js
      readOnly: false # 写操作设为false

三、更新工作负载模块代码

确保请求参数正确关联已部署合约,同时可直接通过Caliper的web3实例调用合约,增强灵活性。修改后的代码如下:

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

class MyWorkload extends WorkloadModuleBase {
    constructor() {
        super();
        this.contractInstance = null;
    }

    async initializeWorkloadModule(workerIndex, totalWorkers, roundIndex, roundArguments, sutAdapter, sutContext) {
        await super.initializeWorkloadModule(workerIndex, totalWorkers, roundIndex, roundArguments, sutAdapter, sutContext);
        // 直接获取已部署的合约实例
        this.contractInstance = this.sutContext.contracts.Storage;
    }

    async submitTransaction() {
        try {
            // 方式1:使用Caliper标准请求格式
            const connectorRequest = [{
                contract: 'Storage',
                verb: 'store',
                args: [25],
                readOnly: false
            }];
            const txStatus = await this.sutAdapter.sendRequests(connectorRequest);
            console.log('交易状态:', txStatus);

            // 方式2:直接通过web3合约实例调用(可选,更灵活)
            // const tx = await this.contractInstance.methods.store(25).send({
            //     from: this.sutContext.fromAddress,
            //     gas: 70000
            // });
            // console.log('交易哈希:', tx.transactionHash);

            console.log('合约调用成功');
        } catch (error) {
            console.error('合约调用失败:', error);
            throw error; // 抛出错误让Caliper统计失败率
        }
    }
}

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

module.exports.createWorkloadModule = createWorkloadModule;

关键修改点:

  • 在initializeWorkloadModule中直接从sutContext获取已部署的合约实例,无需重新部署
  • 确保请求中的contract名称与配置文件中的合约名称完全一致
  • 抛出错误让Caliper正确统计交易失败情况,而非仅打印日志

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 18:21:15