当前位置:网站首页>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模式)
输出截断数据,即为不检查模式
边栏推荐
- Improve yolov5 with gsconv+slim neck to maximize performance!
- Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
- [live broadcast appointment] database obcp certification comprehensive upgrade open class
- Lake shore optimag superconducting magnet system om series
- Once the SQL is optimized, the database query speed is increased by 60 times
- Today, with the popularity of micro services, how does service mesh exist?
- 记一次 .NET 差旅管理后台 CPU 爆高分析
- 市值蒸发740亿,这位大佬转身杀入预制菜
- R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graph, and uses PCH parameters to customize the shape o
- Viewing the whole ecology of Tiktok from a macro perspective
猜你喜欢
[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?
Lake Shore—CRX-EM-HF 型低温探针台
Netease games, radical going to sea
ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
Lumiprobe free radical analysis h2dcfda instructions
Stanford, salesforce|maskvit: masked vision pre training for video prediction
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
Viewing the whole ecology of Tiktok from a macro perspective
B2B e-commerce platform solution for fresh food industry to improve the standardization and transparency of enterprise transaction process
一次SQL优化,数据库查询速度提升 60 倍
随机推荐
How to operate technology related we media well?
Three simple methods of ES6 array de duplication
M91快速霍尔测量仪—在更短的时间内进行更好的测量
Taiaisu M source code construction, peak store app premium consignment source code sharing
Chinese and English instructions human soluble advanced glycation end products receptor (sRAGE) ELISA Kit
【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
Superoptimag superconducting magnet system - SOM, Som2 series
精耕渠道共谋发展 福昕携手伟仕佳杰开展新产品培训大会
How to use the low code platform of the Internet of things for personal settings?
How to realize the bottom layer of read-write lock in go question bank 16
R language ggplot2 visualization: gganimate package transition_ Time function to create dynamic scatter animation (GIF), shadow_ The wake function configures the gradient falloff tailing effect of the
Excel之VBA简单宏编程
Improve yolov5 with gsconv+slim neck to maximize performance!
Intensive cultivation of channels for joint development Fuxin and Weishi Jiajie held a new product training conference
Manufacturing SRM management system supplier all-round closed-loop management, to achieve procurement sourcing and process efficient collaboration
助力数字经济发展,夯实数字人才底座—数字人才大赛在昆成功举办
生鲜行业B2B电商平台解决方案,提高企业交易流程标准化和透明度
Specification of lumiprobe reactive dye indocyanine green
宝,运维100+服务器很头疼怎么办?用行云管家!
实现一个Prometheus exporter