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

边栏推荐
- Cache problems after app release
- Lake Shore低温恒温器的氦气传输线
- 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
- 11、用户、组和权限(1)
- Taiaisu M source code construction, peak store app premium consignment source code sharing
- 一次SQL优化,数据库查询速度提升 60 倍
- ES6 summary "suggestions collection" of array methods find(), findindex()
- R language ggplot2 visualization: visualize the line graph and add customized Y-axis label information to the line graph using the labels function
- June issue | antdb database participated in the preparation of the "Database Development Research Report" and appeared on the list of information technology and entrepreneurship industries
- Three simple methods of ES6 array de duplication
猜你喜欢

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

11、用户、组和权限(1)

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

Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?

Create your own NFT collections and publish a Web3 application to show them (Introduction)

Altair HyperWorks 2022软件安装包和安装教程

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

Today, with the popularity of micro services, how does service mesh exist?

SuperVariMag 超导磁体系统 — SVM 系列

【pytorch记录】自动混合精度训练 torch.cuda.amp
随机推荐
Lean thinking: source, pillar, landing. I understand it after reading this article
R language ggplot2 visualization: visualize the line graph and add customized Y-axis label information to the line graph using the labels function
机械设备行业数字化供应链集采平台解决方案:优化资源配置,实现降本增效
Lumiprobe free radical analysis h2dcfda instructions
中英说明书丨人可溶性晚期糖基化终末产物受体(sRAGE)Elisa试剂盒
SuperVariMag 超导磁体系统 — SVM 系列
B2B e-commerce platform solution for fresh food industry to improve the standardization and transparency of enterprise transaction process
Excel之VBA简单宏编程
VBA simple macro programming of Excel
Viewing the whole ecology of Tiktok from a macro perspective
1. "Create your own NFT collections and publish a Web3 application to show them." what is NFT
How to realize the bottom layer of read-write lock in go question bank 16
nacos配置文件发布失败,请检查参数是否正确的解决方案
The market value evaporated by 74billion yuan, and the big man turned and entered the prefabricated vegetables
Lumiprobe 细胞成像研究丨PKH26细胞膜标记试剂盒
数据仓库(四)之ETL开发
Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
The former 4A executives engaged in agent operation and won an IPO
The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise
Li Kou daily question - Day 32 -1232 Dotted line