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

Add testutils for accessing all storage of a contract #1118

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
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
103 changes: 103 additions & 0 deletions soroban-sdk/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,106 @@ impl Instance {
.unwrap_infallible();
}
}

#[cfg(any(test, feature = "testutils"))]
#[cfg_attr(feature = "docs", doc(cfg(feature = "testutils")))]
mod testutils {
use super::*;
use crate::{testutils, xdr, Map, TryIntoVal};

impl testutils::storage::Instance for Instance {
fn all(&self) -> Map<Val, Val> {
let env = &self.storage.env;
let storage = env.host().with_mut_storage(|s| Ok(s.map.clone())).unwrap();
let address: xdr::ScAddress = env.current_contract_address().try_into().unwrap();
for entry in storage {
let (k, Some((v, _))) = entry else {
continue;
};
let xdr::LedgerKey::ContractData(xdr::LedgerKeyContractData {
ref contract, ..
}) = *k
else {
continue;
};
if contract != &address {
continue;
}
let xdr::LedgerEntry {
data:
xdr::LedgerEntryData::ContractData(xdr::ContractDataEntry {
key: xdr::ScVal::LedgerKeyContractInstance,
val:
xdr::ScVal::ContractInstance(xdr::ScContractInstance {
ref storage,
..
}),
..
}),
..
} = *v
else {
continue;
};
return match storage {
Some(map) => {
let map: Val =
Val::try_from_val(env, &xdr::ScVal::Map(Some(map.clone()))).unwrap();
map.try_into_val(env).unwrap()
}
None => Map::new(env),
};
}
panic!("contract instance for current contract address not found");
}
}

impl testutils::storage::Persistent for Persistent {
fn all(&self) -> Map<Val, Val> {
all(&self.storage.env, xdr::ContractDataDurability::Persistent)
}
}

impl testutils::storage::Temporary for Temporary {
fn all(&self) -> Map<Val, Val> {
all(&self.storage.env, xdr::ContractDataDurability::Temporary)
}
}

fn all(env: &Env, d: xdr::ContractDataDurability) -> Map<Val, Val> {
let storage = env.host().with_mut_storage(|s| Ok(s.map.clone())).unwrap();
let mut map = Map::<Val, Val>::new(env);
for entry in storage {
let (_, Some((v, _))) = entry else {
continue;
};
let xdr::LedgerEntry {
data:
xdr::LedgerEntryData::ContractData(xdr::ContractDataEntry {
ref key,
ref val,
durability,
..
}),
..
} = *v
else {
continue;
};
if d != durability {
continue;
}
let Ok(key) = Val::try_from_val(env, key) else {
continue;
};
let Ok(val) = Val::try_from_val(env, val) else {
continue;
};
map.set(key, val);
}
map
}
}
#[cfg(any(test, feature = "testutils"))]
#[cfg_attr(feature = "docs", doc(cfg(feature = "testutils")))]
pub use testutils::*;
1 change: 1 addition & 0 deletions soroban-sdk/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ mod max_ttl;
mod prng;
mod proptest_scval_cmp;
mod proptest_val_cmp;
mod storage_testutils;
mod token_client;
mod token_spec;
42 changes: 42 additions & 0 deletions soroban-sdk/src/tests/storage_testutils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::{
self as soroban_sdk,
testutils::storage::{Instance as _, Persistent as _, Temporary as _},
Map, Val,
};
use soroban_sdk::{contract, Env};

#[contract]
pub struct Contract;

#[test]
fn all() {
let e = Env::default();
let id = e.register_contract(None, Contract);

e.as_contract(&id, || {
e.storage().instance().set(&1, &2);
e.storage().instance().set(&1, &true);
e.storage().instance().set(&2, &3);
e.storage().persistent().set(&10, &20);
e.storage().persistent().set(&10, &false);
e.storage().persistent().set(&20, &30);
e.storage().temporary().set(&100, &200);
e.storage().temporary().set(&100, &());
e.storage().temporary().set(&200, &300);
});

e.as_contract(&id, || {
assert_eq!(
e.storage().instance().all(),
Map::<Val, Val>::from_array(&e, [(1.into(), true.into()), (2.into(), 3.into())])
leighmcculloch marked this conversation as resolved.
Show resolved Hide resolved
);
assert_eq!(
e.storage().persistent().all(),
Map::<Val, Val>::from_array(&e, [(10.into(), false.into()), (20.into(), 30.into())])
);
assert_eq!(
e.storage().temporary().all(),
Map::<Val, Val>::from_array(&e, [(100.into(), ().into()), (200.into(), 300.into())])
);
});
}
2 changes: 2 additions & 0 deletions soroban-sdk/src/testutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub use mock_auth::{
AuthorizedFunction, AuthorizedInvocation, MockAuth, MockAuthContract, MockAuthInvoke,
};

pub mod storage;

use crate::{Env, Val, Vec};

#[doc(hidden)]
Expand Down
19 changes: 19 additions & 0 deletions soroban-sdk/src/testutils/storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::{Map, Val};

/// Test utilities for [`Persistent`][crate::storage::Persistent].
pub trait Persistent {
/// Returns all data stored in persistent storage for the contract.
fn all(&self) -> Map<Val, Val>;
}

/// Test utilities for [`Temporary`][crate::storage::Temporary].
pub trait Temporary {
/// Returns all data stored in temporary storage for the contract.
fn all(&self) -> Map<Val, Val>;
}

/// Test utilities for [`Instance`][crate::storage::Instance].
pub trait Instance {
/// Returns all data stored in Instance storage for the contract.
fn all(&self) -> Map<Val, Val>;
}