cometbft_rpc/endpoint/
header_by_hash.rs

1//! `/header_by_hash` endpoint JSON-RPC wrapper
2
3use cometbft::{block::Header, Hash};
4use serde::{Deserialize, Serialize};
5
6use crate::dialect::v1;
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<v1::Dialect> for Request {
40    type Response = Response;
41}
42
43impl crate::SimpleRequest<v1::Dialect> for Request {
44    type Output = Response;
45}
46
47/// Header response
48#[derive(Clone, Debug, Deserialize, Serialize)]
49pub struct Response {
50    /// Header data
51    pub header: Option<Header>,
52}
53
54impl crate::Response for Response {}
55
56impl From<super::block_by_hash::Response> for Response {
57    fn from(block_resp: super::block_by_hash::Response) -> Self {
58        Response {
59            header: block_resp.block.map(|b| b.header),
60        }
61    }
62}