cometbft_rpc/endpoint/
block_by_hash.rs

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