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

边栏推荐
- MySQL common graphics management tools | dark horse programmers
- 赋能「新型中国企业」,SAP Process Automation 落地中国
- Team up to learn! 14 days of Hongmeng equipment development "learning, practicing and testing" practical camp, free of charge!
- 小红书上的爱情买卖
- More information about M91 fast hall measuring instrument
- The former 4A executives engaged in agent operation and won an IPO
- Lake Shore M91快速霍尔测量仪
- sql查询去重统计的方法总结
- The difference between indexof and includes
- 见证时代!“人玑协同 未来已来”2022弘玑生态伙伴大会开启直播预约
猜你喜欢

Summary of the core steps in the life cycle of beans

SuperVariMag 超导磁体系统 — SVM 系列

Lumiprobe cell imaging study PKH26 cell membrane labeling kit

【森城市】GIS数据漫谈(一)

Lake shore optimag superconducting magnet system om series

【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?

Lumiprobe 自由基分析丨H2DCFDA说明书

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

XML语法、约束

Intensive cultivation of channels for joint development Fuxin and Weishi Jiajie held a new product training conference
随机推荐
Lumiprobe cell imaging study PKH26 cell membrane labeling kit
The difference between indexof and includes
M91快速霍尔测量仪—在更短的时间内进行更好的测量
Huawei cloud experts explain the new features of gaussdb (for MySQL)
云服务器ECS夏日省钱秘籍,这次@老用户快来领走
[to.Net] C set class source code analysis
Team up to learn! 14 days of Hongmeng equipment development "learning, practicing and testing" practical camp, free of charge!
Solution: you can ping others, but others can't ping me
nacos启动失败问题解决与总结
ETL development of data warehouse (IV)
Digital business cloud: from planning to implementation, how does Minmetals Group quickly build a new pattern of digital development?
Lake Shore continuous flow cryostat transmission line
Learning notes - steps of JDBC connection database operation
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
kubernetes命令入门(namespaces,pods)
中英说明书丨人可溶性晚期糖基化终末产物受体(sRAGE)Elisa试剂盒
Lumiprobe 细胞成像研究丨PKH26细胞膜标记试剂盒
Solution of digital supply chain centralized purchase platform in mechanical equipment industry: optimize resource allocation and realize cost reduction and efficiency increase
Enabling "new Chinese enterprises", SAP process automation landing in China
学习笔记-JDBC连接数据库操作的步骤