cometbft_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 cometbft::{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::v1::Dialect> for Request {
41    type Response = Response;
42}
43
44impl<S: Dialect> crate::SimpleRequest<S> for Request
45where
46    Self: crate::Request<S>,
47    Response: From<Self::Response>,
48{
49    type Output = Response;
50}
51
52/// Response from `/broadcast_tx_commit`.
53#[derive(Clone, Debug, Serialize, Deserialize)]
54pub struct Response {
55    /// `CheckTx` result
56    pub check_tx: abci::response::CheckTx,
57
58    /// Result of executing the transaction.
59    ///
60    /// The JSON field carrying this data is named `deliver_tx` in
61    /// CometBFT versions before 0.38.
62    #[serde(alias = "deliver_tx")]
63    pub tx_result: abci::types::ExecTxResult,
64
65    /// Transaction
66    pub hash: Hash,
67
68    /// Height
69    pub height: block::Height,
70}
71
72impl crate::Response for Response {}
73
74/// Serialization for /broadcast_tx_commit endpoint format in CometBFT 0.34
75pub mod v0_34 {
76    use super::Response;
77    use crate::dialect;
78    use crate::dialect::v0_34::Event;
79    use cometbft::{block, Hash};
80    use serde::{Deserialize, Serialize};
81
82    /// RPC dialect helper for serialization of the response.
83    #[derive(Debug, Deserialize, Serialize)]
84    pub struct DialectResponse {
85        /// `CheckTx` result
86        pub check_tx: dialect::CheckTx<Event>,
87
88        /// `DeliverTx` result
89        pub deliver_tx: dialect::DeliverTx<Event>,
90
91        /// Transaction
92        pub hash: Hash,
93
94        /// Height
95        pub height: block::Height,
96    }
97
98    impl crate::Response for DialectResponse {}
99
100    impl From<DialectResponse> for Response {
101        fn from(msg: DialectResponse) -> Self {
102            Self {
103                check_tx: msg.check_tx.into(),
104                tx_result: msg.deliver_tx.into(),
105                hash: msg.hash,
106                height: msg.height,
107            }
108        }
109    }
110}