datex_core/runtime/
global_context.rs1use crate::{crypto::crypto::CryptoTrait, utils::time::TimeTrait};
2use std::{
3 cell::RefCell,
4 sync::{Arc, Mutex},
5}; #[cfg(feature = "debug")]
8#[derive(Clone, Debug)]
9pub struct DebugFlags {
10 pub allow_unsigned_blocks: bool,
11 pub enable_deterministic_behavior: bool,
12}
13#[cfg(feature = "debug")]
14impl Default for DebugFlags {
15 fn default() -> Self {
16 DebugFlags {
17 allow_unsigned_blocks: true,
18 enable_deterministic_behavior: true,
19 }
20 }
21}
22
23#[derive(Clone)]
24pub struct GlobalContext {
25 pub crypto: Arc<Mutex<dyn CryptoTrait>>,
26 pub time: Arc<Mutex<dyn TimeTrait>>,
27
28 #[cfg(feature = "debug")]
29 pub debug_flags: DebugFlags,
30}
31
32impl GlobalContext {
33 pub fn new(
34 crypto: Arc<Mutex<dyn CryptoTrait>>,
35 time: Arc<Mutex<dyn TimeTrait>>,
36 ) -> GlobalContext {
37 GlobalContext {
38 crypto,
39 time,
40 #[cfg(feature = "debug")]
41 debug_flags: DebugFlags::default(),
42 }
43 }
44}
45
46thread_local! {
47 pub static GLOBAL_CONTEXT: RefCell<Option<GlobalContext>> = const { RefCell::new(None) };
48}
49pub fn set_global_context(c: GlobalContext) {
50 GLOBAL_CONTEXT.replace(Some(c.clone()));
51}
52pub fn get_global_context() -> GlobalContext {
53 match GLOBAL_CONTEXT.with(|c| c.borrow().clone()) {
54 Some(c) => c,
55 None => panic!(
56 "Global context not initialized - call set_global_context first!"
57 ),
58 }
59}