Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added reset function on the StaticApi #1589

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading