tendermint/block/
meta.rs

1//! Block metadata
2
3use serde::{Deserialize, Serialize};
4use tendermint_proto::v0_37::types::BlockMeta as RawMeta;
5
6use super::{Header, Id};
7use crate::prelude::*;
8
9/// Block metadata - Todo: implement constructor and getters
10#[derive(Serialize, Deserialize, Clone, Debug)]
11#[serde(try_from = "RawMeta", into = "RawMeta")]
12pub struct Meta {
13    /// ID of the block
14    pub block_id: Id,
15
16    /// block size - Todo: make this robust (u63)
17    pub block_size: i64,
18
19    /// Header of the block
20    pub header: Header,
21
22    /// Number of transactions - Todo: make this robust (u63)
23    pub num_txs: i64,
24}
25
26tendermint_pb_modules! {
27    use super::Meta;
28    use crate::{error::Error, prelude::*};
29    use pb::types::BlockMeta as RawMeta;
30
31    impl TryFrom<RawMeta> for Meta {
32        type Error = Error;
33
34        fn try_from(value: RawMeta) -> Result<Self, Self::Error> {
35            Ok(Meta {
36                block_id: value
37                    .block_id
38                    .ok_or_else(|| Error::invalid_block("no block_id".to_string()))?
39                    .try_into()?,
40                block_size: value.block_size,
41                header: value
42                    .header
43                    .ok_or_else(|| Error::invalid_block("no header".to_string()))?
44                    .try_into()?,
45                num_txs: value.num_txs,
46            })
47        }
48    }
49
50    impl From<Meta> for RawMeta {
51        fn from(value: Meta) -> Self {
52            RawMeta {
53                block_id: Some(value.block_id.into()),
54                block_size: value.block_size,
55                header: Some(value.header.into()),
56                num_txs: value.num_txs,
57            }
58        }
59    }
60}