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

运行Echidna时为带构造函数参数的合约传参失败求助

为Echidna测试的合约传入构造函数参数的正确方法

你之前尝试的两种方式都不符合Echidna的参数规则:

  • --args 不是Echidna的有效命令行选项,因此会提示无效选项
  • --solc-args 是用来传递给Solidity编译器(solc)的编译参数(比如优化级别、编译器版本等),不能用来指定合约构造函数的参数

下面是两种正确的实现方式:

方法1:编写测试合约继承目标合约

创建一个测试合约,继承你要测试的EchidnaSolution合约,在测试合约的构造函数中直接传入目标合约所需的构造参数,然后让Echidna测试这个测试合约。

示例代码:

// 你的目标合约
contract EchidnaSolution {
    address public immutable owner;

    constructor(address _owner) {
        owner = _owner;
    }

    // 合约逻辑及Echidna测试属性
    function testNotZeroOwner() public view returns (bool) {
        return owner != address(0);
    }
}

// 测试合约:继承并传入构造参数
contract TestEchidnaSolution is EchidnaSolution {
    // 在父类构造函数中传入指定参数
    constructor() EchidnaSolution(0x0000000000000000000000000000000000000001) {}
}

运行命令:

echidna echidna_tests.sol --contract TestEchidnaSolution

方法2:使用Echidna配置文件

创建一个YAML格式的配置文件(比如echidna_config.yaml),在其中指定目标合约和构造函数参数。

配置文件内容:

# 指定要测试的合约名称
contract: "EchidnaSolution"
# 按顺序传入构造函数参数,注意类型匹配
constructorArgs: ["0x0000000000000000000000000000000000000001"]

运行命令:

echidna echidna_tests.sol --config echidna_config.yaml

注意:如果构造函数有多个参数,只需在constructorArgs列表中按顺序添加对应值即可,比如参数为address, uint256,则配置为constructorArgs: ["0x...", "123"]。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 16:07:39