# 使用slither分析复杂合约继承关系

By [Danny0](https://paragraph.com/@danny0) · 2021-12-27

---

slither是Solidity的静态分析工具，工具本身会自动检查合约代码并给出一些安全建议。

该工具还自带了一些print工具，对于合约的功能分析也非常有用。

此文简单说明slither工具的安装和使用。

原理
--

1.  通过官方solc工具生成ast抽象语法树json文件（solc --ast-json contract.sol）
    
2.  解析ast-json文件，生成dot格式的graph源文件
    
3.  将dot文件转为图片格式（例如png）
    

安装
--

安装solc版本管理工具

    pip3 install solc-select
    solc-select use 0.5.17
    

安装分析工具

    pip3 install slither-analyzer --no-use-pep517
    

安装graphviz绘图工具

分析
--

以Ronin跨链桥合约为例，合约地址：[https://etherscan.io/address/0x1a2a1c938ce3ec39b6d47113c7955baa9dd454f2](https://etherscan.io/address/0x1a2a1c938ce3ec39b6d47113c7955baa9dd454f2)

将合约代码保存为一个`proxy.sol`文件中。

查看合约间的继承关系：执行如下命令会生成 `proxy.sol.inheritance-graph.dot` 文件

    slither proxy.sol --print inheritance-graph // 多个合约生成一个依赖关系图
    

将dot文件格式转换为png图片：

    dot proxy.sol.inheritance-graph.dot  -Tpng -o proxy.png
    

![](https://storage.googleapis.com/papyrus_images/85620e7c92d7fc3581e2cb0fff5167405f63df4fad127fa1ef6bf4b52da1bdd8.png)

查看其它信息：

    slither proxy.sol --print call-graph  // 针对每个合约，生成多个dot文件
    slither proxy.sol --print cfg // 针对每个函数，生成多个dot文件
    

如果合约比较复杂，绘制出的调用关系图会比较乱，所以一般就用于从整体的角度浏览各个合约间的继承关系。

`./graph.sh`

    slither $1 --print inheritance-graph
    dot $1.inheritance-graph.dot -Tpng -o $1.png
    

其它使用见官方wiki：

[https://github.com/crytic/slither/wiki/Printer-documentation#data-dependencies](https://github.com/crytic/slither/wiki/Printer-documentation#data-dependencies)

---

*Originally published on [Danny0](https://paragraph.com/@danny0/slither)*
