eryon_mem/blocks/
block.rs

1/*
2    appellation: block <module>
3    authors: @FL03
4*/
5use super::BlockHeader;
6use scsys::{Id, Timestamp};
7
8/// A [`Block`] is a fundamental unit of data within a blockchain, containing a header that
9/// includes metadata such as the block's id, timestamp, and previous hash.
10#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
11#[cfg_attr(
12    feature = "serde",
13    derive(serde_derive::Deserialize, serde_derive::Serialize),
14    serde(rename_all = "snake_case")
15)]
16#[repr(C)]
17pub struct Block {
18    pub(crate) header: BlockHeader,
19}
20
21impl Block {
22    /// returns a new [`Block`] with the given header
23    pub const fn new(header: BlockHeader) -> Self {
24        Self { header }
25    }
26    pub fn from_id(id: Id) -> Self {
27        Self {
28            header: BlockHeader::from_id(id),
29        }
30    }
31    /// returns a reference to the block header
32    pub const fn header(&self) -> &BlockHeader {
33        &self.header
34    }
35    /// returns a mutable reference to the block header
36    pub const fn header_mut(&mut self) -> &mut BlockHeader {
37        &mut self.header
38    }
39    /// returns a copy of the block id
40    pub const fn id(&self) -> Id {
41        self.header().id()
42    }
43    /// returns a copy of the block timestamp
44    pub const fn timestamp(&self) -> Timestamp {
45        self.header().timestamp()
46    }
47}