near_workspaces/types/
block.rs

1use near_account_id::AccountId;
2use near_primitives::views::{BlockHeaderView, BlockView};
3
4use crate::types::{ChunkHeader, NearToken};
5use crate::{BlockHeight, CryptoHash};
6
7/// Struct containing information on block coming from the network
8#[derive(Clone, Debug, Eq, PartialEq)]
9pub struct Block {
10    author: AccountId,
11    header: BlockHeader,
12    chunks: Vec<ChunkHeader>,
13}
14
15impl Block {
16    /// The account id of the block author.
17    pub fn author(&self) -> &AccountId {
18        &self.author
19    }
20
21    /// The block header info.
22    pub fn header(&self) -> &BlockHeader {
23        &self.header
24    }
25
26    /// The list of chunks in this block.
27    pub fn chunks(&self) -> &[ChunkHeader] {
28        &self.chunks
29    }
30
31    /// The block timestamp in nanoseconds.
32    pub fn timestamp(&self) -> u64 {
33        self.header.timestamp_nanosec
34    }
35
36    /// Current height of this block.
37    pub fn height(&self) -> BlockHeight {
38        self.header.height
39    }
40
41    /// The hash of the block itself.
42    pub fn hash(&self) -> &CryptoHash {
43        &self.header.hash
44    }
45
46    /// The id of an epoch this block belongs to.
47    pub fn epoch_id(&self) -> &CryptoHash {
48        &self.header.epoch_id
49    }
50}
51
52/// The block header info. This is a non-exhaustive list of items that
53/// could be present in a block header. More can be added in the future.
54///
55/// NOTE: For maintainability purposes, some items have been excluded. If required,
56/// please submit an issue to [workspaces](https://github.com/near/workspaces-rs/issues).
57#[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    /// Current height of this block.
85    pub fn height(&self) -> BlockHeight {
86        self.height
87    }
88
89    /// The id of an epoch this block belongs to.
90    pub fn epoch_id(&self) -> &CryptoHash {
91        &self.epoch_id
92    }
93
94    /// The next epoch id.
95    pub fn next_epoch_id(&self) -> &CryptoHash {
96        &self.next_epoch_id
97    }
98
99    /// The hash of the block itself.
100    pub fn hash(&self) -> &CryptoHash {
101        &self.hash
102    }
103
104    /// The hash of the previous block.
105    pub fn prev_hash(&self) -> &CryptoHash {
106        &self.prev_hash
107    }
108
109    /// The block timestamp in nanoseconds.
110    pub fn timestamp_nanosec(&self) -> u64 {
111        self.timestamp_nanosec
112    }
113
114    /// The random value of the block.
115    pub fn random_value(&self) -> &CryptoHash {
116        &self.random_value
117    }
118
119    /// The gas price of the block.
120    pub fn gas_price(&self) -> NearToken {
121        self.gas_price
122    }
123
124    /// The block ordinal.
125    pub fn block_ordinal(&self) -> Option<u64> {
126        self.block_ordinal
127    }
128
129    /// The total supply balance of the block.
130    pub fn total_supply(&self) -> NearToken {
131        self.total_supply
132    }
133
134    /// The last final block hash.
135    pub fn last_final_block(&self) -> &CryptoHash {
136        &self.last_final_block
137    }
138
139    /// The last ds final block hash.
140    pub fn last_ds_final_block(&self) -> &CryptoHash {
141        &self.last_ds_final_block
142    }
143
144    /// The next bp hash.
145    pub fn next_bp_hash(&self) -> &CryptoHash {
146        &self.next_bp_hash
147    }
148
149    /// The latest protocol version.
150    pub fn latest_protocol_version(&self) -> u32 {
151        self.latest_protocol_version
152    }
153
154    /// The previous state root.
155    pub fn prev_state_root(&self) -> &CryptoHash {
156        &self.prev_state_root
157    }
158
159    /// The chunk receipts root.
160    pub fn chunk_receipts_root(&self) -> &CryptoHash {
161        &self.chunk_receipts_root
162    }
163
164    /// The chunk headers root.
165    pub fn chunk_headers_root(&self) -> &CryptoHash {
166        &self.chunk_headers_root
167    }
168
169    /// The chunk tx root.
170    pub fn chunk_tx_root(&self) -> &CryptoHash {
171        &self.chunk_tx_root
172    }
173
174    /// The outcome root.
175    pub fn outcome_root(&self) -> &CryptoHash {
176        &self.outcome_root
177    }
178
179    /// The challenges root.
180    pub fn challenges_root(&self) -> &CryptoHash {
181        &self.challenges_root
182    }
183
184    /// The block merkle root.
185    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}