cometbft_rpc/endpoint/
block_results.rs

1//! `/block_results` endpoint JSON-RPC wrapper
2
3use cometbft::{abci, block, consensus, serializers, validator, AppHash};
4use serde::{Deserialize, Serialize};
5
6use crate::dialect::{self, Dialect};
7use crate::prelude::*;
8use crate::request::RequestMessage;
9
10/// Get ABCI results at a given height.
11#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
12pub struct Request {
13    /// Height of the block to request.
14    ///
15    /// If no height is provided, it will fetch results for the latest block.
16    pub height: Option<block::Height>,
17}
18
19impl Request {
20    /// Create a new request for information about a particular block
21    pub fn new(height: block::Height) -> Self {
22        Self {
23            height: Some(height),
24        }
25    }
26}
27
28impl RequestMessage for Request {
29    fn method(&self) -> crate::Method {
30        crate::Method::BlockResults
31    }
32}
33
34impl crate::Request<dialect::v0_34::Dialect> for Request {
35    type Response = self::v0_34::DialectResponse;
36}
37
38impl crate::Request<dialect::v1::Dialect> for Request {
39    type Response = Response;
40}
41
42impl<S: Dialect> crate::SimpleRequest<S> for Request
43where
44    Self: crate::Request<S>,
45    Response: From<Self::Response>,
46{
47    type Output = Response;
48}
49
50/// ABCI result response.
51#[derive(Clone, Debug, Serialize, Deserialize)]
52pub struct Response {
53    /// Block height
54    pub height: block::Height,
55
56    /// Txs results (might be explicit null)
57    pub txs_results: Option<Vec<abci::types::ExecTxResult>>,
58
59    /// Events from FinalizeBlock.
60    ///
61    /// This field is only populated with events since CometBFT version 0.38.
62    #[serde(default, skip_serializing_if = "Vec::is_empty")]
63    #[serde(deserialize_with = "serializers::nullable::deserialize")]
64    pub finalize_block_events: Vec<abci::Event>,
65
66    /// Begin block events (might be explicit null)
67    ///
68    /// This field is not used and set to `None` since CometBFT version 0.38.
69    pub begin_block_events: Option<Vec<abci::Event>>,
70
71    /// End block events (might be explicit null)
72    ///
73    /// This field is not used and set to `None` since CometBFT version 0.38.
74    pub end_block_events: Option<Vec<abci::Event>>,
75
76    /// Validator updates (might be explicit null)
77    #[serde(deserialize_with = "serializers::nullable::deserialize")]
78    pub validator_updates: Vec<validator::Update>,
79
80    /// New consensus params (might be explicit null)
81    pub consensus_param_updates: Option<consensus::Params>,
82
83    /// Merkle hash of the application state
84    ///
85    /// This field is used since CometBFT version 0.38. It's set to a
86    /// default value when converting responses from nodes using earlier
87    /// versions of the protocol.
88    #[serde(default)]
89    #[serde(with = "serializers::apphash")]
90    pub app_hash: AppHash,
91}
92
93impl crate::Response for Response {}
94
95/// Serialization for /block_results endpoint format in CometBFT 0.34
96pub mod v0_34 {
97    use super::Response;
98    use crate::dialect::v0_34::Event;
99    use crate::prelude::*;
100    use crate::{dialect, serializers};
101    use cometbft::{block, consensus, validator};
102    use serde::{Deserialize, Serialize};
103
104    /// RPC dialect helper for serialization of the response.
105    #[derive(Debug, Serialize, Deserialize)]
106    pub struct DialectResponse {
107        /// Block height
108        pub height: block::Height,
109
110        /// Txs results (might be explicit null)
111        pub txs_results: Option<Vec<dialect::DeliverTx<Event>>>,
112
113        /// Begin block events (might be explicit null)
114        pub begin_block_events: Option<Vec<Event>>,
115
116        /// End block events (might be explicit null)
117        pub end_block_events: Option<Vec<Event>>,
118
119        /// Validator updates (might be explicit null)
120        #[serde(deserialize_with = "serializers::nullable::deserialize")]
121        pub validator_updates: Vec<validator::Update>,
122
123        /// New consensus params (might be explicit null)
124        pub consensus_param_updates: Option<consensus::Params>,
125    }
126
127    impl crate::Response for DialectResponse {}
128
129    impl From<DialectResponse> for Response {
130        fn from(msg: DialectResponse) -> Self {
131            Response {
132                height: msg.height,
133                txs_results: msg
134                    .txs_results
135                    .map(|v| v.into_iter().map(Into::into).collect()),
136                finalize_block_events: vec![],
137                begin_block_events: msg
138                    .begin_block_events
139                    .map(|v| v.into_iter().map(Into::into).collect()),
140                end_block_events: msg
141                    .end_block_events
142                    .map(|v| v.into_iter().map(Into::into).collect()),
143                validator_updates: msg.validator_updates,
144                consensus_param_updates: msg.consensus_param_updates,
145                app_hash: Default::default(),
146            }
147        }
148    }
149}