Skip to main content

tendermint_rpc/endpoint/
block_results.rs

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