让我们说,每次用户pay_in到合同,我们+1,贡献者的数量和每个人,withdrew,我们将-1,竞争对手的数目。
在签订了一份合同之后,很显然,如果用户想要足够快地执行withdrew函数,他们就可以得到大量的conterbuters。
想知道是否有办法防止这种情况发生。
我意识到溢出问题和Mathsafe库的使用。然而,在代码中使用数学安全库在计算上是昂贵的,如果用户计数可以从100增加到97,它也不会有帮助。提供一个用户在完成前两个转换之前执行代码3次。因此,我不认为这是一个溢出问题。
/**
* Deposit
**/
function deposit(Store storage self) public {
require(msg.value > 0);
address user = msg.sender;
// we already have the user down as Contributor
if(self.contributor[user].ether_in<=0)
self.numContributors++;
self.contributor[user].ether_in += msg.value;
self.ether_in += msg.value;
}
/**
* withdraw
**/
function withdraw(Store storage self) internal {
address user = msg.sender;
require(self.contributor[user].ether_in>0);
ether_out = self.contributor[user].ether_in;
self.numContributors--;
user.transfer(ether_out);
self.contributor[user].ether_in = 0
}**联系**
import './lib'
contract SimplifiedMainContract {
using Lib for Lib.Store;
Lib.Store private store;
function deposit() public payable {
store.deposit();
}
function withdraw() public {
store.withdraw();
}
}发布于 2018-07-11 12:09:56
您应该搜索变量中的溢出/溢出,并在需要这样计算的地方使用SafeMath库,因为稳健性还没有内置的检查这类事情。
https://ethereum.stackexchange.com/questions/53160
复制相似问题