From 35065b78666935cb0ae2476ca42af2fcc03c5fa9 Mon Sep 17 00:00:00 2001
From: "hinto.janai" <hinto.janai@protonmail.com>
Date: Thu, 12 Sep 2024 20:38:50 -0400
Subject: [PATCH] add `statics.rs`

---
 binaries/cuprated/src/main.rs    |  6 ++++-
 binaries/cuprated/src/statics.rs | 43 ++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 1 deletion(-)
 create mode 100644 binaries/cuprated/src/statics.rs

diff --git a/binaries/cuprated/src/main.rs b/binaries/cuprated/src/main.rs
index b05ecaa..327dec5 100644
--- a/binaries/cuprated/src/main.rs
+++ b/binaries/cuprated/src/main.rs
@@ -17,9 +17,13 @@ mod blockchain;
 mod config;
 mod p2p;
 mod rpc;
+mod statics;
 mod txpool;
 mod version;
 
+use std::sync::LazyLock;
+
 fn main() {
-    todo!()
+    // Initialize global static `LazyLock` data.
+    statics::init_lazylock_statics();
 }
diff --git a/binaries/cuprated/src/statics.rs b/binaries/cuprated/src/statics.rs
new file mode 100644
index 0000000..29e6cb5
--- /dev/null
+++ b/binaries/cuprated/src/statics.rs
@@ -0,0 +1,43 @@
+//! Global `static`s used throughout `cuprated`.
+
+use std::{
+    sync::{atomic::AtomicU64, LazyLock},
+    time::{SystemTime, UNIX_EPOCH},
+};
+
+/// Define all the `static`s in the file/module.
+///
+/// This wraps all `static` inside a `LazyLock` and creates a
+/// [`init_lazylock_statics`] function that must/should be
+/// used by `main()` early on.
+macro_rules! define_lazylock_statics {
+	($(
+		$( #[$attr:meta] )*
+		$name:ident: $t:ty = $init_fn:expr;
+	)*) => {
+    	/// Initialize global static `LazyLock` data.
+		pub fn init_lazylock_statics() {
+			$(
+				LazyLock::force(&$name);
+			)*
+		}
+
+		$(
+			$(#[$attr])*
+			pub static $name: LazyLock<$t> = LazyLock::new(|| $init_fn);
+		)*
+	};
+}
+
+define_lazylock_statics! {
+    /// The start time of `cuprated`.
+    ///
+    /// This must/should be set early on in `main()`.
+    START_INSTANT: SystemTime = SystemTime::now();
+
+    /// Start time of `cuprated` as a UNIX timestamp.
+    START_INSTANT_UNIX: u64 = START_INSTANT
+        .duration_since(UNIX_EPOCH)
+        .expect("Failed to set `cuprated` startup time.")
+        .as_secs();
+}