手把手教你用Echidna测试智能合约
下面介绍下 Echidna 的使用。
准备
- 下载 docker,并且安装。
- 下载对应的仓库
docker pull trailofbits/eth-security-toolbox
- 建立测试目录,并且进入当前目录。
docker run -it --rm -v $PWD:/code trailofbits/eth-security-toolbox
rm:在容器启动时设置--rm 选项,这样在容器退出时就能够自动清理容器内部的文件系统
控制台出现如下,则说明安装成功。
┌── ┌──┐ ──┬──
├─ └──┐ │
└──THEREUM─┴──┴ECURITY─┴OOLBOX
https://github.com/trailofbits/eth-security-toolbox
by ################
##########TRAIL#
####
#### ###########
#### ###########
\### #### ####
/\\# of #### ####
/ \ ############
\__/ #### ####
\### ####
/\\#########
/__\\##BITS#
Security Tools and Resources Installed:
https://github.com/trailofbits/echidna
https://github.com/trailofbits/etheno
https://github.com/trailofbits/manticore
https://github.com/trailofbits/slither
https://github.com/trailofbits/rattle
https://github.com/trailofbits/not-so-smart-contracts
- 查看编译版本
solc --version
solc, the solidity compiler commandline interface
Version: 0.8.9+commit.e5eed63a.Linux.g++
- 切换 solidity 版本
ethsec@36ba204276db:/code$ solc-select install 0.7.6
Installing '0.7.6'...
Version '0.7.6' installed.
ethsec@36ba204276db:/code$ solc-select use 0.7.6
Switched global version to 0.7.6
ethsec@36ba204276db:/code$ solc --version
solc, the solidity compiler commandline interface
Version: 0.7.6+commit.7338295f.Linux.g++
测试入门
参考源码:https://github.com/darrenli6/echidna_example
- 测试
ethsec@36ba204276db:/code$ echidna-test TestEchidna.sol --contract TestCounter
- 测试过程中,如果等待时间长,我们可以限制测试次数。
ethsec@36ba204276db:/code$ echidna-test TestEchidna.sol --test-limit 500 --contract TestCounter
- 如果我们想要测试失败结果,在合约内部加入如下逻辑,返回 false 即可
function echidna_test_fails() public view returns(bool){ return false; }
测试进阶
- 测试
源码[3]
当执行第六次的时候,就出现 failed.
- 测试 assert[4]
ethsec@36ba204276db:/code$ echidna-test TestEchidna4.sol --contract TestAssert --check-asserts
当输入 10 的时候,会出现失败。
- 测试绝对数函数
源码[5]
ethsec@36ba204276db:/code$ echidna-test TestEchidna4.sol --contract TestAssert --check-asserts
- 测试时间戳 block.timestamp
源码[6]
ethsec@36ba204276db:/code$ echidna-test TestEchidnaTestTimeAndCaller.sol --test-limit 5000
当修改源码为
uint delay =70 days;
说明 Echidna 无法快进 70 天,因此没有修改 pass 的状态。
- 设置快进时间,time.yaml,可以快进 70 天
ethsec@36ba204276db:/code$ echidna-test TestEchidnaTestTimeAndCaller.sol --test-limit 5000 --config time.yaml
参考资料
猜你喜欢
比特币提现会被银行查吗?
比特币提现会被银行查吗? 首先,根据《中华人民共和国反洗钱法》、《金融机构大额交易和可疑交易报告管理办法》、《金融机构报告涉嫌恐怖融资的可疑交易管理办法》等法律法规的相关规定,银行会对大额资金的流动做监控,主要是审查来源是否合法,是否涉嫌洗钱。
2022-05-21
比特币暴跌50%!30岁老公玩比特币输了好多钱
比特币暴跌50%!30岁老公玩比特币输了好多钱 过去的一周里,作为一个游曳在币圈边缘的键盘侠,见识了币圈度日如年的跌宕后,仍可以笑看潮起潮落。
2022-05-21
UST爆雷之后,USDT也要爆雷了?
这几天的行情,证明了良心哥的推测非常准确。 首先是5月10日分析luna背后是被人开了黑枪,并且持续看空luna。 次日消息实锤,luna再次跌了个99%。 昨天分析说,luna的死亡螺旋会带崩大盘。
2022-05-21
Luna币7天蒸发2000亿,但更怕的是熊市即将到来!
心哥昨天虽然不知道这里边的细节,但依然非常确定的告诉大家,这是一场狙击战,找的就是这个空档,打出来的子弹是要人命的。 另外排队枪毙这个场景,估计今天很多人也领教了。
2022-05-21
一天蒸发400亿人民币,Luna是如何被狙击的?
你们也都知道良心哥炒币是个渣渣,但良心哥的判断大体还是准确的。 可能这就是从业时间久了的盘感吧。 有人说luna的暴跌,ust抛锚,都他吗赖孙宇晨。 从5月5号孙宇晨宣布进军算法稳定币之后,大盘就崩了
2022-05-21
上一篇
创建并部署ERC20代币
下一篇
使用Echidna测试智能合约库
评论