我在学习如何掌握这个网站。https://github.com/ethereumbook/ethereumbook/blob/develop/09smart-contracts-security.asciidoc
所有的示例代码都使用uint 256。
例如,
pragma solidity ^0.4.19
contract EtherStore {
//unsigned integer type of 256 bits
uint256 public withdrawalLimit = 1 ether;
mapping(address => uint256) public lastWithdrawTime;
mapping(address => uint256) public balances;
function depositFunds() public payable {
balances[msg.sender] += msg.value;
}
function withdrawFunds (uint256 _weiToWithdraw) public {
...
...
}
}uint256 public withdrawalLimit =1醚;
它使用256位,只是分配‘整数1’,我认为这是浪费内存和气体。
如果这个值处理到魏,这些值只需要60位。
我不知道为什么这本书用256位来保存“1”
(谢谢你的帮助:)
发布于 2019-04-03 01:02:36
EVM本机工作在256位字.使用较小的,通常会消耗更多的气体,因为有额外的工作要做:不需要的部分需要掩盖。
一个可能的例外情况是将多个值打包到同一个存储槽中。例如struct MyStruct { uint128 foo; uint128 bar; }。在这里,取决于您的访问模式,您可以将两个值存储在同一个存储槽中,也可能不会节省汽油。
注意,在这个特定的代码中,最好的选择是使用常量。这样的话,这个值就不需要被保存在存储中了,这将是一个非常大的节气。
https://ethereum.stackexchange.com/questions/69201
复制相似问题