Skip to main content

tendermint_rpc/endpoint/
header_by_hash.rs

1//! `/header_by_hash` endpoint JSON-RPC wrapper
2
3use serde::{Deserialize, Serialize};
4use tendermint::{block::Header, Hash};
5
6use crate::dialect::{v0_37, v0_38, Dialect};
7use crate::request::RequestMessage;
8
9/// Get information about a specific block by its hash
10#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
11pub struct Request {
12    /// Hash of the block to request.
13    ///
14    /// If no hash is provided, it will return no header (as if the hash
15    /// did not match any block).
16    ///
17    /// Serialized internally into a hex-encoded string before sending to
18    /// the RPC server.
19    #[serde(default)]
20    #[serde(with = "crate::serializers::option_hash")]
21    pub hash: Option<Hash>,
22}
23
24impl Request {
25    /// Create a new request for information about a particular block
26    pub fn new<H: Into<Hash>>(hash: H) -> Self {
27        Self {
28            hash: Some(hash.into()),
29        }
30    }
31}
32
33impl RequestMessage for Request {
34    fn method(&self) -> crate::Method {
35        crate::Method::HeaderByHash
36    }
37}
38
39impl crate::Request<v0_37::Dialect> for Request {
40    type Response = Response;
41}
42
43impl crate::Request<v0_38::Dialect> for Request {
44    type Response = Response;
45}
46
47impl<S: Dialect> crate::SimpleRequest<S> for Request
48where
49    Self: crate::Request<S>,
50    Response: From<Self::Response>,
51{
52    type Output = Response;
53}
54
55/// Header response
56#[derive(Clone, Debug, Deserialize, Serialize)]
57pub struct Response {
58    /// Header data
59    pub header: Option<Header>,
60}
61
62impl crate::Response for Response {}
63
64impl From<super::block_by_hash::Response> for Response {
65    fn from(block_resp: super::block_by_hash::Response) -> Self {
66        Response {
67            header: block_resp.block.map(|b| b.header),
68        }
69    }
70}