What is solidity unchecked

unchecked 的官网定义

unchecked 在 solidity 0.8 版本中加入,先看下官网的定义

  • Arithmetic operations revert on underflow and overflow. You can use unchecked { … } to use the previous wrapping behaviour.
  • Checks for overflow are very common, so we made them the default to increase readability of code, even if it comes at a slight increase of gas costs.
    因为检查溢出的情况非常普遍,所以即使会增加 gas 费用我们还是将检查溢出设置为默认情况。但是在 0.8 版本之后你可以在函数中添加 unchecked{…}块来使这部分代码不需要再考虑溢出的情况。

接下来我们使用一个例子看下具体的区别

1
2
3
4
5
6
7
8
9
10
11
12
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

contract Counter {
uint8 public count = 0;

function increment() external returns(uint8) {
count --;

return count;
}
}

alt "unchecked"
溢出之后直接报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

contract Counter {
uint8 public count = 0;

function increment() external returns(uint8) {
unchecked{
count --;
}

return count;
}
}

alt "unchecked"
允许溢出 count 的值变为 type(int8).max

Comments