tendermint_rpc/endpoint/
block.rs

1//! `/block` endpoint JSON-RPC wrapper
2
3use serde::{Deserialize, Serialize};
4use tendermint::block::{self, Block};
5
6use crate::dialect::{self, Dialect};
7use crate::request::RequestMessage;
8
9/// Get information about a specific block
10#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
11pub struct Request {
12    /// Height of the block to request.
13    ///
14    /// If no height is provided, it will fetch results for the latest block.
15    pub height: Option<block::Height>,
16}
17
18impl Request {
19    /// Create a new request for information about a particular block
20    pub fn new(height: block::Height) -> Self {
21        Self {
22            height: Some(height),
23        }
24    }
25}
26
27impl RequestMessage for Request {
28    fn method(&self) -> crate::Method {
29        crate::Method::Block
30    }
31}
32
33impl crate::Request<dialect::v0_34::Dialect> for Request {
34    type Response = Response;
35}
36
37impl crate::Request<dialect::v0_37::Dialect> for Request {
38    type Response = Response;
39}
40
41impl crate::Request<dialect::v0_38::Dialect> for Request {
42    type Response = self::v0_38::DialectResponse;
43}
44
45impl<S: Dialect> crate::SimpleRequest<S> for Request
46where
47    Self: crate::Request<S>,
48    Response: From<Self::Response>,
49{
50    type Output = Response;
51}
52
53/// Block responses
54#[derive(Clone, Debug, Deserialize, Serialize)]
55pub struct Response {
56    /// Block ID
57    pub block_id: block::Id,
58
59    /// Block data
60    pub block: Block,
61}
62
63impl crate::Response for Response {}
64
65pub mod v0_38 {
66    use std::vec::Vec;
67
68    use block::{Commit, Header};
69    use tendermint::evidence;
70
71    use tendermint_proto::v0_38::types::Block as RawBlock;
72
73    use super::*;
74
75    #[derive(Clone, Debug, Deserialize, Serialize)]
76    pub struct DialectResponse {
77        /// Block ID
78        pub block_id: block::Id,
79
80        /// Block data
81        pub block: DialectBlock,
82    }
83
84    impl crate::Response for DialectResponse {}
85
86    impl From<DialectResponse> for Response {
87        fn from(msg: DialectResponse) -> Self {
88            Self {
89                block_id: msg.block_id,
90                block: msg.block.into(),
91            }
92        }
93    }
94
95    #[derive(Clone, Debug, Deserialize, Serialize)]
96    #[serde(try_from = "RawBlock", into = "RawBlock")]
97    pub struct DialectBlock {
98        /// Block header
99        pub header: Header,
100
101        /// Transaction data
102        pub data: Vec<Vec<u8>>,
103
104        /// Evidence of malfeasance
105        pub evidence: evidence::List,
106
107        /// Last commit, should be `None` for the initial block.
108        pub last_commit: Option<Commit>,
109    }
110
111    impl From<DialectBlock> for RawBlock {
112        fn from(msg: DialectBlock) -> Self {
113            RawBlock::from(Block::from(msg))
114        }
115    }
116
117    impl TryFrom<RawBlock> for DialectBlock {
118        type Error = <Block as TryFrom<RawBlock>>::Error;
119
120        fn try_from(value: RawBlock) -> Result<Self, Self::Error> {
121            Block::try_from(value).map(DialectBlock::from)
122        }
123    }
124
125    impl From<DialectBlock> for Block {
126        fn from(msg: DialectBlock) -> Self {
127            Self::new(msg.header, msg.data, msg.evidence, msg.last_commit)
128        }
129    }
130
131    impl From<Block> for DialectBlock {
132        fn from(msg: Block) -> Self {
133            Self {
134                header: msg.header,
135                data: msg.data,
136                evidence: msg.evidence,
137                last_commit: msg.last_commit,
138            }
139        }
140    }
141}