Skip to main content

tendermint_rpc/endpoint/
block_by_hash.rs

1//! `/block_by_hash` endpoint JSON-RPC wrapper
2
3use serde::{Deserialize, Serialize};
4use tendermint::{
5    block::{self, Block},
6    Hash,
7};
8
9use crate::dialect::{self, Dialect};
10use crate::request::RequestMessage;
11
12/// Get information about a specific block by its hash
13#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
14pub struct Request {
15    /// Hash of the block to request.
16    ///
17    /// If no hash is provided, it will return no block (as if the hash
18    /// did not match any block).
19    ///
20    /// Serialized internally into a base64-encoded string before sending to
21    /// the RPC server.
22    #[serde(default)]
23    #[serde(with = "crate::serializers::opt_tm_hash_base64")]
24    pub hash: Option<Hash>,
25}
26
27impl Request {
28    /// Create a new request for information about a particular block
29    pub fn new<H: Into<Hash>>(hash: H) -> Self {
30        Self {
31            hash: Some(hash.into()),
32        }
33    }
34}
35
36impl RequestMessage for Request {
37    fn method(&self) -> crate::Method {
38        crate::Method::BlockByHash
39    }
40}
41
42impl crate::Request<dialect::v0_34::Dialect> for Request {
43    type Response = Response;
44}
45
46impl crate::Request<dialect::v0_37::Dialect> for Request {
47    type Response = Response;
48}
49
50impl crate::Request<dialect::v0_38::Dialect> for Request {
51    type Response = self::v0_38::DialectResponse;
52}
53
54impl<S: Dialect> crate::SimpleRequest<S> for Request
55where
56    Self: crate::Request<S>,
57    Response: From<Self::Response>,
58{
59    type Output = Response;
60}
61
62/// Block responses
63#[derive(Clone, Debug, Deserialize, Serialize)]
64pub struct Response {
65    /// Block ID
66    pub block_id: block::Id,
67
68    /// Block data
69    pub block: Option<Block>,
70}
71
72impl crate::Response for Response {}
73
74pub mod v0_38 {
75    use super::*;
76    use crate::endpoint::block::v0_38::DialectBlock;
77
78    #[derive(Clone, Debug, Deserialize, Serialize)]
79    pub struct DialectResponse {
80        /// Block ID
81        pub block_id: block::Id,
82
83        /// Block data
84        pub block: Option<DialectBlock>,
85    }
86
87    impl crate::Response for DialectResponse {}
88
89    impl From<DialectResponse> for Response {
90        fn from(msg: DialectResponse) -> Self {
91            Self {
92                block_id: msg.block_id,
93                block: msg.block.map(Into::into),
94            }
95        }
96    }
97}