1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Block metadata

use super::{Header, Id};
use crate::{Error, Kind};
use serde::{Deserialize, Serialize};
use std::convert::{TryFrom, TryInto};
use tendermint_proto::types::BlockMeta as RawMeta;

/// Block metadata - Todo: implement constructor and getters
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(try_from = "RawMeta", into = "RawMeta")]
pub struct Meta {
    /// ID of the block
    pub block_id: Id,

    /// block size - Todo: make this robust (u63)
    pub block_size: i64,

    /// Header of the block
    pub header: Header,

    /// Number of transactions - Todo: make this robust (u63)
    pub num_txs: i64,
}

impl TryFrom<RawMeta> for Meta {
    type Error = Error;

    fn try_from(value: RawMeta) -> Result<Self, Self::Error> {
        Ok(Meta {
            block_id: value
                .block_id
                .ok_or_else(|| Error::from(Kind::InvalidBlock.context("no block_id")))?
                .try_into()?,
            block_size: value.block_size,
            header: value
                .header
                .ok_or_else(|| Error::from(Kind::InvalidBlock.context("no header")))?
                .try_into()?,
            num_txs: value.num_txs,
        })
    }
}

impl From<Meta> for RawMeta {
    fn from(value: Meta) -> Self {
        RawMeta {
            block_id: Some(value.block_id.into()),
            block_size: value.block_size,
            header: Some(value.header.into()),
            num_txs: value.num_txs,
        }
    }
}