near_workspaces/types/
block.rs1use near_account_id::AccountId;
2use near_primitives::views::{BlockHeaderView, BlockView};
3
4use crate::types::{ChunkHeader, NearToken};
5use crate::{BlockHeight, CryptoHash};
6
7#[derive(Clone, Debug, Eq, PartialEq)]
9pub struct Block {
10 author: AccountId,
11 header: BlockHeader,
12 chunks: Vec<ChunkHeader>,
13}
14
15impl Block {
16 pub fn author(&self) -> &AccountId {
18 &self.author
19 }
20
21 pub fn header(&self) -> &BlockHeader {
23 &self.header
24 }
25
26 pub fn chunks(&self) -> &[ChunkHeader] {
28 &self.chunks
29 }
30
31 pub fn timestamp(&self) -> u64 {
33 self.header.timestamp_nanosec
34 }
35
36 pub fn height(&self) -> BlockHeight {
38 self.header.height
39 }
40
41 pub fn hash(&self) -> &CryptoHash {
43 &self.header.hash
44 }
45
46 pub fn epoch_id(&self) -> &CryptoHash {
48 &self.header.epoch_id
49 }
50}
51
52#[derive(Clone, Debug, Eq, PartialEq)]
58pub struct BlockHeader {
59 height: BlockHeight,
60 epoch_id: CryptoHash,
61 next_epoch_id: CryptoHash,
62 hash: CryptoHash,
63 prev_hash: CryptoHash,
64 timestamp_nanosec: u64,
65 random_value: CryptoHash,
66 gas_price: NearToken,
67 block_ordinal: Option<u64>,
68 total_supply: NearToken,
69 last_final_block: CryptoHash,
70 last_ds_final_block: CryptoHash,
71 next_bp_hash: CryptoHash,
72 latest_protocol_version: u32,
73
74 prev_state_root: CryptoHash,
75 chunk_receipts_root: CryptoHash,
76 chunk_headers_root: CryptoHash,
77 chunk_tx_root: CryptoHash,
78 outcome_root: CryptoHash,
79 challenges_root: CryptoHash,
80 block_merkle_root: CryptoHash,
81}
82
83impl BlockHeader {
84 pub fn height(&self) -> BlockHeight {
86 self.height
87 }
88
89 pub fn epoch_id(&self) -> &CryptoHash {
91 &self.epoch_id
92 }
93
94 pub fn next_epoch_id(&self) -> &CryptoHash {
96 &self.next_epoch_id
97 }
98
99 pub fn hash(&self) -> &CryptoHash {
101 &self.hash
102 }
103
104 pub fn prev_hash(&self) -> &CryptoHash {
106 &self.prev_hash
107 }
108
109 pub fn timestamp_nanosec(&self) -> u64 {
111 self.timestamp_nanosec
112 }
113
114 pub fn random_value(&self) -> &CryptoHash {
116 &self.random_value
117 }
118
119 pub fn gas_price(&self) -> NearToken {
121 self.gas_price
122 }
123
124 pub fn block_ordinal(&self) -> Option<u64> {
126 self.block_ordinal
127 }
128
129 pub fn total_supply(&self) -> NearToken {
131 self.total_supply
132 }
133
134 pub fn last_final_block(&self) -> &CryptoHash {
136 &self.last_final_block
137 }
138
139 pub fn last_ds_final_block(&self) -> &CryptoHash {
141 &self.last_ds_final_block
142 }
143
144 pub fn next_bp_hash(&self) -> &CryptoHash {
146 &self.next_bp_hash
147 }
148
149 pub fn latest_protocol_version(&self) -> u32 {
151 self.latest_protocol_version
152 }
153
154 pub fn prev_state_root(&self) -> &CryptoHash {
156 &self.prev_state_root
157 }
158
159 pub fn chunk_receipts_root(&self) -> &CryptoHash {
161 &self.chunk_receipts_root
162 }
163
164 pub fn chunk_headers_root(&self) -> &CryptoHash {
166 &self.chunk_headers_root
167 }
168
169 pub fn chunk_tx_root(&self) -> &CryptoHash {
171 &self.chunk_tx_root
172 }
173
174 pub fn outcome_root(&self) -> &CryptoHash {
176 &self.outcome_root
177 }
178
179 pub fn challenges_root(&self) -> &CryptoHash {
181 &self.challenges_root
182 }
183
184 pub fn block_merkle_root(&self) -> &CryptoHash {
186 &self.block_merkle_root
187 }
188}
189
190impl From<BlockView> for Block {
191 fn from(view: BlockView) -> Self {
192 Self {
193 author: view.author,
194 header: view.header.into(),
195 chunks: view.chunks.into_iter().map(Into::into).collect(),
196 }
197 }
198}
199
200impl From<BlockHeaderView> for BlockHeader {
201 fn from(header_view: BlockHeaderView) -> Self {
202 Self {
203 height: header_view.height,
204 epoch_id: header_view.epoch_id.into(),
205 next_epoch_id: header_view.next_epoch_id.into(),
206 hash: header_view.hash.into(),
207 prev_hash: header_view.prev_hash.into(),
208 timestamp_nanosec: header_view.timestamp_nanosec,
209 random_value: header_view.random_value.into(),
210 gas_price: header_view.gas_price,
211 block_ordinal: header_view.block_ordinal,
212 total_supply: header_view.total_supply,
213 last_final_block: header_view.last_final_block.into(),
214 last_ds_final_block: header_view.last_ds_final_block.into(),
215 next_bp_hash: header_view.next_bp_hash.into(),
216 latest_protocol_version: header_view.latest_protocol_version,
217
218 prev_state_root: header_view.prev_state_root.into(),
219 chunk_receipts_root: header_view.chunk_receipts_root.into(),
220 chunk_headers_root: header_view.chunk_headers_root.into(),
221 chunk_tx_root: header_view.chunk_tx_root.into(),
222 outcome_root: header_view.outcome_root.into(),
223 challenges_root: header_view.challenges_root.into(),
224 block_merkle_root: header_view.block_merkle_root.into(),
225 }
226 }
227}