当前位置:网站首页>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模式)
输出截断数据,即为不检查模式

边栏推荐
- 摄像头的MIPI接口、DVP接口和CSI接口[通俗易懂]
- 实现一个Prometheus exporter
- Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
- nacos配置文件发布失败,请检查参数是否正确的解决方案
- Leetcode203 remove linked list elements
- M91快速霍尔测量仪—在更短的时间内进行更好的测量
- Lumiprobe 自由基分析丨H2DCFDA说明书
- [quick application] win7 system cannot run and debug projects using Huawei ide
- Qfile read / write file operation in QT
- indexof和includes的区别
猜你喜欢

【直播预约】数据库OBCP认证全面升级公开课

Lumiprobe 活性染料丨吲哚菁绿说明书

Li Kou daily question - Day 32 -589 N × Preorder traversal of tree

微服务大行其道的今天,Service Mesh是怎样一种存在?

前4A高管搞代运营,拿下一个IPO

B2B e-commerce platform solution for fresh food industry to improve the standardization and transparency of enterprise transaction process

如何使用物联网低代码平台进行个人设置?

C端梦难做,科大讯飞靠什么撑起10亿用户目标?

Improve yolov5 with gsconv+slim neck to maximize performance!

生鲜行业B2B电商平台解决方案,提高企业交易流程标准化和透明度
随机推荐
AI training speed breaks Moore's law; Song shuran's team won the RSS 2022 Best Paper Award
Leetcode-128 longest continuous sequence
【pytorch记录】自动混合精度训练 torch.cuda.amp
Manufacturing SRM management system supplier all-round closed-loop management, to achieve procurement sourcing and process efficient collaboration
Superoptimag superconducting magnet system - SOM, Som2 series
11. Users, groups, and permissions (1)
Leetcode-141 circular linked list
Qfile read / write file operation in QT
云服务器ECS夏日省钱秘籍,这次@老用户快来领走
【pytorch记录】模型的分布式训练DataParallel、DistributedDataParallel
小红书上的爱情买卖
The market value evaporated by 74billion yuan, and the big man turned and entered the prefabricated vegetables
Once the SQL is optimized, the database query speed is increased by 60 times
数据仓库(四)之ETL开发
Dlib+Opencv库实现疲劳检测
ES6数组去重的三个简单办法
数商云:从规划到落地,五矿集团如何快速构建数字化发展新格局?
【快应用】Win7系统使用华为IDE无法运行和调试项目
【6.24-7.1】写作社区精彩技术博文回顾
Lake Shore continuous flow cryostat transmission line