Unverified Commit a8b791a0 authored by Hero Bird's avatar Hero Bird Committed by GitHub

Fix single threaded tests in off-chain environment (#382)

* [core] fix single threaded tests

Tests that have been run using:
cargo test -- --test-threads=1

* [lang] fix off-chain testing for contracts
parent f2dda94f
Pipeline #89498 failed with stages
in 5 minutes and 10 seconds
......@@ -78,6 +78,11 @@ impl AccountsDb {
}
}
/// Resets the account DB to uninitialized state.
pub fn reset(&mut self) {
self.accounts.clear()
}
/// Returns the account at the given account ID or creates it.
pub fn get_or_create_account<T>(&mut self, at: &T::AccountId) -> &mut Account
where
......
......@@ -42,6 +42,14 @@ impl ChainSpec {
}
}
/// Resets the chain spec to uninitialized state.
pub fn reset(&mut self) {
self.gas_price = OffBalance::uninitialized();
self.minimum_balance = OffBalance::uninitialized();
self.tombstone_deposit = OffBalance::uninitialized();
self.block_time = OffTimestamp::uninitialized();
}
/// Default initialization for the off-chain specification.
pub fn initialize_as_default<T>(&mut self) -> crate::env::Result<()>
where
......
......@@ -28,6 +28,11 @@ impl Console {
}
}
/// Resets the console to uninitialized state.
pub fn reset(&mut self) {
self.past_prints.clear();
}
/// Prints the contents to the actual console and stores them.
pub fn println(&mut self, contents: &str) {
self.past_prints.push(contents.to_string());
......
......@@ -58,6 +58,11 @@ impl EmittedEventsRecorder {
}
}
/// Resets the emitted events to none.
pub fn reset(&mut self) {
self.emitted_events.clear();
}
/// Records a new emitted event.
pub fn record<T, E>(&mut self, new_event: E)
where
......
......@@ -110,6 +110,36 @@ impl EnvInstance {
}
}
/// Returns `true` if the off-chain environment is uninitialized.
pub fn is_initialized(&self) -> bool {
!self.exec_context.is_empty()
}
/// Either resets or initializes the off-chain environment to default values.
pub fn initialize_or_reset_as_default<T>(&mut self) -> crate::env::Result<()>
where
T: EnvTypes,
<T as EnvTypes>::AccountId: From<[u8; 32]>,
{
if self.is_initialized() {
self.reset()
}
self.initialize_as_default::<T>()?;
Ok(())
}
/// Resets the off-chain environment to unintialized state.
pub fn reset(&mut self) {
self.accounts.reset();
self.exec_context.clear();
self.chain_spec.reset();
self.blocks.clear();
self.console.reset();
self.runtime_storage.reset();
self.runtime_call_handler.reset();
self.emitted_events.reset();
}
/// Initializes the whole off-chain environment.
///
/// # Note
......
......@@ -37,6 +37,11 @@ impl RuntimeCallHandler {
Self { registered: None }
}
/// Resets the runtime call handler to uninitialized state.
pub fn reset(&mut self) {
self.registered = None;
}
/// Register a runtime call handler.
pub fn register<T, F>(&mut self, mut f: F)
where
......
......@@ -34,6 +34,11 @@ impl RuntimeStorage {
}
}
/// Resets the runtime storage to uninitialized state.
pub fn reset(&mut self) {
self.entries.clear();
}
/// Stores the value under the given key.
pub fn store<T>(&mut self, key: Vec<u8>, value: T)
where
......
......@@ -288,14 +288,13 @@ where
///
/// - Initializes the off-chain environment with default values that fit most
/// uses cases.
/// - The off-chain environment _must_ be initialized before use.
pub fn initialize_as_default<T>() -> Result<()>
pub fn initialize_or_reset_as_default<T>() -> Result<()>
where
T: EnvTypes,
<T as EnvTypes>::AccountId: From<[u8; 32]>,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
instance.initialize_as_default::<T>()
instance.initialize_or_reset_as_default::<T>()
})
}
......@@ -307,7 +306,7 @@ where
F: FnOnce(DefaultAccounts<T>) -> Result<()>,
<T as EnvTypes>::AccountId: From<[u8; 32]>,
{
initialize_as_default::<T>()?;
initialize_or_reset_as_default::<T>()?;
let default_accounts = default_accounts::<T>()?;
f(default_accounts)
}
......
......@@ -56,7 +56,7 @@ impl GenerateCode for TestWrapper<'_> {
type Wrapped = TestableStorage;
fn instantiate() -> Self::Wrapped {
ink_core::env::test::initialize_as_default::<ink_core::env::DefaultEnvTypes>()
ink_core::env::test::initialize_or_reset_as_default::<ink_core::env::DefaultEnvTypes>()
.expect("encountered already initialized off-chain environment");
let mut contract: Self = unsafe {
let mut alloc =
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment