Tollcraft/soroban-cost-linter

GitHub: Tollcraft/soroban-cost-linter

面向 Stellar Soroban 智能合约的 Rust 静态分析 linter,在编译期检测会导致高昂资源计费的结构性代码反模式。

Stars: 30 | Forks: 18

soroban-cost-linter

The static analysis shield for Soroban smart contracts

CI Status License

Documentation · Demo

`soroban-cost-linter` 是一款面向 Stellar 智能合约开发者的静态分析工具。它会在编译前分析你的 Rust 代码,以检测出那些与输入无关且在结构上存在高昂开销的模式,这些模式会不必要地增加你的 Soroban 资源计量和网络费用。 该工具作为 Tollcraft 双层成本流水线中的预防性屏障,在概念上与我们的运行时测试工具 [`soroban-budget-assert`](https://github.com/Tollcraft/soroban-budget-assert) 搭配使用。 ## 问题所在 Soroban 会对 CPU 指令、内存分配和存储操作收费。虽然通过针对网络测试你的合约是衡量*依赖于输入的*成本(例如无界循环或动态 vector 大小调整)的唯一方法,但许多昂贵的错误在结构上非常明显,甚至不需要运行代码就能看出来。 在 `for` 循环内编写 `env.storage().instance().set()` 在数学上就注定了其开销会很高。`soroban-cost-linter` 可以直接在你的 IDE 或 CI/CD 流水线中捕获这些结构性反模式,防止它们进入测试网。 ## 功能 该 linter 挂载到 Rust 编译器的 AST 中,以捕获特定的 Soroban 反模式。`v0.1.1` 版本内置了三个 lint: * **[`soroban_storage_in_loop`](docs/lints/soroban_storage_in_loop.md):** 标记位于循环体内的存储读/写操作,并建议改用内存聚合。 * **[`redundant_env_clone`](docs/lints/redundant_env_clone.md):** 检测在 Soroban `Env` 对象上不必要的 `.clone()` 调用。 * **[`unnecessary_host_function_call`](docs/lints/unnecessary_host_function_call.md):** 识别对 host 函数(例如获取账本序列号)的冗余调用,此类函数本应只调用一次并绑定到局部变量。 ## 它如何融入 Tollcraft `soroban-cost-linter` 旨在作为你的成本感知流水线的第 1 阶段: 1. **Linter (`soroban-cost-linter`):** 在编译时(或通过 `cargo check`)运行。捕获明显的静态结构性缺陷。 2. **Assert (`soroban-budget-assert`):** 在测试时运行。将你通过 lint 检查的代码与网络进行模拟,以基于真实的 runtime 输入衡量实际的执行成本。 这两个工具通过统一的 `budget.toml` 配置文件来共享阈值和抑制规则。 ## 快速开始 ### 前置条件 由于 `soroban-cost-linter` 直接挂载到 Rust 的 AST 上,它依赖于 [Dylint](https://github.com/trailofbits/dylint) 来运行动态库 lint。该 linter 库需要 `^6.0.1` 版本的 Dylint。 ``` cargo install cargo-dylint dylint-link --version "^6.0.1" ``` ### 安装说明 将 linter 添加到你的 Soroban 工作区: ``` cargo install --git https://github.com/Tollcraft/soroban-cost-linter.git cargo-cost-lint ``` ### 运行说明 在整个工作区运行 linter: ``` cargo cost-lint ``` 要抑制误报或刻意为之的高开销操作,我们完全支持标准的 Rust 属性。请将其直接放置在被标记的函数或代码块上方: ``` #[allow(soroban_storage_in_loop)] fn deliberate_storage_loop(env: Env) { for item in items { // Deliberate storage loop } } ``` ### 配置 (`budget.toml`) 你可以在 `soroban-budget-assert` 所使用的同一个 `budget.toml` 文件中,定义项目级别的 lint 规则和严重性等级。请将其放置在你的工作区根目录下: ``` [lints] # 设置为 "warn"、"deny" 或 "allow" soroban_storage_in_loop = "deny" redundant_env_clone = "warn" unnecessary_host_function_call = "warn" ``` ## 维护者 | 名称 | 角色 | 联系方式 | |---|---|---| | [mallison031](https://github.com/mallison031) | 维护者 | [GitHub](https://github.com/mallison031) | | Tollcraft 团队 | 核心维护者 | [Tollcraft 的 Telegram](https://t.me/+Gflo5jZStw1jMjE0) | ## 贡献者 [![Contributors](https://contrib.rocks/image?repo=Tollcraft/soroban-cost-linter)](https://github.com/Tollcraft/soroban-cost-linter/graphs/contributors)
标签:LNA, Rust, Stellar, 云安全监控, 代码审查, 区块链, 可视化界面, 智能合约, 网络流量审计, 通知系统, 静态分析