fp_evm/storage_oog.rs
1// This file is part of Frontier.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18environmental::environmental!(STORAGE_OOG: bool);
19
20use crate::{ExitError, ExitReason};
21use sp_core::U256;
22
23pub fn handle_storage_oog<R, F>(gas_limit: u64, f: F) -> (ExitReason, R, u64, U256)
24where
25 F: FnOnce() -> (ExitReason, R, u64, U256),
26 R: Default,
27{
28 STORAGE_OOG::using_once(&mut false, || {
29 let (reason, retv, used_gas, effective_gas) = f();
30
31 STORAGE_OOG::with(|storage_oog| {
32 if *storage_oog {
33 (
34 ExitReason::Error(ExitError::OutOfGas),
35 Default::default(),
36 used_gas,
37 U256([gas_limit, 0, 0, 0]),
38 )
39 } else {
40 (reason, retv, used_gas, effective_gas)
41 }
42 })
43 // This should always return `Some`, but let's play it safe.
44 .expect("STORAGE_OOG not defined")
45 })
46}
47
48pub fn set_storage_oog() {
49 STORAGE_OOG::with(|storage_oog| {
50 *storage_oog = true;
51 });
52}