2016年7月30日,爱就投金服,创投未来领袖私享会,陆家嘴区块链产业联盟主办的《区块链技术专题沙龙》,ethfans.org 成员张亚宁的分享 PPT。
PDF版本下载:best_practice.pdf
Smart contract best practices
安全不容忽视
General Philosophy
Prepare for failure
- bug 不可避免
- 断路机制(谁可以做,如何去处理,需要探索)
- 资金安全(频率,额度,周期等)
- 升级方案(数据迁移等,需要探索)
Roll out carefully
- 尽可能在生产环境之前发现bug
- 测试(比传统软件测试要难)
- testnet
- 迭代式
- bounty
Keep contracts simple
- 复杂容易产生bug
- 逻辑简单
- 模块化
- 区块链混合传统数据库(场景:去中心化和健壮性,信任依赖。需要探索)
Stay up to date
- 发现bug及时更新
- 尽可能最新的库和工具
- 接受新的安全技术
Be aware of blockchain properties
- 小心外部调用 (不同于传统web开发)
- private data是可以被任何人看到的 (私密性)
- 注意gas消耗和gas limit
- 调用栈深度
Known Attacks
Call Depth Attack
Race Conditions
- Reentrancy (send > call.value())
- Cross-function Race Conditions (share data)
Pitfalls in Race Condition Solutions
- Transaction-Ordering Dependence (internal work first)
- mutex (deadlocks and livelocks)
- COP
DoS with (Unexpected) Throw
- Throw 不是万能的
- 解决方案:withdraw模式
站在攻击者的角度去写代码
Software Engineering Techniques
- Smart Contract Security coding specification
- Circuit Breakers (Pause contract functionality)
- Speed Bumps (Delay contract actions)
- Rate Limiting
- Assert Guards (Token match ethers)
- Contract Rollout
- Security Tools
链接
smart-contract-best-practices:
https://github.com/ConsenSys/smart-contract-best-practices
如何使用Solidity编写安全的智能合约代码?
http://ethfans.org/rubyu2/articles/128
send 和 call 的区别
http://ethfans.org/topics/419
智能合约中常见错误及如何避免
http://ethfans.org/posts/136
面向条件的编程
http://ethfans.org/posts/condition-orientated-programming
古尔丹之手:TheDAO的攻击手法补全
http://ethfans.org/posts/117
TheDAO 攻击中 RACE To Empty
http://ethfans.org/posts/116
从技术角度剖析针对THE DAO的攻击手法
http://ethfans.org/posts/114
TheDAO 攻击代码简介
http://ethfans.org/posts/115
区块链VS数据库
http://ethfans.org/posts/blockchains-vs-centralized-databases