-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryTypesPracticeInput.sol
165 lines (131 loc) · 4.68 KB
/
MemoryTypesPracticeInput.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.12;
import "@openzeppelin/contracts/access/Ownable.sol";
interface IMemoryTypesPractice {
function setA(uint256 _a) external;
function setB(uint256 _b) external;
function setC(uint256 _c) external;
function calc1() external view returns(uint256);
function calc2() external view returns(uint256);
function claimRewards(address _user) external;
function addNewMan(
uint256 _edge,
uint8 _dickSize,
bytes32 _idOfSecretBluetoothVacinationChip,
uint32 _iq
) external;
function getMiddleDickSize() external view returns(uint256);
function numberOfOldMenWithHighIq() external view returns(uint256);
}
contract MemoryTypesPracticeInput is IMemoryTypesPractice, Ownable {
// Owner part. Cannot be modified.
IUserInfo public userInfo;
uint256 public a;
uint256 public b;
uint256 public c;
uint256 public constant MIN_BALANCE = 12000;
mapping(address => bool) public rewardsClaimed;
constructor(address _validator, address _userInfo) {
transferOwnership(_validator);
userInfo = IUserInfo(_userInfo);
addNewMan(1, 1, bytes32('0x11'), 1);
}
function setA(uint256 _a) external onlyOwner {
a = _a;
}
function setB(uint256 _b) external onlyOwner {
b = _b;
}
function setC(uint256 _c) external onlyOwner {
c = _c;
}
// End of the owner part
// Here starts part for modification. Remember that function signatures cannot be modified.
// to optimize 1
// Now consumes 27835
// Should consume not more than 27830 as execution cost for non zero values
function calc1() external view returns(uint256) {
uint256 _localA = a;
return b + c * a;
}
// to optimize 2
// Now consumes 31253
// Should consume not more than 30000 as execution cost for non zero values
function calc2() external view returns(uint256) {
return ((b + c) * (b + a) + (c + a) * c + c/a + c/b + 2 * a - 1 + a * b * c + a + b * a^2)/
(a + b) * c + 2 * a;
}
// to optimize 3
// Now consumes 55446
// Should consume not more than 54500 as execution cost
function claimRewards(address _user) external {
require(userInfo.getUserInfo(_user).unlockTime <= block.timestamp,
"MemoryTypesPracticeInput: Unlock time has not yet come");
require(!rewardsClaimed[_user],
"MemoryTypesPracticeInput: Rewards are already claimed");
require(userInfo.getUserInfo(_user).balance >= MIN_BALANCE,
"MemoryTypesPracticeInput: To less balance");
rewardsClaimed[_user] = true;
}
// to optimize 4
struct Man {
uint256 edge;
uint8 dickSize;
bytes32 idOfSecretBluetoothVacinationChip;
uint32 iq;
}
Man[] men;
// Now consumes 115724
// Should consume not more than 94000 as execution cost
function addNewMan(
uint256 _edge,
uint8 _dickSize,
bytes32 _idOfSecretBluetoothVacinationChip,
uint32 _iq
) public {
men.push(Man(_edge, _dickSize, _idOfSecretBluetoothVacinationChip, _iq));
}
// to optimize 5
// Now consumes 36689
// Should consume not more than 36100 as execution cost for 6 elements array
function getMiddleDickSize() external view returns(uint256) {
uint256 _sum;
for (uint256 i = 0; i < men.length; i++) {
_sum += men[i].dickSize;
}
return _sum/men.length;
}
// to optimize 6
// Now consumes 68675
// Should consume not more than 40000 as execution cost for 6 elements array
function numberOfOldMenWithHighIq() external view returns(uint256) {
uint256 _count;
for (uint256 i = 0; i < men.length; i++) {
Man memory man = men[i];
if (man.edge > 50 && man.iq > 120) _count++;
}
return _count;
}
}
// Cannot be modified
interface IUserInfo {
struct User {
uint256 balance;
uint256 unlockTime;
}
function addUser(address _user, uint256 _balance, uint256 _unlockTime) external;
function getUserInfo(address _user) external view returns(User memory);
}
// Cannot be modified.
contract UserInfo is IUserInfo, Ownable {
mapping(address => User) users;
constructor(address _validator) {
transferOwnership(_validator);
}
function addUser(address _user, uint256 _balance, uint256 _unlockTime) external onlyOwner {
users[_user] = User(_balance, _unlockTime);
}
function getUserInfo(address _user) external view returns(User memory) {
return users[_user];
}
}