diff --git a/runtime-sdk/modules/evm/src/test.rs b/runtime-sdk/modules/evm/src/test.rs index e6d61c065e..6e3b794087 100644 --- a/runtime-sdk/modules/evm/src/test.rs +++ b/runtime-sdk/modules/evm/src/test.rs @@ -282,18 +282,21 @@ fn test_evm_calls() { #[test] fn test_c10l_evm_calls_enc() { + let _guard = crypto::signature::context::test_using_chain_context(); crypto::signature::context::set_chain_context(Default::default(), "test"); do_test_evm_calls::(false); } #[test] fn test_c10l_evm_calls_plain() { + let _guard = crypto::signature::context::test_using_chain_context(); crypto::signature::context::set_chain_context(Default::default(), "test"); do_test_evm_calls::(true /* force_plain */); } #[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 ctx = mock.create_ctx(); @@ -791,6 +794,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 fce3821aff..8a5d008737 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",