当前位置:网站首页>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.

uncheckedCode 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
uncheckedThe 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

边栏推荐
- Nacos configuration file publishing failed, please check whether the parameters are correct solution
- Contos 7 set up SFTP to create users, user groups, and delete users
- Improve yolov5 with gsconv+slim neck to maximize performance!
- The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise
- ECS summer money saving secret, this time @ old users come and take it away
- Three simple methods of ES6 array de duplication
- 微服务大行其道的今天,Service Mesh是怎样一种存在?
- Huawei game failed to initialize init with error code 907135000
- Lean thinking: source, pillar, landing. I understand it after reading this article
- Cdga | if you are engaged in the communication industry, you should get a data management certificate
猜你喜欢

Stanford, salesforce|maskvit: masked vision pre training for video prediction

PMP是被取消了吗??

Lake Shore低温恒温器的氦气传输线

Once the SQL is optimized, the database query speed is increased by 60 times

6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单

nacos配置文件发布失败,请检查参数是否正确的解决方案

生鲜行业B2B电商平台解决方案,提高企业交易流程标准化和透明度

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

C-end dream is difficult to achieve. What does iFLYTEK rely on to support the goal of 1billion users?

Lake Shore continuous flow cryostat transmission line
随机推荐
Implement a Prometheus exporter
ubuntu14安装MySQL并配置root账户本地与远程访问
PostgreSQL varchar[] array type operation
Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
PostgreSQL varchar[] 数组类型操作
11. Users, groups, and permissions (1)
The difference between indexof and includes
Supervarimag superconducting magnet system SVM series
CDGA|从事通信行业,那你应该考个数据管理证书
制造业SRM管理系统供应商全方位闭环管理,实现采购寻源与流程高效协同
【快应用】Win7系统使用华为IDE无法运行和调试项目
Write it down once Net travel management background CPU Explosion Analysis
B2B e-commerce platform solution for fresh food industry to improve the standardization and transparency of enterprise transaction process
Appgallery connect scenario development practice - image storage and sharing
Three ways for redis to realize current limiting
More information about M91 fast hall measuring instrument
组队学习! 14天鸿蒙设备开发“学练考”实战营限时免费加入!
精益思想:来源,支柱,落地。看了这篇文章就懂了
Intensive cultivation of channels for joint development Fuxin and Weishi Jiajie held a new product training conference
Dlib+Opencv库实现疲劳检测