Skip to content

Commit

Permalink
Merge pull request #1589 from gfusee/fix/static_api_manual_reset
Browse files Browse the repository at this point in the history
Added reset function on the StaticApi
  • Loading branch information
andrei-marinica authored Apr 30, 2024
2 parents 73ff74e + d9d7b9f commit f9e9f67
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions framework/scenario/src/api/impl_vh/static_api.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::sync::Mutex;
use multiversx_chain_vm::{
executor::VMHooks,
vm_hooks::{StaticApiVMHooksHandler, VMHooksDispatcher, VMHooksHandler},
Expand All @@ -14,9 +15,9 @@ fn new_static_api_vh() -> VMHooksDispatcher {
}

thread_local! {
static STATIC_API_VH_CELL: VMHooksDispatcher = new_static_api_vh();
static STATIC_API_VH_CELL: Mutex<VMHooksDispatcher> = Mutex::new(new_static_api_vh());

static STATIC_API_STATIC_CELL: StaticVarData = StaticVarData::default();
static STATIC_API_STATIC_CELL: Mutex<StaticVarData> = Mutex::new(StaticVarData::default());
}

#[derive(Clone)]
Expand All @@ -29,14 +30,20 @@ impl VMHooksApiBackend for StaticApiBackend {
where
F: FnOnce(&dyn VMHooks) -> R,
{
STATIC_API_VH_CELL.with(|vh| f(vh))
STATIC_API_VH_CELL.with(|vh_mutex| {
let vh = vh_mutex.lock().unwrap();
f(&*vh)
})
}

fn with_static_data<R, F>(f: F) -> R
where
F: FnOnce(&StaticVarData) -> R,
{
STATIC_API_STATIC_CELL.with(|data| f(data))
STATIC_API_STATIC_CELL.with(|data_mutex| {
let data = data_mutex.lock().unwrap();
f(&data)
})
}
}

Expand All @@ -50,6 +57,18 @@ impl StaticApi {
pub fn is_current_address_placeholder(address: &Address) -> bool {
address.as_array() == StaticApiVMHooksHandler::CURRENT_ADDRESS_PLACEHOLDER.as_array()
}

pub fn reset() {
STATIC_API_VH_CELL.with(|vh_mutex| {
let mut vh = vh_mutex.lock().unwrap();
*vh = new_static_api_vh()
});

STATIC_API_STATIC_CELL.with(|data_mutex| {
let mut data = data_mutex.lock().unwrap();
*data = StaticVarData::default()
})
}
}

impl std::fmt::Debug for StaticApi {
Expand Down

0 comments on commit f9e9f67

Please sign in to comment.