Skip to content

Commit

Permalink
SingleTxApi test
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Jul 11, 2023
1 parent 037e809 commit 09970d5
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 15 deletions.
9 changes: 9 additions & 0 deletions framework/base/src/storage/storage_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ impl<M: ManagedTypeApi> From<ManagedBuffer<M>> for StorageKey<M> {
}
}

impl<M: ManagedTypeApi> From<&str> for StorageKey<M> {
#[inline]
fn from(s: &str) -> Self {
StorageKey {
buffer: ManagedBuffer::from(s),
}
}
}

impl<M, const N: usize> From<ManagedByteArray<M, N>> for StorageKey<M>
where
M: ManagedTypeApi + ErrorApi,
Expand Down
11 changes: 10 additions & 1 deletion framework/scenario/src/api/impl_vh/single_tx_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ 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;

Expand Down Expand Up @@ -51,7 +53,7 @@ impl SingleTxApi {
})
}

pub fn with_global<F, R>(&mut self, f: F) -> R
pub fn with_global<F, R>(f: F) -> R
where
F: FnOnce(&mut SingleTxApiData) -> R,
{
Expand All @@ -60,6 +62,13 @@ impl SingleTxApi {
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 {
Expand Down
47 changes: 47 additions & 0 deletions framework/scenario/tests/single_tx_api_test.rs
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());
});
}
30 changes: 16 additions & 14 deletions vm/src/vm_hooks/vh_impl/vh_single_tx_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ pub struct SingleTxApiData {
pub current_block_info: BlockInfo,
}

impl SingleTxApiData {
pub fn with_account_mut<R, F>(&self, address: &VMAddress, f: F) -> R
where
F: FnOnce(&mut AccountData) -> R,
{
let mut accounts = self.accounts.borrow_mut();
let account = accounts
.entry(address.clone())
.or_insert(AccountData::new_empty(address.clone()));
f(account)
}
}

#[derive(Default, Debug, Clone)]
pub struct SingleTxApiVMHooksHandler(Rc<SingleTxApiData>);

Expand All @@ -38,17 +51,6 @@ impl SingleTxApiVMHooksHandler {
.expect("could not retrieve mutable reference to SingleTxApi data");
f(data)
}

fn with_account_mut<R, F>(&self, address: &VMAddress, f: F) -> R
where
F: FnOnce(&mut AccountData) -> R,
{
let mut accounts = self.0.accounts.borrow_mut();
let account = accounts
.entry(address.clone())
.or_insert(AccountData::new_empty(address.clone()));
f(account)
}
}

impl VMHooksHandlerSource for SingleTxApiVMHooksHandler {
Expand Down Expand Up @@ -77,13 +79,13 @@ impl VMHooksHandlerSource for SingleTxApiVMHooksHandler {
}

fn storage_read_any_address(&self, address: &VMAddress, key: &[u8]) -> Vec<u8> {
self.with_account_mut(address, |account| {
self.0.with_account_mut(address, |account| {
account.storage.get(key).cloned().unwrap_or_default()
})
}

fn storage_write(&self, key: &[u8], value: &[u8]) {
self.with_account_mut(&self.0.tx_input_box.to, |account| {
self.0.with_account_mut(&self.0.tx_input_box.to, |account| {
account.storage.insert(key.to_vec(), value.to_vec());
});
}
Expand All @@ -97,7 +99,7 @@ impl VMHooksHandlerSource for SingleTxApiVMHooksHandler {
}

fn account_data(&self, address: &VMAddress) -> AccountData {
self.with_account_mut(address, |account| account.clone())
self.0.with_account_mut(address, |account| account.clone())
}

fn account_code(&self, _address: &VMAddress) -> Vec<u8> {
Expand Down

0 comments on commit 09970d5

Please sign in to comment.