diff --git a/runtime-sdk/modules/evm/src/test.rs b/runtime-sdk/modules/evm/src/test.rs index 7220ad6a08..ea4dd9569c 100644 --- a/runtime-sdk/modules/evm/src/test.rs +++ b/runtime-sdk/modules/evm/src/test.rs @@ -281,12 +281,14 @@ fn test_evm_calls() { #[test] fn test_c10l_evm_calls() { + let _guard = crypto::signature::context::test_using_chain_context(); crypto::signature::context::set_chain_context(Default::default(), "test"); do_test_evm_calls::(); } #[test] fn test_c10l_evm_balance_transfer() { + let _guard = crypto::signature::context::test_using_chain_context(); crypto::signature::context::set_chain_context(Default::default(), "test"); let mut mock = mock::Mock::default(); let mut ctx = mock.create_ctx(); @@ -771,6 +773,7 @@ fn test_evm_runtime() { #[test] fn test_c10l_evm_runtime() { + let _guard = crypto::signature::context::test_using_chain_context(); crypto::signature::context::set_chain_context(Default::default(), "test"); do_test_evm_runtime::(); } diff --git a/runtime-sdk/src/crypto/signature/context.rs b/runtime-sdk/src/crypto/signature/context.rs index 7dcc128f5a..0dd8687d12 100644 --- a/runtime-sdk/src/crypto/signature/context.rs +++ b/runtime-sdk/src/crypto/signature/context.rs @@ -69,20 +69,32 @@ pub fn set_chain_context(runtime_id: Namespace, consensus_chain_context: &str) { *guard = Some(ctx.into_bytes()); } +/// Test helper to serialize unit tests using the global chain context. The chain context is reset +/// when this method is called. +/// +/// # Example +/// +/// ```rust +/// # use oasis_runtime_sdk::crypto::signature::context::test_using_chain_context; +/// let _guard = test_using_chain_context(); +/// // ... rest of the test code follows ... +/// ``` +#[cfg(any(test, feature = "test"))] +pub fn test_using_chain_context() -> std::sync::MutexGuard<'static, ()> { + static TEST_USING_CHAIN_CONTEXT: Lazy> = Lazy::new(Default::default); + let guard = TEST_USING_CHAIN_CONTEXT.lock().unwrap(); + *CHAIN_CONTEXT.lock().unwrap() = None; + + guard +} + #[cfg(test)] mod test { use super::*; - static TEST_GUARD: Lazy> = Lazy::new(Default::default); - - fn reset_chain_context() { - *CHAIN_CONTEXT.lock().unwrap() = None; - } - #[test] fn test_chain_context() { - let _guard = TEST_GUARD.lock().unwrap(); - reset_chain_context(); + let _guard = test_using_chain_context(); set_chain_context( "8000000000000000000000000000000000000000000000000000000000000000".into(), "643fb06848be7e970af3b5b2d772eb8cfb30499c8162bc18ac03df2f5e22520e", @@ -94,8 +106,7 @@ mod test { #[test] fn test_chain_context_not_configured() { - let _guard = TEST_GUARD.lock().unwrap(); - reset_chain_context(); + let _guard = test_using_chain_context(); let result = std::panic::catch_unwind(|| get_chain_context_for(b"test")); assert!(result.is_err()); @@ -103,8 +114,7 @@ mod test { #[test] fn test_chain_context_already_configured() { - let _guard = TEST_GUARD.lock().unwrap(); - reset_chain_context(); + let _guard = test_using_chain_context(); set_chain_context( "8000000000000000000000000000000000000000000000000000000000000000".into(), "643fb06848be7e970af3b5b2d772eb8cfb30499c8162bc18ac03df2f5e22520e",