-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1156 from multiversx/scenario-static-api-3
SingleTxApi
- Loading branch information
Showing
23 changed files
with
426 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use std::cell::RefCell; | ||
|
||
use multiversx_chain_vm::{ | ||
executor::VMHooks, | ||
types::VMAddress, | ||
vm_hooks::{SingleTxApiData, SingleTxApiVMHooksHandler, VMHooksDispatcher}, | ||
world_mock::AccountData, | ||
}; | ||
use multiversx_sc::api::RawHandle; | ||
|
||
use crate::debug_executor::StaticVarData; | ||
|
||
use super::{VMHooksApi, VMHooksApiBackend}; | ||
|
||
thread_local! { | ||
static SINGLE_TX_API_VH_CELL: RefCell<SingleTxApiVMHooksHandler> = RefCell::default(); | ||
|
||
static SINGLE_TX_API_STATIC_CELL: StaticVarData = StaticVarData::default(); | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct SingleTxApiBackend; | ||
|
||
impl VMHooksApiBackend for SingleTxApiBackend { | ||
type HandleType = RawHandle; | ||
|
||
fn with_vm_hooks<R, F>(f: F) -> R | ||
where | ||
F: FnOnce(&dyn VMHooks) -> R, | ||
{ | ||
SINGLE_TX_API_VH_CELL.with(|cell| { | ||
let handler = cell.borrow().clone(); | ||
let dispatcher = VMHooksDispatcher::new(Box::new(handler)); | ||
f(&dispatcher) | ||
}) | ||
} | ||
|
||
fn with_static_data<R, F>(f: F) -> R | ||
where | ||
F: FnOnce(&StaticVarData) -> R, | ||
{ | ||
SINGLE_TX_API_STATIC_CELL.with(|data| f(data)) | ||
} | ||
} | ||
|
||
/// Similar to the `StaticApi`, but offers allows calls to storage, input, and even creating results. | ||
pub type SingleTxApi = VMHooksApi<SingleTxApiBackend>; | ||
|
||
impl SingleTxApi { | ||
pub fn clear_global() { | ||
SINGLE_TX_API_VH_CELL.with(|cell| { | ||
let _ = cell.take(); | ||
}) | ||
} | ||
|
||
pub fn with_global<F, R>(f: F) -> R | ||
where | ||
F: FnOnce(&mut SingleTxApiData) -> R, | ||
{ | ||
SINGLE_TX_API_VH_CELL.with(|cell| { | ||
let mut handler = cell.borrow_mut(); | ||
handler.with_mut_data(f) | ||
}) | ||
} | ||
|
||
pub fn with_global_default_account<F, R>(f: F) -> R | ||
where | ||
F: FnOnce(&mut AccountData) -> R, | ||
{ | ||
Self::with_global(|data| data.with_account_mut(&VMAddress::zero(), f)) | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for SingleTxApi { | ||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
f.debug_struct("SingleTxApi").finish() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use multiversx_sc::contract_base::StorageRawWrapper; | ||
use multiversx_sc_scenario::api::SingleTxApi; | ||
|
||
#[test] | ||
fn single_tx_api_test() { | ||
let storage_raw = StorageRawWrapper::<SingleTxApi>::new(); | ||
let storage_key = "test-num"; | ||
|
||
// unitialized, we get the default | ||
let x: i32 = storage_raw.read(storage_key); | ||
assert_eq!(x, 0); | ||
|
||
// write, as if from a contract | ||
storage_raw.write(storage_key, &5i32); | ||
let x: i32 = storage_raw.read(storage_key); | ||
assert_eq!(x, 5); | ||
|
||
// check directly in storage | ||
SingleTxApi::with_global_default_account(|account| { | ||
let value = account.storage.get(storage_key.as_bytes()).unwrap(); | ||
assert_eq!(value, &vec![5u8]); | ||
|
||
// change value directly in storage | ||
account | ||
.storage | ||
.insert(storage_key.as_bytes().to_vec(), vec![7u8]); | ||
}); | ||
|
||
// read again | ||
let x: i32 = storage_raw.read(storage_key); | ||
assert_eq!(x, 7); | ||
|
||
// clear everything, globally | ||
SingleTxApi::clear_global(); | ||
let x: i32 = storage_raw.read(storage_key); | ||
assert_eq!(x, 0); | ||
|
||
// checking directly in storage | ||
SingleTxApi::with_global_default_account(|account| { | ||
let value = account | ||
.storage | ||
.get(storage_key.as_bytes()) | ||
.cloned() | ||
.unwrap_or_default(); | ||
assert!(value.is_empty()); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.