当前位置:网站首页>Solidity - truncated and checked modes of arithmetic operations - new features of 0.8.0
Solidity - truncated and checked modes of arithmetic operations - new features of 0.8.0
2022-07-01 19:25:00 【ling1998】
0.8.0 Version start , Arithmetic operation has two calculation modes , One is “wrapping”( truncation ) Mode or “unchecked”( Do not check ) Pattern , That is, in case of overflow “ truncation ”, Failure exception will not be triggered , Therefore, we have to introduce additional checklists to solve this problem ( Such as OpenZeppelin Medium SafeMath library ); The other is "checked"( Check ) Pattern . By default , Arithmetic operations use “checked” Pattern , Overflow check will be performed , If the result overflows , Failure exception fallback occurs .
Take a chestnut :
uint8 The maximum value is 255,255 Add a greater than 0 The integer of will overflow ( Overflow ), add unchecked{} Do not check for overflow , Overflow truncates the display ; Otherwise, the truncation mode is used by default to check for overflow , Abnormal fallback occurs .
test 0.8.0 Or above
Test code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
/// @dev Example contract :uint8 The maximum value is 255,255 Add a greater than 0 The integer of will overflow , add unchecked Does not check for overflow
contract TestUncheck {
/// @dev Check for spillage (checked Pattern )
function withoutUnchecked() external pure returns (uint8) {
uint8 a = 255;
uint8 b = 5;
return (a + b);
}
/// @dev Do not check for overflow (unchecked Mode, or truncation mode )
function withUnchecked() external pure returns (uint8) {
uint8 a = 255;
uint8 b = 5;
// Do not check for overflow , Output overflow result
unchecked {
return (a + b);
}
}
}
Test check mode (checked)
Check mode , Due to data overflow , Abnormal fallback occurs .
Test truncation mode (unchecked or wrapping)
Truncation mode , although 255+5 Overflowed , But the overflow result is output 4.
unchecked
Code blocks can be used anywhere in a code block , But it can't replace the whole function code block , It can't be nested either .This setting only affects the syntax at
unchecked
The statement within a block . The function invoked in the block does not affect this .
test 0.8.0 The previous version
0.8.0 The previous version was “wrapping”( truncation ) Pattern , That is, in case of overflow “ truncation ”, Failure exception will not be triggered , Therefore, we have to introduce additional checklists to solve this problem ( Such as OpenZeppelin Medium SafeMath library )
Test code
Test a distance 0.8.0 The latest version 0.7.6, The contract code is as follows :
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.6;
/// @dev test 0.8.0 Previous version , Overflow is in truncation mode
contract TestOverflow {
/// @dev Do not check for overflow (wrapping Pattern )
function overflow() external pure returns (uint8) {
uint8 a = 255;
uint8 b = 5;
return (a + b);
}
}
test result (wrapping Pattern )
Output truncated data , That is, do not check mode
边栏推荐
- 线程的并行、并发、生命周期
- 记一次 .NET 差旅管理后台 CPU 爆高分析
- Helium transmission line of lake shore cryostat
- Is PMP cancelled??
- 机械设备行业数字化供应链集采平台解决方案:优化资源配置,实现降本增效
- PostgreSQL varchar[] 数组类型操作
- Implement a Prometheus exporter
- Cache problems after app release
- The former 4A executives engaged in agent operation and won an IPO
- Witness the times! "The future of Renji collaboration has come" 2022 Hongji ecological partnership conference opens live broadcast reservation
猜你喜欢
SuperVariMag 超导磁体系统 — SVM 系列
Lumiprobe 细胞成像研究丨PKH26细胞膜标记试剂盒
Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
Supervarimag superconducting magnet system SVM series
sql查询去重统计的方法总结
案例分享:QinQ基本组网配置
小红书上的爱情买卖
nacos配置文件发布失败,请检查参数是否正确的解决方案
Manufacturing SRM management system supplier all-round closed-loop management, to achieve procurement sourcing and process efficient collaboration
Lumiprobe phosphide hexaethylene phosphide specification
随机推荐
More information about M91 fast hall measuring instrument
Learn MySQL from scratch - database and data table operations
ubuntu14安装MySQL并配置root账户本地与远程访问
Helium transmission line of lake shore cryostat
网易游戏,激进出海
Improve yolov5 with gsconv+slim neck to maximize performance!
[live broadcast appointment] database obcp certification comprehensive upgrade open class
PostgreSQL varchar[] 数组类型操作
Specification of lumiprobe reactive dye indocyanine green
Openai video pre training (VPT): action learning based on watching unmarked online videos
制造业SRM管理系统供应商全方位闭环管理,实现采购寻源与流程高效协同
Cdga | if you are engaged in the communication industry, you should get a data management certificate
【pytorch记录】自动混合精度训练 torch.cuda.amp
M91 fast hall measuring instrument - better measurement in a shorter time
Viewing technological changes through Huawei Corps (VI): smart highway
记一次 .NET 差旅管理后台 CPU 爆高分析
Once the SQL is optimized, the database query speed is increased by 60 times
kubernetes命令入门(namespaces,pods)
Contos 7 set up SFTP to create users, user groups, and delete users
生鲜行业B2B电商平台解决方案,提高企业交易流程标准化和透明度