Skip to main content

amadeus_node/consensus/
mod.rs

1#![allow(clippy::module_inception)]
2pub mod consensus;
3pub mod doms;
4pub mod fabric;
5pub mod genesis;
6
7// Re-export DST constants from amadeus_utils
8pub use amadeus_utils::constants::{
9    DST, DST_ANR, DST_ANR_CHALLENGE, DST_ATT, DST_ENTRY, DST_MOTION, DST_NODE, DST_POP, DST_TX, DST_VRF,
10};
11
12use crate::utils::misc::TermExt;
13use crate::utils::rocksdb::RocksDb;
14use amadeus_utils::constants::CF_SYSCONF;
15use eetf::Term;
16
17/// Chain epoch accessor (Elixir: Consensus.chain_epoch/0)
18/// Returns current epoch calculated as height / 100_000
19pub fn chain_epoch(db: &RocksDb) -> u32 {
20    chain_height(db) / 100_000
21}
22
23/// Chain height accessor - gets current blockchain height
24pub fn chain_height(db: &RocksDb) -> u32 {
25    match db.get(CF_SYSCONF, b"temporal_height") {
26        Ok(Some(bytes)) => {
27            // Elixir stores as ETF term with `term: true`
28            match Term::decode(&bytes[..]) {
29                Ok(term) => TermExt::get_integer(&term).unwrap_or(0) as u32,
30                Err(_) => 0, // fallback if deserialization fails
31            }
32        }
33        _ => 0, // fallback if key not found
34    }
35}