tendermint_rpc/endpoint/broadcast/
tx_commit.rs

1//! `/broadcast_tx_commit`: only returns error if `mempool.CheckTx()` errs or
2//! if we timeout waiting for tx to commit.
3
4use serde::{Deserialize, Serialize};
5
6use tendermint::{abci, block, Hash};
7
8use crate::dialect::{self, Dialect};
9use crate::{prelude::*, request::RequestMessage, serializers};
10
11/// `/broadcast_tx_commit`: only returns error if `mempool.CheckTx()` errs or
12/// if we timeout waiting for tx to commit.
13///
14/// If `CheckTx` or `DeliverTx` fail, no error will be returned, but the
15/// returned result will contain a non-OK ABCI code.
16#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
17pub struct Request {
18    /// Transaction to broadcast
19    #[serde(with = "serializers::bytes::base64string")]
20    pub tx: Vec<u8>,
21}
22
23impl Request {
24    /// Create a new commit transaction broadcast RPC request
25    pub fn new(tx: impl Into<Vec<u8>>) -> Request {
26        Request { tx: tx.into() }
27    }
28}
29
30impl RequestMessage for Request {
31    fn method(&self) -> crate::Method {
32        crate::Method::BroadcastTxCommit
33    }
34}
35
36impl crate::Request<dialect::v0_34::Dialect> for Request {
37    type Response = self::v0_34::DialectResponse;
38}
39
40impl crate::Request<dialect::v0_37::Dialect> for Request {
41    type Response = Response;
42}
43
44impl crate::Request<dialect::v0_38::Dialect> for Request {
45    type Response = Response;
46}
47
48impl<S: Dialect> crate::SimpleRequest<S> for Request
49where
50    Self: crate::Request<S>,
51    Response: From<Self::Response>,
52{
53    type Output = Response;
54}
55
56/// Response from `/broadcast_tx_commit`.
57#[derive(Clone, Debug, Serialize, Deserialize)]
58pub struct Response {
59    /// `CheckTx` result
60    pub check_tx: abci::response::CheckTx,
61
62    /// Result of executing the transaction.
63    ///
64    /// The JSON field carrying this data is named `deliver_tx` in
65    /// CometBFT versions before 0.38.
66    #[serde(alias = "deliver_tx")]
67    pub tx_result: abci::types::ExecTxResult,
68
69    /// Transaction
70    pub hash: Hash,
71
72    /// Height
73    pub height: block::Height,
74}
75
76impl crate::Response for Response {}
77
78/// Serialization for /broadcast_tx_commit endpoint format in Tendermint 0.34
79pub mod v0_34 {
80    use super::Response;
81    use crate::dialect;
82    use crate::dialect::v0_34::Event;
83    use serde::{Deserialize, Serialize};
84    use tendermint::{block, Hash};
85
86    /// RPC dialect helper for serialization of the response.
87    #[derive(Debug, Deserialize, Serialize)]
88    pub struct DialectResponse {
89        /// `CheckTx` result
90        pub check_tx: dialect::CheckTx<Event>,
91
92        /// `DeliverTx` result
93        pub deliver_tx: dialect::DeliverTx<Event>,
94
95        /// Transaction
96        pub hash: Hash,
97
98        /// Height
99        pub height: block::Height,
100    }
101
102    impl crate::Response for DialectResponse {}
103
104    impl From<DialectResponse> for Response {
105        fn from(msg: DialectResponse) -> Self {
106            Self {
107                check_tx: msg.check_tx.into(),
108                tx_result: msg.deliver_tx.into(),
109                hash: msg.hash,
110                height: msg.height,
111            }
112        }
113    }
114}
115
116/// Serialization for /broadcast_tx_commit endpoint format in CometBFT 0.37
117pub mod v0_37 {
118    use super::Response;
119    use crate::dialect;
120    use serde::{Deserialize, Serialize};
121    use tendermint::{abci::Event, block, Hash};
122
123    /// RPC dialect helper for serialization of the response.
124    #[derive(Debug, Deserialize, Serialize)]
125    pub struct DialectResponse {
126        /// `CheckTx` result
127        pub check_tx: dialect::CheckTx<Event>,
128
129        /// `DeliverTx` result
130        pub deliver_tx: dialect::DeliverTx<Event>,
131
132        /// Transaction
133        pub hash: Hash,
134
135        /// Height
136        pub height: block::Height,
137    }
138
139    impl crate::Response for DialectResponse {}
140
141    impl From<DialectResponse> for Response {
142        fn from(msg: DialectResponse) -> Self {
143            Self {
144                check_tx: msg.check_tx.into(),
145                tx_result: msg.deliver_tx.into(),
146                hash: msg.hash,
147                height: msg.height,
148            }
149        }
150    }
151}