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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use primitive_types::{H256, U256};
use evm_core::ExitError;
use crate::Config;
use crate::consts::*;
pub fn call_extra_check(gas: U256, after_gas: u64, config: &Config) -> Result<(), ExitError> {
if config.err_on_call_with_more_gas && U256::from(after_gas) < gas {
Err(ExitError::OutOfGas)
} else {
Ok(())
}
}
pub fn suicide_refund(already_removed: bool) -> i64 {
if already_removed {
0
} else {
R_SUICIDE
}
}
pub fn sstore_refund(original: H256, current: H256, new: H256, config: &Config) -> i64 {
if config.sstore_gas_metering {
if current == new {
0
} else {
if original == current && new == H256::default() {
config.refund_sstore_clears
} else {
let mut refund = 0;
if original != H256::default() {
if current == H256::default() {
refund -= config.refund_sstore_clears;
} else if new == H256::default() {
refund += config.refund_sstore_clears;
}
}
if original == new {
if original == H256::default() {
refund += (config.gas_sstore_set - config.gas_sload) as i64;
} else {
refund += (config.gas_sstore_reset - config.gas_sload) as i64;
}
}
refund
}
}
} else {
if current != H256::default() && new == H256::default() {
config.refund_sstore_clears
} else {
0
}
}
}
pub fn create2_cost(len: U256) -> Result<u64, ExitError> {
let base = U256::from(G_CREATE);
let sha_addup_base = len / U256::from(32) +
if len % U256::from(32) == U256::zero() { U256::zero() } else { U256::one() };
let sha_addup = U256::from(G_SHA3WORD).checked_mul(sha_addup_base)
.ok_or(ExitError::OutOfGas)?;
let gas = base.checked_add(sha_addup).ok_or(ExitError::OutOfGas)?;
if gas > U256::from(u64::max_value()) {
return Err(ExitError::OutOfGas)
}
Ok(gas.as_u64())
}
pub fn exp_cost(power: U256, config: &Config) -> Result<u64, ExitError> {
if power == U256::zero() {
Ok(G_EXP)
} else {
let gas = U256::from(G_EXP)
.checked_add(
U256::from(config.gas_expbyte)
.checked_mul(U256::from(crate::utils::log2floor(power) / 8 + 1))
.ok_or(ExitError::OutOfGas)?
)
.ok_or(ExitError::OutOfGas)?;
if gas > U256::from(u64::max_value()) {
return Err(ExitError::OutOfGas)
}
Ok(gas.as_u64())
}
}
pub fn verylowcopy_cost(len: U256) -> Result<u64, ExitError> {
let wordd = len / U256::from(32);
let wordr = len % U256::from(32);
let gas = U256::from(G_VERYLOW).checked_add(
U256::from(G_COPY).checked_mul(
if wordr == U256::zero() {
wordd
} else {
wordd + U256::one()
}
).ok_or(ExitError::OutOfGas)?
).ok_or(ExitError::OutOfGas)?;
if gas > U256::from(u64::max_value()) {
return Err(ExitError::OutOfGas)
}
Ok(gas.as_u64())
}
pub fn extcodecopy_cost(len: U256, config: &Config) -> Result<u64, ExitError> {
let wordd = len / U256::from(32);
let wordr = len % U256::from(32);
let gas = U256::from(config.gas_ext_code).checked_add(
U256::from(G_COPY).checked_mul(
if wordr == U256::zero() {
wordd
} else {
wordd + U256::one()
}
).ok_or(ExitError::OutOfGas)?
).ok_or(ExitError::OutOfGas)?;
if gas > U256::from(u64::max_value()) {
return Err(ExitError::OutOfGas)
}
Ok(gas.as_u64())
}
pub fn log_cost(n: u8, len: U256) -> Result<u64, ExitError> {
let gas = U256::from(G_LOG)
.checked_add(U256::from(G_LOGDATA).checked_mul(len).ok_or(ExitError::OutOfGas)?)
.ok_or(ExitError::OutOfGas)?
.checked_add(U256::from(G_LOGTOPIC * n as u64))
.ok_or(ExitError::OutOfGas)?;
if gas > U256::from(u64::max_value()) {
return Err(ExitError::OutOfGas)
}
Ok(gas.as_u64())
}
pub fn sha3_cost(len: U256) -> Result<u64, ExitError> {
let wordd = len / U256::from(32);
let wordr = len % U256::from(32);
let gas = U256::from(G_SHA3).checked_add(
U256::from(G_SHA3WORD).checked_mul(
if wordr == U256::zero() {
wordd
} else {
wordd + U256::one()
}
).ok_or(ExitError::OutOfGas)?
).ok_or(ExitError::OutOfGas)?;
if gas > U256::from(u64::max_value()) {
return Err(ExitError::OutOfGas)
}
Ok(gas.as_u64())
}
pub fn sstore_cost(original: H256, current: H256, new: H256, gas: u64, config: &Config) -> Result<u64, ExitError> {
if config.sstore_gas_metering {
if config.sstore_revert_under_stipend {
if gas < config.call_stipend {
return Err(ExitError::OutOfGas)
}
}
Ok(if new == current {
config.gas_sload
} else {
if original == current {
if original == H256::zero() {
config.gas_sstore_set
} else {
config.gas_sstore_reset
}
} else {
config.gas_sload
}
})
} else {
Ok(if current == H256::zero() && new != H256::zero() {
config.gas_sstore_set
} else {
config.gas_sstore_reset
})
}
}
pub fn suicide_cost(value: U256, target_exists: bool, config: &Config) -> u64 {
let eip161 = !config.empty_considered_exists;
let should_charge_topup = if eip161 {
value != U256::zero() && !target_exists
} else {
!target_exists
};
let suicide_gas_topup = if should_charge_topup {
config.gas_suicide_new_account
} else {
0
};
config.gas_suicide + suicide_gas_topup
}
pub fn call_cost(
value: U256,
is_call_or_callcode: bool,
is_call_or_staticcall: bool,
new_account: bool,
config: &Config,
) -> u64 {
let transfers_value = value != U256::default();
config.gas_call +
xfer_cost(is_call_or_callcode, transfers_value) +
new_cost(is_call_or_staticcall, new_account, transfers_value, config)
}
fn xfer_cost(
is_call_or_callcode: bool,
transfers_value: bool
) -> u64 {
if is_call_or_callcode && transfers_value {
G_CALLVALUE
} else {
0
}
}
fn new_cost(
is_call_or_staticcall: bool,
new_account: bool,
transfers_value: bool,
config: &Config,
) -> u64 {
let eip161 = !config.empty_considered_exists;
if is_call_or_staticcall {
if eip161 {
if transfers_value && new_account {
G_NEWACCOUNT
} else {
0
}
} else if new_account {
G_NEWACCOUNT
} else {
0
}
} else {
0
}
}