solidity错误处理
官方文档:
https://solidity.readthedocs.io/en/develop/control-structures.html#error-handling-assert-require-revert-and-exceptions
require和assert方法的区别:
https://ethereum.stackexchange.com/questions/15166/difference-between-require-and-assert-and-the-difference-between-revert-and-thro
程序示例:
pragma solidity ^0.4.;
contract Sharer {
function sendHalf(address addr) public payable returns (uint balance) {
require(msg.value % == ); // Only allow even numbers
uint balanceBeforeTransfer = this.balance;
addr.transfer(msg.value / );
// Since transfer throws an exception on failure and
// cannot call back here, there should be no way for us to
// still have half of the money.
assert(this.balance == balanceBeforeTransfer - msg.value / );
return this.balance;
}
}
require和assert适用范围:
An assert-style exception is generated in the following situations:
- If you access an array at a too large or negative index (i.e.
x[i]wherei >= x.lengthori < 0). - If you access a fixed-length
bytesNat a too large or negative index. - If you divide or modulo by zero (e.g.
5 / 0or23 % 0). - If you shift by a negative amount.
- If you convert a value too big or negative into an enum type.
- If you call a zero-initialized variable of internal function type.
- If you call
assertwith an argument that evaluates to false.
A require-style exception is generated in the following situations:
- Calling
throw. - Calling
requirewith an argument that evaluates tofalse. - If you call a function via a message call but it does not finish properly (i.e. it runs out of gas, has no matching function, or throws an exception itself), except when a low level operation
call,send,delegatecallorcallcodeis used. The low level operations never throw exceptions but indicate failures by returningfalse. - If you create a contract using the
newkeyword but the contract creation does not finish properly (see above for the definition of “not finish properly”). - If you perform an external function call targeting a contract that contains no code.
- If your contract receives Ether via a public function without
payablemodifier (including the constructor and the fallback function). - If your contract receives Ether via a public getter function.
- If a
.transfer()fails.
Internally, Solidity performs a revert operation (instruction 0xfd) for a require-style exception and executes an invalid operation (instruction 0xfe) to throw an assert-style exception. In both cases, this causes the EVM to revert all changes made to the state. The reason for reverting is that there is no safe way to continue execution, because an expected effect did not occur. Because we want to retain the atomicity of transactions, the safest thing to do is to revert all changes and make the whole transaction (or at least call) without effect. Note that assert-style exceptions consume all gas available to the call, while require-style exceptions will not consume any gas starting from the Metropolis release.
总得来说,两者都会使EVM回滚交易,不会造成任何变更。require适用于做输入检查,assert用来检查运行时内部错误(比如数组下标越界和除零错误)。require在Solidity Metropolis版本后将不消耗gas,而assert会消耗完所有的gas(gaslimit)。
https://medium.com/taipei-ethereum-meetup/%E6%AF%94%E8%BC%83-require-assert-%E5%92%8C-revert-%E5%8F%8A%E5%85%B6%E9%81%8B%E4%BD%9C%E6%96%B9%E5%BC%8F-30c24d534ce4
solidity错误处理的更多相关文章
- solidity 错误
solidity版本 0.7.5 Member "transfer" not found or not visible after argument-dependent looku ...
- 智能合约语言 Solidity 教程系列9 - 错误处理
这是Solidity教程系列文章第9篇介绍Solidity 错误处理. Solidity系列完整的文章列表请查看分类-Solidity. 写在前面 Solidity 是以太坊智能合约编程语言,阅读本文 ...
- 航空概论(历年资料,引之百度文库,PS:未调格式,有点乱)
航空航天尔雅 选择题1. 已经实现了<天方夜谭>中的飞毯设想.—— A——美国2. 地球到月球大约—— C 38 万公里3. 建立了航空史上第一条定期空中路线—— B——德国4. 对于孔明 ...
- 以太坊solidity编程常见错误(不定期更新)
1.报错: Expected token Semicolon got 'eth_compileSolidity' funtion setFunder(uint _u,uint _amount){ 解决 ...
- 安装solidity遇见的问题——unused variable 'returned'
在编译安装solidity的过程中遇见了一个很奇怪的问题 webthree-umbrella/libethereum/libethereum/Executive.cpp: In member func ...
- 智能合约语言 Solidity 教程系列4 - 数据存储位置分析
写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解, 如果你还不了解,建议你先看以太坊是什么 这部分的内容官方英文文档讲的不是很透,因此我在参考Soli ...
- 智能合约语言 Solidity 教程系列5 - 数组介绍
写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解, 如果你还不了解,建议你先看以太坊是什么 本文前半部分是参考Solidity官方文档(当前最新版本: ...
- Solidity 中文文档 —— 第一章:Introduction to Smart Contracts
第一章:智能合约简介 粗略地翻译了 Ethereum 的智能合约开发语言的文档:Solidity.欢迎转载,注明出处. 有任何问题请联系我,本人微信:wx1076869692,更多详情见文末. 我是 ...
- 智能合约语言 Solidity 教程系列8 - Solidity API
这是Solidity教程系列文章第8篇介绍Solidity API,它们主要表现为内置的特殊的变量及函数,存在于全局命名空间里. 写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应 ...
随机推荐
- HDU - 6129 :Just do it (杨辉三角)
There is a nonnegative integer sequence a 1...n a1...n of length n n . HazelFan wants to do a type ...
- Freemarker 自定义标签 实现TemplateDirectiveModel
1 自定义标签需要实现TemplateDirectiveModel这个接口中的execute方法 实例代码如下 public class UserListDirective implements Te ...
- asp.net core microservices 架构之eureka服务发现
一 简介 微服务将需多的功能拆分为许多的轻量级的子应用,这些子应用相互调度.好处就是轻量级,完全符合了敏捷开发的精神.我们知道ut(单元测试),不仅仅提高我们的程序的健壮性,而且可以强制将类和方法的设 ...
- SSH项目配置数据源的方法(jndi)
1.在tomcat6.0/conf/context.xml加入以下代码 [xhtml] view plain copy <Resource name="jdbc/oracleD ...
- python操作RabbitMQ(不错)
一.rabbitmq RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消息队列 ...
- Maven依赖调解
引用来自maven实战中的一段话.
- Mac电脑Tomcat下载及安装(详细)
下载Tomcat 1.打开Apache Tomcat官网,选择你需要的版本进行下载: 地址http://tomcat.apache.org/download-70.cgi 2.解压apache-t ...
- 使用Revel(go)开发网站(全面版)
Revel很好的利用了Go语言的goroutine,把每一个request都分配到了goroutine里.不用再写一大堆的回调.如果你写过nodejs的话就会深刻的体会到callback hell是什 ...
- php获取当前月月初至月末的时间戳,上个月月初至月末的时间戳
当前月 <?php $thismonth = date('m'); $thisyear = date('Y'); $startDay = $thisyear . '-' . $thismonth ...
- 转:oracle几组重要的常见视图-v$undostat,v$open_cursor,v$rowcache,v$session_longops,v$waitstat
v$undostat 本视图监控当前实例中undo空间以及事务如何运行.并统计undo空间开销,事务开销以及实例可用的查询长度. V$UNDOSTAT中的常用列 Endtime:以10分钟为间隔的结束 ...