/
gas_counter.rs
368 lines (329 loc) · 13.4 KB
/
gas_counter.rs
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use crate::{HostError, VMLogicError};
use near_primitives::types::TrieNodesCount;
use near_primitives_core::config::ExtCosts::read_cached_trie_node;
use near_primitives_core::config::ExtCosts::touching_trie_node;
use near_primitives_core::runtime::fees::Fee;
use near_primitives_core::{
config::{ActionCosts, ExtCosts, ExtCostsConfig},
profile::ProfileData,
types::Gas,
};
use std::collections::HashMap;
use std::fmt;
#[inline]
pub fn with_ext_cost_counter(f: impl FnOnce(&mut HashMap<ExtCosts, u64>)) {
#[cfg(feature = "costs_counting")]
{
thread_local! {
static EXT_COSTS_COUNTER: std::cell::RefCell<HashMap<ExtCosts, u64>> =
Default::default();
}
EXT_COSTS_COUNTER.with(|rc| f(&mut *rc.borrow_mut()));
}
#[cfg(not(feature = "costs_counting"))]
let _ = f;
}
type Result<T> = ::std::result::Result<T, VMLogicError>;
/// Fast gas counter with very simple structure, could be exposed to compiled code in the VM.
#[repr(C)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FastGasCounter {
/// The following three fields must be put next to another to make sure
/// generated gas counting code can use and adjust them.
/// We will share counter to ensure we never miss synchronization.
/// This could change and in such a case synchronization required between compiled WASM code
/// and the host code.
/// The amount of gas that was irreversibly used for contract execution.
pub burnt_gas: u64,
/// Hard gas limit for execution
pub gas_limit: u64,
/// Single WASM opcode cost
pub opcode_cost: u64,
}
/// Gas counter (a part of VMlogic)
pub struct GasCounter {
/// Shared gas counter data.
fast_counter: FastGasCounter,
/// Gas that was attached to the promises.
promises_gas: Gas,
/// Hard gas limit for execution
max_gas_burnt: Gas,
/// Amount of prepaid gas, we can never burn more than prepaid amount
prepaid_gas: Gas,
/// If this is a view-only call.
is_view: bool,
ext_costs_config: ExtCostsConfig,
/// Where to store profile data, if needed.
profile: ProfileData,
}
impl fmt::Debug for GasCounter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("").finish()
}
}
impl GasCounter {
pub fn new(
ext_costs_config: ExtCostsConfig,
max_gas_burnt: Gas,
opcode_cost: u32,
prepaid_gas: Gas,
is_view: bool,
) -> Self {
use std::cmp::min;
// Ignore prepaid gas limit when in view.
let prepaid_gas = if is_view { Gas::MAX } else { prepaid_gas };
Self {
ext_costs_config,
fast_counter: FastGasCounter {
burnt_gas: 0,
gas_limit: min(max_gas_burnt, prepaid_gas),
opcode_cost: Gas::from(opcode_cost),
},
max_gas_burnt: max_gas_burnt,
promises_gas: 0,
prepaid_gas,
is_view,
profile: Default::default(),
}
}
/// Accounts for burnt and used gas; reports an error if max gas burnt or
/// prepaid gas limit is crossed. Panics when trying to burn more gas than
/// being used, i.e. if `burn_gas > use_gas`.
fn deduct_gas(&mut self, burn_gas: Gas, use_gas: Gas) -> Result<()> {
assert!(burn_gas <= use_gas);
let promise_gas = use_gas - burn_gas;
let new_promises_gas =
self.promises_gas.checked_add(promise_gas).ok_or(HostError::IntegerOverflow)?;
let new_burnt_gas =
self.fast_counter.burnt_gas.checked_add(burn_gas).ok_or(HostError::IntegerOverflow)?;
let new_used_gas =
new_burnt_gas.checked_add(new_promises_gas).ok_or(HostError::IntegerOverflow)?;
if new_burnt_gas <= self.max_gas_burnt && new_used_gas <= self.prepaid_gas {
use std::cmp::min;
if promise_gas != 0 && !self.is_view {
self.fast_counter.gas_limit =
min(self.max_gas_burnt, self.prepaid_gas - new_promises_gas);
}
self.fast_counter.burnt_gas = new_burnt_gas;
self.promises_gas = new_promises_gas;
Ok(())
} else {
Err(self.process_gas_limit(new_burnt_gas, new_used_gas).into())
}
}
// Optimized version of above function for cases where no promises involved.
pub fn burn_gas(&mut self, value: Gas) -> Result<()> {
let new_burnt_gas =
self.fast_counter.burnt_gas.checked_add(value).ok_or(HostError::IntegerOverflow)?;
if new_burnt_gas <= self.fast_counter.gas_limit {
self.fast_counter.burnt_gas = new_burnt_gas;
Ok(())
} else {
Err(self.process_gas_limit(new_burnt_gas, new_burnt_gas + self.promises_gas).into())
}
}
pub fn process_gas_limit(&mut self, new_burnt_gas: Gas, new_used_gas: Gas) -> HostError {
use std::cmp::min;
// Never burn more gas than what was paid for.
let hard_burnt_limit = min(self.prepaid_gas, self.max_gas_burnt);
self.fast_counter.burnt_gas = min(new_burnt_gas, hard_burnt_limit);
debug_assert!(hard_burnt_limit >= self.fast_counter.burnt_gas);
// Technically we shall do `self.promises_gas = 0;` or error paths, as in this case
// no promises will be kept, but that would mean protocol change.
// See https://github.com/near/nearcore/issues/5148.
// TODO: consider making this change!
let used_gas_limit = min(self.prepaid_gas, new_used_gas);
assert!(used_gas_limit >= self.fast_counter.burnt_gas);
self.promises_gas = used_gas_limit - self.fast_counter.burnt_gas;
// If we crossed both limits prefer reporting GasLimitExceeded.
// Alternative would be to prefer reporting limit that is lower (or
// perhaps even some other heuristic) but old code preferred
// GasLimitExceeded and we’re keeping compatibility with that.
if new_burnt_gas > self.max_gas_burnt {
HostError::GasLimitExceeded
} else {
HostError::GasExceeded
}
}
pub fn pay_wasm_gas(&mut self, opcodes: u32) -> Result<()> {
let value = Gas::from(opcodes) * self.fast_counter.opcode_cost;
self.burn_gas(value)
}
/// Very special function to get the gas counter pointer for generated machine code.
/// Please do not use, unless fully understand Rust aliasing and other consequences.
/// Can be used to emit inlined code like `pay_wasm_gas()`, i.e.
/// mov base, gas_counter_raw_ptr
/// mov rax, [base + 0] ; current burnt gas
/// mov rcx, [base + 16] ; opcode cost
/// imul rcx, block_ops_count ; block cost
/// add rax, rcx ; new burnt gas
/// jo emit_integer_overflow
/// cmp rax, [base + 8] ; unsigned compare with burnt limit
/// mov [base + 0], rax
/// ja emit_gas_exceeded
pub fn gas_counter_raw_ptr(&mut self) -> *mut FastGasCounter {
use std::ptr;
ptr::addr_of_mut!(self.fast_counter)
}
#[inline]
fn inc_ext_costs_counter(&mut self, cost: ExtCosts, value: u64) {
with_ext_cost_counter(|cc| *cc.entry(cost).or_default() += value)
}
#[inline]
fn update_profile_host(&mut self, cost: ExtCosts, value: u64) {
self.profile.add_ext_cost(cost, value)
}
#[inline]
fn update_profile_action(&mut self, action: ActionCosts, value: u64) {
self.profile.add_action_cost(action, value)
}
/// A helper function to pay a multiple of a cost.
pub fn pay_per(&mut self, cost: ExtCosts, num: u64) -> Result<()> {
let use_gas = num
.checked_mul(cost.value(&self.ext_costs_config))
.ok_or(HostError::IntegerOverflow)?;
self.inc_ext_costs_counter(cost, num);
self.update_profile_host(cost, use_gas);
self.burn_gas(use_gas)
}
/// A helper function to pay base cost gas.
pub fn pay_base(&mut self, cost: ExtCosts) -> Result<()> {
let base_fee = cost.value(&self.ext_costs_config);
self.inc_ext_costs_counter(cost, 1);
self.update_profile_host(cost, base_fee);
self.burn_gas(base_fee)
}
/// A helper function to pay per byte gas fee for batching an action.
/// # Args:
/// * `per_byte_fee`: the fee per byte;
/// * `num_bytes`: the number of bytes;
/// * `sir`: whether the receiver_id is same as the current account ID;
/// * `action`: what kind of action is charged for;
pub fn pay_action_per_byte(
&mut self,
per_byte_fee: &Fee,
num_bytes: u64,
sir: bool,
action: ActionCosts,
) -> Result<()> {
let burn_gas =
num_bytes.checked_mul(per_byte_fee.send_fee(sir)).ok_or(HostError::IntegerOverflow)?;
let use_gas = burn_gas
.checked_add(
num_bytes.checked_mul(per_byte_fee.exec_fee()).ok_or(HostError::IntegerOverflow)?,
)
.ok_or(HostError::IntegerOverflow)?;
self.update_profile_action(action, burn_gas);
self.deduct_gas(burn_gas, use_gas)
}
/// A helper function to pay base cost gas fee for batching an action.
/// # Args:
/// * `base_fee`: base fee for the action;
/// * `sir`: whether the receiver_id is same as the current account ID;
/// * `action`: what kind of action is charged for;
pub fn pay_action_base(
&mut self,
base_fee: &Fee,
sir: bool,
action: ActionCosts,
) -> Result<()> {
let burn_gas = base_fee.send_fee(sir);
let use_gas =
burn_gas.checked_add(base_fee.exec_fee()).ok_or(HostError::IntegerOverflow)?;
self.update_profile_action(action, burn_gas);
self.deduct_gas(burn_gas, use_gas)
}
/// A helper function to pay base cost gas fee for batching an action.
/// # Args:
/// * `burn_gas`: amount of gas to burn;
/// * `use_gas`: amount of gas to reserve;
/// * `action`: what kind of action is charged for;
pub fn pay_action_accumulated(
&mut self,
burn_gas: Gas,
use_gas: Gas,
action: ActionCosts,
) -> Result<()> {
self.update_profile_action(action, burn_gas);
self.deduct_gas(burn_gas, use_gas)
}
pub fn add_trie_fees(&mut self, count: &TrieNodesCount) -> Result<()> {
self.pay_per(touching_trie_node, count.db_reads)?;
self.pay_per(read_cached_trie_node, count.mem_reads)?;
Ok(())
}
pub fn prepay_gas(&mut self, use_gas: Gas) -> Result<()> {
self.deduct_gas(0, use_gas)
}
pub fn burnt_gas(&self) -> Gas {
self.fast_counter.burnt_gas
}
/// Amount of gas used through promises and amount burned.
pub fn used_gas(&self) -> Gas {
self.promises_gas + self.fast_counter.burnt_gas
}
/// Remaining gas based on the amount of prepaid gas not yet used.
pub fn unused_gas(&self) -> Gas {
self.prepaid_gas - self.used_gas()
}
pub fn profile_data(&self) -> ProfileData {
self.profile.clone()
}
}
#[cfg(test)]
mod tests {
use crate::{ExtCostsConfig, HostError};
use near_primitives_core::types::Gas;
fn make_test_counter(max_burnt: Gas, prepaid: Gas, is_view: bool) -> super::GasCounter {
super::GasCounter::new(ExtCostsConfig::test(), max_burnt, 1, prepaid, is_view)
}
#[test]
fn test_deduct_gas() {
let mut counter = make_test_counter(10, 10, false);
counter.deduct_gas(5, 10).expect("deduct_gas should work");
assert_eq!(counter.burnt_gas(), 5);
assert_eq!(counter.used_gas(), 10);
}
#[test]
#[should_panic]
fn test_burn_gas_must_be_lt_use_gas() {
let _ = make_test_counter(10, 10, false).deduct_gas(5, 2);
}
#[test]
#[should_panic]
fn test_burn_gas_must_be_lt_use_gas_view() {
let _ = make_test_counter(10, 10, true).deduct_gas(5, 2);
}
#[test]
fn test_burn_too_much() {
fn test(burn: Gas, prepaid: Gas, view: bool, want: Result<(), HostError>) {
let mut counter = make_test_counter(burn, prepaid, view);
assert_eq!(counter.burn_gas(5), Ok(()));
assert_eq!(counter.burn_gas(3), want.map_err(Into::into));
}
test(5, 7, false, Err(HostError::GasLimitExceeded));
test(5, 7, true, Err(HostError::GasLimitExceeded));
test(5, 5, false, Err(HostError::GasLimitExceeded));
test(5, 5, true, Err(HostError::GasLimitExceeded));
test(7, 5, false, Err(HostError::GasLimitExceeded));
test(7, 5, true, Err(HostError::GasLimitExceeded));
}
#[test]
fn test_deduct_too_much() {
fn test(burn: Gas, prepaid: Gas, view: bool, want: Result<(), HostError>) {
let mut counter = make_test_counter(burn, prepaid, view);
assert_eq!(counter.deduct_gas(5, 5), Ok(()));
assert_eq!(counter.deduct_gas(3, 3), want.map_err(Into::into));
}
test(5, 7, false, Err(HostError::GasLimitExceeded));
test(5, 7, true, Err(HostError::GasLimitExceeded));
test(5, 5, false, Err(HostError::GasLimitExceeded));
test(5, 5, true, Err(HostError::GasLimitExceeded));
test(7, 5, false, Err(HostError::GasLimitExceeded));
test(7, 5, true, Err(HostError::GasLimitExceeded));
test(5, 8, false, Err(HostError::GasLimitExceeded));
test(5, 8, true, Err(HostError::GasLimitExceeded));
test(8, 5, false, Err(HostError::GasExceeded));
test(8, 5, true, Ok(()));
}
}