-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlockGasDOS.sol
92 lines (71 loc) · 2.06 KB
/
BlockGasDOS.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
pragma solidity ^0.4.9;
/// @title
/// @author Adam Lemmon <[email protected]>
contract BlockGasDOS {
/**
* Storage
*/
struct Balance {
address addr;
uint256 value;
}
Balance[] balances;
uint256 nextRefundIndex; // FIX
/**
* External
*/
function itertionRefund() external {
uint8 i = 0;
while (i < balances.length) {
balances[i].addr.send(balances[i].value);
i++;
}
nextRefundIndex = i;
}
function safeIterationRefund() external {
uint256 i = nextRefundIndex;
while (i < balances.length && msg.gas > 200000) {
balances[i].addr.send(balances[i].value);
i++;
}
nextRefundIndex = i;
}
function singleRefund(uint index) external {
uint amount = balances[index].value;
balances[index].value = 0;
if (!balances[index].addr.send(amount)) throw;
}
function getRefundLength()
external
constant
returns(uint)
{
return balances.length;
}
}
/*Real World Example: OpenZeppelin VestedToken*/
/**
* @title Vested token
* @dev Tokens that can be vested for a group of addresses.
*/
/*contract VestedToken {
// FIX for block gas DOS
uint256 MAX_GRANTS_PER_ADDRESS = 20;
mapping (address => TokenGrant[]) public grants;
function grantVestedTokens() public {
// To prevent a user being spammed and have his balance locked (out of gas attack when calculating vesting).
if (tokenGrantsCount(_to) > MAX_GRANTS_PER_ADDRESS) throw;
uint count = grants[_to].push(TokenGrant());
transfer(_to, _value);
}
function transferableTokens(address holder, uint64 time) {
uint256 grantIndex = tokenGrantsCount(holder);
// Iterate through all the grants the holder has, and add all non-vested tokens
uint256 nonVested = 0;
for (uint256 i = 0; i < grantIndex; i++) {
nonVested = SafeMath.add(nonVested, nonVestedTokens(grants[holder][i], time));
}
// Balance - totalNonVested is the amount of tokens a holder can transfer at any given time
uint256 vestedTransferable = SafeMath.sub(balanceOf(holder), nonVested);
}
}*/