Skip to main content

imbibe_domain/
block.rs

1use core::fmt::{self, Debug, Formatter};
2
3use bon::Builder;
4use bytes::Bytes;
5use jiff::Timestamp;
6
7#[cfg(feature = "serde")]
8use serde::{Deserialize, Serialize};
9
10use super::{Address, NonEmptyBz, Sha256};
11
12#[derive(Debug, Clone, Builder)]
13#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
14pub struct Block<T = Bytes> {
15	header: Header,
16	gas_used: u64,
17	hash: Sha256,
18	data: BlockData<T>,
19}
20
21#[derive(Debug, Clone, Builder)]
22#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
23pub struct Header {
24	chain_id: String,
25	height: u64,
26	time: Timestamp,
27	validators_hash: Sha256,
28	next_validators_hash: Sha256,
29	consensus_hash: Sha256,
30	app_hash: AppHash,
31	proposer: Address,
32	last_commit_hash: Option<Sha256>,
33	data_hash: Option<Sha256>,
34	last_results_hash: Option<Sha256>,
35	evidence_hash: Option<Sha256>,
36}
37
38#[derive(Clone)]
39#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
40pub struct AppHash(Vec<u8>);
41
42#[derive(Debug, Clone)]
43#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
44pub struct BlockData<T>(Vec<NonEmptyBz<T>>);
45
46impl Block {
47	pub fn header(&self) -> &Header {
48		&self.header
49	}
50
51	pub fn data(&self) -> &BlockData<Bytes> {
52		&self.data
53	}
54
55	pub fn gas_used(&self) -> u64 {
56		self.gas_used
57	}
58
59	pub fn hash(&self) -> &Sha256 {
60		&self.hash
61	}
62}
63
64impl Header {
65	pub fn chain_id(&self) -> &str {
66		&self.chain_id
67	}
68
69	pub fn height(&self) -> u64 {
70		self.height
71	}
72
73	pub fn time(&self) -> &Timestamp {
74		&self.time
75	}
76
77	pub fn validators_hash(&self) -> &Sha256 {
78		&self.validators_hash
79	}
80
81	pub fn next_validators_hash(&self) -> &Sha256 {
82		&self.next_validators_hash
83	}
84
85	pub fn consensus_hash(&self) -> &Sha256 {
86		&self.consensus_hash
87	}
88
89	pub fn app_hash(&self) -> &AppHash {
90		&self.app_hash
91	}
92
93	pub fn proposer(&self) -> &Address {
94		&self.proposer
95	}
96
97	pub fn last_commit_hash(&self) -> Option<&Sha256> {
98		self.last_commit_hash.as_ref()
99	}
100
101	pub fn data_hash(&self) -> Option<&Sha256> {
102		self.data_hash.as_ref()
103	}
104
105	pub fn last_results_hash(&self) -> Option<&Sha256> {
106		self.last_results_hash.as_ref()
107	}
108
109	pub fn evidence_hash(&self) -> Option<&Sha256> {
110		self.evidence_hash.as_ref()
111	}
112}
113
114impl AppHash {
115	pub const fn new(hash: Vec<u8>) -> Self {
116		Self(hash)
117	}
118
119	pub fn get(&self) -> &[u8] {
120		self.0.as_slice()
121	}
122
123	pub fn into_bytes(self) -> Vec<u8> {
124		self.0
125	}
126}
127
128impl<T> BlockData<T> {
129	pub fn new(data: Vec<NonEmptyBz<T>>) -> Option<Self> {
130		Some(Self(data))
131	}
132
133	pub fn get(&self) -> &[NonEmptyBz<T>] {
134		&self.0
135	}
136}
137
138impl Debug for AppHash {
139	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
140		write!(f, "AppHash({})", const_hex::encode(self.get()))
141	}
142}