当前位置:网站首页>Solidity - 算术运算的截断模式(unchecked)与检查模式(checked)- 0.8.0新特性
Solidity - 算术运算的截断模式(unchecked)与检查模式(checked)- 0.8.0新特性
2022-07-01 18:43:00 【ling1998】
0.8.0版本开始,算术运算有两个计算模式,一个是“wrapping”(截断)模式或为“unchecked”(不检查)模式,即在发生溢出的情况下会进行“截断”,不会触发失败异常,从而得靠引入额外的检查库来解决这个问题(如OpenZeppelin中的SafeMath库);另一个是"checked"(检查)模式。默认情况下,算术运算使用的是“checked”模式,会进行溢出检查,如果结果溢出,会出现失败异常回退。
举个栗子:
uint8最大值为255,255加上一个大于0的整数会溢出(上溢),加上unchecked{}则不检查是否溢出,溢出则截断显示;否则默认使用截断模式检查溢出,发生异常回退。
测试0.8.0或以上版本
测试代码
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
/// @dev 示例合约:uint8最大值为255,255加上一个大于0的整数会溢出,加上unchecked不会检查是否溢出
contract TestUncheck {
/// @dev 检查溢出(checked模式)
function withoutUnchecked() external pure returns (uint8) {
uint8 a = 255;
uint8 b = 5;
return (a + b);
}
/// @dev 不检查溢出(unchecked模式或称为截断模式)
function withUnchecked() external pure returns (uint8) {
uint8 a = 255;
uint8 b = 5;
// 不检查溢出,输出溢出结果
unchecked {
return (a + b);
}
}
}测试检查模式(checked)
检查模式,由于数据溢出,发生异常回退。

测试截断模式 (unchecked或wrapping)
截断模式,虽然255+5溢出了,但输出了溢出结果4。

unchecked代码块可以在代码块中的任何位置使用,但不可以替代整个函数代码块,同样不可以嵌套。此设置仅影响语法上位于
unchecked块内的语句。 在块中调用的函数不会此影响。
测试0.8.0之前版本
0.8.0之前版本为“wrapping”(截断)模式,即在发生溢出的情况下会进行“截断”,不会触发失败异常,从而得靠引入额外的检查库来解决这个问题(如OpenZeppelin中的SafeMath库)
测试代码
测试一个离0.8.0最近版本0.7.6,合约代码如下:
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.6;
/// @dev 测试0.8.0以前版本,溢出为截断模式
contract TestOverflow {
/// @dev 不检查溢出(wrapping模式)
function overflow() external pure returns (uint8) {
uint8 a = 255;
uint8 b = 5;
return (a + b);
}
}测试结果(wrapping模式)
输出截断数据,即为不检查模式

边栏推荐
- bean的生命周期核心步骤总结
- Create your own NFT collections and publish a Web3 application to show them (Introduction)
- 【快应用】Win7系统使用华为IDE无法运行和调试项目
- Docker deploy mysql8.0
- The R language cartools package divides the data, the scale function scales the data, the KNN function of the class package constructs the k-nearest neighbor classifier, and the table function calcula
- 华为游戏初始化init失败,返回错误码907135000
- 华为云专家详解GaussDB(for MySQL)新特性
- 宝,运维100+服务器很头疼怎么办?用行云管家!
- 寶,運維100+服務器很頭疼怎麼辦?用行雲管家!
- 3. "Create your own NFT collections and publish a Web3 application to show them" cast NFT locally
猜你喜欢

从零开始学 MySQL —数据库和数据表操作

linux下清理系统缓存并释放内存

精耕渠道共谋发展 福昕携手伟仕佳杰开展新产品培训大会

B2B e-commerce platform solution for fresh food industry to improve the standardization and transparency of enterprise transaction process
![[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?](/img/66/674a06d8e45a31ae879b81554ef373.png)
[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?

Lumiprobe free radical analysis h2dcfda instructions

Huawei game failed to initialize init with error code 907135000

AI training speed breaks Moore's law; Song shuran's team won the RSS 2022 Best Paper Award

SuperVariMag 超导磁体系统 — SVM 系列

Supervarimag superconducting magnet system SVM series
随机推荐
摄像头的MIPI接口、DVP接口和CSI接口[通俗易懂]
[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?
Solution of digital supply chain centralized purchase platform in mechanical equipment industry: optimize resource allocation and realize cost reduction and efficiency increase
Today, with the popularity of micro services, how does service mesh exist?
Lumiprobe 亚磷酰胺丨六甘醇亚磷酰胺说明书
Clean up system cache and free memory under Linux
毕业总结
前4A高管搞代运营,拿下一个IPO
Go语言自学系列 | go语言数据类型
M91快速霍尔测量仪—在更短的时间内进行更好的测量
Huawei cloud experts explain the new features of gaussdb (for MySQL)
M91 fast hall measuring instrument - better measurement in a shorter time
Excel之VBA简单宏编程
Leetcode-141 circular linked list
Netease games, radical going to sea
2. Create your own NFT collections and publish a Web3 application to show them start and run your local environment
【6.24-7.1】写作社区精彩技术博文回顾
网易游戏,激进出海
linux下清理系统缓存并释放内存
PMP是被取消了吗??